/***************************************************************
 * @author: christopher a. guy
 * @company: Tribal DDB Chicago
 * @copyright: copyright (c) 2005 Tribal DDB Chicago
 * @date: 2005/25/03
 ***************************************************************/

/**
 * checkNavZone : gets the zone for the navigation
 */
function checkNavZone(){
	var fVariables;
	var query = this.window.location.search;
	var pathname = this.window.location.pathname;

	if(pathname.indexOf(':\\') != -1){
		slash = '\\'; // for running IE locally on Windows. Stupid Windows.
	}else{
		slash = '/';
	}
	
	var zone = getVariables();
	if(zone['zn'] == null){
		// zone is missing so we contruct it from the file name
		var filename = pathname.substring(pathname.lastIndexOf(slash)+1,pathname.length);
		var tempArray = filename.split("_");
		fVariables = "zn="+tempArray[0]+","+tempArray[1];
	}else{
		fVariables = "zn="+zone['zn'];
	}
	return fVariables;
}
/**
 * getVariables : collects the variables in the query string
 */
function getVariables(){
	if(this.param == null){
		this.param = new Object();
	}
	var search = this.window.location.search;

	vars = search.substring(search.indexOf('?')+1,search.length);
	pairs = vars.split("&");

	for(var i=0;i<pairs.length;i++){
		if(temp = pairs[i].split("=")){this.param[temp[0]] = temp[1];}
	}
	return this.param;
}

/**
 * Popup window functions
 */
var myWindow=0;
function newWindow(url,name,w,h,scroller,toolbar,locationbar,menubar,resizable){

	var features,size,position;

	this.nWidth = (w != '')? w : 640;
	this.nHeight = (h != '')? h : 480;
	this.nScrollbar = (scroller != '')? scroller : 0;
	this.nToolbar = (toolbar != '')? toolbar : 0;
	this.nLocationbar = (locationbar != '')? locationbar : 0;
	this.nMenubar = (menubar != '')? menubar : 0;
	this.nResizable = (resizable != '')? resizable : 0;

	this.nleft = (screen.availWidth - this.nWidth)/2;
	this.ntop = (screen.availHeight - this.nHeight)/2;

	if(url == null || url.length < 5 || url.indexOf(".") == -1){
		alert("Please provide the name and path of a file to display");
	}else{
		
		features = 'toolbar='+this.nToolbar;
		features += ',scrollbars='+this.nScrollbar;
		features += ',location='+this.nLocationbar;
		features += ',menubar='+this.nMenubar;
		features += ',resizable='+this.nResizable;
		features += ',status=yes';
		features += ',copyhistory=yes';
		features += ',directories=yes';

		size = 'width='+this.nWidth;
		size += ',height='+this.nHeight;

		position = 'left='+this.nleft;
		position += ',top='+this.ntop;
		position += ',screenX='+this.nleft;
		position += ',screenY='+this.ntop;

		myWindow = window.open(url,name,features +","+ size +","+ position);
		myWindow.focus();
	}
}
function popUpWindow(url,name,w,h){
	newWindow(url,name,w,h);
}
function popFullWindow(url,name,w,h){
	newWindow(url,name,w,h,'yes','yes','yes','yes','yes');
}
function popScrollWindow(url,name,w,h){
	newWindow(url,name,w,h,'yes');
}


function launchGatorade(url){
	if(this.opener != null && !this.opener.closed){
		popFullWindow(url,'gatorade',screen.availWidth-200,screen.availHeight-200);
	}else{
		popFullWindow(url,'gatorade',screen.availWidth-200,screen.availHeight-200);
	}
}

/**
 * Image preloader and rollover functions
 */
function changeImage(img, src){
	if(document.images){
		if(src != 'none'){ document.images[img].src = src;}
	}
}
// preloadImages(array);
function preloadImages(args){
	if(document.imageArray == null){
		document.imageArray = new Array();
	}
	var len = document.imageArray.length;
	for(var i=len;i<(len+args.length);i++){
		document.imageArray[i] = new Image();
		document.imageArray[i].src = args[i-len];
	}
 }
 // loads primary alt navigation first because it's always going to be used.
 // only adds 6k to initial download
 function preloadPrimaryNav(){
	if(document.images){
		var img = new Array();

		img[0] = "../images/nav/navbar_01-over.gif";
		img[1] = "../images/nav/navbar_02-over.gif";
		img[2] = "../images/nav/navbar_03-over.gif";
		img[3] = "../images/nav/navbar_04-over.gif";
		img[4] = "../images/nav/navbar_05-over.gif";
		preloadImages(img);
	}
}
preloadPrimaryNav();

/**
 * Cookie functions
 */

// alert(readCookie('myCookie'));
function readCookie(name){
	var cookieValue = '';
	var search = name +'=';
	if(document.cookie.length > 0){
		offset = document.cookie.indexOf(search);
		if(offset != -1){
			end = document.cookie.indexOf(';', offset);
			if(end == -1)end = document.cookie.length;
			cookieValue = unescape(document.cookie.substring(offset,end));
		}
	}
	return cookieValue;
}
// writeCookie('myCookie','cookieValue',24);
// Stores the string 'cookieValue' in the cookie 'myCookie' which expires after 24 hours
function writeCookie(name,value,hours){
	var expire = '';
	if(hours != null){
		expire = new Date((new Date()).getTime() + hours * 3600000);
		expire = "; expires=" + expire.toGMTString();
	}
	document.cookie = name + "=" + escape(value) + "; path=/" +expire;
}

