//<script language="JavaScript">
/*
	Author:			Robert Graves
	Created:		10/04/2000
	Last Modified:	11/21/2000

	Description:
		Common client side functions on the USAToday.com website
*/
var usat = new clsUsat();
usat.init();

function clsUsat () {
	/*
		Comments
		Class containing commonly used objects and functions on the 
		USAToday.com website
	*/
	this.util = new clsUtil();
	this.page = new clsPage();
	this.cookie = new clsCookie();
	this.init = fxInit;
	/*
		Members
	*/
	function fxInit () {   // member of clsUsat
		this.page.removeEnemyFrames();
		this.util.init();
		this.page.init();
	}   // fxInit

	function clsUtil () {   // member of clsUsat
		/*
			Comments
			Class containing common utility functions such as 
			trimming a string
		*/
		this.init = fxInit;
		this.trim = fxTrim;
		this.openBareWindow = fxOpenBareWindow;
		this.popunder = fxPopUnder
		/*
			Members
		*/
		function fxInit () {   // member of clsUtil
			/*
				Comments
				ok this is kind of funky.  String in JavaScript does not contain a
				trim function.  So during load, dynamically add this trim function
				to the prototype of String.  Now every string object has a trim
				function.
			*/
			String.prototype.trim = fxTrim;
		}  // fxInit

		function fxTrim (strInput) {   // member of clsUtil
			/*
				Comments
				Trim all leading and trailing whitespace using Regular Expressions
			*/
			var strResult = null;
			if (strInput == null)
				strInput = this;
			if (strInput){
				strResult = new String(strInput);
				strResult = strResult.replace(/^\s+/, "");
				strResult = strResult.replace(/\s+$/, "");
			}
			return(strResult);
		}   // fxTrim

		function fxOpenBareWindow(url, title, width, height){   // member of clsUtil
			/*
				Comments
				Opens a window with the specified url, title, height, and width
				but with no decorations (scroll bar, toolbar, etc.)
			*/
			window.open(url, title, "scrollbars=no,menubar=no,toolbar=no,status=no,top=0,left=0,screenx=0,screeny=0,width=" + width + ",height=" + height + ",resizable=no");
		}   // fxOpenBareWindow

	}   // clsUtil

	function fxPopUnder(strURL) {
			/*
				Comments
				Setting up the properties behind the behind-pop.
			*/

			var w = 760;
			var h = 420;
			props = "width=" + w + ",";
			props += "height=" + h + ",";
			props += "titlebar=1,";
			props += "toolbar=1,";
			props += "location=1,";
			props += "menubar=1,"
			props += "scrollbars=1,"
			props += "resizable=1,"
			props += "channelmode=0,"
			props += "directories=0,"
			props += "status=1";
			var popwin = window.open("","popwin",props,true);
			popwin.location = strURL;
			popwin.opener.focus();
		}	//fxPopUnder

	function clsPage () {   // member of clsUsat
		/*
			Comments
			Class containing page manipulation funtions
		*/
		this.arrOnLoadList = new Array();
		this.removeFrames = fxRemoveFrames;
		this.removeEnemyFrames = fxRemoveEnemyFrames;
		this.jumpSelect = fxJumpSelect;
		this.onLoad = fxOnLoad;
		this.addLoadEvent = fxAddLoadEvent;
		this.init = fxInit;
		this.onResize = fxOnResize;
		/*
			Members
		*/

		function fxInit() {    //member of clsPage
			/*
			Initializing the fxResize function
			*/
			fxOnResize(true);
		}
		     // fxInit	
		
		function fxOnResize(init) {   // member of clsPage
			/*
				reloads the window if Nav4 resized
			*/
			if (init==true) with (navigator)  {
				if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
					document.MM_pgW=innerWidth; 
					document.MM_pgH=innerHeight; 
					onresize=fxOnResize; 
				}   //if
			}   
			else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) {
				location.reload();
			}
		}    // fxOnResize
		
		function fxRemoveFrames () {   // member of clsPage
			/*
				Comments
				Jump out of the current frameset if in any
			*/
			if (top.location != self.location) {
				top.location.replace(self.location);
			 }
		}   // fxRemoveFrames

		function fxRemoveEnemyFrames () {
			/*
				Comments
				Prevents other sites from framing the current page
			*/
			var strLocalDomain = fxGetDomain(self.location);
			var strTopDomain = fxGetDomain(top.location);
			if (strLocalDomain != strTopDomain){
				top.location.replace(self.location);
			}   // if
		}   // fxRemoveEnemyFrames

		function fxGetDomain (strURL) {   // member of clsPage
			/*
				Comments
				Given a URL return the domain name(just top and second).
				Returns empty string if loaded from a file.
				Returns the server name if loaded on an intranet.
			*/
			var intIndexStartName, intIndexEndName, strServer, arrServer;
			strServer = new String(strURL);
			intIndexStartName = strServer.indexOf("://") + 3;
			intIndexEndName = strServer.indexOf("/", intIndexStartName);
			strServer = strServer.substr(intIndexStartName, intIndexEndName - intIndexStartName);
			arrServer = strServer.split(".");
			switch (arrServer.length) {
				case 0:		// no server (maybe from file:///)
					strServer = "";
					break;
				case 1:
					strServer = arrServer[0];
					break;
				default:
					strServer = arrServer[arrServer.length-2] + "." + arrServer[arrServer.length-1];
			}   // switch
			return strServer;
		}   // fxGetDomain
		
		function fxJumpSelect (objSelect) {   // member of clsPage
			/*
				Comments
				Jump to the location specified by the value of the
				option at the selected index of a Select box.
			*/
			var strLocation = objSelect.options[objSelect.selectedIndex].value;
			if (strLocation){
				document.location = strLocation;
			}   // if
			objSelect.selectedIndex = 0;
		}   // fxJumpMenu

		function fxOnLoad(){   // member of clsPage
			var fx;
			for (var i=0;i<this.arrOnLoadList.length;i++){
				fx = this.arrOnLoadList[i];
				fx();
			}   // for
		}   // fxOnLoad
		
		function fxAddLoadEvent(objFunction){
			/*
				Add a function pointer to the list of functions
				that need to be executed when the page loads.
			*/
			this.arrOnLoadList[this.arrOnLoadList.length] = objFunction;
		}   // fxAddLoadEvent
	}   // clsPage

	function clsCookie () {   // member of clsUsat
		/*
			Class for manipulating cookies
		*/
		this.set = fxSet;
		this.get = fxGet;
		this.remove = fxRemove;
		this.buildMatrix = fxBuildMatrix;
		
		/*
			Members
		*/
		function fxSet (strKey, strValue, dtExpires,
						strPath, strDomain, blnSecure) {   // member of clsCookie
			/*
				Comments
				Sets a cookie in the browser.
				If the cookie already exists it is overwritten.
				Only strKey and strValue parameters are required
			*/
			var strCookie = strKey + "=" + escape(strValue)+ ";";
			if (dtExpires){
				strCookie += "expires=" + dtExpires.toUTCString()+ ";";
			}   // if
			if ((strPath) && (strPath != "")){
				strCookie += "path=" + strPath+ ";";
			}   // if
			if ((strDomain) && (strDomain != "")){
				strCookie += "domain=" + strDomain + ";";
			}   // if
			if (blnSecure){
				strCookie += "secure";
			}   // if
			document.cookie = strCookie;
		}   // fxSet

		function fxGet (strKey) {   // member of clsCookie
			/*
				Comments
				Returns the value of the specified cookie.
				Only returns the first one if multiple exist.
				Returns null if cookie doesn't exist.
			*/
			var strValue = null;
			var arrCookies = this.buildMatrix(new String(document.cookie));
			for (var intIndex=0;intIndex<arrCookies.length;intIndex++) {
				if (arrCookies[intIndex][0] == strKey){
					strValue = arrCookies[intIndex][1];
					break;
				}
			}   // for
			return strValue;
		}   // fxGet

		function fxBuildMatrix (strCookies) {   // member of clsCookie
			/*
				Comments
				Builds a 2 dimensional array of key = value pairs.
			*/
			var arrCookie;
			var arrCookies = strCookies.split(";");
			for (var intIndex=0;intIndex<arrCookies.length;intIndex++){
				arrCookie = arrCookies[intIndex].trim().split("=");
				arrCookie[1] = unescape(arrCookie[1]);
				arrCookies[intIndex] = arrCookie;
			}   // for
			return arrCookies;
		}   // fxBuildMatrix

		function fxRemove (strKey, strPath, strDomain) {   // member of clsCookie
			/*
				Comments
				Removes the cookie and returns the value.
				Returns null if it doesn't exist.
			*/
			var strCookie, dtYesterday;
			var strValue = this.get(strKey);
			if (strValue){   // the cookie exists so delete it.
				strCookie = strKey + "=;";
				if ((strPath) && (strPath != "")){
					strCookie += "path=" + strPath + ";";
				}   // if
				if ((strDomain) && (strDomain != "")){
					strCookie += "domain=" + strDomain + ";";
				}   // if
				dtYesterday = new Date();
				dtYesterday.setDate(dtYesterday.getDate() - 1);
				strCookie += "expires=" + dtYesterday.toGMTString() + ";";
				document.cookie = strCookie;
			}   // if
			return strValue;
		}   // fxRemove
	}   // clsCookie
}   // clsUsat

/*
	Comments
	Multimedia related functions
*/
function openPopUp (theurl,thewidth,theheight) {
	/*
		Comments
		Opens a window with the specified source, height, and width.
	*/
	var theargs = "width="+thewidth+",height="+theheight+"top=100,left=100";
	window.open(theurl,'earpopup',theargs);
}   // openPopUp

var APlayerSrc = "";

function bluefishPopUp () {
	/*
		Comments
		A behind-pop will appear behind the window once the user closes the window
	*/

	var url = "http://ads.bluefishnetwork.com/rotator/536/0/viewCGI?pool=530&theme=129&type=77";
	var domain = "usatoday.com";
	var min_timeout = 1440;
	expired = document.cookie.indexOf("popunder");
	if (expired == -1) {
		setCookie(min_timeout);
		popUnder();
	}   // if	
}	//bluefishPopUp

function setCookie(min_timeout) {
	/*
		Comments
		Setting up a cookie for the pop-up.
	*/
	var d = new Date();
	var timeout = d.getTime() + (min_timeout * 60 * 1000);
	var expires = new Date();
	expires.setTime(timeout);
	var stamp = d.valueOf();
	if (document.domain.indexOf(domain) == -1) {
		domain = ""; 
	}   // if
	if (domain.length > 0 ) {
		domain = "domain=." + domain + ";";
	}   // if
	var expires_str = expires.toGMTString();
	var cookie_str = "popunder=" + stamp + ";path=/;"
	cookie_str +=  domain + "expires=" + expires_str;
	document.cookie = cookie_str; 
}	//setCookie

function popUnder() {
	/*
		Comments
		Setting up the properties behind the behind-pop.
	*/

	var w = 760;
	var h = 420;
	props = "width=" + w + ",";
	props += "height=" + h + ",";
	props += "titlebar=1,";
	props += "toolbar=1,";
	props += "location=1,";
	props += "menubar=1,"
	props += "scrollbars=1,"
	props += "resizable=1,"
	props += "channelmode=0,"
	props += "directories=0,"
	props += "status=1";
	var popwin = window.open("","popwin",props,true);
	popwin.location = url;
	popwin.opener.focus();

}	//popUnder

function OpenAudio (url) {
	/*
		Comments
		Opens a window pointing to /audio/aplay1v*.htm
		* is 1 if the JavaScript version is >= 1.2, 2 otherwise
		the APlayerSrc variable is set so it can be referenced by the opened page
	*/
	APlayerSrc = url;
	var page = "/audio/aplay1v1.htm";
	if (_version < 12){
		page = "/audio/aplay1v2.htm";
	}   // if
	usat.util.openBareWindow(page, "RAPlayer", 390, 220);
}   // OpenAudio

var VPlayerSrc = "";

function OpenVideo (url) {
	/*
		Comments
		Opens a window pointing to /video/mplay5v*.htm
		* is 1 if the JavaScript version is >= 1.2, 2 otherwise
		the VPlayerSrc variable is set so it can be referenced by the opened page
	*/
	VPlayerSrc = url;
	var page = "/video/mplay5v1.htm";
	if (_version < 12){
		page = "/video/mplay5v2.htm";
	}   // if
	usat.util.openBareWindow(page, "RMPlayer", 425, 345);
}   // OpenVideo

function OpenVideobig (url) {
	/*
		Comments
		Identical to OpenVideo except the new window's height is larger
	*/
	VPlayerSrc = url;
	var page = "/video/mplay6v1.htm";
	if (_version < 12){
		page = "/video/mplay6v2.htm";
	}   // if
	usat.util.openBareWindow(page, "RMPlayer", 425, 425);
}   // OpenVideobig

var _version = 10;
detectJSVersion();
function detectJSVersion(){
	/*
		Comments
		_version is provided for backwards compatability with old scripts
		In order to detect different javascript versions, we need to have
		a seperate script tag for each version with only 1 line of code.  
		Instead of having to write x script tags on every page, if this 
		script is included all the necessary version detection tags will
		be written automatically.
	*/
	document.write("<scr" + "ipt language=\"JavaScript1.1\">_version = 11;</S" + "CRIPT>");
	document.write("<scr" + "ipt language=\"JavaScript1.2\">_version = 12;</S" + "CRIPT>");
}   // detectJSVersion

// Dream Weaver functions for backwards compatability
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.0
	var p,i,x;  
		if(!d) d=document; 
			if((p=n.indexOf("?"))>0&&parent.frames.length) {
				d=parent.frames[n.substring(p+1)].document; 
				n=n.substring(0,p);}
					if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
						for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
							if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

