//**************************************************/
//	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)
	{
		/*var langlink = document.getElementById("langlink");
		if (typeof(langlink)!='undefined' && langlink!=null) 
		{
			langlink.href = langlink.href.slice(langlink.href.indexOf("?"));
		}*/

		if (typeof(jQuery) != 'undefined')
		{

			var h = document.location.href.toLowerCase();

			// fix a zIndex problem on the home page with the menu appearing behind the featured items
			if (h.indexOf("content/home.asp")!=-1 && h.indexOf("designmode")==-1 && navigator.appName.indexOf("Microsoft")!=-1)
			{
				$(function() {
					var zIndexNumber = 1000;
					$('div').each(function() {
					$(this).css('zIndex', zIndexNumber);
					zIndexNumber -= 10;
					});
				});
		
			}
		}
	}

	if (bInitNav) initNav();

	/*else if (mode==0)
	{
		if (bInitNav) 
		{
			initNav();
			initLeftNav();
		}
	
	}*/
	
}

function initNav()
{

	// can't get the style to work for some reason, so here's another (poor) way.

	var box = document.getElementById("Banner");

	if (box!=null && typeof(box)=='object')
	{
		var a1, a2, i, j, p, cnt;
		a1 = box.getElementsByTagName("UL");
		var bIsIE = navigator.appName.indexOf("Microsoft")!=-1;

		for (i=1; i<a1.length; i++)
		{		
			
			for (j=a1[i].childNodes.length-1; j>-1; j--)
			{
				if (a1[i].childNodes[j].nodeType==1)
				{
					a1[i].childNodes[j].style.borderBottom = "1px solid #FF0000";

					cnt = 0;
					p = a1[i].childNodes[j];
					
					// add a top border to all the sub menus that expand to the right
					while (p=p.parentNode)
					{
						if (p.tagName=="DIV") break;		
						cnt++;
					}

					if (cnt>4) 
					{
						a1[i].getElementsByTagName("LI")[0].style.borderTop = "1px solid #FF0000";
						// sigh

						//if (bIsIE) a1[i].style.left = (a1[i].parentNode.scrollWidth-8) + "px";
					}

					break;
				}
			}
				//a1.childNodes[a1.childNodes.length-1].style.borderBottom = "1px solid #FF0000";
			//a2 = a1[i].getElementsByTagName("LI");
			//a2[a2.length-1].style.borderBottom = "1px solid #FF0000";
		}
	}
}

function initLeftNav()
{
	var href = document.location.href.toLowerCase();
	href = href.slice(0, href.indexOf(".asp") + 4);
	
	var menu = document.getElementById("SubNav");
			
	if (typeof(menu)!='undefined')
	{
		if (menu!=null)
		{
			var arrMenuLinks = menu.getElementsByTagName("A");
			var url;
						
			for (var i=0; i<arrMenuLinks.length; i++)
			{									
				url = arrMenuLinks[i].href.toLowerCase(); // get the url of the link
				url = url.replace(" ", "%20"); // replace spaces with escaped characters
				url = url.slice(0, url.indexOf(".asp") + 4);													
				
				if (url==href)
				{
					arrMenuLinks[i].className="SubNavLinkSelected"
					return;
				}
			}
		}
	}
}

function ShowNews(on, off)
{
	document.getElementById(on).style.display = "block";
	document.getElementById(off).style.display = "none";
}

