//**************************************************/
//	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.slice(0, h.indexOf("/"));
	
}

//--------------------------------------------------------------------------------------
//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)
{	
	
	// no current way to access the two outer containers built in template builder, so we do it dynamically at startup
	var arrDiv = document.getElementsByTagName("DIV");
	arrDiv[1].style.position = "relative"; // so the footer can be positioned absolute at the bottom
	arrDiv[1].style.textAlign = "left";

	// create a home page link on their logo, for pages with shared banner only
	var frm = document.getElementById("frmGlobalSearch");
	if (document.location.href.indexOf("designmode")==-1 && typeof(frm)!='undefined' && frm!=null)
	{
		var homelink = document.createElement("A");
		homelink.style.position = "absolute";
		homelink.style.top = "0";
		homelink.style.left = "0";
		homelink.style.display = "block";
		homelink.style.width="330px";
		homelink.style.height = "150px";
		if (document.location.href.indexOf("designmode=3")!=-1) homelink.style.zIndex = 1;
		homelink.href = "http://www.cscpacific.ca/content/home.asp";

		homelink.style.textDecoration = "none";
		arrDiv[2].appendChild(homelink);
	}

	// 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()
	}

	UpdateSubNav();

	

}

// display "clicked" background image
function NavClicked(n, bOn)
{

	var p = null;

	// get the proper containers
	while (n && n.id!="navbox")
	{
		if (n.tagName=="A") break;
		n = n.parentNode;
	}

	if (n!=null)
	{
		if (n.id!="navbox")
		{
			p = n.parentNode
			while (p && p.id!="navbox")
			{
				if (p.tagName=="TD") break;
				p = p.parentNode;
			}


			if (p!=null && p.id=="navbox") p = null;
		}
		else
			n = null;
		
	}

	if (p!=null)
	{
		var imgpath = "http://www.cscpacific.ca/images/template/"; // change this to site domain later


		if (bOn)
		{
			p.style.backgroundImage = "url(" + imgpath + "navclick_l.jpg)";
			p.style.backgroundPosition = "top left";
			p.style.backgroundRepeat = "no-repeat";

			n.style.backgroundImage = "url(" + imgpath + "navclick_r.jpg)";
			n.style.backgroundPosition = "top right";
			n.style.backgroundRepeat = "no-repeat";			
		}
		else
		{
			p.style.backgroundImage = "none";
			n.style.backgroundImage = "none";
		}
	}
}

function UpdateSubNav()
{

	var menu = document.getElementById("LeftSubNav");
	var nodes = null;
	var i;
	//var dbg = document.location.href.indexOf("debug")!=-1;
	//if (dbg) alert(menu.innerHTML);

	if (typeof(menu)!='undefined' && menu!=null)
	{
		

		for (i=0; i<menu.childNodes.length; i++) // find the slide menu
		{
			if (typeof(menu.childNodes[i].className)!='undefined' && menu.childNodes[i].className.indexOf("SMMenu")!=-1)
			{
				nodes = menu.childNodes[i].getElementsByTagName("DIV");			
			}		
		}
	}
	
	if (nodes!=null)
	{
		var oc;
		
		

		for (i=0; i<nodes.length-1; i++)
		{
			if (typeof(nodes[i].className)!='undefined' && nodes[i].className.indexOf("SMParent")!=-1)
			{
			
 				// nodes[i].style.paddingTop = "7px"; // can't seem to get this in styles


				// indicates sub nodes
				if (nodes[i+1].id.indexOf("__")==-1) nodes[i].style.backgroundImage = "url(http://www.cscpacific.ca/custom/dynamicmenu/slidemenu/leftnav/lnavemptybg.jpg)";			
				//if (dbg) alert(nodes[i+1].id);
			}
			//if (dbg) alert(nodes[i].className);
		}
		
		
		if (nodes[nodes.length-1].className.indexOf("Parent")!=-1) nodes[nodes.length-1].style.backgroundImage = "url(http://www.cscpacific.ca/custom/dynamicmenu/slidemenu/leftnav/lnavemptybg.jpg)";
		// var id = nodes[nodes.length-1].id;
		//if (!document.getElementById("__" + id)) nodes[nodes.length-1].style.backgroundImage = "url(http://www.cscpacific.ca/custom/dynamicmenu/slidemenu/leftnav/lnavemptybg.jpg)";
	}

}


