// JavaScript Document


// This searches the site via the header search
function searchSite() {
    alert("Search needs work.");
    window.location = "/search/";
}


// This highlights the active link in the Left Navigation
function leftNavHighlight() {

    leftNavFirstLast();

    var theNav = document.getElementById('siteLeftNav');
    var currentUrl = window.location.href;

    if (theNav) {


        var navLinks = theNav.getElementsByTagName('a');

        for (j = 0; j < navLinks.length; j++) {

            //alert(navLinks[j].href + " : " + location.href);
            //alert(currentUrl.substr(0, currentUrl.length-1));

            //remember active link and set it after the loop. this causes only the last matching item to be highlighted.
            if (typeof(activeLink) != "undefined" && activeLink != null && (navLinks[j].href == currentUrl || navLinks[j].href == currentUrl.substr(0, currentUrl.length - 1) || navLinks[j] + "default.aspx" == currentUrl || navLinks[j] == currentUrl + "default.aspx")) {
                activeLink = navLinks[j];
            }

        }

        if (typeof (activeLink) != "undefined" && activeLink != null)
            activeLink.className = "mmhide_active";
    }
}


// This sets class="first" or class="last" on the first and last <li> tags in the Left Navigation
function leftNavFirstLast() {

    var siteLeftNav = document.getElementById('siteLeftNav');

    if (siteLeftNav) {
        listItems = siteLeftNav.getElementsByTagName('li');

        //start at the front and move forward to find the first top level link
        for (li = 0; li < listItems.length; li++) {
            if (listItems[li].parentNode.parentNode.id == "siteLeftNav") {
                listItems[li].className = "mmhide_first";
                break;
            }
        }

        if (listItems.length > 0) {
            //start at the end and move backwards to find the last top level link
            for (li = listItems.length - 1; li > 0; li--) {
                if (listItems[li].parentNode.parentNode.id == "siteLeftNav") {
                    listItems[li].className = "mmhide_last";
                    break;
                }
            }
        }

    }

}




// This sets the mmhide_active class on the appropriate gloval nav link
function navHighlight() {

    theNav = document.getElementById('siteHeader');
    currentPage = window.location.href;

    if (theNav) {
        navLinks = theNav.getElementsByTagName('a');

        for (h = 0; h < navLinks.length; h++) {

            //get the first folder in nav link
            tmpSplitLink = navLinks[h].href.split("/");
            while (tmpSplitLink[3].lastIndexOf('.aspx') > 0 || tmpSplitLink[tmpSplitLink.length - 1] == "") {
                tmpSplitLink.pop();
            }

            //get the last folder in the current location
            currentSplitLink = currentPage.split("/");
            if (currentSplitLink.length > 3) {
                while (currentSplitLink[3].lastIndexOf('.aspx') > 0 || tmpSplitLink[currentSplitLink.length - 1] == "") {
                    currentSplitLink.pop();
                }
            }

            //even the arrays	
            while (currentSplitLink.length > tmpSplitLink.length) {
                currentSplitLink.pop();
            }
            while (tmpSplitLink.length > currentSplitLink.length) {
                tmpSplitLink.pop();
            }


            //alert(tmpSplitLink + " : " + currentSplitLink);

            //see if they're a match
            matchFound = true;
            for (m = 0; m < tmpSplitLink.length; m++) {

                if (tmpSplitLink[m] != currentSplitLink[m]) {

                    matchFound = false;
                }
            }
            if (matchFound) {


                if (navLinks[h].className == "") {
                    navLinks[h].className = "mmhide_active";
                } else {
                    navLinks[h].className += " mmhide_active";
                }
            }
            //alert(tmpSplitLink.length + " : " + currentSplitLink.length);
        }
    }
}








function setHomeBanner() {
    try {

        var banner = $("#homeBanner");
        var bannerSource;

        var reflection = $(".mmhide_home");
        var reflectionSource;

        var homeBannerCookie = parseInt(getCookie("homeBanner"));
        var maxBanners = 4;

        banner.css("background-image", "");
        banner.css("display", "none");

        if (homeBannerCookie > 0 && homeBannerCookie != maxBanners) {
            bannerSource = "/home/images/home-banner" + (homeBannerCookie + 1) + ".jpg";
            reflectionSource = "/home/images/home-reflection" + (homeBannerCookie + 1) + ".jpg";
            setCookie("homeBanner", (homeBannerCookie + 1));
        } else {
            bannerSource = "/home/images/home-banner1.jpg";
            reflectionSource = "/home/images/home-reflection1.jpg";
            setCookie("homeBanner", 1);

        }

        banner.attr("src", bannerSource);
        banner.animate({ opacity: 'show' }, 'slow');

        reflection.css("background-image", "url(" + reflectionSource + ")");


        //localTrace(bannerSource);
    } catch (err) {
        localTrace("Could not set random banner.\n" + err);
    }
}








function inputStyles() {
    pageInputs = document.getElementsByTagName('input');


    //style buttons
    for (pi = 0; pi < pageInputs.length; pi++) {
        if (pageInputs[pi].className.length <= 0) {
            if (pageInputs[pi].type == 'submit' || pageInputs[pi].type == 'reset' || pageInputs[pi].type == 'button') {
                pageInputs[pi].className = "input-button";
            } else if (pageInputs[pi].type == "checkbox" || pageInputs[pi].type == "radio") {
                pageInputs[pi].className = "input-checkbox";
            }
        }
    }

}




function setCookie(Name, Value) {
    document.cookie = Name + "=" + Value;
}



function getCookie(Name) {
    var search = Name + "="
    var returnvalue = "";

    if (document.cookie.length > 0) {
        offset = document.cookie.indexOf(search)
        // if cookie exists
        if (offset != -1) {
            offset += search.length
            // set index of beginning of value
            end = document.cookie.indexOf(";", offset);
            // set index of end of cookie value
            if (end == -1) end = document.cookie.length;
            returnvalue = unescape(document.cookie.substring(offset, end))
        }
    }

    return returnvalue;
}



function localTrace(msg) {
    if (debug) {
        try {
            console.log(msg);
        } catch (ex) {
            //alert(ex.message);	
            alert(msg);
        }
    }
}
