//*****************************************************************
//*****************************************************************
//***                                                           ***
//***      Copyright (c) 2008 Infor			                        ***
//***      All Rights Reserved.                                 ***
//***                                                           ***
//*****************************************************************
//*****************************************************************
var originalFramesetColsNav = null;
var originalFramesetRowsTop = null;
var activeTabId = null;
var varTabsObj = null; // to keep track of the toolbar tabs

var helpUiSettings = new Object();
helpUiSettings['uiCreateImageButtonTemplate'] = '<table class="button-enabled" onMouseOver="javascript:onMouseOverButton2(this);" onMouseOut="javascript:onMouseOutButton2(this);" ><tr><td class="button-left-img"> </td><td class="button-middle-img"><img align="left" class="button" src="%IMAGE%" onMouseOver="javascript:onMouseOverButton(this);" onMouseOut="javascript:onMouseOutButton(this);" onClick="%HREF%" title="%TOOLTIP%"/></td><td class="button-right-img"> </td></tr></table>';
helpUiSettings['uiCreateTextButtonTemplate' ] = '<table class="button-enabled" onMouseOver="javascript:onMouseOverButton2(this);" onMouseOut="javascript:onMouseOutButton2(this);"><tr><td class="button-left"> </td><td class="button-middle"><a href="%HREF%" title="%TOOLTIP%">%LABEL%</a></td><td class="button-right"> </td></tr></table>';
helpUiSettings['feedbackEmail'] = 'documentation@infor.com';
helpUiSettings['theme'] = 'InforBlue'; /* default turquoise */
helpUiSettings['constrast'] = 'high';
helpUiSettings['themeParameterName'] = "inforThemeName"; /* inforThemeName=InforBlue */
helpUiSettings['themeDefaultFolder'] = "blue";

/* only in use yet to show/hide 3thrd level index entries */
function doExpandTOC(idPara,idArrow) {
		//expand the paragraph and rotate the arrow; collapse and rotate it back
		
		paraNum = document.getElementById(idPara);
		/* arrowNum = document.getElementById(idArrow); */
		
		if (paraNum.style.display=="none")
		{
			paraNum.style.display="";
			/* arrowNum.src="navc.gif"; */
		}
		else 
		{
			paraNum.style.display="none";
			/* arrowNum.src="nave.gif"; */
		}
		return;
}

function loadContentPage(defaultPage) {
		var theme = getParam(helpUiSettings.themeParameterName);
		
		if (theme.length <= 0) {
			theme = helpUiSettings.theme;
		} 
		var themeStr = "?" + helpUiSettings.themeParameterName + "=" + theme;
		
		var helpcontent = getParam("helpcontent");
		/* if the helpcontent contains some/path/filename.html^subid it is translated into
		                               some/path/filename.html#subid		    */
		if (helpcontent.length > 0) {
			/*              changeParam does add the Theme string in a controlled way */
			var addr = changeParam(helpcontent,helpUiSettings.themeParameterName,theme);
			body.location = addr;
		} else {
			if (defaultPage.length > 0) {
				body.location = defaultPage + themeStr;
			}
			else {
				body.location = "about:blank";
			}
		}
		toolbar.location = "skin/nav_toolbar.html" + themeStr;
		navigation.location = "skin/nav_toc.html" + themeStr;
}

function themeInit() {
	var contrast = getParam("contrast");
	if (contrast.length <= 0) {
		contrast = helpUiSettings.contrast;
	}
	var theme = getParam(helpUiSettings.themeParameterName);
	if (theme .length <= 0) {
		theme = helpUiSettings.theme;
	}
	switchTheme(theme,contrast);
	/* also add the theme to all links */
	$("a[href]").each(function() {
		$(this).attr({ href: "" + changeALinksTheme($(this).attr("href"),theme)  });
	});
}

function pageInit() {
	var helpcontent = getParam("helpcontent");
	if (helpcontent.length > 0) {
		body.location = helpcontent;
	}
	var contrast = getParam("contrast");
	if (contrast.length <= 0) {
		contrast = helpUiSettings.contrast;
	}
	var theme = getParam(helpUiSettings.themeParameterName);
	if (theme .length <= 0) {
		theme = helpUiSettings.theme;
	}
	alert("pageInit():Still in use?");
}

function switchTheme(th,cntst) {
	/* cleanup the theme name from  InforBlue to simply blue  */
	var cleanThemeName = th.toLowerCase().replace("infor","");
	/* it is not 100% ok to replace the fixed "default" */
	var newTheme= $("#active-theme").attr("href").replace(helpUiSettings.themeDefaultFolder,cleanThemeName);
	$("#active-theme").attr({ href: "" + newTheme});
}

/* AIM: switch the frames and let them load the new theme */
function switchThemeInFrames(th,cntst) {
	top.toolbar.location.href = 	 changeParam(top.toolbar.location.href,		helpUiSettings.themeDefaultFolder,th);
	top.body.location.href = 			 changeParam(top.body.location.href,			helpUiSettings.themeDefaultFolder,th);
	top.navigation.location.href = changeParam(top.navigation.location.href,helpUiSettings.themeDefaultFolder,th);
	
}

/* AIM: get a specific parameter from the parameter string */
function getParam(paramName) {
		fullHref = document.location.search;
		
    if (fullHref == "")
        return "";
		 
		// remove the question mark
		var hasQuestion = (fullHref.indexOf("?")>0)? true : false;
		var hasHash 		= (fullHref.indexOf("#")>0)? true : false;
		
    var halfs = fullHref.split("\?");
    if (halfs.length!=2) { 
    	if (hasHash) {
    		halfs = fullHref.split("#");
    		return halfs[0] + "?" + changePair + "#" + halfs[1]; /* the order is important! */
    	} else {
    		return halfs[0] + "?" + changePair; /* it doesn't contain a querystring so add param=value and stop */
    	}
		}
		
		var url = halfs[0] + "?";
		var querystring = halfs[1];
		var anchor = "";
		
		/* preserve the anchor */
		if (hasHash) {
    		var halfs2 = querystring.split("#");
    		querystring = halfs2[0];
    }
		
		var arg = (querystring.split("&"));
		
    for (i=0; i < arg.length; i++) {
        var arg_values = arg[i].split("=")
        if (unescape(arg_values[0]) == paramName) 
        {
        	var ret = unescape(arg_values[1]);
        	// To be able to: http://example.com/myhelp.default.html?helpcontent=my/path/page.html^innersectionid
        	AccentCirconflexe = ret.split("^");
        	if (AccentCirconflexe.length > 1) 
        	{
        		ret = AccentCirconflexe[0] + "#" + AccentCirconflexe[1];
        	}
          return ret;
        }
    }
    return "";
}

/* AIM: check an existing parameter from the parameter string 
        if it exists, change the value
        if it does not exist, add the parameter=value 
   The syntax must be:
   
   ...something...?querystring#anchor
        
        */
function changeParam(fullHref, searchParamName, replaceParamValue) {
		var changePair = searchParamName + "=" + replaceParamValue;
		if (fullHref == "") {
        return "";
		}
		// remove the question mark
		var hasQuestion = (fullHref.indexOf("?")>0)? true : false;
		var hasHash 		= (fullHref.indexOf("#")>0)? true : false;
		
    var halfs = fullHref.split("\?");
    if (halfs.length!=2) { 
    	if (hasHash) {
    		halfs = fullHref.split("#");
    		return halfs[0] + "?" + changePair + "#" + halfs[1]; /* the order is important! */
    	} else {
    		return halfs[0] + "?" + changePair; /* it doesn't contain a querystring so add param=value and stop */
    	}
		}
		// TODO handle combination of ?querystring#anchor
		
		/* alert("1:" + halfs[0] + "\n2:" + halfs[1]); */
		var url = halfs[0] + "?";
		var querystring = halfs[1];
		var anchor = "";
		
		/* preserve the anchor */
		if (hasHash) {
    		var halfs2 = querystring.split("#");
    		querystring = halfs2[0];
    		anchor = "#" + halfs2[1];
    }
		
		var found = false;
		
		var arg = (querystring.split("&"));
		for (i=0; i < arg.length; i++) {
        var arg_values = arg[i].split("=")
        if (unescape(arg_values[0]) == searchParamName) {
        	/* found! this parameter, preserve this name-value pair */
        	found = true;
        	url = url + "&" + changePair;
        } else {
        	/* we don't look for this parameter, preserve this name-value pair */
        	url = url + "&" + arg[i];
        }
    }
    if (!found) {
    	/* the list of parameters was checked, but it didn't contain the searched one */
    	url = url + "&" + changePair;
    }
    
    return url + anchor; /* http:// ......?querystring#anchor */
}

function switchNavigationOnOff() {
		var frameset = parent.document.getElementById("framesetNavBody");
		var frameset2 = parent.document.getElementById("framesetMain");
		
		if (originalFramesetRowsTop == null) {
			// alert("originalFramesetCols is null");
			if (frameset != null) {
		  	originalFramesetColsNav = frameset.cols;
		  	frameset.cols = "0, *";
		  } 
		  if (frameset2 != null) {
		  	originalFramesetRowsTop = frameset2.rows;
		  	frameset2.rows = "0, *";
		  }
		} else {
			// alert("originalFramesetCols is " + originalFramesetCols);
			
			if (frameset != null) {
		  	frameset.cols = originalFramesetColsNav;
		  	originalFramesetColsNav = null;
		  }
		  if (frameset2 != null) {
		  	frameset2.rows = originalFramesetRowsTop;
		  	originalFramesetRowsTop = null;
		  }
		}
	}

function hideNavigation(icon) {
	icon.src = icon.src.replace('hide','show');
	icon.onclick = function(){showNavigation(icon);};
	var bodyFrame = document.getElementById('bodyFrame');
	bodyFrame.className = 'bodyFrame';
	switchNavigationOnOff();
}
function showNavigation(icon) {
	icon.src = icon.src.replace('show','hide');
	icon.onclick = function(){hideNavigation(icon);};
	var bodyFrame = document.getElementById('bodyFrame');
	bodyFrame.className = 'bodyFrameOff';
	switchNavigationOnOff();
}
	
/* Toolbar Tabs */
function uiTabsInit(tabsID) {
	varTabsObj = document.getElementById(tabsID);
	varTabsObj.className='tabs';
	
	var id = '';
	var tabs = varTabsObj.getElementsByTagName('table');
	
	for(var no=0;no<tabs.length;no++){
		id = tabs[no].id;
		if (no==0) {
			tabs[no].className = 'tab-active';
			tabs[no].onmouseover 	= null;
			tabs[no].onmouseout 	= null;
			activeTabId = id;
		}
		else if (tabs[no].className == 'tab-space-till-end') {
			// skip
		}
		else {
			tabs[no].className = 'tab-inactive';
			tabs[no].onmouseover 	= uiTabMouseOverEvent;
			tabs[no].onmouseout 	= uiTabMouseOutEvent;
			tabs[no].onclick 			=	function(){
																	xId=this.id + '';
																	uiTabClickEvent(xId);
																				};
		}
				
	} // end for				
} // end function

function uiTabClickEvent(nId) {
	// set the current active tab to inactive
	var previousActiveTab = document.getElementById(activeTabId);
	if (previousActiveTab) {
		previousActiveTab.className = 'tab-inactive';
		previousActiveTab.onmouseover 	= uiTabMouseOverEvent;
		previousActiveTab.onmouseout 	= uiTabMouseOutEvent;
		previousActiveTab.onclick 			=	function(){
																	xId=this.id + '';
																	uiTabClickEvent(xId);
																				};
	}
	// set the new active tab
	var clickedTab = document.getElementById(nId);
	if (clickedTab) {
		clickedTab.className 		= 'tab-active';
		clickedTab.onmouseover 	= null;
		clickedTab.onmouseout 	= null;
		clickedTab.onclick 			=	null;
		activeTabId = clickedTab.id;
	}
}

function uiTabMouseOverEvent() {
	this.className = 'tab-mouse-over';
}
function uiTabMouseOutEvent() {
	this.className = 'tab-inactive';
}

function onMouseOverButton(obj) {
	obj.className = 'buttonHover';
}
function onMouseOutButton(obj) {
	obj.className = 'button';
}
function onMouseOverButton2(obj) {
	obj.className = 'button-hover';
}
function onMouseOutButton2(obj) {
	obj.className = 'button-enabled';
}
function goBack() {
	// window.back(); did not work fine in IE8
	history.go(-1); // tested in FF3.6, IE8-6, Safari, Opera, Chrome 
}
function sendFeedback() {
	var version = $("meta[name='Infor.version']").attr("content");
	var lang = $("html").attr("lang");
	var path = $("meta[name='DC.identifier']").attr("content");
	document.location = 'mailto:' + helpUiSettings.feedbackEmail + '?Subject=' + helpUiLabels.email_subject_feedbackOn + ' ' + path + ', ' + version + ', ' + lang;
}

function changeALinksTheme(currentHref,theme) {
	var cHref = currentHref + "";
	if (cHref.indexOf(".html")>0) {
		cHref = changeParam(currentHref,
												helpUiSettings.themeParameterName,
												theme);
	}
	/* alert("before:" + currentHref + "\nafter:" + cHref + "\nIndexOf:" + cHref.indexOf(".html")); */
	return cHref;
}

function uiTocInit(theme) {
	$("li").children("a").prepend("<div class='icon'>&nbsp;</div>");
	$("a").each(function() {
		$(this).attr({ href: "" + changeALinksTheme($(this).attr("href"),theme)  });
		if ($(this).parent().hasClass('expLi')) {
			$(this).click(function() {
				$("*.selected").removeClass("selected"); /* reset current one */
				$(this).addClass("selected"); /* set current one selected */
				
				if ($(this).children("div").hasClass("colLi")){
					$(this).children("div").removeClass("colLi"); /* toggle between expanded and collapsed icon */
					$(this).parent().addClass("expLi");
				}
				else {
					$(this).children("div").addClass("colLi"); /* toggle between expanded and collapsed icon */
					$(this).parent().removeClass("expLi");
				}
				$(this).parent().children("ul:1").toggleClass("visibleUl");	/* toggle between visible hidden */
			}); /* end click expandable section */
			$(this).hover(
				function () {
			    $(this).children("div").addClass("hover"); /* change the icon */
			  }, 
			  function () {
			    $(this).children("div").removeClass("hover"); /* change back */
			  }
			 ); /* end hover expandable section */
		} /* end hasClass */ 
		else {
			$(this).click(function() {
				$("*.selected").removeClass("selected"); /* reset current one */
				$(this).addClass("selected"); /* set current one selected */
			}); /* end click regular node item */
		}
	}); 
}

function uiCreateContentToolbar( anchor ) {
	var pathToRoot = $("meta[name='Infor.project-root']").attr("content") + "";
	uiCreateImageButton( anchor, pathToRoot + "/skin/toolbar_print.gif", "javascript:window.print();", helpUiLabels.button_print_tooltip );
  uiCreateImageButton( anchor, pathToRoot + "/skin/toolbar_hidenavigation.gif", "javascript:hideNavigation(this);", helpUiLabels.button_hidenavigation_tooltip );
  uiCreateTextButton( anchor, helpUiLabels.button_back_label, "javascript:goBack();", helpUiLabels.button_back_tooltip );
  uiCreateImageButton( anchor, pathToRoot + "/skin/toolbar_feedback.gif", "javascript:sendFeedback();", helpUiLabels.button_feedback_tooltip );
  /* uiCreateTextButton( anchor, "My button", "../../default.html", "My default Tooltip" );*/
}

function uiCreateImageButton( anchor, imagesrc, href, tooltip ) {
	var html = helpUiSettings.uiCreateImageButtonTemplate.replace(
		"%TOOLTIP%",tooltip).replace(
		"%IMAGE%",imagesrc).replace(
		"%HREF%",href);
  $(anchor).append(html);
}

function uiCreateTextButton( anchor, label, href, tooltip ) {
	var html = helpUiSettings.uiCreateTextButtonTemplate.replace(
		"%TOOLTIP%",tooltip).replace(
		"%LABEL%",label).replace(
		"%HREF%",href);
  $(anchor).append(html);
}
