﻿addListener(window, "load", init);

function init()
{
	// Set font size functionality
	var textLarger = document.getElementById("TextLarger");
	var textSmaller = document.getElementById("TextSmaller");

	if (textLarger)
	{
		addListener(textLarger, "click", changeFontSize);
	}
	if (textSmaller)
	{
		addListener(textSmaller, "click", changeFontSize);
	}
	
	
	// Hook up flyouts for IE 6
	if (document.all && !window.XMLHttpRequest)
	{   
	    //added in a check for "TopNav" as we are hiding this div on the CIS page.
	    if(document.getElementById("TopNav")){
		    var parents = document.getElementById("TopNav").getElementsByTagName("li");
		    for (var i=0; i<parents.length; i++)
		    {
			    if (parents[i].className != "")
			    {
				    parents[i].onmouseover = function()
				    {
					    this.className += " Hover";
				    }
				    parents[i].onmouseout = function()
				    {
					    this.className = this.className.replace("Hover", "");
				    }
			    }
		    }
		}
	}
}

function openWindow(url, name, width, height, resizable, scrollbars, statusbar, menubar, toolbar)
{
	var win = null;
	var optionString = "";
	if (width) optionString += "width=" + width + ",";
	if (height) optionString += "height=" + height + ",";
	if (resizable) optionString += "resizable=1,";
	if (scrollbars) optionString += "scrollbars=1,";
	if (statusbar) optionString += "status=1,";
	if (menubar) optionString += "menubar=1,";
	if (toolbar) optionString += "toolbar=1,";
	if (optionString != "") optionString = optionString.substr(0, optionString.length - 1);
	win = window.open(url, name, optionString);
	win.focus();
	return win;
}

function printPage()
{
	if (window.print) window.print();
	else alert("Please select \"Print...\" from the File menu.");
}

var subActive = false;
function subHover(e)
{
	subActive = true;
}
function subOut(e)
{
	subActive = false;
}

function parentHover(e)
{
	// Find top-most li
	var li = e.srcElement;
	while (li.tagName != "LI")
	{
		li = li.parentNode;
	}
	
	// Show the child ul
	if (li)
	{
		var uls = li.getElementsByTagName("ul");
		if (uls.length == 1)
		{
			uls[0].className = " Hover";
		}
	}
}

function parentOut(e)
{
	// Find top-most li
	window.status = e.srcElement.innerHTML;
	var li = e.srcElement;
	while (li.tagName != "LI")
	{
		li = li.parentNode;
	}
	
	// Show the child ul
	if (li)
	{
		var uls = li.getElementsByTagName("ul");
		if (uls.length == 1)
		{
			uls[0].className = uls[0].className.replace("Hover", "");
		}
	}
}

function setActiveStyleSheet(title)
{
	var i, a, main;
	for(i=0; (a = document.getElementsByTagName("link")[i]); i++)
	{
		if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title"))
		{
			a.disabled = true;
			if (a.getAttribute("title") == title) a.disabled = false;
		}
	}
}

function changeFontSize(e)
{
	
	var trigger = (e.srcElement) ? e.srcElement : e.target;
	
			
	
	var amount = (trigger.innerHTML.indexOf("Increase") != -1) ? 1 : -1;
	if (e.preventDefault)
	{
		e.preventDefault();
	}
	else
	{
		e.returnValue = false;
	}

	var currentFontSize = parseInt(getCookie("FontSize"));
	if (isNaN(currentFontSize)) currentFontSize = 0;
	currentFontSize += amount;
	if (currentFontSize > 1) currentFontSize = 1;
	else if (currentFontSize < -1) currentFontSize = -1;
	
	switch (currentFontSize)
	{
		case -1 :
		{
			setActiveStyleSheet("Small text size");
			break;
		}
		case 0 :
		{
			setActiveStyleSheet("Default text size");
			break;
		}
		case 1 :
		{
			setActiveStyleSheet("Large text size");
			break;
		}
	}
	
	var nextYear = new Date();
	nextYear.setFullYear(nextYear.getFullYear() + 1);
	setCookie("FontSize", currentFontSize, nextYear);
}

function trim(theString)
{
	var newString = theString;
	while (newString.charAt(0) == " " || newString.charCodeAt(0) == 10 || newString.charCodeAt(0) == 13 || newString.charCodeAt(0) == 9)
	{
		newString = newString.substring(1,newString.length);
	}
	while (newString.charAt(newString.length - 1) == " " || newString.charCodeAt(newString.length - 1) == 10 || newString.charCodeAt(newString.length - 1) == 13 || newString.charCodeAt(newString.length - 1) == 9)
	{
		newString = newString.substring(0,newString.length - 1);
	}
	return newString;
}

function getCookie(name)
{
	var cookies = document.cookie.split(";")
	for (var i=0; i<cookies.length; i++)
	{
		var nameVal = cookies[i].split("=");
		if (nameVal.length == 2 && trim(nameVal[0]) == trim(name))
		{
			return trim(nameVal[1]);
		}
	}
	return "";
}

function setCookie(name, value, expires)
{
	document.cookie = name + "=" + value + ";path=/;" + (expires ? " expires=" + expires.toGMTString() + ";" : "");
}

function removeCookie(name)
{
	setCookie(name, "", new Date("2 January 1970"));
}

function getObjectWidth(obj)
{
	var result = 0;
	if (obj.offsetWidth) result = obj.offsetWidth;
	else if (obj.clip && obj.clip.width) result = obj.clip.width;
	else if (obj.style && obj.style.pixelWidth) result = obj.style.pixelWidth;
	return parseInt(result);
}

function getObjectHeight(obj)
{
	var result = 0;
	if (obj.offsetHeight) result = obj.offsetHeight;
	else if (obj.clip && obj.clip.height) result = obj.clip.height;
	else if (obj.style && obj.style.pixelHeight) result = obj.style.pixelHeight;
	return parseInt(result);
}

function addListener(object, eventType, handler)
{
	if (window.attachEvent)
	{
		object.attachEvent("on" + eventType, handler);
	}
	else if (window.addEventListener)
	{
		object.addEventListener(eventType, handler, false);
	}
}


function showSelected(object, block)
{
    var infoBlocks = document.getElementById("PortalHome").getElementsByTagName("div");
    for (var i=0; i<infoBlocks.length; i++)
    {
        infoBlocks[i].style.display = "none";
    }
    var portalLinks = document.getElementById("PortalHome").getElementsByTagName("li");
    for (var i=0; i<portalLinks.length; i++)
    {
        if(portalLinks[i].className != "Last")
        {
            portalLinks[i].className = "";
        }
    }
    var linkParent = document.getElementById(object).parentNode;
    linkParent.className = "Selected";
    var displayBlock = document.getElementById(block);
    displayBlock.style.display = "block";
}
