﻿var NewsletterSignup = new Class({
    initialize: function(container, txt, img) {
        this.container = $(container);
        this.textbox = $(txt);
        this.img = $(img);
        
        this.img.addEvent('click', function() {
            this.doSignup();
        }.bind(this));
        this.textbox.addEvent('keydown', function(event) {
            if (event.key == 'enter')
                this.doSignup();
        }.bind(this));
    },
    
    doSignup: function() {
        if (this.textbox.value.length == 0) {
            alert('Please enter your e-mail address');
            return;
        }
        if (this.textbox.value.indexOf('@') == -1) {
            alert('Please enter a valid e-mail address');
            return;
        }
        
        new Request({ 'url': '/ufm/mailit.php?cid=3', 'method': 'post', 'data': 'email=' + this.textbox.value,
            'onSuccess': function(result) {
                this.showSuccess();
            }.bind(this),
            'onFailure': function(result) {
                this.showSuccess();
            }.bind(this)
        }).send();
    },
    
    showSuccess: function() {
        this.container.empty();
        var el = new Element('div', { 'styles': { 'margin': '8px 2px 0 0', 'padding': '2px', 'background-color': '#f00', 'color': '#fff', 'font-size': '8pt' }}).set('text', 'Thank you for subscribing to our Newsletter!').inject(this.container);
        window.setTimeout(function() {
            var fx = new Fx.Tween(el, { 'duration': 1000 });
            fx.start('background-color', '#0C67a5');
        }, 700);
    }
});
