﻿$(document).ready(function () {

    // 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");

    });

    // Sets default widths for dropdown menus.
    $('.dropdown dd:has(ul)').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("a").first().clone().removeClass("ir").insertBefore($(this).children('ul').children("li").first()).wrap('<li></li>').append(' Overview');

        $(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() > $('#wrapper').offset().left + $('#wrapper').innerWidth() - 17) {
                $$.css('left', 'auto');
                $$.css('right', count + 'px');
                count++;
            }

            $$.css({ 'display': '', 'visibility': '' })
            $$.children().css({ 'white-space': 'normal', 'width': '100%' });

            $$.prev('a').addClass('haschildren');
        });
    });

    // Dropdown hover function
    $('.dropdown dd:has(ul) > a').toggle(function () {
        $('.dropdown dd.active > a').click();

        if ($(this).parent().children('ul').length > 0) {
            $(this).parent().children('ul')
            .animate({ "height": "hide" }, 2000)
            .animate({
                "height": "show",
                "opacity": "show"
            }, "slow", "swing", function () {
                $(this).parent().addClass('active');
                $(this).css({ "height": "", "opacity": "", "display": "" });
            });
        } else {
            $(this).addClass('active');
        }

    }, function () {
        $(this).children('ul').stop(true, false);
        if ($(this).parent().children('ul').length > 0) {
            $(this).parent().children('ul')
                .animate({ "height": "show" }, 5000)
                .animate({
                    "height": "hide",
                    "opacity": "hide"
                }, "slow", "swing", function () {
                    $(this).parent().removeClass('active');
                    $(this).css({ "height": "", "opacity": "", "display": "" });
                });
        } else {
            $(this).removeClass('active');
        }
    });

    //    $('#mainnav dd a.AT').each(function () {
    //        $$ = $(this).parent().prev();

    //        $$.children("a").first().css("background-image", $(this).parent().css("background-image"));
    //    });

    $('#mainnav dd, #secondnav dd').hover(function () {
        $$ = $(this).prev();
        $(this).addClass("over");

        $$.children("a").first().css("background-image", $(this).css("background-image"));

    }, function () {
        $$ = $(this).prev();
        $(this).removeClass("over");

        $$.children("a").first().css("background-image", "");
    });

    /*$('#secondnav dd').hover(function () {
        $$ = $(this).prev();
        $(this).addClass("over");

        $$.children("a").first().css("background-image", $(this).children("a").first().css("background-image"));

    }, function () {
        $$ = $(this).prev();
        $(this).removeClass("over");

        $$.children("a").first().css("background-image", "");
    });*/

    // Open offsite links in new window.
    $("a[href^=http://]").each(function () {
        var rexcludedomains = new RegExp(window.location.hostname, "i");
        if (this.hostname.search(rexcludedomains) == -1)
            $(this).attr("target", "_blank");
    });

    // Open links set as external and pdfs in a new window.
    $("a[rel*=external], a[href$=.pdf]").each(function () {
        $(this).attr("target", "_blank");
    });

    $("ul.tabs").tabs("div.panes > div");

    $("ul.tabs a").click(function () {
        $$ = $(this).parent().parent();

        $$.find('a').each(function () {
            if ($(this).hasClass("current")) {
                $$.addClass($(this).attr("href").replace("#", ""))
            } else {
                $$.removeClass($(this).attr("href").replace("#", ""))
            }
        })
    });

    $('a.event').each(function () {
        $(this).bt({
            ajaxPath: ["$(this).attr('href') + '&popup=1'", 'div#eventsListing'],
            trigger: ['none', 'click'],
            closeWhenOthersOpen: true,
            width: '300px',
            centerPointY: .1,
            spikeGirth: 15,
            spikeLength: 25,
            cornerRadius: 25,
            fill: '#FFF',
            strokeStyle: '#71849D',
            shadow: true,
            shadowColor: '#71849D',
            noShadowOpts: { strokeStyle: '#71849D' },
            shadowBlur: 12,
            shadowOffsetX: 0,
            shadowOffsetY: 5,
            cssStyles: {
                minHeight: '150px'
            },
            preShow: function (box) {
                setTimeout("ReplaceFonts()", 1000);
            }
        })
    });

    $('a.event').hoverIntent({
        over: function () { $(this).btOn(); },
        out: function () { /*do nothing *//*$(this).btOff();*/ },
        interval: 500
    });

    setTimeout("ReplaceFonts()", 1000);
});

function ReplaceFonts() {
    if (!$.font.test('DroidSerifRegular')) {
        Cufon.replace('.times, h1, h2:not([class="pageHead"]), h3, h4, h5, h6', { fontFamily: 'Times LT Std' });
    }

    if (!$.font.test('LeagueGothicRegular')) {
        Cufon.replace('.gothic, .pageHead', { fontFamily: 'Alternate Gothic No2 D' });
    }
}
