//**************************************************/
//	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
var _gaq = _gaq || []; // for google analytics

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();
			initLeftNav();
		}	
	}

	// countdown clock
	/*if (document.location.href.indexOf("designmode=3")==-1)
	{
		var g_CountDwn = document.getElementById("AwardsCountDown");
	 	if (typeof(g_CountDwn)!='undefined' && g_CountDwn!=null)
		{

			try 
			{

			$(function() { var endTime = new Date(); endTime = new Date(2010, 9-1, 4-1, 16-1, 0, 0); 
			$('#defaultCountdown').countdown({
   				 format:'d', 
				 compact: true,
				 description: '',
				 compactLabels: ['', '', '', ' days left to nominate'],
			until: $.countdown.UTCDate(-8, endTime)}); });

			g_CountDwn.style.display = 'block';

			}
			catch(ex)
			{

			}
		}
	}*/

	// every child has rights home page image
	/* js 2011-08-18:  the "every child" campaign is over */
	/*
	if (mode==0)
	{
		if (document.location.href.toLowerCase()=="http://www.talktotherep.ca/content/index.asp")
		{
			var foot = document.getElementById("P80Footer");
			if (typeof(foot)!='undefined' && foot!=null)
			{
				var anch = document.createElement("A");
				anch.href = "http://www.everychild.ca";
				anch.target = "_blank";
				anch.id = "EveryChild";
				
				var im = document.createElement("IMG");
				im.border = "0";
				im.src = "http://www.talktotherep.ca/images/everychild.gif";
				
				anch.appendChild(im);
				foot.appendChild(anch);
			}
		}
	}
	*/
	// google analytics
	if (mode==0)
	{
		
 		_gaq.push(['_setAccount', 'UA-20270473-3']);
  		_gaq.push(['_trackPageview']);

  		(function() {
    			var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    			ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    			var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  		})();
	}
}


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");
	var selLink = null;
			
	if (typeof(menu)!='undefined')
	{
		if (menu!=null)
		{
			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("&", "and"); // yeah
					linkname = linkname.replace(/[^\da-zA-Z\- _]/gi, ""); // remove non letters and invalid chars
																	
					if (linkname==thisfolder)
					{
						arrMenuLinks[i].className="MainNavLinkSelected";
						break;
					}
				}
			}
		}
		/*else
		{
			var arrMenuLinks = menu.getElementsByTagName("A");
			if (arrMenuLinks.length>0) selLink = arrMenuLinks[0];
		}*/
	}	
}

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 TTTRFade(arr)
{
	if (document.location.href.indexOf("designmode=3")==-1)
	{
		var it = 1000;
		var s;
		var ids = "";

		for (var i=0; i<arr.length; i++)
		{
			s = "$('#" + arr[i] + "').fadeIn(1000); setTimeout(\"clearFade('" + arr[i] + "')\", 1500);";

			setTimeout(s, it);
			it += 1000;
		}

		/*for (var i=0; i<arr.length; i++)
		{
			s = "clearFade('" + arr[i] + "');";
			setTimeout(s, it);	
		}*/

	}

}


function clearFade(id)
{
	var d = document.getElementById(id);
	
	if (d.style.removeAttribute)
	{
		d.style.removeAttribute("zoom");
		d.style.removeAttribute("filter");
	}	
}

function TTTRRotate(arr, clearID)
{	
	try
	{
		for (var i=0; i<arr.length-1; i++)
		{
			obj = document.getElementById(arr[i]);
			if (obj.style.display == "block")
			{
				obj.style.display = "none";
				document.getElementById(arr[i+1]).style.display = "block";
				return;			
			}
		}

		if (arr.length>1) // if we got here then make the first one visible and the last invisible
		{
			document.getElementById(arr[0]).style.display = "block";
			document.getElementById(arr[arr.length-1]).style.display = "none";

		}
	}

	catch(ex)
	{
		if (clearID) clearInterval(clearID);
	}
	
}


// appends an event-listener to an element so it doesn't
// overwrite any existing listeners
function AddListener(obj, functionName, functionCode) {
	 if (obj.addEventListener) {
		 obj.addEventListener (functionName, functionCode, false );
	 } else if (obj.attachEvent) {
		 obj.attachEvent( "on" + functionName, functionCode);
	 } else {
	  AddEvent(obj, functionName, functionCode);
	 }
}
