//--------------------------------------------------------------------------------
// JavaScript Document
//
// Authors:		Sunny Kalsi / InfoReliance Inc
// Date:		April 08, 2005
// File:		nps.js
// Notes:		Main javascript file for NPS.gov
// Modification Log:
// Change#		Date		Author		        Remarks				
// --------------------------------------------------------
//
//
//
//--------------------------------------------------------------------------------

//window.onerror = function() {return true;}
function doCatch(o) {
	alert(o.name);
}
//window.onerror = doCatch(this);


function printerFriendly() { setActiveStyleSheet('print',0);}
function makeNormal() {	setActiveStyleSheet('normal',0);}
function makeLarger() {	setActiveStyleSheet('larger',0);}
function makeLargest() {setActiveStyleSheet('largest',0);}
//font size adjuster
function setActiveStyleSheet(title,store) {
	var i, a, main;
	//if (!menus_included) { //only run this script if the pagemode ui's are not there...
		switch (title) {
			case "normal": css = 'nps.css'; break;
			case "larger": css = 'npsb.css'; break;
			case "largest": css = 'npsc.css'; break;	
			case "print": css = 'print.css'; break;	
			default: css = 'nps.css'; break;		
		}
//		css=='print.css'?alert(css):"";
		for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
			if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("href")) {
				a.disabled = true;
				if(a.getAttribute("href").indexOf(css)!=-1) {
					a.disabled = false;					
				}
			}
		}	
		if (store == 1) {
			createCookie("npsstyle", title, 1);
		}

		
	//} else {
		//alert('Changes in the text size will not occur if...\n\n...you have logged into the CMS.\n\nPlease log out in order to activate this feature.');
	//}
}






function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	} else expires = "";

	document.cookie = name+"="+value+expires+"; path=/; domain=nps.gov;";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');	
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];	
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}	
	return null;
}

function setStyle() {
	var style = readCookie("npsstyle");
//	alert("setStyle() - style = "+style);
//	if (style == null) {
//		setActiveStyleSheet(style,0);
//		setActiveStyleSheet(style,'nps.css',1);
//	}
    if (style == null) {
          style="normal";
     }
	if (window.name == 'printfriendly') {
		style="print";
	}
	setActiveStyleSheet(style,0);
	//document.write('style:'+style);
}
//setStyle();

//*(D)*//
function onclickH(e)
{
	var alert_string = "Event model: "+emod+"\n";
	var interceptURLStr = "http://cms.nps.gov/nps/r.htm?q=";
	var interceptPage = "/nps/r.htm";
	//alert(alert_string);
	switch (emod)	{
		case "NN4":
			switch (window.location.href.indexOf(interceptPage))	{
				case -1:
					if (e.target.href) {
						alert_string += "clientX: "+e.clientX+"\n";
						alert_string += "clientY: "+e.clientY+"\n";
						alert_string += "target.href: "+e.target.href+"\n";
						if (e.target.href.indexOf("javascript:")==-1 && e.target.href.indexOf("doDisplayOptionsMenu")==-1  && (e.target.href.indexOf(".nps.gov")==-1 || e.target.href.indexOf("nps.gov")==-1)) {
							e.target.href = interceptURLStr + e.target.href;
						}						
						alert_string += "REDIRECTED target.href "+e.target.href+"\n";							
						alert_string += "target.text: "+e.target.text+"\n"; 
						alert_string += "type: "+e.type+"\n";							
					}
				break;
				// any other case and it'll default to the std URL link w/o any redirection
			}				
			break;
			
		case "W3C":
			switch (window.location.href.indexOf(interceptPage))	{
				case -1:
					if (e.target.href) {
						alert_string += "clientX: "+e.clientX+"\n";
						alert_string += "clientY: "+e.clientY+"\n";
						alert_string += "target.href: "+e.target.href+"\n";
						if (e.target.href.indexOf("javascript:")==-1 && e.target.href.indexOf("doDisplayOptionsMenu")==-1  && (e.target.href.indexOf(".nps.gov")==-1 || e.target.href.indexOf("nps.gov")==-1)) {
							e.target.href = interceptURLStr + e.target.href;
						}						
						alert_string += "REDIRECTED target.href "+e.target.href+"\n";							
						alert_string += "target.text: "+e.target.text+"\n"; 
						alert_string += "type: "+e.type+"\n";							
					}
				break;
				// any other case and it'll default to the std URL link w/o any redirection
			}				
			break;
			
		case "IE4+":
			/* (E) */
			e = window.event;
			
			if (navigator.appName=="Netscape" && e.target.nodeType==3){ //if safari and user clicked on a link
				var urlStr = e.target.parentNode.toString();
				
				if (e.target.parentNode && !e.target.parentNode.toString().toLowerCase().indexOf(interceptPage)) {
					alert_string += "clientX: "+e.clientX+"\n";
					alert_string += "clientY: "+e.clientY+"\n";
					alert_string += "target.parentNode (before)"+e.target.parentNode+"\n";
				
					if (urlStr.indexOf("javascript:")==-1 && urlStr.indexOf("doDisplayOptionsMenu")==-1 && (urlStr.indexOf(".nps.gov")==-1 || urlStr.indexOf("nps.gov")==-1)) {
						e.target.href = interceptURLStr + urlStr;
						e.target.parentNode = e.target.href;
					}						
					alert_string += "target.parentNode (after)"+e.target.parentNode+"\n";					
					alert_string += "REDIRECTED e.target.href "+e.target.href+"\n";			
					alert_string += "target.innerText "+e.target.innerText+"\n";			
					alert_string += "target.text: "+e.target.text+"\n"; 					
					alert_string += "type: "+e.type+"\n";
				}								
					alert(alert_string);
			} else { //else IE
				urlStr=e.srcElement.href;
				
				switch (window.location.href.indexOf(interceptPage))	{
					case -1:
						if (urlStr) {
							alert_string += "clientX: "+e.clientX+"\n";
							alert_string += "clientY: "+e.clientY+"\n";
							alert_string += "target.href: "+e.srcElement.href+"\n";
							if (e.srcElement.href.indexOf("javascript:")==-1 && e.srcElement.href.indexOf("doDisplayOptionsMenu")==-1  && (e.srcElement.href.indexOf(".nps.gov")==-1 || e.srcElement.href.indexOf("nps.gov")==-1)) {
								e.srcElement.href = interceptURLStr + e.srcElement.href;
							}						
							alert_string += "REDIRECTED target.href "+e.srcElement.href+"\n";							
							alert_string += "target.text: "+e.srcElement.text+"\n"; 
							alert_string += "type: "+e.type+"\n";							
						}
					break;
					// any other case and it'll default to the std URL link w/o any redirection
				}			
			}			
			
			break;
			
		case "unknown":
			/* (E) */
			switch (window.location.href.indexOf(interceptPage))	{
				case -1:
					if (e.target.href) {
						alert_string += "clientX: "+e.clientX+"\n";
						alert_string += "clientY: "+e.clientY+"\n";
						alert_string += "target.href: "+e.target.href+"\n";
						if (e.target.href.indexOf("javascript:")==-1 && e.target.href.indexOf("doDisplayOptionsMenu")==-1  && (e.target.href.indexOf(".nps.gov")==-1 || e.target.href.indexOf("nps.gov")==-1)) {
							e.target.href = interceptURLStr + e.target.href;
						}						
						alert_string += "REDIRECTED target.href "+e.target.href+"\n";							
						alert_string += "target.text: "+e.target.text+"\n"; 
						alert_string += "type: "+e.type+"\n";							
					}
				break;
				// any other case and it'll default to the std URL link w/o any redirection
			}			
			break;
			
	}
	
	//alert(alert_string);
	return true;
}

function onloadH(e)
{
	//*(A)*//
	emod = (e) ? (e.eventPhase) ? "W3C" : "NN4" : (window.event) ? "IE4+"  : "unknown";

	//*(B)*//
	if (emod == "NN4") {
		document.captureEvents(Event.CLICK);
	}
	
	//*(C)*//
	//document.onclick = onclickH;
	return true;
}

//global vars
var emod; //the event model

//define the event handler for the onload event

function onloadfx() {
	if (!menus_included) {	onloadH();	}
	setStyle();
}


//if (!js_userID) {	onloadH();	}	


//used in the Navigation, DidYouKnows's, Highlights, Page Picture....
function openCustomWindow(newURL) {
	var w = 800, h = 600;
	if (document.all || document.layers)
	{
	  w = screen.availWidth;
	  h = screen.availHeight;
	}
	var popW = 800, popH = 600;
	var leftPos = (w-popW)/2, topPos = (h-popH)/2;
	newCustomWindow = window.open(newURL,'popup','width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos + ',scrollbars=yes' + ',resizable=yes');
	newCustomWindow.opener = window;
	newCustomWindow.focus();
}

// Used in Redirect App
function openRedirectWindow(newURL) {
	var w = 500, h = 660;
	if (document.all || document.layers)
	{
	  w = screen.availWidth;
	  h = screen.availHeight;
	}
	var popW = 500, popH = 660;
	var leftPos = (w-popW)/2, topPos = (h-popH)/2;
	newCustomWindow = window.open(newURL,'popup','width=' + popW + ',height=' + popH + ',top=' + topPos + ',left=' + leftPos + ',scrollbars=no' + ',resizable=yes');
	newCustomWindow.opener = window;
	newCustomWindow.focus();
}

//pencil icon edit buttons for custom apps.
function doNavMenu(dlgloader,pageid,event)
{
//	alert(jsUserID);
	if (jsUserID == '1000005') {
//		alert(useragent);
//		if (document.all) {alert('ie')} else {alert('not ie')};
//		(document.all) ? thisMenu = document.all("NavMenu") : thisMenu = document.getElementById("NavMenu",event);
//		var thisMenu = document.getElementById("NavMenu",event);
//		var thisMenu = document.all("NavMenu");
//		alert(thisMenu);
		calcMenuPos ("NavMenu", event);
		window.event.returnValue = false;		
	} else {
		var thisMenu = document.all("NavMenu");
		calcMenuPos ("NavMenu");
		window.event.returnValue = false;		
	}
}

function redirectScript(yurl) {
	if (!js_userID) { //to prevent redirection from occuring while you are logged into the CMS
		document.location.href = yurl;
	}
}




