﻿$(document).ready(function () {
    (function ($) {
        //Function to calculate total width of all ul's
        jQuery.fn.calcWidth = function () {
            $(this).each(function () {
                var $$ = $(this);

                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).parent()).width();
                $('#menu-fontsize').remove();


                $$.css({ 'display': 'block' });
                $$.children().css({ 'white-space': 'nowrap', 'width': '100%' });
                $$.css('width', 'auto');

                var emWidth = (Math.max.apply(Math, $$.children().map(function () { return $(this).outerWidth(); }).get())) / fontsize; ;

                if (emWidth > maxWidth) emWidth = maxWidth;
                if (emWidth < minWidth) emWidth = minWidth;

                $$.css({ 'width': emWidth + 'em' });

                //                var count = 0 - $$.outerWidth();
                //                while ($$.offset().left + $$.outerWidth() > $('#pageWrapper').offset().left + $('#pageWrapper').innerWidth()) {
                //                    $$.css('left', 'auto');
                //                    $$.css('right', count + 'px');
                //                    count++;
                //                }

                $(this).children('li').children('ul').calcWidth();

                $$.css({ 'display': '', 'visibility': '' })
                $$.children().css({ 'white-space': '', 'width': 'auto' });

                $$.prev('a').addClass('haschildren');
            });
        };
    })(jQuery);

    $('.dropdown dd').each(function () {
        $(this).children('ul').calcWidth();
        $(this).find('ul').each(function () {
            $$ = $(this)
            var maxChildren = 25;
            var parentWidth = parseFloat($$.parents('ul').css('width'))

            while ($$.children("li").length > maxChildren) {
                var newContainer = $("<ul></ul>");


                $$.children('li:eq(' + (maxChildren - 1) + ')').nextAll().detach().appendTo(newContainer);

                newContainer.insertAfter($$);
                newContainer.children('li').first().addClass("first");
                newContainer.children('li').eq(-1).clone(true).appendTo($$);

                newContainer.css('width', $$.css('width'));
                newContainer.css('left', (parentWidth + (parseFloat($$.css('width')) * (newContainer.prevAll().length - 1))) + 'em');
                //newContainer.css('left', parentWidth * newContainer.prevAll().length + 'em');

                $$ = newContainer;
            }
        });

        //$($('.dropdown dd ul').get(0)).find('li').filter(function(index){return $('a span', this).html() && $('a span', this).html().indexOf('A') == 0;})
    });

    $('.dropdown dd').hoverIntent({
        over: function () {
            $('.dropdown dd').children('ul').stop(true, true).parent().removeClass('over');
            $(this).children('a').addClass('over');
            if ($(this).children('ul').length > 0) {
                $(this).css({ "z-index": "2" });
                $(this).children('ul')
                //.animate({ "height": "hide" }, 2000)
                .animate({
                    "height": "show",
                    "opacity": "show"
                }, "slow", "swing", function () {
                    $(this).parent().addClass('over').css({ "z-index": "" });
                    $(this).css({ "height": "", "opacity": "", "display": "" });
                }).css('overflow', 'visible');
            } else {
                $(this).addClass('over');
            }

        },
        out: function () {
            $(this).children('ul').stop(true, false);
            $(this).children('a').removeClass('over');
            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');
            }
        },
        timeout: 500
    });

    $('.dropdown dd > ul').children('li').hoverIntent({
        over: function () {
            $('.dropdown dd > ul li').children('ul').stop(true, true).parent().removeClass('over');
            $(this).children('a').addClass('over');
            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');
            }

        },
        out: function () {
            $(this).children('ul').stop(true, false);
            $(this).children('a').removeClass('over');
            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');
            }
        },
        timeout: 200
    });

    $("a[href^=http://]").each(function () {
        var rexcludedomains = new RegExp(window.location.hostname, "i");
        if (this.hostname.search(rexcludedomains) == -1)
            $(this).attr("target", "_blank");
    });
    $("a[rel*=external], a[href^=/out-click.aspx]").each(function () {
        $(this).attr("target", "_blank");
    });


    // 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");

    });


});
