var FCEEmailPop = {
    emailSenderURL : '/email_sender.php',
    enabled : false,


    init : function() {
        $('#iconEmail > a.imap').bind('click', function(e) { $(this).blur(); FCEEmailPop.show(); return false; }); // show it
        $('#hEmailPop > span').bind('click', function(e) { $('#wEmailPop').hide(); }); // close popup on tab click
        $('#wEmailPop > div.btnclose').bind('click', function(e) { $('#wEmailPop').hide(); }); // close popup on X click
        $('#formEmailPop').bind('submit', function(e) { return FCEEmailPop.check(this); } );
        $(document).bind('click', function(e) { FCEEmailPop.disable(); } );
        $('#iconEmail').bind('click', function(e) { 
            if ($(e.target).attr('class') != 'btnclose' && $(e.target).parents('#iconEmail').length > 0) e.stopPropagation();
         } );

        this.defaultText1 = $('#formEmailPop > p > strong').text();
        this.defaultText2 = $('#formEmailPop > p > span').text();
        this.fieldDefaultText = $('#opEmail').val();
        this.defaultBtnSrc = $('#opEmailBtn').attr('src');
        this.defaultBtnAlt = $('#opEmailBtn').attr('alt');

        $('#opEmail').bind('focus', function() {
            if ($(this).val() == FCEEmailPop.fieldDefaultText) {
                $(this).val('');
            }
        });
        $('#opEmail').bind('blur', function() {
            if ($(this).val() == '') {
                $(this).val(FCEEmailPop.fieldDefaultText);
            }
        });

    },

    show : function() {
        if (this.enabled) {
            this.disable();
        }
        else {
            FCEEmailPop.resetForm();
            $('#formEmailPopErr').empty();
            //$('#wEmailPop').slideDown('fast');
            $('#wEmailPop').show();
        }

    },

    disable : function() {
        $('#wEmailPop').hide();
    },

    check : function(elem) {
        var val = $('#opEmail').attr('value');
        if (!val || val == '') {
            $('#formEmailPopErr').text('Please enter one or more email addresses in the box to continue.');
            $('#emailPop').addClass('err');
            return false;
        }
        else {
            var addrs = val.split(',');
            for (var i=0; i<addrs.length; i++) {
                var a = $.trim(addrs[i]);
                if (!a.match(/^([-a-zA-Z0-9_.]+)@(([-a-zA-Z0-9_]+[.])+[a-zA-Z]+)$/)) {
                    $('#formEmailPopErr').text('Sorry, "' + a + '" is not a proper email address.');
                    $('#emailPop').addClass('err');
                    return false;
                }
            }
            this.sendMail(val);
        }

        return false;
    },

    // send via AJAX to server script for processing
    sendMail : function(val) {
        //$('#opEmailBtn').attr('disabled', true);
        $('#opEmailBtn').blur();
        $('#emailPop').append('<div class="pwait">please wait...</div>');

        data = { 'addrs':val,
                 'page': $('body').attr('id')
               };

        if (typeof(FCEAptSearch) != 'undefined') {
            data.apt = FCEAptSearch.currUnitID;
        }

        /*$.ajax({
        type: "POST",
        url: this.emailSenderURL,
        data: data,
        success: this.sendSuccess,
        error: this.sendError
        });
        this.sendSuccess(); */
        EmailBrochure.SendBrochure(data.addrs, data.page, data.apt, this.sendSuccess, this.sendError);
    },

    sendSuccess : function() {
        $('#formEmailPop > p > strong').text('Thank you.');

        var thing = 'gallery';
        if (typeof(FCEAptSearch) != 'undefined') {
            thing = 'apartment information';
        }
        $('#formEmailPop > p > span').text('This ' +thing+' has been sent to the address you entered.');

        $('#formEmailPopErr').empty();
        $('#emailPop').removeClass('err').addClass('emailPop_ok');
        $('#emailPop p').prependTo('#formEmailPop');
        $('#opEmailBtn').attr('src', 'img/btn.op_email_ok.gif').attr('alt', 'OK').addClass('opEmailBtn_ok');
        $('#opEmail').hide();
        $('#emailPop > div.pwait').remove();
        $('#opEmailBtn').bind('click', function() { 
                                            FCEEmailPop.disable();
                                            FCEEmailPop.resetForm();
                                            return false;
                                       } );
    },

    sendError : function() {
        $('#formEmailPopErr').text('Sorry, there seems to be a problem. Please try again later.');
        $('#emailPop').addClass('err');
        $('#emailPop > div.pwait').remove();
    },

    resetForm : function() {
        $('#formEmailPopErr').empty();
        $('#emailPop').removeClass('err').removeClass('emailPop_ok');
        $('#emailPop > div.pwait').remove();
        $('#formEmailPop > p > strong').text(this.defaultText1);
        $('#formEmailPop > p > span').text(this.defaultText2);
        $('#opEmailBtn').attr('src', this.defaultBtnSrc).attr('alt', this.defaultBtnAlt);
        $('#opEmailBtn').unbind('click').removeClass('opEmailBtn_ok');
        //$('#opEmailBtn').removeAttr('disabled');
        $('#opEmail').show().attr('value', '');
        $('#emailPop p').appendTo('#formEmailPop');
    }
};


$(document).ready(function(){
     FCEEmailPop.init();
});

