/// <reference name="MicrosoftAjax.js"/>
/// <reference name="~/_IncludeFiles/Scripts/Knotia.Knowledge.Enums.js"/>
/// <reference name="~/_IncludeFiles/Scripts/Knotia.Knowledge.StateManager.js"/>
/// <reference name="~/_IncludeFiles/Scripts/Knotia.Knowledge.TreeView.js"/>
/// <reference name="~/UserControls/SearchBar/Scripts/SearchBar.js"/>
/// <reference name="~/UserControls/Toolbar/Scripts/Toolbar.js"/>

var lockedImage = "NULL";
//var glb_bSetDivOnly = false;
//var bPreviewPrint = false;
function hideSelectBox() {
	var boxObj = getObj("formSelectBox");
	var i = 1;
	while (boxObj) {
		boxObj.visibility = "hidden";
		i = i + 1;
		boxObj = getObj("formSelectBox" + i);
	}
}
function restoreSelectBox() {
	var boxObj = getObj("formSelectBox");
	var i = 1;
	while (boxObj) {
		boxObj.visibility = "visible";
		i = i + 1;
		boxObj = getObj("formSelectBox" + i);
	}
}
function showImage(menuId) {
	var imgName = menuId + "Image";
	var offImg = document [imgName].src;
	var onImgFile = offImg.substring(offImg.lastIndexOf("/")+1, offImg.lastIndexOf("."));
	if (onImgFile.indexOf("FX") == -1)
		document [imgName].src = "/_IncludeFiles/DefinitionFiles_EYO/Images/TopMenu/" + onImgFile + "FX.gif";
	else
		lockedImage = onImgFile;
}
function hideImage(menuId) {
	var imgName = menuId + "Image";
	var onImg = document [imgName].src;
	var offImgFile = onImg.substring(onImg.lastIndexOf("/")+1, onImg.lastIndexOf("FX."));
	if (lockedImage.indexOf(offImgFile) == -1)
		document [imgName].src = "/_IncludeFiles/DefinitionFiles_EYO/Images/TopMenu/" + offImgFile + ".gif";
}
function PopupWindow(pageURL, winName, winDimensions) {
	// pageUrl = destination
	// winName = name of window Created
	// winDimensions = WidthxHeight eg/ 800x600
	
	if (PopupWindow.arguments.length == 4) {
		var toolbarStr = PopupWindow.arguments[3];
	} else {
		var toolbarStr = "toolbar=no,scrollbars=no,resizeable=yes";
	}
	var winWidth = eval(winDimensions.substring(0, winDimensions.indexOf("x")));
	var winHeight = eval(winDimensions.substring(winDimensions.indexOf("x")+1, winDimensions.length));
	var xPos = (screen.availWidth - winWidth) / 2;
	var yPos = (screen.availHeight - winHeight) / 2;
	toolbarStr = toolbarStr + ",height="+winHeight+",width="+winWidth;
	var detWin = window.open(pageURL, winName, toolbarStr);
	try
	{
		detWin.moveTo(xPos, yPos);
		detWin.focus();
	}
	catch (exc)
	{
		// do nothing, sometimes it just won't do this
	}
}
function loadPage(newPage) {
	var dt = new Date();
	if (newPage.indexOf("?") >= 0) {
		var gotoPage = newPage + "&";
	} else {
		var gotoPage = newPage + "?";
	}
	gotoPage = gotoPage + "nc=" + dt.getTime();
	window.location = gotoPage;
}
function noFrames() {
	if (parent.frames[0]) {
		top.location.href = self.location;
	}
}
function doNothing() { }
function printWindow(destinationPage) {
	if (document.submitThisFormToPrint) {
		document.submitThisFormToPrint.printFriendly.value = "2";
		document.submitThisFormToPrint.target = "_blank";
		document.submitThisFormToPrint.submit();
	} else {
		window.open(destinationPage, "", "")
	}
}

// ////////////////////////////////////////////
// Stuff for the left-hand navigation rollovers
// ////////////////////////////////////////////

// constant, kDelayTime, the number of milliseconds the page waits before changing open/close status of menus
var kDelayTimeShow = 250;
var kDelayTimeHide = 450;

// global vars
var glb_LeftHandNavMenus_DisplayedPopoutMenu = null;
var glb_LeftHandNavMenus_iEventDoSoon = new Array(0, 0, 0);

// function createDelayedEvent (function to call, after i seconds, the priority group of the event)
function createDelayedEvent (sEvent, iDelayTime, iGroup)
{
	// an iGroup event kills all events with the same iGroup or lower
	for (var i=0 ; i<=iGroup ; i++)
	{
		if (glb_LeftHandNavMenus_iEventDoSoon[i] > 0)
		{
			window.clearTimeout(glb_LeftHandNavMenus_iEventDoSoon[i]);
		}
	}
	glb_LeftHandNavMenus_iEventDoSoon[iGroup] = window.setTimeout(sEvent, iDelayTime);
}

// function LeftHandNavMenus_Highlight (item to highlight, whether to fully highlight or just outline (true, false))
function LeftHandNavMenus_HighlightItem (objItem, bFullyHighlight)
{
	if (document.all)
	{
		objItem.style.borderColor = "black";
		if (bFullyHighlight)
		{
			objItem.style.backgroundColor = "#333333";
		}
	}
}

// function LeftHandNavMenus_Unhighlight (item to unhighlight)
function LeftHandNavMenus_UnhighlightItem (objItem)
{
	if (document.all)
	{
		objItem.style.borderColor = glb_sPageColor;
		objItem.style.backgroundColor = glb_sPageColor;
	}
}

// fired by the PLBs when a mouse passes over a menu item, see if it has a popout
function LeftHandNavMenus_PopOutIfExists (sMenuName)
{
	if (document.all)
	{
		if (document.all["elPopout_" + sMenuName])
		{
			LeftHandNavMenus_EnablePopoutMenu(sMenuName, event);
		}
	}
}

// fired by the PLBs when a mouse passes out of a menu item, hide it's popout (if exists)
function LeftHandNavMenus_UnpopOutIfExists (sMenuName)
{
	if (document.all)
	{
		if (document.all["elPopout_" + sMenuName])
		{
			LeftHandNavMenus_DisablePopoutMenu();
		}
	}
}

// function LeftHandNavMenus_EnablePopoutMenu (name of element, firing element) -- causes a menu to become displayed
function LeftHandNavMenus_EnablePopoutMenu (sMenuName, evt)
{
	if (document.all)
	{
		if (document.all["elPopout_" + sMenuName].style.top == "0px")
		{
			var iThisElementTop = evt.clientY - 6 + document.body.scrollTop; // line is 12px, so 6 is the average of the pos within it that we will be
			createDelayedEvent("LeftHandNavMenus_private_hideAllPopoutMenus();LeftHandNavMenus_private_showPopoutMenu('" + sMenuName+ "', " + iThisElementTop +")", kDelayTimeShow, 2);
		}
		else
		{
			createDelayedEvent("LeftHandNavMenus_private_hideAllPopoutMenus();LeftHandNavMenus_private_showPopoutMenu('" + sMenuName+ "')", kDelayTimeShow, 2);
		}
	}
}

// function LeftHandNavMenus_MaintainPopoupMenu (name of element) -- causes a menu to remain displayed
function LeftHandNavMenus_MaintainPopoutMenu (sMenuName)
{
	if (document.all)
	{
		createDelayedEvent("LeftHandNavMenus_private_showPopoutMenu('" + sMenuName+ "')", kDelayTimeShow, 2);
		LeftHandNavMenus_HighlightItem(document.all["LeftHandNavMenu_" + sMenuName], true);
	}
}

// function LeftHandNavMenus_DisablePopoutMenu () -- causes all open menus to become hidden
function LeftHandNavMenus_DisablePopoutMenu ()
{
	if (document.all)
	{
		createDelayedEvent("LeftHandNavMenus_private_hideAllPopoutMenus()", kDelayTimeHide, 0);
	}
}

// function showPopoutMenu (name of element, optional height of menu) -- displays the specified popout menu
function LeftHandNavMenus_private_showPopoutMenu (sMenuName)
{
	if (document.all)
	{
		var iSetElementTop = null;
		if (LeftHandNavMenus_private_showPopoutMenu.arguments.length > 1)
		{
			iSetElementTop = LeftHandNavMenus_private_showPopoutMenu.arguments[1];
		}
		if (document.all["elPopout_" + sMenuName] != null)
		{
			document.all["elPopout_" + sMenuName].style.visibility = "visible";
			LeftHandNavMenus_HighlightItem(document.all["LeftHandNavMenu_" + sMenuName], true);
			glb_LeftHandNavMenus_DisplayedPopoutMenu = sMenuName;
			
			if (iSetElementTop != null)
			{
				document.all["elPopout_" + sMenuName].style.left = document.all["elPopout_" + sMenuName].style.pixelWidth - 7;
				document.all["elPopout_" + sMenuName].style.top = iSetElementTop - 5;
			}
		}
	}
}

// function hideAllPopoutMenus -- hides any popout menus that are visible
function LeftHandNavMenus_private_hideAllPopoutMenus ()
{
	if (document.all)
	{
		if (glb_LeftHandNavMenus_DisplayedPopoutMenu != null)
		{
			document.all["elPopout_" + glb_LeftHandNavMenus_DisplayedPopoutMenu].style.visibility = "hidden";
			LeftHandNavMenus_UnhighlightItem(document.all["LeftHandNavMenu_" + glb_LeftHandNavMenus_DisplayedPopoutMenu]);
			glb_LeftHandNavMenus_DisplayedPopoutMenu = null;
		}
	}
}

function RememberSelectedText()
{
	try
	{
		if (window.getSelection)
		{
			document.firefoxSelection = window.getSelection();
		}
	}
	catch (e)
	{
	}
}

