/*******************************************************************************
____________________________ API DOCUMENTATION BEGIN ___________________________
````````````````````````````````````````````````````````````````````````````````
Variables and functions used throughout website.

````````````````````````````````````````````````````````````````````````````````
_____________________________ API DOCUMENTATION END ____________________________
*******************************************************************************/

$(function () {
    if ($('body').hasClass('home')) {
        var max = 0;
        $('.callouts .callout').each(function () {
            if ($(this).height() > max)
                max = $(this).height();
        });
        $('.callouts .callout').each(function () {
            $(this).css('padding-bottom', max - $(this).height());
        });
    }
    if ($('body').hasClass('interior')) {
        $('.calloutBorder .callout').height($('.calloutBorder .callout div').height() + 35);
        $('.holder').height($('.calloutBorder .callout').height() + 88);
    }

    $('.slide').each(function () {
        $(this).next().hide();
    });
    $('.slide').click(function () {
        $(this).next().slideToggle();
        return false;
    });

    $('.btnEnlarge').each(function () {
        var message = $('.wrapper>div:last').clone();
        message.find('.qi_msg').addClass('qi_custom');
        if ($(this).hasClass('wide'))
            message.find('.btnClose').addClass('wide');
        $(this).children().clone(true).prependTo(message.children('div.qi_msg'));
        $(this).qi_dialog({
            type: 'custom',
            msg: message.html(),
            mask_opacity: .5,
            fn_launch: function () {
                $('.qi_msg').width($('.qi_msg img').width());
                setImageBehav($('.qi_msg img'));
                $('.qi_cancel').css('top', $('.qi_msg img').height() - 37 + 'px');
            }
        });
    });

    $('.flipper').each(function () {
        if (flashInstalled && flashVersion >= 9) {
            var message = $('.wrapper>div:last').clone();
            message.find('.qi_msg').addClass('qi_flipper').children().remove();
            message.find('.qi_msg').append('<iframe frameborder="0" src="' + $(this).attr('href') + '"></iframe>');

            $(this).qi_dialog({
                type: 'custom',
                msg: message.html(),
                mask_opacity: .5
            });
        }
        else {
            $(this).click(function () { $('a.noflash').click(); return false; });
        }
    });

    setImageBehav($('.chartBox img'));

    $('.topCallout').each(function () {
        $(this).mouseenter(function () {
            $('a.topCallout').addClass("calloutHover");
        });
        $(this).mouseleave(function () {
            $('a.topCallout').removeClass("calloutHover");
        });
        $(this).click(function () {
            $('a.topCallout').click();
        });
    });

    $('.ext, .ext1, .pdf').each(function () {
        var e = $(this);
        var t = (e.hasClass('ext') ? 'ext' : (e.hasClass('ext1') ? 'ext1' : 'pdf'));
        e.qi_dialog({ type: t, mask_opacity: .5, popup_title: getPopupTitle(e) });
    });

    $('.pdf').click(function () {
        var link = $(this).attr('href').replace(".aspx", ".pdf");
        if (link.charAt(0) == '.')
            link = link.substring(1);
        if (link.charAt(0) != '/')
            link = '/' + link;

        _gaq.push(['_trackPageview', link]);
    });

    $('#nav li a').mouseenter(function () {
        if (!$(this).parent().prev('#nav li').hasClass('open')) {
            $(this).parent().prev('#nav li').children('a').addClass("fullBorderOver");
            if ($(this).parent().hasClass('firstSubNav'))
                $(this).parent().parent().prev('a').addClass('fullBorderOver');
        }
    });
    $('#nav li a').mouseleave(function () {
        $(this).parent().prev('#nav li').children('a').removeClass("fullBorderOver");
        $(this).parent().parent().prev('a').removeClass('fullBorderOver');
    });

    $('.hideReg .register').attr('href', '#Form');

    $('.chartBox').each(function () {
        $(this).width($(this).children('img').width());
    });

    $('.continue').height($('.continue .toFront').height());

    $('.wrapper').prepend($('.CMSEditableRegionEdit'));
});

function trimPopupTitle(value, title) {
    
    var text = ($.trim(value) != "" ? value : title);
    if (text.length > 60) {
        var index = text.indexOf(" ", 55);
        var res = text.substr(0, index);
        if (res.charAt(res.length - 1) == ',' || res.charAt(res.length - 1) == '.' || res.charAt(res.length - 1) == ':' || res.charAt(res.length - 1) == ';' || res.charAt(res.length - 1) == '-')
            res = res.substr(0, res.length - 1);
        return res + "...";
    }
    return text;
}

function getPopupTitle(obj) {
    var value = "";
    if (obj.children('span').length > 0)
        value = obj.children('span:first').html();
    else if ($.trim(obj.text()) != "")
        value = obj.html();
    else
        value = "";
    return trimPopupTitle(value, obj.attr('title'));
}

function setImageBehav(img) {
    img.mouseenter(function () {
        $(this).parent().addClass("chartBoxHover");
    });
    img.mouseleave(function () {
        $(this).parent().removeClass("chartBoxHover");
    });
    img.click(function () {
        $(this).siblings('a').click();
    });
}

function highlightMenu(sitemap) {
    var id = sitemap;
    if ($('#' + id)) {
        $('#' + id).parent().addClass('sel');
        $('#' + id).parents('#nav li').addClass('open');

        $('li.open').find('a:last').each(function () {
            if (!$(this).parent().hasClass('subNav'))
                $(this).addClass('fullBorder');
        });
        $('li.sel, li.open').prev('#nav li').children('a').addClass('fullBorder');
        if ($('li.sel').hasClass('firstSubNav'))
            $('li.sel').parent().prev('a').addClass('fullBorder');
    }
}
