/*
 --------------------------------------------------

 Javascript Application Code [application.js]

 Joe Morrow [joe.morrow@acquitygroup.com]
 04/21/2009

 Copyright © 2009 Acquity Group LLC

 --------------------------------------------------
 */


$(document).ready(function() {

    // Ship To Multiples Switching
    $(".savedShippingAddresses dl").hide();
    $("." + $(".savedShippingAddressesSelect option:selected").val()).show();
    $(".savedShippingAddressesSelect").change(function() {
        $(".savedShippingAddresses").find("dl").hide();
        $("." + $(".savedShippingAddressesSelect option:selected").val()).show();
    });


    // BGIframe
    //$('.fix-z-index').bgiframe();
    $('#nav li ul').bgiframe();

    // Browser & OS Detection
    if (/Safari/.test(navigator.userAgent))
        $("body").addClass("safari");


    // Print Link
    $('#printPage a').click(function() {
        window.print();
        return false;
    });


    // Auto-clear inline prompt text, stored in "title" attribute
    $(".inline-prompt").each(function() {
        if ($(this).attr("title").length == 0)
            $(this).attr("title", $(this).val());
    }).focus(function() {
        if ($(this).val() == $(this).attr("title"))
            $(this).val("").removeClass("inline-prompt");
    }).blur(function() {
        if ($(this).val() == "")
            $(this).val($(this).attr("title")).addClass("inline-prompt");
    });


    // Handle checkbox for different billing and shipping address
    if ($("#shipping-different").length > 0) {
        var billingDifferentChange = function() {
            if (!($(this).attr("checked")))
                $("#shipping-group").removeClass("disabled").find("input, select, textarea").removeAttr("disabled"); else
                $("#shipping-group").addClass("disabled").find("input, select, textarea").attr("disabled", "disabled");
        };
        $("#shipping-different").each(billingDifferentChange);
        if (isIE())    // IE only fires a change event on fade
            $("#shipping-different").click(billingDifferentChange); else
            $("#shipping-different").change(billingDifferentChange);
    }


    // Handle checkbox for saving an address for future use
    if (!$("input.toggleActivator").length > 0) {
        var addressSaveChange = function() {
            if (!$(this).attr("checked"))
                $(this).parents(".displayToggle").children("fieldset.toggleDependent").show().children("input").removeAttr("disabled"); else
                $(this).parents(".displayToggle").children("fieldset.toggleDependent").hide().children("input").attr("disabled", "disabled");
        };
        $("input.toggleActivator").each(addressSaveChange);
        if (isIE())    // IE only fires a change event on fade
            $("input.toggleActivator").click(addressSaveChange); else
            $("input.toggleActivator").change(addressSaveChange);
    }


    // Handle select for selecting a stored credit card
    if ($("form#checkout-form fieldset#payment-saved-group select").length == 1) {
        var savedCreditCardChange = function() {
            if ($(this).val()) {

                $("form#checkout-form fieldset#payment-card-group, form#checkout-form fieldset#payment-holderName-group, form#checkout-form fieldset#payment-cardNumber-group, form#checkout-form fieldset#payment-creditCardExpirationMonth-group, form#checkout-form fieldset#payment-creditCardExpirationYear-group,  form#checkout-form fieldset#payment-save-group, form#checkout-form fieldset#payment-referenceName-group").addClass("disabled").find("input, select, textarea").attr("disabled", "disabled");
                //TOGGLE TO THE SAVED CC CVV
                $('form#checkout-form fieldset#payment-security-group #payment-security-new').hide();
                $('form#checkout-form fieldset#payment-security-group #payment-security-new').prev().hide();
                $('form#checkout-form fieldset#payment-security-group #payment-security-saved').show();
                $('form#checkout-form fieldset#payment-security-group #payment-security-saved').prev().show();
            } else {

                $("form#checkout-form fieldset#payment-card-group, form#checkout-form fieldset#payment-holderName-group, form#checkout-form fieldset#payment-cardNumber-group, form#checkout-form fieldset#payment-creditCardExpirationMonth-group, form#checkout-form fieldset#payment-creditCardExpirationYear-group,  form#checkout-form fieldset#payment-save-group, form#checkout-form fieldset#payment-referenceName-group").removeClass("disabled").find("input, select, textarea").removeAttr("disabled");
                //TOGGLE TO THE NEW CC CVV
                $('form#checkout-form fieldset#payment-security-group #payment-security-saved').hide();
                $('form#checkout-form fieldset#payment-security-group #payment-security-saved').prev().hide();
                $('form#checkout-form fieldset#payment-security-group #payment-security-new').show();
                $('form#checkout-form fieldset#payment-security-group #payment-security-new').prev().show();
            }
        };
        $("form#checkout-form fieldset#payment-saved-group select").each(savedCreditCardChange);
        if (isIE())
            $("form#checkout-form fieldset#payment-saved-group select").click(savedCreditCardChange); else
            $("form#checkout-form fieldset#payment-saved-group select").change(savedCreditCardChange);
    }
    $(".editShippingLink").click(function() {
        $("#popHeaderString").html("Edit Shipping Recipient Information");
        $(".multipopwin").show();
    });
    $(".addNewShippingLink").click(function() {
        $("#popHeaderString").html("Add New Shipping Recipient");
        $(".multipopwin").show();
    });


    //$(".viewShippingOptions").hover(function(){$(".shippopwin").show();},function(){$(".shippopwin").hide();});


    $(".viewShippingOptions").click(function() {
        $(".shippopwin").show();
    }),$(".hideShippingOptions").click(function() {
        $(".shippopwin").hide();
    });


    // Top Nav Menu Drop-Down Suckerfish
    // http://htmldog.com/articles/suckerfish/dropdowns/
    sfHover = function() {
        var sfEls = document.getElementById("nav").getElementsByTagName("LI");
        for (var i = 0; i < sfEls.length; i++) {
            sfEls[i].onmouseover = function() {
                this.className += " sfhover";
            }
            sfEls[i].onmouseout = function() {
                this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
            }
        }
    }
    if (window.attachEvent) window.attachEvent("onload", sfHover);

    // Datepicker
    if ($(".datepicker").size() > 0) {

        if ($("body.es").size() > 0) {
            // date localization for locale 'es'
            // generated by Jörn Zaefferer using Java's java.util.SimpleDateFormat
            Date.dayNames = ['domingo', 'lunes', 'martes', 'mi?rcoles', 'jueves', 'viernes', 's?bado'];
            Date.abbrDayNames = ['dom', 'lun', 'mar', 'mi?', 'jue', 'vie', 's?b'];
            Date.monthNames = ['enero', 'febrero', 'marzo', 'abril', 'mayo', 'junio', 'julio', 'agosto', 'septiembre', 'octubre', 'noviembre', 'diciembre'];
            Date.abbrMonthNames = ['ene', 'feb', 'mar', 'abr', 'may', 'jun', 'jul', 'ago', 'sep', 'oct', 'nov', 'dic'];
        }

        Date.firstDayOfWeek = 0;
        Date.format = 'mm/dd/yyyy';

        $(".datepicker").closest("#contactUs-form").length > 0 ? $(".datepicker").datePicker({startDate:'01/01/1984'}) : $(".datepicker").datePicker();
    }


    // end Top Nav Menu Drop-Down Suckerfish

});


