/*
'Author					: Stefan Kruger
'Date Created			: 09-Dec-2005
'Last Changed by		: Stefan Kruger
'Date Changed			: 22-Dec-2005
'Version				: 1.0.22122005
'Comments/Changes:
'Date:			Person:					Desc:
'------------------------------------------------------------------------------------------------------
'09-Dec-2005	Stefan Kruger			Created file
'14-Dec-2005	Stefan Kruger			Added proper handling of browser distinction
'20-Dec-2005	Stefan Kruger			Fixed the menu scrolling

'ToDo List:
'Date:			Person:					Desc:
'------------------------------------------------------------------------------------------------------
*/

//////////////////////////////////////////////////////////////////////////////////////////////
//Expanding and contracting menus
//////////////////////////////////////////////////////////////////////////////////////////////

function hideAllContractableMenus(){
	//hsmenu-inner
	//document.getElementById("hsmenu-inner").style.display = "none";
	manageDisplayWindow("hsmenu-inner","hide");
}

//////////////////////////////////////////////////////////////////////////////////////////////
//TAB SETUP
//////////////////////////////////////////////////////////////////////////////////////////////
var menuwidth = 500;
var menuspeed = 4;
var actualwidth = -1;
var lefttime = null,righttime = null;

if (!(isie)) {
	menuspeed = 1*menuspeed;
}

/*
Function that adds a temporary span for Mozilla to correctly pick up offsetWidth for scrolling tab menu.
*/
function addTempTabObject(id,isDiv){
	var type = "span";
	if (isDiv) type = "div"
	var tempspan = document.createElement(type);
	tempspan.id = id;
	document.body.appendChild(tempspan);
	return tempspan;
}

//////////////////////////////////////////////////////////////////////////////////////////////
//PREDEFINED USER FUNCTIONS USED IN THE POST-LOADING OF TABS
//////////////////////////////////////////////////////////////////////////////////////////////
/*
This function is called on successful loading of the tab page.
*/
var tabLoader = function (oXML,outputDiv) {
	//Check for expiry of content before loading
	if (checkForExpiry(oXML.responseText)){  
		expireSession();
	}
	else {        
		//Debug
		//debugMe(oXML.responseText);
		
		// The following was taken out to get rid of the temp div - kdt - 10-Sep-2007
		var tempdivname = "temp";
		addTempTabObject(tempdivname);
		outputDiv.innerHTML = oXML.responseText;
		//document.getElementById("tabcontent").innerHTML = oXML.responseText;
		document.getElementById(tempdivname).innerHTML = document.getElementById("tabcontent").innerHTML;
		actualwidth = document.getElementById(tempdivname).offsetWidth;
		//alert (actualwidth)
		if (actualwidth > menuwidth) {
			showTabArrows(true,"tab");
		}
		else {
			showTabArrows(false,"tab");
		}
	}
	hideLoading();
	oXML = null;
};

//////////////////////////////////////////////////////////////////////////////////////////////
//TAB FUNCTIONS
//////////////////////////////////////////////////////////////////////////////////////////////
function showTabArrows(display,mode){
	try {
		var leftarrowname = "";
		var rightarrowname = "";
		if (mode == "tab") {
			leftarrowname = "leftarrow";
			rightarrowname = "rightarrow";
		}
		else if (mode == "tabsub"){
			leftarrowname = "leftarrowsub";
			rightarrowname = "rightarrowsub";
		}
		else {
			alert("Invalid arror shoing mode requested.");
			return;
		}
		if (display) {
			if (document.getElementById(leftarrowname) && document.getElementById(rightarrowname)){
				document.getElementById(leftarrowname).style.visibility = "visible";
				document.getElementById(rightarrowname).style.visibility = "visible";
			}
		}
		else {
			if (document.getElementById(leftarrowname) && document.getElementById(rightarrowname)){
				document.getElementById(leftarrowname).style.visibility = "hidden";
				document.getElementById(rightarrowname).style.visibility = "hidden";
			}
		}
	}
	catch (ex){
		alert("Error : Occured during hiding submenu arrows.");
	}
}

/*
The actual tab loading/clearing function
loadOrUnload = true --> load
loadOrUnload = false --> unload (for Logoff)
*/
function getTabMenu(loadOrUnload){
	if (document.getElementById("tab-container")){
		if (loadOrUnload) {
			var page = "/tabs.asp";
			genericSubmit(page,"",null,null,"tab-container",tabLoader,tabloadingstring,false);
		}
		else {
			document.getElementById("tab-container").innerHTML = "";
		}
	}
	else {
		alert("Error : Tab container not found in page.");
	}
}

function menuHider(){
	manageDisplayWindow("tabsub-container","close");
}

function executeTabAction(uniquepageid,relation,firstmenuuniquepageid){
	//Hide previous menus
	menuHider();
	//Hide all contractable menus
	//hideAllContractableMenus();
	//Load menu item
	//alert(relation.indexOf('83'));
	if (relation.indexOf('83') == -1 & relation.indexOf('99') == -1 & relation.indexOf('101') == -1 & relation.indexOf('110') == -1)
	{
	    getPage(uniquepageid,relation);
    	
	    //Load firstmenu
	    if (firstmenuuniquepageid){
		    window.setTimeout(function(){
			    getPage(firstmenuuniquepageid);
		    },150);
	    }
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////
//SUBMENU FUNCTIONS
//////////////////////////////////////////////////////////////////////////////////////////////
var menuwidthsub = 400;
var menuspeedsub = 4;
var actualwidthsub = -1;

if (!(isie)) {
	menuspeed = 3*menuspeed;
}

function subMenuPostLoad(){
	//alert("post");
	var outputDiv = document.getElementById("tabsubcontent");
	var tempDiv = addTempTabObject("tempsub");
	tempDiv.innerHTML = outputDiv.innerHTML;
	actualwidthsub = tempDiv.offsetWidth;
	if (actualwidthsub > menuwidthsub) {
		showTabArrows(true,"tabsub");
	}
	else {
		showTabArrows(false,"tabsub");
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////
//MENU SCROLLING FUNCTIONS
//////////////////////////////////////////////////////////////////////////////////////////////

function moveleft(divisionToMove,widthToUse,menuWidthToUse,menuSpeedToUse){
	var tabs = document.getElementById(divisionToMove);
	try {
		var left = tabs.style.left;
		//alert("Left : " + left +"\nIntLeft :" + parseInt(left) + "\nDifference : " + (menuwidth - actualwidth) + "\nActual Width : " + actualwidth + "\nMenu Width : " + menuwidth);
		if (parseInt(left) > (menuWidthToUse - widthToUse)){
			tabs.style.left = (parseInt(left) - menuSpeedToUse);
		}
		lefttime = window.setTimeout(function() {
			moveleft(divisionToMove,widthToUse,menuWidthToUse,menuSpeedToUse);
		},5);
	}
	catch (ex){
		alert("Error scrolling left :\n" + ex.message);
	}
	finally {
		left = null;
	}
	tabs = null;
}

function moveright(divisionToMove,menuSpeedToUse){
	var tabs = document.getElementById(divisionToMove);
	try {
		var left = tabs.style.left;
		//alert("Left : " + left +"\nIntLeft :" + parseInt(left));
		if (parseInt(left) < 0){
			tabs.style.left = (parseInt(left) + menuSpeedToUse);
		}
		righttime = window.setTimeout(function(){
			moveright(divisionToMove,menuSpeedToUse);
		},5);
	}
	catch (ex){
		alert("Error scrolling right :\n" + ex.message);
	}
	finally {
		left = null;
	}
	tabs = null;
}

function stopleft(){
	try {
		clearTimeout(lefttime);
	}
	catch (ex){
		alert("Error clearing left timeout:\n" + ex.message);
	}
}

function stopright(){
	try {
		clearTimeout(righttime);
	}
	catch (ex){
		alert("Error clearing right timeout:\n" + ex.message);
	}
}

//////////////////////////////////////////////////////////////////////////////////////////////
//UNDER CONTRUCTION
//////////////////////////////////////////////////////////////////////////////////////////////
/*
A function that can return a rule item from a specific style.
In future it needs to be addressable by class.
*/
/*
function getStyleObject(styleSheetIndex,ruleIndex) {
	if (document.styleSheets) {
		var styleSheet = document.styleSheets[styleSheetIndex];
		if (styleSheet) {
			var rule;
			if (styleSheet.cssRules) {
				rule = styleSheet.cssRules[ruleIndex];
			}
			else if (styleSheet.rules) {
				rule = styleSheet.rules[ruleIndex];
			}
			if (rule && rule.style) {
				return rule.style;
			}
			else {
				return null;
			}
		}
		else {
			return null;
		}
	}
	else {
		return null;
	}
}
*/

/*
//For dropdown third-level menu.
var menuStayTimeout = null;

//Functions for drop down menus
function showDropDown(displayItem, sourceItem,evt){
	displayItem = document.getElementById(displayItem);
	sourceItem = document.getElementById(sourceItem);
	if (displayItem.style.display == "none"){
		if (displayItem && sourceItem){
			var top = sourceItem.offsetTop + sourceItem.offsetHeight;
			//Compensate for scrolling
			var left = sourceItem.parentNode.offsetLeft + 85 + parseInt(sourceItem.parentNode.parentNode.offsetLeft);
			displayItem.style.top = top;
			displayItem.style.left = left;
			displayItem.style.display = "";
			toggleItemsForMenu(false);
		}
		else {
			alert("Display or source item element not found.");
		}
	}
}

function removeDropDown(displayItem){
	displayItem = document.getElementById(displayItem);
	if (displayItem){
		menuStayTimeout = window.setTimeout(function(){
			displayItem.style.display = "none";
			toggleItemsForMenu(true);
		},100);
	}
}

function stayDropDown(displayItem){
	if (menuStayTimeout) {
		clearTimeout(menuStayTimeout);
	}
	displayItem = document.getElementById(displayItem);
	if (displayItem){
		displayItem.style.display = "";
	}
}

function toggleItemsForMenu(show){
	if (show) {
		if (document.getElementsByTagName){
			var list = document.getElementsByTagName("select");
			for (var k = 0; k < list.length; k++){
				list[k].style.visibility = "visible";
			}
		}
	}
	else {
		if (document.getElementsByTagName){
			var list = document.getElementsByTagName("select");
			for (var k = 0; k < list.length; k++){
				list[k].style.visibility = "hidden";
			}
		}
	}
}
*/