﻿/// <reference path="External/jquery-1.3.2-vsdoc2.js" />
/// <reference path="GAWrap-1.js" />

$(document).ready(function () {
    accordions();
    GATracking();
    ie6fixes();
    bottomToolbar();
    alterMenuForVerticalAlign();
    fixLastMenu();
    alterMoreButtons();
    alterRightNavForLogin();
    fixLongBreadCrumbs();
});

function accordions() {
    $('.accordion .item .item-header a').click(function(e) {
        e.preventDefault();

        var accordionItem = $(this).parents('.item:first');
        var accordion = $(this).parents('.accordion:first');
        var isSelected = accordionItem.hasClass('selected');

        $('.item.selected', accordion).each(function() {
            var $this = $(this);
            $('.item-content', this).slideUp('normal', function() {
                $this.removeClass('selected');
            });
        });

        if (isSelected == false) {
            accordionItem.addClass('selected');
            $('.item-content', accordionItem).slideDown('normal');
        }
    }).each(function() {
        var $this = $(this);
        var hash = window.location.hash;

        var href = $this.attr('href');
        var index = href.indexOf('#');
        if(index > -1)
            href = href.substring(index);

        if (hash.length > 0 && hash == href) {
            var accordionItem = $this.parents('.item:first');
            accordionItem.addClass('selected');
            $('.item-content', accordionItem).slideDown('normal');
        }
    });
};

function GATracking() {
    var g = new GAWrap(environment, trackingData);
};

function fixFloatElement() {
    function getClientHeight() {
        return typeof (window.innerHeight) !== "undefined" ?
            window.innerHeight :
            document.documentElement.clientHeight;
    }
    // get client height
    var offset = getClientHeight();

    // get the element you want to change position to
    var footer = document.getElementById('bottomToolbar');
    // set styles to the element to emulate position:fixed
    footer.style.position = 'absolute';
    footer.style.top = (document.documentElement.scrollTop + offset - footer.offsetHeight) + 'px';
}

function ie6fixes() {

    if ($('.IE6').length > 0) {
        $(window).bind('resize', fixFloatElement);
        $(window).bind('scroll', fixFloatElement);
        $(window).bind('load', fixFloatElement);
    }

    var hasArray = ['hasChildren', 'contact', 'share', 'search', 'sitemap'];
    var i = 0;
    var count = hasArray.length;
    var hasClass = '';

    $('.IE6 .mainMenu li, .IE6 #bottomToolbar ul.social li, .IE6 #bottomToolbar ul.navigation li').hover(function() {
        var s = $(this);
        s.addClass('hover');

        for (i = 0; i < count; i++) {
            hasClass = hasArray[i];
            if (s.hasClass(hasClass) == true) {
                s.addClass(hasClass + 'Hover');
            }
        }

    }, function() {
        $(this).removeClass('hover');
        for (i = 0; i < count; i++) {
            hasClass = hasArray[i];
            $(this).removeClass(hasClass + 'Hover');
        }
    });
};

function bottomToolbar() {
    $('#bottomToolbar li.search input').watermark('Search the site');

    $('#bottomToolbar li a[href="?"]').click(function(e) {
        e.preventDefault();
    });

    $('#bottomToolbar li.print form a').click(function(e) {
        $(this).parent().trigger('submit');
    });

    $('#bottomToolbar li.contact input[type=text], #bottomToolbar li.contact textarea').click(function(e) {
        var $this = $(this);
        var tab = $this.parents('li.tab:first');
        // So the tab doesn't hide
        tab.addClass('contactShow');

        e.preventDefault();
    });

    $('#bottomToolbar li.contact a.close').click(function(e) {
        var $this = $(this);
        var tab = $this.parents('li.tab:first');
        tab.removeClass('contactShow');

        var content = $this.parents('.content:first');
        content.removeClass('thankyou');

        e.preventDefault();
        e.stopPropagation();
    });

    $('#bottomToolbar li.share a.close').click(function(e) {
        var $this = $(this);
        var tab = $this.parents('li.tab:first');
        tab.removeClass('shareShow');

        var content = $this.parents('.content:first');
        content.removeClass('emailpage');
        content.removeClass('thankyou');

        e.preventDefault();
    });

    $('#bottomToolbar li.share a.email').click(function(e) {
        var $this = $(this);
        var tab = $this.parents('li.tab:first');
        // So the tab doesn't hide
        tab.addClass('shareShow');

        var content = $this.parents('.content:first');
        content.addClass('emailpage');

        e.preventDefault();
    });

    function showErrors(errorMap, errorList) {
        with (this) {
            if (errorList.length > 0) {
                $(errorList).each(function() {
                    $("[for='" + idOrName(this.element) + "']").addClass(" LabelError");
                });
            } else {
                $(this.lastElement).each(function() {
                    $("[for='" + idOrName(this) + "']").removeClass("LabelError");
                });
            }
        }
        // uncomment or implement if needed this.defaultShowErrors();
    };

    //Hook form submission up to Submission handler page
    function submitHandler(form) {
        $(form).ajaxSubmit({
            target: ".EmailReturnMessage",
            success: function() {
                $(form).clearForm();
                var content = $(form).parents('.content:first');
                content.removeClass('emailpage');
                content.addClass('thankyou');
            }
        });
        return false;
    }

    $('#bottomToolbar li.share .emailpage form').validate({
        showErrors: showErrors,
        submitHandler: submitHandler
    });

    $('#bottomToolbar li.contact form').validate({
        showErrors: showErrors,
        submitHandler: submitHandler
    });

    // $('#bottomToolbar .navigation li.sitemap').remove();
};

jQuery.validator.addMethod("orange", function (value, element) {
    var elementValue = $(element).val().toLowerCase();

    //no value, but perhaps there is something in the element it self
    if (!elementValue || elementValue.length == 0) {
        elementValue = $(element).html();
    }
    //if we still dont have anything invalid!
    if (!elementValue || elementValue.length == 0) {
        return false;
    }

    if (elementValue && elementValue == "orange" && elementValue.length > 0) {
        //we are safe!
        //at least for the constraints i care about.
        return true;
    }
    //nothing good can happen past here
    return false;
});

function fixLastMenu() {
    $("ul.purpleMenu li:last").addClass('last');
}

function DoSearch(searchBoxClientID) {
    var searchQuery = $("#" + searchBoxClientID).val();
    if (searchQuery == "") {
        alert("Please enter one or more search terms.");
        return false;
    }
}

function alterMoreButtons() {
    $('.More').each(function (i) {
        $(this).wrapInner('<span class="Label" />');
        $(this).append('<span class="Button" />');
    });
}

function alterMenuForVerticalAlign() {
    $('ul#SubMenu li a, ul#SubMenu li span').not('.left ul#SubMenu li ul li ul li a, .left ul#SubMenu li ul li ul li span').each(function () {
        if ($(this).text().length > 21) {
            $(this).css({ 'line-height': '14px','position':'relative','top':'2px' });
            $(this).parent('li').height(34);
        }
    });

    var LI;
    if ($('ul#SubMenu li ul li ul li').length > 0) // 3 levels of menus 
    {
        var lvl1LI = $('ul#SubMenu > .CMSListMenuLI');
        if ($('ul#SubMenu .CMSListMenuLI .CMSListMenuHighlightedLI').length > 0) {
            $(lvl1LI).find('a:eq(0)').css({ 'font-weight': 'bold', 'color': '#FFF' });
        }

        LI = $('ul#SubMenu li ul li ul li.CMSListMenuHighlightedLI').parent().parent();
        $(LI).css({ 'background': '#beb3ce' });
        $(LI).find('a').not('ul li ul li ul li a').css({ 'font-weight': 'bold', 'border-bottom': '0' });
        $('ul#SubMenu li ul li ul li.CMSListMenuHighlightedLI').parent().show();
    }
    else { // only 2.  highlight only the menu above selected
        LI = $('ul#SubMenu li ul li.CMSListMenuHighlightedLI').parent().parent();
        $(LI).find('a').not('ul li ul li a').css({ 'font-weight': 'bold', 'border-bottom': '0', 'color': '#FFF' });
        $('ul#SubMenu li ul').not('ul#SubMenu li.CMSListMenuHighlightedLI ul').hide();
        $('ul#SubMenu li ul li.CMSListMenuHighlightedLI').parent().show();
    }

}

function alterRightNavForLogin() {
    if ($('.right table .submitForm').length > 0) {
        $('.right').css('margin-left', '0');
    }
}

function fixLongBreadCrumbs() {
    var breadCrumbWidth = $('.BreadCrumbs').width();
    var totalWidth = 0;

    $('.BreadCrumbs').find('.CMSBreadCrumbsLink, .GreaterThan, .CMSBreadCrumbsCurrentItem').each(function () {
        totalWidth += $(this).outerWidth();
    });

    // drop first crumb if too long.
    while (totalWidth > breadCrumbWidth) {
        $(".BreadCrumbs .CMSBreadCrumbsLink:eq(0), .BreadCrumbs .GreaterThan:eq(0)").each(function () {
            totalWidth -= $(this).outerWidth();
            $(this).remove();
        })
    }
}
