var fadeTimeout;

$(document).ready(function() {

    // ====================
    // MESSAGES
    // ====================
    $('div.alert.closable p, div.success.closable p').append(' <a href="#" class="close">Close</a>');
    $('div.alert a.close, div.success a.close').click(function() {
        $(this).parent().parent().fadeOut(1000);
    });
    fadeTimeout = setTimeout("fadeMessages()", "2000");

    // =====================
    // BASKET
    // =====================
    $('input#txtDiscountCode').keypress(function(e) {
        if (e.which == 13) {
            if ($('input#btnApplyDiscount').length) {
                $('input#btnApplyDiscount').click();
                return false;
            } else if ($('input#btnApplyDiscountImage').length) {
                $('input#btnApplyDiscountImage').click();
                return false;
            }
        }
    });
    $('input.quantity').keypress(function(e) {
        if (e.which == 13) {
            $('input#ucBasket_btnUpdateQuantitiesImage, input#ucBasket_btnUpdateQuantities').click();
            return false;
        }
    });

    // ===========================
    // IDENTIFICATION
    // ===========================
    $('input#txtLogInPassword, input#txtLogInEmail').keypress(function(e) {
        if (e.which == 13) {
            if ($('input#btnLogIn').length) {
                $('input#btnLogIn').click();
                return false;
            } else if ($('input#btnLogInImage').length) {
                $('input#btnLogInImage').click();
                return false;
            }
        }
    });
    $('input#txtRegisterEmail, input#txtRegisterPassword, input#txtRegisterConfirmPassword').keypress(function(e) {
        if (e.which == 13) {
            if ($('input#btnRegister').length) {
                $('input#btnRegister').click();
                return false;
            } else if ($('input#btnRegisterImage').length) {
                $('input#btnRegisterImage').click();
                return false;
            }
        }
    });

    // ====================
    // GENERAL FORMS
    // ====================
    $('div.form p.prompt').each(function() {
        $(this).hide();
        $(this).before('<a href="#" class="help" title="Help"><img src="../images/icons/help.png" alt="Help" title="Help" /></a>');
    });
    $('div.form a.help').click(function() {
        $(this).siblings('p.prompt').toggle();
        return false;
    });
    $('div.radiobutton-list ul li input').click(function() {
        var me = $(this).attr('id');
        $(this).parent().parent().find('input').each(function() {
            if ($(this).attr('id') != me) {
                $(this).attr('checked', '');
            }
        });
    });

    // ====================
    // DETAILS
    // ====================
    // Billing
    if ($('input#rdoBillingUseAddressYes').attr('checked')) {
        $('div#billing-details div.form-elements').hide();
    }
    $('input#rdoBillingUseAddressYes').click(function() {
        if ($('div#billing-details div.form-elements').is(':visible')) {
            $('div#billing-details div.form-elements').slideUp('slow');
        }
    });
    $('input#rdoBillingUseAddressNo').click(function() {
        if ($('div#billing-details div.form-elements').is(':visible') == false) {
            $('div#billing-details div.form-elements').slideDown('slow');
        }
    });

    // Shipping
    if ($('input#rdoShippingUseAddressYes').attr('checked')) {
        $('div#shipping-details div.form-elements').hide();
    }
    $('input#rdoShippingUseAddressYes').click(function() {
        if ($('div#shipping-details div.form-elements').is(':visible')) {
            $('div#shipping-details div.form-elements').slideUp('slow');
        }
    });
    $('input#rdoShippingUseAddressNo').click(function() {
        if ($('div#shipping-details div.form-elements').is(':visible') == false) {
            $('div#shipping-details div.form-elements').slideDown('slow');
        }
    });

    // =====================
    // DONATION
    // =====================
    if ($('ul#donation-amounts').length) {
        var $divDonationOther = $('div#donation-other');
        var $rdoOther = $('#divDonation div.radiobutton-list ul li input#rdoDonationOther');
        if (!$rdoOther.attr('checked')) {
            $divDonationOther.hide();
        }
        $('#divDonation div.radiobutton-list ul li input').click(function() {
            $('input#btnClearDonationImage').show();
            if ($('div.radiobutton-list ul li:last input').attr('checked')) {
                $divDonationOther.show();
            } else {
                $divDonationOther.hide();
                $divDonationOther.children('input').val('');
            }
        });
        $('input#btnClearDonationImage').hide();
        if ($('ul#donation-amounts li input[type=radio]:checked').length) {
            $('input#btnClearDonationImage').show();
        }
    }

    // =====================
    // DELIVERY
    // =====================
    $('div#divDeliveryOptions td input').click(function() {
        var me = $(this).attr('id');
        $('div#divDeliveryOptions td input').each(function() {
            if ($(this).attr('id') != me) {
                $(this).attr('checked', '');
            }
        });
    });
    //

    // =====================
    // Confirmation
    // =====================
    if ($('input#btnContinueImage').length > 0) {
        var path = $('input#btnContinueImage').attr('src').replace('Continue', 'Processing');
        $('input#btnContinueImage').after('<input id="btnProcessingImage" type="image" disabled="disabled" alt="Processing..." src="' + path + '" style="display:none;"></input>');
        $('input#btnContinueImage').click(function() {
            $('input#btnContinueImage').hide();
            $('input#btnProcessingImage').show();
        });
    }
    if ($('iframe#three-d-secure-iframe').length > 0) {
        $('a, input[type=submit], input[type=image]').click(function() {
            return !confirm('You have entered the 3D authorization process.  If you leave this page your order will not be completed.\n\nClick "OK" to continue with the payment process or "Cancel" to abandon your order.');
        });
    }


});

function fadeMessages() {
	$('div.fade').fadeOut(1000);
}