$(document).ready(function(){
	$("#copyright span").html(new Date(SERVER_DATE).getFullYear())
	

	//PIXEL
	if (document.images)
	{
		var pix_new = "0";
		if( GetCookie("SeenPath") == "1") pix_new = "1";
	
		home_page_pix = new Image(1,1);
		home_page_pix.src	= "http://www.pch.com/pix/gif.ashx?PixId=312&mon=" + pix_new;
	}
	
	//COOKIE & SHOW HIDE BANNER
	if(document.getElementById)
	{
		if(GetCookie("SeenPath")) {
			document.getElementById("hpTopMenu").style.display = "block";
			//document.getElementById("hpPromoWrap").style.display = "block";
			document.getElementById("hpBottomMenu").style.display = "block";
			//IF NEED TO SHOW BOTTOM BANNER FOR COOKIED - USE THIS			
			fPCHProperties();
		}else{
			var seenPathDate = new Date();
			seenPathDate.setFullYear(seenPathDate.getFullYear()+ 2);
			SetCookie('SeenPath', '1', seenPathDate , '/')
		}
	}
	
	//IF NEED TO ALWAYS SHOW BOTTOM BANNER - USE THIS
	//fPCHProperties();

	if(getURLValue("dnv")) _navFullStop=true;	
});


/*
function fContinue(){
	_navFullStop=true;
	
	//48HR
	var strRdr = "http://prism.pch.com/Path/Jan2010NI/REMUSER.aspx?tid=59b16a88-b4ac-4e0f-afa5-4024397762c8";
	
	document.location=strRdr;
	return false;
}
*/

var arrPCHProperties=[];
var bHPLoaded=false;
//IMAGE, LINK, ALT TEXT, WEIGHT
//arrPCHProperties.push(["assets/banner_pchcashback.jpg","http://cashback.pch.com/?cm_re=PCH.com-_-BotBanner-_-Jun08Cashback","PCHCashBack",1, false]);
//arrPCHProperties.push(["assets/banner_pchcoupons.jpg","http://www.pchcoupons.com/?cm_re=PCH.com-_-BotBanner-_-Jun08Coupons","PCHCoupons",1, false]);
arrPCHProperties.push(["assets/banner_pchgames.jpg","http://www.pchgames.com/?cm_sp=BannerOnPCH-_-TVPath-_-Dec08Games","PCHGames",0, false]);
arrPCHProperties.push(["assets/banner_pchlotto.jpg","http://www.pchlotto.com/?cm_sp=BannerOnPCH-_-TVPath-_-Dec08Lotto","PCHLotto",0, false]);
arrPCHProperties.push(["assets/banner_pchsearchandwin.jpg","http://search.pch.com/?src=pchclsbanr&cm_sp=BannerOnPCH-_-TVPath-_-Dec08SearchNWin","PCHSearch&Win!",1, false]);
//arrPCHProperties.push(["assets/banner_pchquiz4cash.jpg","http://www.pchquiz4cash.com/?cm_sp=BannerOnPCH-_-TVPath-_-Dec08Quiz4Cash","PCHQuiz4Cash",0, false]);
//arrPCHProperties.push(["assets/banner_pchscratchcards.jpg","http://www.pchscratchcards.com/?cm_sp=BannerOnPCH-_-TVPath-_-Dec08ScratchCards","PCHScratchCards",0, false]);
//arrPCHProperties.push(["assets/banner_pchdigital.jpg","http://sb.pch.com/ptr/07/49-99/PCHDigitalHomepage.html","PCHDigital",1, false]);
//arrPCHProperties.push(["assets/banner_pchtv.jpg","http://www.pchtv.com/?cm_re=PCH.com-_-BotBanner-_-AugPCHtv","PCHTV",0, false]);
//arrPCHProperties.push(["assets/banner_prizepatrol.jpg","http://prizepatrol.pch.com?cm_sp=BannerOnPCH-_-TVPath-_-PrizePatrolLive","Prize Patrol",1, false]);

//WEIGHTING RANDOM FEATURE - 2006.06.30 - markm...
var totalweight=0;
for(var iRnd=0;iRnd<arrPCHProperties.length; iRnd++) totalweight+=arrPCHProperties[iRnd][3];

var weighedPCH=[]; //new array to hold "weighted"
var weighedLookup=[];
var currentPCH=0;

while (currentPCH<arrPCHProperties.length){ //step through each arrPCHCntst[] element
	for (i=0; i<arrPCHProperties[currentPCH][3]; i++){
		weighedLookup.push(currentPCH);
		weighedPCH.push(arrPCHProperties[currentPCH]);
	
	}
	currentPCH++;
}

var rnd=Math.floor(Math.random()*totalweight);
var iCurrProp = weighedLookup[rnd]
//rnd=1 //for testing...


//SHOW THE RANDOM PCH PROPERTY IMG,LINK & ALT
function fPCHProperties(){
	//show banner area
	if(arrPCHProperties.length) $("#hpPromoWrap").show();
	//show navigator
	if(arrPCHProperties.length>1)$("#hpPromoNav").show();
	//get random banner
	fPromoGet();
	bHPLoaded=true;
}

function fPromoNext(){
	//alert("fire ELEMENT tag")
	cmCreatePageElementTag("Banner Nav Right","Homepage Banner: " + unescape(arrPCHProperties[iCurrProp][2]))

	iCurrProp++;
	fPromoGet();
	return false;
}

function fPromoBack(){
	cmCreatePageElementTag("Banner Nav Left","Homepage Banner: " + unescape(arrPCHProperties[iCurrProp][2]))
	iCurrProp--;
	fPromoGet();
	return false;	
}

function fPromoGet(){
	var iPreL, iPreR;

	if(iCurrProp<0){
		iCurrProp=arrPCHProperties.length-1;
	}else if(iCurrProp==arrPCHProperties.length){
		iCurrProp=0;
	}

	arrPCHProperties[iCurrProp][3]=true;
	
	$("#hpPromoLink").attr({ 
		href: arrPCHProperties[iCurrProp][1]
       });
	
	$("#hpPromoImg").attr({ 
		src: arrPCHProperties[iCurrProp][0],
		alt: arrPCHProperties[iCurrProp][2]	
       });
	
	
	if(bHPLoaded){
		//fire MANUAL IMPRESSION tag
		var pchLink = arrPCHProperties[iCurrProp][1];
		var pchSP = pchLink.substr(pchLink.lastIndexOf("-_-")+3,pchLink.length)
		//alert(pchSP)
		cmCreateManualImpressionTag("Home Page Main Path", pchSP);
	}
	
	if (document.images){
		//preload imgs	
	
		if(iCurrProp==0){
			iPreL = arrPCHProperties.length-1;
		}else{
			iPreL = iCurrProp-1;
		}
		
		if(iCurrProp==arrPCHProperties.length-1){
			iPreR = 0;
		}else{
			iPreR = iCurrProp+1;
		}
		
		if(!arrPCHProperties[iPreL][3]){ 
			preImgL = new Image();
			preImgL.src = arrPCHProperties[iPreL][0]
			arrPCHProperties[iPreL][3] = true;
		}

		if(!arrPCHProperties[iPreR][3]){ 
			preImgR = new Image();
			preImgR.src = arrPCHProperties[iPreR][0]
			arrPCHProperties[iPreR][3] = true;
		}
		
	}
}

function popupScroll(inThis, w, h){
	var url = inThis.href;
	var left = (screen.width - w) / 2;
	var top = (screen.height - h) / 2;
	features = 'width='+w+',height='+h+',top='+top+',left='+left+',toolbar=no,location=yes,status=no,menubar=no,scrollbars=yes,resizable=yes';

/*time out to deal with IE8 take over of hp*/
	window.setTimeout(function(){
		newwindow=window.open(url,'newwin',features);
		newwindow.focus();
	},111)
	return false;
}

function getURLValue(v){
	q = document.location.search.substring(1);
	if (q=="") return q;
	pairs = q.split("&");
	for (i=0; i<pairs.length; i++){
		if (pairs[i].split("=")[0].toLowerCase() == v.toLowerCase()) return unescape(pairs[i].split("=")[1]);
	}
	return "";
}

/******************************************************************/
//NAV AWAY
/******************************************************************/
var _navFullStop = false;
if(!GetCookie("SeenPath")) {
	var _navShortStop = false;
	window.onbeforeunload = function() {
		if(_navFullStop) return;
		if (_navShortStop){
			_navShortStop = false; //reset it
			return;
		}
	
		var strAlert ="";
		strAlert +="\n------------------------------------ WARNING! ---------------------------------------------\n";
		strAlert +="YOUR ENTRY WILL BE FORFEITED IF YOU LEAVE THIS PAGE.\n";
		strAlert +="Press CANCEL to stay on this page to claim your chance to win.\n";
		strAlert +="------------------------------------------------------------------------------------------------------";
	
		//return "You have not yet completed your entry!";
		
		return strAlert;
	}
}

function fAddOnclick(){	
	var elem =  document.getElementsByTagName('A');
	var strTest;	
	
	for(var i = 0; i < elem.length; i++){
		if(!elem[i].onclick){	
			elem[i].onclick = function(){_globalSubmit = true;}
		}
		else
		{
			
			strTest = new String(elem[i].onclick);			
			if(strTest.indexOf("_globalSubmit")==-1 && strTest.indexOf("return ")==-1){
				elem[i].onclick = '_globalSubmit=true;'+elem[i];
				//alert("PCH ERROR: An Href already exists with an ONCLICK EVENT, you will have to add it manually before continuing.\n\n The Href in question is:\n\n" + elem[i] + "\n\n Adding the code manually will stop this alert from showing.");				
			}
		}		
	}
	
}

//IE only attaching onclick for navaway on anchors
if(document.all && !GetCookie("SeenPath")) window.attachEvent("onload", fAddOnclick);
