
var menus = {
    data: {},
    index: 0,
    max: 9
};

function menuHover() {
    var x = this.offsetLeft;
    var w = Math.max($(this).width(), 140);

    // Hack to get around the IE6 z-index issue with popup DHTML content
    if ($.browser.msie && $.browser.version < 7) {
        $(".wrap-content-2 ul li select").css("visibility", "hidden");
    }

    $(this).css("background-color", "#0071bb");

    $("ul.dropdown", this).addClass("hover");
    $("ul.dropdown", this).css("left", x);
    $("ul.dropdown", this).css("width", w);

    $(this).children("a").css("text-decoration", "underline");
}

function menuOut() {
    $(this).css("background-color", "transparent");
    $(this).css("border", "none");

    $("ul.dropdown", this).removeClass("hover");
    $("ul.dropdown", this).css("left", "-20000px");

    $(this).children("a").css("text-decoration", "none");

    // Hack to get around the IE6 z-index issue with popup DHTML content
    if ($.browser.msie && $.browser.version < 7) {
        $(".wrap-content-2 ul li select").css("visibility", "visible");
    }

}

function createMore(context) {
    var li = $("<li>").appendTo(context);
    li.append("<a class='more'>more</a>").click(
            function() {
        menus.index += menus.max;
        $("#menus").html("");
        createMenus();
    }
        );

    return li;
}

function createPrev(context) {
    var li = $("<li>").appendTo(context);
    li.append("<a class='prev'>prev</a>").click(
            function() {
        menus.index -= menus.max;
        $("#menus").html("");
        createMenus();
    }
        );
    return li;
}

function createMenu(context, menu) {
    // get list item where the menu is placed
    var li = $("<li>");

    // add the menu link 
    li.append("<a href='" + menu.href + "'>" + menu.text + "</a>");

    // things go a little different when there are items vs. none
    if (menu.items != null && menu.items.length > 0) {
        // give it the assigned class
        li.addClass(menu.cssclass).appendTo(context).hover(menuHover, menuOut);

        // add dropdown control    
        var ul = $("<ul>").addClass("dropdown").appendTo(li);

        // add sub items
        $.each(menu.items, function(i, menuitem) {
            var li_item = $("<li>").appendTo(ul);
            li_item.append("<a class='" + menuitem.cssclass + "' href='" + menuitem.href + "'>" + menuitem.text + "</a>");
        });

    }
    else {
        li.addClass('menutitle_noarrow').appendTo(context).hover(menuHover, menuOut);
    }

    return li;
}

function createMenus() {
    if (menus.index > 0) {
        createPrev($("#menus"));
        $("#menus").append("<li class='menubreak'>|</li>");
    }

    for (i = menus.index; i < menus.data.length; i++) {

        if (i < menus.index + menus.max) {
            createMenu($("#menus"), menus.data[i]);
            if (i < menus.data.length - 1) {
                $("#menus").append("<li class='menubreak'>|</li>");
            }
        }
        else {
            createMore($("#menus"));
            break;
        }
    }
}

$(document).ready(function() {
    // Load the fly over menus  
    menus.data = HNavJSON;
    createMenus();
});

function IEbelow7() {
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
        var ieversion = new Number(RegExp.$1);
        if (ieversion < 7) {
            return true;
        }
        else { return false; }
    }
    else { return false; }
}

// This will expand a block of text with a "more..." link
// Text to expand must be in the next control on the page
function ShowExpBlock(Exp_Ctrl)
{
    $(Exp_Ctrl).parent().children('.ExpBlockLocator').hide().next().hide().next().show();
}

// This will hide a block of text with a "less..." link
// Text to hide must be in the parent control and the "more..." link the first child control in the next parent
function HideExpBlock(Hide_Ctrl)
{
    $(Hide_Ctrl).parent().parent().children('.ExpBlockLocator').show().next().show().next().hide();
}

/*
Scrolliing functions can be called from client controls with these properties...
class="ScrollingText"
title="The text that you wish to scroll"
onmouseover="ScrollTitle(this,true)"
onmouseout="ScrollTitle(this,false)" 
*/
// Global variables for controling text scrolling
var ScrollingOn = false;
var MouseOverScrollText = false;
var ScrollingPosition = 0;
var OrgScrollText = "";
var ScrollingText = "";
var ScrollingCtrl = null;

// Initiate starting or stopping scrolling text with the mouseover/mouseout event
function ScrollTitle(thisCtrl, mouseState) {
    // Set and track the mouseover event because setTimeout may recall the StartScrolling after the mouseout event
    MouseOverScrollText = mouseState;
    // Mouse IS over the control so we should start scrolling
    if (MouseOverScrollText) {
        // Set a global copy so we know if the recursive ScrollText is from the last mouseover
        ScrollingCtrl = thisCtrl;
        StartScrolling(thisCtrl);
    }
    // Mouse is NOT over the control so we should stop scrolling
    else {
        // Clear the control object and stop scrolling
        ScrollingCtrl == null;
        StopScrolling(thisCtrl); 
    }
}

function StartScrolling(thisCtrl) {
    // Scrolling should be false or else something is wrong.  Also compare the cntrl and mouseover for redundancy.
    if ((ScrollingOn == false) && (MouseOverScrollText == true) && (ScrollingCtrl == thisCtrl)) {
        // Do nothing if we don't have a scrolling text value
        if (thisCtrl.title != null) {
            // Set the original text from before scrolling
            OrgScrollText = $(thisCtrl).html();
            // Lock in width so that we do not shift when we scroll
            thisCtrl.style.width = $(thisCtrl).width() + "px";
            // Lock in height so that we do not shift when we scroll
            thisCtrl.style.height = $(thisCtrl).height() + "px";
            // Set the scrolling text
            ScrollingText = thisCtrl.title;
            // Clear the Title attribute so that we don't see a tooltip
            thisCtrl.title = "";
            // Reset the starting position to 0
            ScrollingPosition = 0;
            // Turn on the scrolling tracker
            ScrollingOn = true;
            // Call the scroll function
            ScrollText(thisCtrl);
        }
    }
}

function StopScrolling(thisCtrl) {
    if ((ScrollingOn == true) && (ScrollingCtrl == thisCtrl)) {
        // Turn off the scrolling tracker
        ScrollingOn = false;
        // Reset the control to the original text
        $(thisCtrl).html(OrgScrollText);
        // Return the Title attribute in case we need it later
        thisCtrl.title = ScrollingText;
        // Clear the original text from before scrolling
        OrgScrollText = "";
        // Clear the scrolling text
        ScrollingText = "";
        // Reset the starting position to 0
        ScrollingPosition = 0;
    }
}

function ScrollText(thisCtrl) {
    if ((ScrollingOn == true) && (ScrollingCtrl == thisCtrl)) {
        // If we're not at the end, scroll the text
        if (ScrollingPosition < (ScrollingText.length - (OrgScrollText.length-4))) {
            // Make a recursive call to continue scrolling the next character
            window.setTimeout(function() { ScrollText(thisCtrl); }, 100);
            // Get text to display this time through.  Stay 2 characters shorter since overflow:hidden trims oddly.
            var curText = ScrollingText.substring(ScrollingPosition, ScrollingPosition + OrgScrollText.length - 2);
            // Put text into the control
            $(thisCtrl).html(curText);
            // Increment out scrolling position to account for the change
            ScrollingPosition += 1;
        }
        // If we are at the end, restart the scroll
        else {
            // Pause at the end of the string, then clear the scroll
            window.setTimeout(function() { StopScrolling(thisCtrl); }, 2000);
            // Restart the scroll after a small pause to see the text
            window.setTimeout(function() { StartScrolling(thisCtrl); }, 4000);
        }
    }
}

// Call all the controls in the list and hides them if they are not large enough.
// This is for controls such as ads which may or may not exist for the client.
$(document).ready(
// Hides controls with HiddenCtrl class which are smaller than 20px.
// This is for controls with a class of 'HiddenCtrl', such as ads which may or may not exist for the client.
    function HideHiddenControls() {
    	$('.HiddenCtrl').each(function() {
    		if ($(this).height() < 20 && $(this).height() != null)
    		{ $(this).hide(); }
    	})
    }
);

    // This will launch a new window with a print preview
    function PrintPreview() {
    	var _url = location.href.toString();
    	var _queryIndex = _url.indexOf("?", 0)
    	var _bookmarkIndex = _url.indexOf("#", 0)
    	
    	// If we have a bookmark, remove it.
    	if (_bookmarkIndex > -1) {
    		_url = _url.substring(0, _bookmarkIndex);
    	}
    	// If we don't have a querystring we use "?"
    	if (_queryIndex > -1 && _queryIndex < _url.length) {
    		window.open(_url + "&printpreview=true", 'Preview', 'menubar=no,width=800,height=600,scrollbars=yes');
    	}
    	// If we do have a querystring we use "&"
    	else {
    		window.open(_url + "?printpreview=true", 'Preview', 'menubar=no,width=800,height=600,scrollbars=yes');
    	}
	}

    // This waits until the page is created to see if it is a PrintPreview
    $(document).ready(
    function PrintPreviewRequested() {
        // Get the querystrings "print" value
        if (qsValue("printpreview") == "true") {
            // Set the windows print stylesheet to an active screen stylesheet
            var i, a;
            for (i = 0; (a = document.getElementsByTagName("link")[i]); i++) {
                if (a.getAttribute("rel") == "stylesheet" && a.getAttribute("media") == "print") {
                    a.setAttribute("media", "all");
                    a.disabled = false;
                }
            }
        }
    }
    );
    
    function qsValue(id) {
        // Parses the query string into a name/value array
        var qs = location.search.slice(1).split("&"), GET = [];
        for (i in qs)
        { GET[qs[i].split("=")[0]] = qs[i].split("=")[1]; }
        // If there is no matching value, return an empty string
        if (GET[id] == "undefined")
        { return ""; }
        // Return the value matching the id name
        return GET[id];
    }
    
    // Hides the print button, opens a print dialog box, then closes the page
    function PrintAndClose() {
        $(".PrintAndClose").hide();
        window.print();
        window.opener = 'x';
        window.close();
    }


