/* Top Nav Scripts - Begin */
function prepareHeaderMenus(language)
{
	if (document.getElementById("topNav"))
	{
		if (document.getElementById("topNav").getElementsByTagName("a").length > 0)
		{
			initTopNavMenus(language);
		}
	}
}
function findPosX(obj)
{
	var curleft = 0;
	if(obj.offsetParent)
	while(1) 
	{
	curleft += obj.offsetLeft;
	if(!obj.offsetParent)
	break;
	obj = obj.offsetParent;
	}
	else if(obj.x)
	curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if(obj.offsetParent)
	while(1)
	{
	curtop += obj.offsetTop;
	if(!obj.offsetParent)
	break;
	obj = obj.offsetParent;
	}
	else if(obj.y)
	curtop += obj.y;
	return curtop;
}
function hoverBranchImg(imgElem)
{
	if (imgElem.src.indexOf("_off") > 0)
	{
		imgElem.src = imgElem.src.replace("_off","_on");
	}
	else
	{
		imgElem.src = imgElem.src.replace("_on","_off");
	}
}
function hoverTopNavItem(indexVal,language)
{
	var topNavImg = document.getElementById("hdr_nav" + indexVal + "_img");
	var topNavMenu = document.getElementById("topNav" + indexVal + "Menu");
	// Activation of the drop-down
	if (topNavImg)
	{
		hoverTopNavImg(indexVal);
	}
	if (topNavImg.src.indexOf("_on") > 0)
	{
		if (topNavMenu)
		{
			postionTopNavMenu(topNavImg,topNavMenu);
			toggleDisplay(topNavMenu);
		}
	}
	else
	{
		toggleDisplay(topNavMenu);
	}
}
function hoverTopNavImg(indexVal)
{
	if (selectedTopNavItem == 0 || selectedTopNavItem != indexVal)
	{
		/*
		if (selectedTopNavItem != 0)
		{
			hoverBranchImg(document.getElementById("hdr_nav" + selectedTopNavItem + "_img"));
		}
		*/
		hoverBranchImg(document.getElementById("hdr_nav" + indexVal + "_img"));
	}
}
function postionTopNavMenu(refItem,targetItem)
{
	var isIE = /*@cc_on!@*/false;
	if (isIE)
	{
		var xPos = findPosX(refItem) - 2;
	}
	else
	{
		var xPos = findPosX(refItem) - 3;
	}
	
	var yPos = findPosY(refItem) + refItem.clientHeight;
	var widthVal = refItem.clientWidth + 4;
	targetItem.style.left = xPos + "px";
	targetItem.style.top = yPos + "px";
	targetItem.style.width = widthVal + "px";
}
function toggleDisplay(obj)
{
	if (obj.style.display)
	{
		if (obj.style.display == "block")
		{
			obj.style.display = "none";
		}
		else
		{
			obj.style.display = "block";
		}
	}
	else
	{
		obj.style.display = "block";
	}
}
function newXMLHttpRequest()
{   var xmlreq = false;
	if (window.XMLHttpRequest) {
		// Create XMLHttpRequest object in non-Microsoft browsers
		xmlreq = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		// Create XMLHttpRequest via MS ActiveX
		try {
			// Try to create XMLHttpRequest in later versions
			// of Internet Explorer
			xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e1) {
			try {
				// Try version supported by older versions
				// of Internet Explorer
				xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2)
			{  doError(e2);
			}
		}
	}
	return xmlreq;
}
function getInitTopNavMenusHandler(req,language)
{
	return function () 
	{
		if (req.readyState == 4)
		{
			if (req.status == 200)
			{
				var response = req.responseXML;
				var menuCount = document.getElementById("topNav").getElementsByTagName("a").length;
				for (ii = 0; ii < menuCount; ii++)
				{
					buildTopNavMenu(response,ii,language);	
				}
			}
		}	
	}
}
function initTopNavMenus(language)
{
	try { document.execCommand('BackgroundImageCache', false, true);} catch(e) {}
	url = '/_LAYOUTS/1033/CanaccordPCS/XML/LeftNav.xml';
	async = true;
	var req = newXMLHttpRequest();
	var handlerFunction = getInitTopNavMenusHandler(req,language);
	req.onreadystatechange = handlerFunction;
	req.open('GET',url,async);
	req.send(null);
	
}
function buildTopNavMenu(xmlObj,categoryIndex,language)
{
	var isIE = /*@cc_on!@*/false;
	// Check if the selected top nav item needs to be updated
	if (selectedTopNavItem == 0)
	{
		if (checkForSelectedTopNavItem("/" + language + xmlObj.getElementsByTagName("LeftNav")[0].getElementsByTagName("TopLevelSite")[categoryIndex].getAttribute("URL")))
		{
			setSelectedTopNavItem((categoryIndex + 1));	
		}
	}
	topLevelCategories = xmlObj.getElementsByTagName("LeftNav")[0].getElementsByTagName("TopLevelSite")[categoryIndex].childNodes;
	var containerTable = document.createElement('table');
	containerTable.setAttribute("cellpadding", "0");
	containerTable.setAttribute("cellspacing", "0");
	containerTable.setAttribute("class", "topNavMenu");
	containerTable.setAttribute("border", "0");
	containerTable.setAttribute("id", "topNav" + (categoryIndex + 1) + "Menu");
	containerTable.setAttribute("style", "display:none;");
	containerTable.setAttribute("onmouseover", "this.style.display = 'block';hoverTopNavImg(" + (categoryIndex + 1) + ");");
	containerTable.setAttribute("onmouseout", "this.style.display = 'none';hoverTopNavImg(" + (categoryIndex + 1) + ");");
	if (isIE)
	{
		containerTable.style.display = "none";
		containerTable.className = "topNavMenu";
		containerTable.onmouseover = function (){this.style.display = 'block';hoverTopNavImg(categoryIndex + 1);};
		containerTable.onmouseout = function (){this.style.display = 'none';hoverTopNavImg(categoryIndex + 1);};
	}
	
	
	var tableBody = document.createElement("tbody");
	
	// Create first row items
	var firstRow = document.createElement('tr');
	var firstRowTd1 = document.createElement('td');
	firstRowTd1.setAttribute("class","leftEnd");
	firstRowTd1.setAttribute("width","2");
	firstRowTd1.setAttribute("nowrap","nowrap");
	firstRowTd1.setAttribute("style","vertical-align:bottom;");
	if (isIE)
	{
		firstRowTd1.style.verticalAlign = "bottom";
		firstRowTd1.className = "leftEnd";
	}
	var firstRowTd1Img = document.createElement('img');
	firstRowTd1Img.setAttribute("src","/_layouts/images/CanaccordPCS/glbl_spacer.gif");
	firstRowTd1Img.setAttribute("width","2");
	firstRowTd1Img.setAttribute("height","1");
	firstRowTd1.appendChild(firstRowTd1Img);
	var firstRowTd2 = document.createElement('td');
	firstRowTd2.setAttribute("class","middle");
	firstRowTd2.setAttribute("width","100%");
	if (isIE)
	{
		firstRowTd2.className = "middle";
	}
	var firstRowTd3 = document.createElement('td');
	firstRowTd3.setAttribute("class","rightEnd");
	firstRowTd3.setAttribute("width","2");
	firstRowTd3.setAttribute("nowrap","nowrap");
	firstRowTd3.setAttribute("style","vertical-align:bottom;");
	if (isIE)
	{
		firstRowTd3.style.verticalAlign = "bottom";
		firstRowTd3.className = "rightEnd";
	}
	var firstRowTd3Img = document.createElement('img');
	firstRowTd3Img.setAttribute("src","/_layouts/images/CanaccordPCS/glbl_spacer.gif");
	firstRowTd3Img.setAttribute("width","2");
	firstRowTd3Img.setAttribute("height","1");
	firstRowTd3.appendChild(firstRowTd3Img);
	
	// Create second row items
	var secondRow = document.createElement('tr');
	var secondRowTd1 = document.createElement('td');
	secondRowTd1.setAttribute("height","3");
	secondRowTd1.setAttribute("class","leftCorner");
	var secondRowTd1Img = document.createElement('img');
	secondRowTd1Img.setAttribute("src","/_layouts/images/CanaccordPCS/glbl_spacer.gif");
	secondRowTd1Img.setAttribute("width","2");
	secondRowTd1Img.setAttribute("height","1");
	firstRowTd1.appendChild(secondRowTd1Img);
	secondRowTd1.appendChild(secondRowTd1Img);
	var secondRowTd2 = document.createElement('td');
	secondRowTd2.setAttribute("height","3");
	secondRowTd2.setAttribute("class","bottomEnd");
	var secondRowTd3 = document.createElement('td');
	secondRowTd3.setAttribute("height","3");
	secondRowTd3.setAttribute("class","rightCorner");
	secondRowTd3.appendChild(firstRowTd3Img);
	if (isIE)
	{
		secondRowTd1.className = "leftCorner";
		secondRowTd2.className = "bottomEnd";
		secondRowTd3.className = "rightCorner";
	}
	
	var ul = document.createElement("ul");
	for (i = 0; i < topLevelCategories.length; i++)
	{
		if (topLevelCategories[i].nodeName == "Nodes")
		{
			var curLevelCategories = topLevelCategories[i].childNodes;
			for (iii = 0; iii < curLevelCategories.length; iii++)
			{
				if (curLevelCategories[iii].nodeName == "Node")
				{
					var li = document.createElement("li");
					li.setAttribute("onmouseover","this.className = 'selected';");
					li.setAttribute("onmouseout","this.className = '';");
					if (isIE)
					{
						li.onmouseover = function (){this.className = 'selected';};
						li.onmouseout = function (){this.className = '';};
					}
					var anchorTag = document.createElement("a");
					anchorTag.setAttribute("href", "/" + language + curLevelCategories[iii].getAttribute("URL"));
					if (selectedTopNavItem == 0)
					{
						if (checkForSelectedTopNavItem("/" + language + curLevelCategories[iii].getAttribute("URL")))
						{
							setSelectedTopNavItem((categoryIndex + 1));
						}
					}
					var anchorText = document.createTextNode(curLevelCategories[iii].getAttribute("Title_" + language));
					anchorTag.appendChild(anchorText);
					li.appendChild(anchorTag);
					ul.appendChild(li);
				}
			}
		}	
	}
	
	firstRowTd2.appendChild(ul);
	firstRow.appendChild(firstRowTd1);
	firstRow.appendChild(firstRowTd2);
	firstRow.appendChild(firstRowTd3);
	
	secondRow.appendChild(secondRowTd1);
	secondRow.appendChild(secondRowTd2);
	secondRow.appendChild(secondRowTd3);
	
	tableBody.appendChild(firstRow);
	tableBody.appendChild(secondRow);
	containerTable.appendChild(tableBody);
	
	document.getElementById("topNavMenus").appendChild(containerTable); 
}
function checkForSelectedTopNavItem(urlSegment)
{
	var foundMatch = false;
	var segmentComponents = urlSegment.split("/");
	if (segmentComponents.length >= 3)
	{
		// Assumption: /LANGUAGE/TOPLEVEL_IDENTIFIER is always the base for a child category url
		var token = "/" + segmentComponents[1] + "/" + segmentComponents[2] + "/";
		if (location.href.indexOf(token) >= 0)
		{
			foundMatch = true;
		}
	}

	return foundMatch;
}
function setSelectedTopNavItem(itemIndex)
{
	selectedTopNavItem = itemIndex;
	var targetTopNavItem = document.getElementById("hdr_nav" + itemIndex + "_img");
	if (targetTopNavItem)
	{
		targetTopNavItem.src = targetTopNavItem.src.replace("_off", "_on");
	}
}
/* Top Nav Scripts - End */
/* Content Area Scripts - Begin */
function updateMarqueeArea(tabCount)
{
	if (document.getElementById("contentOverlayArea") && document.getElementById("marqueeContent" + tabCount))
	{
		document.getElementById("contentOverlayArea").innerHTML = document.getElementById("marqueeContent" + tabCount).innerHTML;
	}
	if (document.getElementById("marqueeImage"))
	{
		curImagePath = document.getElementById("marqueeImage").src;
		newImagePath = curImagePath.replace("marquee" + selectedTab,"marquee" + tabCount);
		document.getElementById("marqueeImage").src = newImagePath;
	}
	if (document.getElementById("marqueeLi" + selectedTab))
	{
		document.getElementById("marqueeLi" + selectedTab).className = document.getElementById("marqueeLi" + selectedTab).className.replace("selected", "");
	}
	if (document.getElementById("marqueeLi" + tabCount))
	{
		if (document.getElementById("marqueeLi" + tabCount).className.indexOf("selected") < 0)
		{
			document.getElementById("marqueeLi" + tabCount).className = document.getElementById("marqueeLi" + tabCount).className.concat(" selected");
		}
	}
	selectedTab = tabCount;
}
function hoverHTMLElem(HTMLElem,styleClass)
{
	if (HTMLElem.className.indexOf(styleClass) > -1)
	{
		HTMLElem.className = HTMLElem.className.replace(styleClass, "");	
	}
	else
	{
		HTMLElem.className = HTMLElem.className + " " + styleClass;
	}
}
/* Content Area Scripts - End */

/* Variable Initializations - Begin */
var selectedTab = 0;
var selectedTopNavItem = 0;
/* Variable Initializations - End */
