﻿$.fn.pause = function (duration) {
    $(this).animate({ dummy: 1 }, duration);
    return this;
};

$(document).ready(function () {

    var isIE6 = ($.browser.msie && $.browser.version.substr(0, 1) < 7);

    // Adds the scroll down animation to the dropdowns and adds the over class
    // The commented out parts is the fade effect which is not supported in IE
    $("#mainNavAction dd").hover(function () {
        $(this).children('.DropDownMenu').stop(true, true);
        $(this).children('.DropDownMenu').pause(250).animate({
            "height": "show"
            //"opacity": "show"
        }, "fast", "swing", function () {
            $(this).parent().addClass('over');
            //$(this).css({ "height": "", "opacity": "" });
            $(this).css({ "height": "" });
        });
    },
    function () {
        $(this).children('.DropDownMenu').stop(true, true);
        $(this).children('.DropDownMenu').animate({
            "height": "hide"
            //"opacity": "hide"
        }, "normal", "swing", function () {
            $(this).parent().removeClass('over');
            //$(this).css({ "height": "", "opacity": "" });
            $(this).css({ "height": "" });
        });
    });

    // This adds the over class to the dropdown LIs so that we can get an over effect in IE6
    $(".DropDownMenu li").hover(function () {
        $(this).addClass('over');
    },
    function () {
        $(this).removeClass('over');
    });

    // This adds the little shift in the dropdown menu
    if (!isIE6) {
        $('.DropDownMenu li a').hover(function () {
            $(this).stop(true, true);
            $(this).animate({ marginLeft: "4" }, { duration: 200 });
        }, function () {
            $(this).stop(true, true);
            $(this).animate({ marginLeft: "0" }, { duration: 200 });
        });
    }

    // This is the simplified img hover script
    $('img[hvr]').hover(function () {
        var currentImg = $(this).attr('src');
        $(this).attr('src', $(this).attr('hvr'));
        $(this).attr('hvr', currentImg);
    }, function () {
        var currentImg = $(this).attr('src');
        $(this).attr('src', $(this).attr('hvr'));
        $(this).attr('hvr', currentImg);
    });

    // This adds a watermark to the textbox using the ToolTip attribute for <asp:TextBox or the Title attribute on input.
    $("input[title]").each(function () {
        if ($(this).attr('title') != '') {
            $(this).val($(this).attr('title'));
            $(this).addClass("water");
        }
    }).focus(function () {
        if ($(this).val() == $(this).attr('title')) {
            $(this).val("");
            $(this).removeClass("water");
        }
    }).blur(function () {
        if ($.trim($(this).val()) == "") {
            $(this).val($(this).attr('title'));
            $(this).addClass("water");
        }
    });

    // Autocreate captions for images.
    $("img[longdesc]").each(function () {

        $(this).wrap('<div class="autoImage" />');
        $(this).after('<div class="autoImageCaption">' + $(this).attr('longdesc') + '</div>');
        $(this).parent().attr('style', $(this).attr('style')).attr('class', $(this).attr('class'));
        $(this).removeAttr("style").removeAttr("class");

    });


    $('.dropdown dd').each(function () {
        var minWidth = 12;
        var maxWidth = 25;
        var fontsize = $('<dd id="menu-fontsize">&#8212;</dd>').css({
            'padding': 0,
            'position': 'absolute',
            'top': '-999em',
            'width': 'auto'
        }).appendTo($(this)).width();
        $('#menu-fontsize').remove();

        $(this).children('ul').each(function () {
            var $$ = $(this);
            $$.css({ 'display': 'block', 'visibility': 'hidden' });
            $$.css('width', 'auto');
            $$.children().css('white-space', 'nowrap')

            var emWidth = $$.outerWidth() / fontsize; ;

            if (emWidth > maxWidth) emWidth = maxWidth;
            if (emWidth < minWidth) emWidth = minWidth;

            $$.css({ 'width': emWidth + 'em' });

            var count = 0 - $$.outerWidth();
            while ($$.offset().left + $$.outerWidth() > $(document.forms[0]).offset().left + $(document.forms[0]).innerWidth()) {
                $$.css('left', 'auto');
                $$.css('right', count + 'px');
                count++;
            }

            $$.css({ 'display': '', 'visibility': '' })
            $$.children().css({ 'white-space': 'normal', 'width': '100%' });

            $$.prev('a').addClass('haschildren');
        });
    });

    $('.dropdown dd').hover(function () {
        $('.dropdown dd').find('ul').stop(true, true).parent().removeClass('over').css("z-index", "");

        if ($(this).children('ul').length > 0) {
            $(this).children('ul')
            .animate({ "height": "hide" }, 2000)
            .animate({
                "height": "show",
                "opacity": "show"
            }, "slow", "swing", function () {
                $(this).parent().addClass('over');
                $(this).css({ "height": "", "opacity": "", "display": "" });
            });
        } else {
            $(this).addClass('over');
        }

    }, function () {
        $(this).children('ul').stop(true, false);
        if ($(this).children('ul').length > 0) {
            $(this).children('ul')
                .animate({ "height": "show" }, 5000)
                .animate({
                    "height": "hide",
                    "opacity": "hide"
                }, "slow", "swing", function () {
                    $(this).parent().removeClass('over');
                    $(this).css({ "height": "", "opacity": "", "display": "" });
                });
        } else {
            $(this).removeClass('over');
        }
    });

    $('[class*="padding"]').each(function () {
        var regEx = /padding([\d]*)/;
        var match = regEx.exec(this.className);
        var $$ = $(this);

        if (this.tagName.toLowerCase() != 'img')
            $$ = $$.find('img');

        if (match && match.length > 1) {
            var qs = "paddingWidth=" + match[1] + "&borderWidth=1&borderColor=000000";

            var src = $$.attr('src');

            src += (src.indexOf('?') > -1 ? "&" : "?") + qs;

            $$.attr('src', src);
        }
    });
});
