﻿$(document).ready(function() {
    setupRollovers();

    // ensure events are rebound on ajax refreshes
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
});

// called after each asp.net ajax callback
function EndRequestHandler(sender, args) {
    setupRollovers();
}

function setupRollovers() {
    $(".rollover").each(
        function() {
            var overSrc = "";
            if (this.src.endsWith(".gif"))
                overSrc = this.src.replace(".gif", "_over.gif");
            if (this.src.endsWith(".jpg"))
                overSrc = this.src.replace(".jpg", "_over.jpg");
            if (this.src.endsWith(".png"))
                overSrc = this.src.replace(".png", "_over.png");

            jQuery("<img>").attr("src", overSrc);
        }
    );

    $(".rollover").hover(
        function() {
            if (this.src.search("-over") < 0) {
                var overSrc = "";
                if (this.src.endsWith(".gif"))
                    overSrc = this.src.replace(".gif", "_over.gif");
                if (this.src.endsWith(".jpg"))
                    overSrc = this.src.replace(".jpg", "_over.jpg");
                if (this.src.endsWith(".png"))
                    overSrc = this.src.replace(".png", "_over.png");

                this.src = overSrc;
            }
        },
        function() {
            this.src = this.src.replace("_over", "");
        });

        // stop things mucking up if we run this twice
        $(".rollover").removeClass("rollover");
}

//handles fields with default values
$(document).ready(function () {
    $(".DefaultText").addClass("DefaultTextIdle");

    $(".DefaultText").focus(function () {
        $(this).removeClass("DefaultTextIdle").addClass("DefaultTextActive");
        if (this.value == this.defaultValue) {
            this.value = '';
        }
        if (this.value != this.defaultValue) {
            this.select();
        }
    });

    $(".DefaultText").blur(function () {
        if ($.trim(this.value) == '') {
            $(this).removeClass("DefaultTextActive").addClass("DefaultTextIdle");
            this.value = (this.defaultValue ? this.defaultValue : '');
        }
    });


    //toggle between text field and password field so that password field can show "password" as a default value
    $('.password-clear').show();
    $('.password-password').hide();

    $('.password-clear').focus(function () {
        $('.password-clear').hide();
        $('.password-password').show();
        $('.password-password').focus();
    });

    $('.password-password').blur(function () {
        if ($('.password-password').val() == '') {
            $('.password-clear').show();
            $('.password-password').hide();
        }
    });

    //toggle size textbox when order preproduction is checked
    $('.preprod-check').each(function () {
        if ($(this).find('input:checkbox').is(":checked")) {
            var name = $(this).attr("name");
            $('.txt' + name).show();
        }
    });

    $('.preprod-check').click(function () {
        var name = $(this).attr("name");
        if ($(this).find('input:checkbox').is(":checked")) {
            $('.txt' + name).show();
            $('.txt' + name).focus();
        }
        else {
            $('.txt' + name).hide();
        }
    });
});
