// Begin - Add in Alt & Title tags to the +/- for simple ADA compliance
if (typeof(jQuery) != 'undefined' && typeof(gui_loaded) == 'undefined') {
    //To make sure we only load this once
    var gui_loaded = true;

    $(document).ready(function () {

        // SR 07/06/2010 - Hack to allow buttons to appear properly in a updatepanel postback
        if (typeof (Sys) != "undefined") {
            try {
                var manager = Sys.WebForms.PageRequestManager.getInstance();
                manager.add_endRequest(initGUI_Buttons);
            }
            catch (err) { }
        }

        //Check for review mode and adjust the sidenav location
        var $alert_reviewsite = $("#alert_reviewsite"),
            $gui_menu = $('#gui_menu');

        if ($alert_reviewsite.length && $gui_menu.length) {
            $gui_menu.css({ top: ($gui_menu.offset().top + $alert_reviewsite.height() - 12) + "px" });
        }

        //Regular expression to see if we're in vosnet
        var checkPath = new RegExp(/\/vosnet\//i);
        var inVosnet = checkPath.test(window.location.pathname);

        // Category expanding
        var menuCookie = returnGUI_MenuCookie();
        var menuTrees = $("#gui_menu div h2");
        var storedAssistedUserId = (readCookie('assisted_userid')) ? parseInt(readCookie('assisted_userid')) : 0;
        var currAssistedUserId = (document.getElementById('gui_assisted_userid')) ? parseInt(document.getElementById('gui_assisted_userid').value) : 0;

        for (var i = 0; i < menuTrees.length; i++) {
            var menuHeader_Link = menuTrees[i].firstChild;
            var menuHeader_TreeControl = document.createElement("span");
            menuHeader_TreeControl.className = "expand";
            menuHeader_TreeControl.setAttribute("title", "Expand menu");
            menuHeader_TreeControl.onclick = function () { toggleGUI_Menu(this, 'expand'); return false; };
            menuHeader_TreeControl.appendChild(document.createTextNode(" "));
            menuTrees[i].insertBefore(menuHeader_TreeControl, menuHeader_Link);
            if (menuTrees[i].id == 'assisted_menu' && storedAssistedUserId != currAssistedUserId) {
                toggleGUI_Menu(menuHeader_TreeControl, 'collapse');
            }
            else {
                if (menuCookie) {
                    var menuCookie_Values = menuCookie.split("-");
                    if (menuCookie_Values[i] == "0")
                        toggleGUI_Menu(menuHeader_TreeControl, 'collapse');
                    else
                        toggleGUI_Menu(menuHeader_TreeControl, 'expand');
                }
                else {
                    toggleGUI_Menu(menuHeader_TreeControl, 'expand');
                }
            }
        }

        if (storedAssistedUserId != currAssistedUserId)
            createCookie('assisted_userid', currAssistedUserId);

        var menuItems = $("#gui_menu li"),
            docheight = ($("body").height() > $(document).height()) ? $("body").height() : $(document).height(),
            maxMenuHeight = 400;

        for (var i = 0; i < menuItems.length; i++) {
            if (menuItems[i].getElementsByTagName("ul").length > 0) {
                menuItems[i].firstChild.className = "parent";
                menuItems[i].firstChild.innerHTML = "<span>" + menuItems[i].firstChild.innerHTML + "</span>";
            }
        }

        //Bottom menu goes crazy...let's fix that
        var TopMenus = $("#gui_menu > div > ul > li");
        if (TopMenus.length > 0) {
            if (TopMenus[TopMenus.length - 1].getElementsByTagName("ul").length == 0) {
                TopMenus[TopMenus.length - 1].style.position = 'relative';
            }
        }

        $("#gui_menu div li").hover(function () {

            var $menu = $(this),
                $list = $menu.find("ul"),
                $anchor = $menu.find("a"),
                $list_items = $list.find("li"),
                list_menus_count = $list_items.length,
                $list_submenus = $list.find("ul");

            if ($list_items.length > 0) {
                if ($list_items[$list_items.length - 1].className != 'gui_menu_bottom2')
                    $list_items[$list_items.length - 1].className = 'gui_menu_bottom';
            }

            //Some browser trickery to fix some stuff
            if ($.browser.msie && (parseInt($.browser.version) < 8 || !inVosnet))
                $menu.css("display", "inline");
            else
                $menu.css("display", "block"); // set to block to fix FF issue

            $anchor.addClass("hover");
            $list.show(0);

            //Sub-menus are auto-showing on hover...let's stop that
            if ($list_submenus.length > 0) {
                $list_submenus.hide(0);
            }

            //Routine to figure out if menu should "fly-up", "fly-middle", or "shrink"
            if ($list[0]) {
                var m_height = ($list[0].clientHeight < maxMenuHeight) ? $list[0].clientHeight : maxMenuHeight,
                    menu_pos = $menu.offset().top + m_height,
                    avail_space = ($gui_menu.height() + $gui_menu.offset().top) - menu_pos,
                    top_pos = (m_height - $menu.height() - Math.round(list_menus_count / 3));

                if ((avail_space < 0 || m_height == maxMenuHeight) && $list.data('resized') != true) {
                    //Make sure we have the smaller one of the two so we don't move menus up way too much
                    top_pos = (Math.abs(avail_space) < top_pos) ? Math.abs(avail_space) : top_pos;
                    $list
                        .css({
                            top: "-" + top_pos + "px"
                        });
                    //additional handling when the menu is too long
                    if (m_height == maxMenuHeight) {
                        $list
                            .css({
                                height: m_height + "px",
                                "overflow-x": "hidden",
                                "overflow-y": "scroll",
                                "border-top": "2px outset #999999",
                                "border-bottom": "2px inset #999999",
                                width: '182px'
                            });
                    }
                    $list.data('resized', true);
                }
            }
        }, function () {

            var $elem = $(this),
                $list = $elem.find("ul"),
                $list_items = $list.find("li"),
                m_height = 0;

            if ($list[0]) {
                m_height = $list[0].clientHeight;
            }
            // put things back to normal
            $elem
                .removeClass("hover")
                .find("ul")
                .hide(0);

            if ($list_items.length > 0) {
                $list_items[$list_items.length - 1].className = 'gui_menu_bottom2';
            }

            $elem.css("display", "block");
        });

        initGUI_Buttons();
        initGUI_HelpBubbles();
    });

    function NavLinkClick(li) {
        var $link = $(li).find("a");
        window.location = $link.attr("href");
        return false;
    }
}

function initGUI_Buttons()
{
	if (document.createDocumentFragment)
	{
		var buttons = document.getElementsByTagName("input");
		if (buttons)
		{
			for (var i = 0; i < buttons.length; i++)
			{
				if ((buttons[i].type == "button" || buttons[i].type == "submit") && buttons[i].style.display != "none")
				{
					var buttonContainer = document.createDocumentFragment();
					var buttonTopLeft = document.createElement("span");
					buttonTopLeft.className = "gui_button_topleft";
					buttonTopLeft.setAttribute("name",buttons[i].value);
					var buttonTopRight = document.createElement("span");
					buttonTopRight.className = "gui_button_topright";
					var buttonBottomLeft = document.createElement("span");
					buttonBottomLeft.className = "gui_button_bottomleft";
					var buttonBottomRight = document.createElement("span");
					buttonBottomRight.className = "gui_button_bottomright";
					buttonTopLeft.appendChild(buttonTopRight);
					buttonTopRight.appendChild(buttonBottomLeft);
					buttonBottomLeft.appendChild(buttonBottomRight);
					buttonBottomRight.appendChild(buttons[i].cloneNode(true));
					buttonContainer.appendChild(buttonTopLeft);
					if (buttons[i].parentNode.className != "gui_button_bottomright") {
					    buttons[i].parentNode.replaceChild(buttonContainer, buttons[i]);
					}
				}
			}
		}

	}
}

function initGUI_HelpBubbles()
{
	if (document.getElementsByTagName)
	{
		var images = document.getElementsByTagName("img");
		if (images)
		{
			for (var i = 0; i < images.length; i++)
			{
				if (images[i].src.indexOf("help16") != -1)
				{
					images[i].style.verticalAlign = "bottom";		
				}
			}
		}
	}
}

function GUI_StartPage_ToggleNews(id)
{
	var newsCategories = document.getElementById("startpage_news").getElementsByTagName("ul");
	for (var i = 0; i < newsCategories.length; i++)
	{
		newsCategories[i].style.display = "none";
	}
	var newsContent = document.getElementById(id).getElementsByTagName("ul")[0];
	newsContent.style.display = "block";
}

function returnGUI_MenuCookie() {
    var cookies = document.cookie.split(";");
    for (var i = 0; i < cookies.length; i++) {
        while (cookies[i].charAt(0) == " ") cookies[i] = cookies[i].substring(1, cookies[i].length);
        if (cookies[i].indexOf("menuGroups") == 0) return unescape(cookies[i].substring(11, cookies[i].length));
    }
    return null;
}

function toggleGUI_Menu(treeControl, action) {
    var menuHeader = treeControl.parentNode;
    var menuHeader_Class = (action == "expand") ? "active" : "";
    menuHeader.className = menuHeader_Class;
    
    var treeControl_Class = (action == "expand") ? "collapse" : "expand";
    var treeControl_Action = (action == "expand") ? "collapse" : "expand";
    var treeControl_Title = (action == "expand") ? "Collapse menu" : "Expand menu";
    treeControl.className = treeControl_Class;
    treeControl.setAttribute("title", treeControl_Title);
    treeControl.onclick = function () { toggleGUI_Menu(this, treeControl_Action); return false; };
    
    var child = menuHeader.parentNode.getElementsByTagName("ul")[0];
    var child_Display = (action == "expand") ? "gui_menu_show" : "gui_menu_hide";
    child.className = child_Display;

    //Classic hack
    var childListItems = menuHeader.parentNode.getElementsByTagName("li");
    for (var i = 0; i < childListItems.length; i++) {
        childListItems[i].style.display = (action == "expand") ? "block" : "none";
    }

    var menuCookie_Values = "";
    var menuTrees = $("#gui_menu div h2");
    for (var i = 0; i < menuTrees.length; i++) {
        menuCookie_Values += (menuTrees[i].className == "active") ? "1-" : "0-";
    }
    menuCookie_Values = menuCookie_Values.substring(0, menuCookie_Values.length - 1);
    document.cookie = "menuGroups=" + menuCookie_Values + "; path=/";
}

var winSettings;
var rootpath = '';
function Settings() {
    var url = rootpath + "userprefs.asp?pm=1";
    var height = 500;
    var width = 620;
    if (navigator.appVersion.indexOf("MSIE") > -1 || (navigator.appName == "Netscape" && parseInt(navigator.appVersion.charAt(0)) >= 4)) {
        var h = (screen.availHeight - height) / 2;
        var w = (screen.availWidth - width) / 2;
        var center = ",top=" + h + ",left=" + w;
    }
    if (!winSettings || winSettings.closed) {
        winSettings = window.open(url, "winSettings", "resizable=no,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,height=" + height + ",width=" + width + center);
        if (!winSettings.opener) { winSettings.opener = window; }
    }
    else {
        winSettings.close();
        winSettings = window.open(url, "winSettings", "resizable=no,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,height=" + height + ",width=" + width + center);
        if (!winSettings.opener) { winSettings.opener = window; }
    }
}

/**
*  For use when having multiple window.onload events
**/
function addWindowLoadEvent(func)
{
    var oldonload = window.onload;
    if (typeof window.onload != 'function')
    {
        window.onload = func;
    }
    else
    {
        window.onload = function ()
        {
            if (oldonload)
            {
                oldonload();
            }
            func();
        }
    }
}

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = '; expires=' + date.toGMTString();
    }
    else {
        expires = '';
    }
    document.cookie = name + '=' + value + expires+ "; path=/";

}

function readCookie(name) {
    var nameEQ = name + '=';
    var ca = document.cookie.split(';');

    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];

        while (c.charAt(0) == ' ') {
            c = c.substring(1, c.length);
        }

        if (c.indexOf(nameEQ) == 0) {
            return c.substring(nameEQ.length, c.length);
        }
    }

    return null;
}

/* 
SR 12/29/2011 # 179919 Firefox has issues with printing fieldsets that span multiple pages.
Below are some functions to change fieldsets to divs.  This occurs on the onbeforeprint event and 
are reverted back on the onafterprint event.
*/

$(function () {
    //Check to see if browser is Firefox and supports onbeforeprint (Firefox 7.x+)
    if (navigator.userAgent.indexOf("Firefox") != -1 && window.onbeforeprint !== undefined) {
        window.onbeforeprint = removeFieldsets;
        window.onafterprint = addFieldsets;
    }
});

function removeFieldsets() {
    if (navigator.userAgent.indexOf("Firefox") != -1) {
        fs = $("fieldset fieldset fieldset");
        for (i = 0; i < fs.length; i++) {
            var that = $(fs[i]);
            var fieldsetContent = that.html();
            that.replaceWith('<div class="divfieldset3">' + fieldsetContent + '</div>');
        }
        fs = $("fieldset fieldset");
        for (i = 0; i < fs.length; i++) {
            var that = $(fs[i]);
            var fieldsetContent = that.html();
            that.replaceWith('<div class="divfieldset2">' + fieldsetContent + '</div>');
        }
        fs = $("fieldset");
        for (i = 0; i < fs.length; i++) {
            var that = $(fs[i]);
            var fieldsetContent = that.html();
            that.replaceWith('<div class="divfieldset1">' + fieldsetContent + '</div>');
        }
    }
}
function addFieldsets() {
    if (navigator.userAgent.indexOf("Firefox") != -1) {
        fs = $(".divfieldset3");
        for (i = 0; i < fs.length; i++) {
            var that = $(fs[i]);
            var fieldsetContent = that.html();
            that.replaceWith('<fieldset>' + fieldsetContent + '</fieldset>');
        }
        fs = $(".divfieldset2");
        for (i = 0; i < fs.length; i++) {
            var that = $(fs[i]);
            var fieldsetContent = that.html();
            that.replaceWith('<fieldset>' + fieldsetContent + '</fieldset>');
        }
        fs = $(".divfieldset1");
        for (i = 0; i < fs.length; i++) {
            var that = $(fs[i]);
            var fieldsetContent = that.html();
            that.replaceWith('<fieldset>' + fieldsetContent + '</fieldset>');
        }

    }
}
