//**************************************************/
//	File:			incPage.js		
//	Author(s):		KS
//  Created:		11/04/2004	
//	Description:	Onclicks for administering a page, and dynamic nav menu functions
//	Dependencies:
//	Functions:		
//	Notes:
//	TODO:		
//	History:
//		
//**************************************************/

var g_LinkArray = new Array() // list of all links on the page
var g_TargetArray = new Array() // list of all targets on the page


function EditChangeStyle(Object,StyleClass)
{
	Object.className=StyleClass;
}

function EditArea(Object,PageID,RegionID)
{	
	var win;
		
	if (typeof(OrgID)=='undefined' || typeof(LangID)=='undefined') return;
	
	var address = GetAppRootAddress()
	
    if (document.all)
        var xMax = screen.width, yMax = screen.height;
    else
        if (document.layers)
            var xMax = window.outerWidth, yMax = window.outerHeight;
        else
            var xMax = 640, yMax=480;
	
    var xOffset = (xMax - 640)/2;
    var yOffset = (yMax - 480)/2;
    var URL
    URL=address + "/Common/ContentManagement/FCKEditor/index.asp?PageID="+PageID+"&RegionID="+RegionID+"&folder=/Images"; 	
	URL+="&OrgID=" + OrgID + "&LangID=" + LangID;
	EditObject=Object;
	win=window.open(URL,"Editor","width=640,height=480,screenX="+xOffset+",screenY="+yOffset+",top="+yOffset+",left="+xOffset+"");	
	win.focus();
	
}

function AddResource(PageID, RegionID)
{
	var win;
		
	var URL = GetAppRootAddress() + "/Resources/conAddResource.asp?PageID="+PageID+"&RegionID="+RegionID;	
	win=window.open(URL,"Resources", "width=450,height=300,scrollbars=yes");	
	win.focus();
}

function GetAppRootAddress()
{
	var h = document.location.href.toLowerCase(); // get the entire server address
	var idx = h.indexOf("/resourcecentre");
	
	if (idx!=-1)
	{
		h = (h.slice(0, idx + 15));
	}
	else
	{
		h = h.slice(0, h.indexOf("/"));
	}
	return h;
	
}

//--------------------------------------------------------------------------------------
//Public Function SaveHREF()
//Usage:	Temporarily stores all the link locations.
//
//Input:	None
//Output:	None
//Notes:	
	
function SaveHREF()
{
	for (var i=0; i<document.links.length; i++) {
		g_LinkArray[i]=document.links[i].toString()
		g_TargetArray[i]=document.links[i].target.toString()
	}
}

//--------------------------------------------------------------------------------------
//Public Function RestoreHREF()
//Usage:	Resets all links back to their original values
//
//Input:	None
//Output:	None
//Notes:	

function RestoreHREF()
{
	var prefix;
	
	for (var i=0; i<document.links.length; i++) 
		{    
        // Takes first 3 chars from the name
        //prefix=document.links[i].name.slice(0,3);
        //if (prefix!="NEL") { // All links that remain clickable are named using the NEL prefix
			//document.links[i].onclick = '';
			document.links[i].href = g_LinkArray[i];
			document.links[i].target = g_TargetArray[i];
		//}
    }

}

//--------------------------------------------------------------------------------------
//Public Function DisableHREF()
//Usage:	Changes all links without a NEL prefix in their name to links that do nothing.
//          This is to prevent errors that occured when trying to edit content that had links
//          embedded in it.
//
//Input:	None
//Output:	None
//Notes:	

function DisableHREF() 
{
	var prefix;
	
	for (var i=0; i<document.links.length; i++) 
		{    
        /*if (document.links[i].name!=null) {
			prefix=document.links[i].name.slice(0,3); // looks for the NEL prefix
        } else
			prefix="";
		}
        if (prefix!="NEL") {*/
			//document.links[i].onclick = '';		
			document.links[i].href = 'javascript:;'; //nullifies the link
			document.links[i].target = '';
		//}
    }
	
}

//--------------------------------------------------------------------------------------
//Public Function InitPage(mode, bInitNav)
//Usage:	Saves all links and disables them if in design mode
//			else updates dynamic menu
//Input:	None
//Output:	None
//Notes:	

function initPage(mode, bInitNav)
{	
	// disabled links if in page edit mode
	if (mode==1) {
		// Save all the links if the user is in design mode
			SaveHREF()
		// Disable all links 
		DisableHREF()
	}
	//else if (mode==0)
	//{
	//	if (bInitNav) initNav();
	//}
	
}

function initNav()
{

	// get the current page address and remove any extra parameters
	var thispage = document.location.href.toLowerCase();	
	var thisfolder = thispage.slice(thispage.indexOf("/content/")+9);
	
	var menu = document.getElementById("MainNav");
			
	if (typeof(menu)!='undefined')
	{
		var arrMenuLinks = menu.getElementsByTagName("A");
	
		if (thisfolder.indexOf("/")!=-1)
		{
			thisfolder = thisfolder.slice(0, thisfolder.indexOf("/")).replace(/%20/gi, " ");
				
			var linkname;
				
			for (var i=0; i<arrMenuLinks.length; i++)
			{									
				linkname = arrMenuLinks[i].childNodes[0].nodeValue.toLowerCase(); // get the title of the link
				linkname = linkname.replace(/[^\da-zA-Z\- _]/gi, ""); // remove non letters and invalid chars
																
				if (linkname==thisfolder)
				{
					arrMenuLinks[i].className="MainNavSelected";
					return;
				}
			}
		}
		else
		{
			var linkname;
			thispage = thispage.slice(0, thispage.indexOf(".asp") + 4);
				
			for (var i=0; i<arrMenuLinks.length; i++)
			{			
				linkname = arrMenuLinks[i].href.toLowerCase();
				linkname = linkname.slice(0, linkname.indexOf(".asp") + 4);
									
				if (linkname==thispage) 
				{
					arrMenuLinks[i].className="MainNavSelected";
					return;
				}
			}
		}
	}
}

function OpenSubMenu(id)
{
	// close any other open menus
	var obj;
	
	for (var i=1; i<5; i++) 
	{
		obj = GetSubMenu("MainSub" + i);
		if (obj!=null) obj.style.display = "none";
	}
		
	obj = GetSubMenu(id);
	
	if (obj!=null) 
	{
		obj.style.display = "block";
		obj.style.top = (document.getElementById("MainNav").clientHeight + 1) + "px";
	}
}

function CloseSubMenu(e, id)
{
	var obj = GetSubMenu(id);
	
	if (obj!=null) 
	{
		if (!CheckPos(e, obj)) obj.style.display = "none";
	}
}



function GetSubMenu(id)

{
	var obj = document.getElementById(id);
	
	if (typeof(obj)=='undefined' || document.location.href.toLowerCase().indexOf("designmode=1")>-1) obj = null;
		
	return obj;
}

function CheckPos(e, obj)
{
	var xMousePos;
	var yMousePos;
	var objMinX, objMaxX, objMinY, objMaxY;
	
	if (document.all) { // grab the x-y pos.s if browser is IE
		xMousePos = window.event.clientX;
		yMousePos = window.event.clientY;
	} else {  // grab the x-y pos.s if browser is NS
		xMousePos = e.pageX;
		yMousePos = e.pageY;
	}
	
	xMousePos += document.body.scrollLeft;
	yMousePos += document.body.scrollTop;
	
	objMinX = findPosX(obj);
	objMaxX = objMinX + obj.scrollWidth;
	objMinY = findPosY(obj);
	objMaxY = objMinY+obj.scrollHeight;
	
	//alert(document.body.scrollLeft + " " + document.body.scrollTop);
		//x+= document.body.scrollLeft;
	//y+= document.body.scrollTop;
	
	//var str = ((xMousePos<objMinX) + " " + (xMousePos>objMaxX) + " " + (yMousePos<objMinY) + " " +  (yMousePos> objMaxY));
	//alert("mousex: " + xMousePos + " mousey: " + yMousePos + 
	//	"\nobjxmin: " + objMinX + " objymin: " + objMinY + "\nobjxmax: " + objMaxX + 
	//	" objymax: " + objMaxY + "\n" + str + "\n" + document.body.scrollLeft + " " + document.body.scrollTop);
	
	
	if (xMousePos<objMinX || xMousePos>objMaxX || yMousePos<objMinY || yMousePos> objMaxY)
		return false;
	else
		return true;

}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
