
var _showMailinglistSignup_data_loaded = false;



function mailinglistSignupStage2() {
    
    var dat = { subscriber_email_address: $('#mailinglist_signup_email').val(),
                subscriber_given_name: $('#mailinglist_signup_givenname').val(), 
                subscriber_family_name: $('#mailinglist_signup_familyname').val() };
    qstr = '';
    for (k in dat) {
        qstr += (qstr ? '&' : '') + escape(k) + '=' + escape(dat[k]);
    }
    
    /// If all fields are filled, post the data
    if (dat.subscriber_email_address.length > 0
        && dat.subscriber_given_name.length > 0 
        && dat.subscriber_family_name.length > 0) {
    
        /// Assemble a list of the mailinglists that the user wants to subscribe to 
        var list_of_lists = section_newsletter.label;
        var delim = ',';
        $('#mailinglist_signup_others input:checked').each(function (k,v) {
            list_of_lists+=((list_of_lists ? delim : '') + $(v).val());
        });
    
        /// Assemble POST url, marshal data and transmit
        var xmiturl = 'http://' + site_host.dynamic + '/mailer/subscribe.dx/' + list_of_lists + '/?' + qstr;
        
        /// Using getScript to get around the SOP
        $.getScript(xmiturl, function(data) {
                    $('#mailinglist_signup_form #mailinglist_signup_form_fields').slideUp();
                    $('#mailinglist_signup_form #mailinglist_signup_form_thanks').slideDown();
                    $('#mailinglist_signup_form').dialog('option', 'buttons', {'Close': function(){$(this).dialog('close');}});
                }
        );
        
    }
    /// Otherwise, error
    else {
        alert('Please fill in all fields.');
    }
    
}

function showMailinglistSignup_ui() {
    /// Populate mailinglist signup box with mailinglist data
    if (!_showMailinglistSignup_data_loaded) {
        $.getJSON("/mailer/lists.dx/"+ site_domain +"/?callback=?", function (data) {
                $.each(data.items, function(i, item) {
                    if (item.label != section_newsletter.label) {
                        var elemname = 'mailinglist_subscribeadditional_' + item.label;
                        var trgElem = $('#mailinglist_signup_others');
                        $('<input/>')
                                    .attr('type', 'checkbox')
                                    .attr('name', elemname)
                                    .attr('id', elemname)
                                    .attr('value', item.label)
                                    .appendTo(trgElem);
                        $('<label>'+item.name+'</label>')
                                    .attr('for', elemname)
                                    .attr('style', 'padding-left:1ex;')
                                    .appendTo(trgElem);
                        $('<br/>')
                                    .appendTo(trgElem);
                    }
                });
            });
        _showMailinglistSignup_data_loaded = true;
    }
        
    $('#mailinglist_signup_form #mailinglist_signup_form_thanks').hide();
    $('#mailinglist_signup_form #mailinglist_signup_form_fields').show();
    $('#mailinglist_signup_form').dialog('option', 'buttons', {'Cancel':function (){$(this).dialog('close');}, 'Subscribe': mailinglistSignupStage2});
    $('#mailinglist_signup_form').dialog('open');
}


/// Configure fade effect
$.effects.fade = function(o) {
    return this.queue(function() {
                // Create element
                var el = $(this);
                // Set options
                var speed = o.options.speed || 500;
                var mode = o.options.mode || 'show'; // Set Mode
                // Animate
                if (mode == 'show') {
                        el.fadeIn(speed);
                } else {
                        el.fadeOut(speed);
                };
                el.queue('fx', function() { el.dequeue(); });
                el.dequeue();
    });
}

var showMailinglistSignup;
/// onLoad initializer
$(function () {
    if (jQuery.browser.msie && jQuery.browser.version == '6.0') {
        showMailinglistSignup = function() {
            window.location = '/mailer/subscribe/' + section_newsletter.label + '/';
        };
    }
    else {
        showMailinglistSignup = showMailinglistSignup_ui;
    
        /// Configure mailinglist signup form title and dialog
        if (section_newsletter.name) {
            $('#mailinglist_signup_form').attr('title', "Subscribe to " + section_newsletter.name);
        }
        $('#mailinglist_signup_form').dialog({
            autoOpen: false,
            modal: true,
            show: 'fade',
            hide: 'fade',
            buttons: {},        /// These buttons are defined programmatically
            resizable: false,
            draggable: false
        });
    }
});
