var sharethis = {
    init: function(shareLinks) {
        var that = this;
        if (shareLinks) {
            shareLinks.each(function(i) {
                var a = $(this);
                var li = a.parents("li:eq(0)");
                var windowOptions = "status=1,resizable=1,toolbar=0,location=0,scrollbars=1,menubar=0";
                var windowName = null;

                //customize behavior for different sites
                if (li.is('.fb')) {
                    windowOptions += ",width=626,height=436";
                }
                else if (li.is('.del')) {
                    windowOptions += ",width=550,height=550";
                }
                else if (li.is('.digg')) {
                }
                else if (li.is('.twitter')) {
                    windowOptions += ",width=782,height=559";
                }
                else if (li.is('.linkedin')) {
                    windowOptions += ",width=520,height=570";
                }
                a.click(function(e) {
                    that.popWindow(this.href, windowName, windowOptions);
                    e.preventDefault();
                });
            });
        }
    },
    popWindow: function(url, windowName, windowOptions) {
        var name = windowName || "sharethis";
        var options = windowOptions || "";
        window.open(url, name, options);
    }
};

var DonateForm = {
    init: function() {
        this.honorarium = $("fieldset.personalGift label.honorarium:first");
        if (this.honorarium && this.honorarium.length > 0) {
            var honorCheckbox = this.honorarium.find("input");
            this.nameFields = $("fieldset.memorialGift");
            this.msgFields = $("fieldset.acknowledgement");
            this.msgRadios = this.msgFields.find("label.radio");

            //Show mesage options if gift is in honor of someone
            var that = this;
            honorCheckbox.bind("click", function() {
                that.toggleMsgOptions(honorCheckbox.is(":checked"));
            });
            //accomodate javascript state caching on pageload
            this.toggleMsgOptions(honorCheckbox.is(":checked"));

            //check the email acknowledgement and show its fields by default
            var checkedMsgRadio = this.msgRadios.find("input:checked");
            if (checkedMsgRadio.length < 1) {
                var checkedMsgRadio = this.msgRadios.find("input[id*=EmailAcknowledgement]").attr("checked", "checked");
            }
            this.toggleMsgOptionDetailFields(checkedMsgRadio);

            //Select gift type buttons (one-time, recurring) if input is clicked
            $("div.subRadioRow input").focus(function() {
                $(this).parents("div.subRadioRow:eq(0)").siblings("label.radio").find("input:first").attr("checked", "checked");
            });

            //total donation multiplication
            this.getTotalDonation.init($("fieldset.personalGift div.recurringGift"));
        }
        //Create an onclick handler which disables the confirmation button.
        $('div.donateConfirmation div.submits button').bind("click", function () {
            $(this).attr('disabled','disabled');
            $(this).addClass('disabled');
        });
    },
    getTotalDonation: {
        init: function(recurringGiftDiv) {
            this.amtInput = recurringGiftDiv.find("label.giftAmount input:first");
            this.frequencyInput = recurringGiftDiv.find("label.numPayments input:first");
            this.totalInput = recurringGiftDiv.find("label.totalGift input:first");

            var that = this;
            this.amtInput.add(this.frequencyInput).bind("keyup", function() {
                that.multiplyFields();
            });
        },
        multiplyFields: function() {
            var total = this.amtInput.val() * this.frequencyInput.val();
            if (isNaN(total)) {
                this.totalInput.val("--");
            }
            else {
                this.totalInput.val(total);
            }
        }

        //var amtInput = $("div.

        /*
        init: function() {
        var amtInput;
        var frequencyInput;
        var totalInput;
            
        var amt;
        var frequency;
        var totalInput;
        }*/
    },
    toggleMsgOptions: function(show) {
        if (show) {
            this.nameFields.css("position", "static").show();
            this.msgFields.css("position", "static").show();

            var that = this;
            this.msgRadios.find("input[type='radio']").bind("click", function() {
                that.toggleMsgOptionDetailFields(this);
            });
        }
        else {
            this.nameFields.removeAttr("style");
            this.msgFields.removeAttr("style");
        }
    },
    toggleMsgOptionDetailFields: function(changedElem) {
        var radioLabel = $(changedElem).parents("label:first");
        radioLabel.parents("div:first").siblings("div").children("label:not(.radio)").removeAttr("style");
        radioLabel.siblings("label").css("position", "static").show();
    }
};

var RegistrationForm = {
    init: function() {
        
        this.attendeeFields = $("fieldset#attendeeInformation");
        this.attendeeRadios = this.attendeeFields.find("label.radio");

        //Show mesage options if gift is in honor of someone
        var that = this;
        this.attendeeRadios.find("input[type='radio']").bind("click", function() {
                that.toggleAttendeeOptionFields(this);
        });

        //check the email acknowledgement and show its fields by default
        var checkedAttendeeRadio = this.attendeeRadios.find("input:checked");
        if (checkedAttendeeRadio.length < 1) {
            var checkedAttendeeRadio = this.attendeeRadios.find("input[id*=Individual]").attr("checked", "checked");
        }
        this.toggleAttendeeOptionFields(checkedAttendeeRadio);

        //Create an onclick handler which disables the confirmation button.
        $('div.donateConfirmation div.submits button').bind("click", function () {
            $(this).attr('disabled','disabled');
            $(this).addClass('disabled');
        });
    },
    toggleAttendeeOptionFields: function(changedElem) {
        var radioLabel = $(changedElem).parents("label:first");
        radioLabel.parents("div:first").siblings("div").children("div.group").removeAttr("style");
        radioLabel.siblings("div.group").css("position", "static").show();
    }
};

//on DOM ready (Jquery shortcut)
$(function() {
    $("body").addClass("jsEnabled");
    DonateForm.init();
    RegistrationForm.init();
    sharethis.init($("div.sharethis a"));
});
