var pchRandomnumber;
$(document).ready(function(){	
	// FLASH ////////////////////////////////////////////////////////////////
	var bShowIt=true;
	
//December 15th – December 28th.	

	var pchNow = new Date(SERVER_DATE);
	
	var pchWinterStart = new Date("12/15/2009");
	pchWinterStart.setHours(0);
	pchWinterStart.setMinutes(0);
	pchWinterStart.setSeconds(0);
	pchWinterStart.setMilliseconds(0);
	
	var pchWinterEnd = new Date("12/28/2009");
	pchWinterEnd.setHours(23);
	pchWinterEnd.setMinutes(59);
	pchWinterEnd.setSeconds(59);
	pchWinterEnd.setMilliseconds(999);					
	
	var strCookie="pchSG"
	if(getURLValue("killCookie")){
		if(GetCookie(strCookie)) setNameVal(strCookie,"seenGlobe",0);		
	}else{
		//AUTO START & STOP
		if(pchNow<pchWinterStart || pchNow>pchWinterEnd){
			sGlobeHide();
			return;
		}
	}

	if(GetCookie(strCookie)){
		if(parseInt(getNameVal(strCookie,"seenGlobe"))) bShowIt=false;
	}
    
	if(bShowIt){
		//ENTER DATE @ MIDNIGHT		
		var tomorrow = new Date(SERVER_DATE);
		tomorrow.setHours(23);
		tomorrow.setMinutes(59);
		tomorrow.setSeconds(59);
		tomorrow.setMilliseconds(999);				
		setNameVal(strCookie,"seenGlobe",1,tomorrow);
		
		var flashvars = {
		};
		
		var params = {
		  swliveconnect : "true",
		  allowscriptaccess : "always",
		  wmode:"transparent"
		};
		
		var attributes = {
			id: "pchSnowGlobeFlashObject",
			name: "pchSnowGlobeFlashObject"
		};
		
		
		pchRandomnumber=Math.floor(Math.random()*2)
		var strFlash;
		if(pchRandomnumber){
			strFlash= MediaBasePath + "assets/winterCash/assets/pchSnowGlobeHPGames.swf"
		}else{
			strFlash= MediaBasePath + "assets/winterCash/assets/pchSnowGlobeHPSearch.swf"			
		}
		swfobject.embedSWF(strFlash, "pchSnowGlobeFlashObject", "360", "450", "9.0.0", "expressInstall.swf", flashvars, params, attributes);	
		swfobject.createCSS("#pchSnowGlobeFlashObject","outline:none;"); 
		
		var globePos= $("body").width()/2+144;
		
		$("#pchSnowGlobe").css({left: globePos+"px"})
	}else{
		sGlobeHide();
	}
})


function sGlobeHide(){
	$("#pchSnowGlobe").hide().remove();
}

function showGlobe(){
	$("#pchSnowGlobe").animate({top: "100px"},444, function(){
		var flashy = document.getElementById("pchSnowGlobeFlashObject");																							   
		flashy.sGlobeBounce();		
	});
}


function sGlobeClicked(){
	sGlobeHide();
	var sURL;

	if(pchRandomnumber){ //GAMES
		sURL="http://www.pchgames.com/signin/winter.aspx";	
	}else{ //SEARCH
		sURL="http://search.pch.com/pchmaincustomreg";
	}
	var w= Math.floor($("body").width()*.9);
	var h= Math.floor($(window).height()*.9);
	
	newwindow=window.open(sURL,"nextwin","scrollbars=yes,toolbar=no,directories=no,menubar=no,resizable=yes,status=no,width="+w+",height="+h);
	newwindow.window.focus();
}

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 "";
}


/****************************************************************/
// Document modified by Hens Breet, August 2004
// No expiration date is supplied when creating th cookie, making the cookie SESSION-ONLY

//checkCookieEnable("/errors/cookiesoff.html")

//This function is used to retrieve a value from a name value pair
//stored in a cookie.  There are several assumptions being made
//each one is tested for and a false value is returned if the assumptions/requirements
//are not met
//1.  The cookie name value pairs are delimited by a semicolon ";"
//2.  Each name value pair is delimited by an equal sign "name=value"

//added 5/04 by JL
function setNameVal(cookieName, keyName, keyValue,expires)
{

    var cookieString;
    var cookieArray;
    var cookieOutStr = "";
    var cookieKeyFound = false;
	
	if(!expires){
		expires = new Date(SERVER_DATE);
		expires.setFullYear(expires.getFullYear()+5);
	}
	
    cookieString = GetCookie(cookieName);

    if (cookieString != null)
    {
        cookieArray = cookieString.split(";");

        for (j = 0;  j < cookieArray.length; j++)
        {
             var nameVal = cookieArray[j].split("=");
             if(nameVal.length > 0 && nameVal[0] == keyName)
             {
                 cookieOutStr += keyName + "=" + keyValue + ";" ;
                 cookieKeyFound = true;
             }
             else
             {
                     if(cookieArray[j] != "") cookieOutStr += cookieArray[j] + ";";
             }
        }
     }

    if(!cookieKeyFound) cookieOutStr += keyName + "=" + keyValue + ";" ;
    
   	//alert("setting cookie " + cookieName + "\nto " + cookieOutStr + "\nexpiring on " + expdate)
	//function SetCookie (name,value,expires,path,domain,secure)

	SetCookie(cookieName,cookieOutStr,expires,"/")
}

function getNameVal(cookieName,valName) 
{
    
    var cookieString;
    var cookieArray;
    var nameValArray;
    var nameValStr;
    var nameValLength;
    var i;
    
    cookieString = GetCookie(cookieName);
    
    if (cookieString == null)
    {
        //alert("Debugg -- Cookie " + cookieName + " does not exist");
        return false;
    }
    
    cookieArray = getStringArr(cookieString,";");
    
    if (cookieArray.length == 0) 
    {
        //alert("Debugg -- Cookie does not contain values");
        return false;
    }
        
    nameValStr = setArrayStr(cookieArray,"=");
    nameValArray = getStringArr(nameValStr,"=");
    
    if (nameValArray == 0)
    {
        //alert("Debugg -- nameValArray == 0");
        return false;
    }
    
    nameValLength = nameValArray.length;
    
    for (i = 0; i < nameValLength;)
    {
        //alert(nameValArray[i]);
        if (nameValArray[i].toLowerCase() == valName.toLowerCase())
        {
            //alert(nameValArray[i+1]);
            //return stringReplace(nameValArray[i+1], "+", " ");
            return unescape(nameValArray[i+1]);
        }
        i=i+1; //increment every other array member to just get the key values
    }
   // alert("no entry found in the " + cookieName + " for " + valName);
}


//Purpose of this function is to create a "symbol" delimited
//string out of an array of values
function setArrayStr(strArray,sDelimiter)
{
    var arrLength;
    var sReturn = ""; 
    var i; 
    arrLength = strArray.length;
    if (arrLength == 0)
    {
        //alert("Nothing to concatenate");
        return false;
    }
    else
    {
        for (i=0;i<arrLength;i++)
        {
            sReturn = sReturn + strArray[i] + sDelimiter; 
            
        }
    }
    return sReturn
}   
    
    
    
//Purpose of this function is to create a string array
//out of a "symbol" delimited string
function getStringArr(strCookieVals,sDelimiter)
{
    var sReturn
    sReturn = (sDelimiter.length > 0)? strCookieVals.split(sDelimiter) : false;
    if (! sReturn)
    {
        alert("Delimiter not specified")
        return false
    }
    else
    {   
        return sReturn
    }
}
    
    
function checkCookieEnable(sRedirect)
{
    
    var sCheckCookieName = "check"
    var sCheckCookieVal = "000"
    var sCookieVal = ""
    var expdate = new Date ();
    expdate.setTime (expdate.getTime() + (24 * 60 * 60 * 1000));
    SetCookie(sCheckCookieName,sCheckCookieVal);
    sCookieVal = GetCookie(sCheckCookieName)
    DeleteCookie(sCheckCookieName,"/")
    
    if (sCookieVal==null)
    {
        document.location.href = sRedirect
    }
    
    
}


function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}
//
//  Function to correct for 2.x Mac date bug.  Call this function to
//  fix a date object prior to passing it to SetCookie.
//  IMPORTANT:  This function should only be called *once* for
//  any given date object!  See example at the end of this document.
//
function FixCookieDate (date) {
  var base = new Date(0);
  var skew = base.getTime(); // dawn of (Unix) time - should be 0
  if (skew > 0)  // Except on the Mac - ahead of its time
    date.setTime (date.getTime() - skew);
}
//
//  Function to return the value of the cookie specified by "name".
//    name - String object containing the cookie name.
//    returns - String object containing the cookie value, or null if
//      the cookie does not exist.
//
function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0) break; 
  }
  return null;
}
//

//
//  The first two parameters are required.  The others, if supplied, must
//  be passed in the order listed above.  To omit an unused optional field,
//  use null as a place holder.  For example, to call SetCookie using name,
//  value and path, you would code:
//
//      SetCookie ("myCookieName", "myCookieValue", null, "/");

function SetCookie (name,value,expires,path,domain,secure) {
    document.cookie = name + "=" + escape (value) +
    ((expires) ? "; expires=" + expires.toGMTString() : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

//  Function to delete a cookie. (Sets expiration date to start of epoch)
//    name -   String object containing the cookie name
//    path -   String object containing the path of the cookie to delete.  This MUST
//             be the same as the path used to create the cookie, or null/omitted if
//             no path was specified when creating the cookie.
//    domain - String object containing the domain of the cookie to delete.  This MUST
//             be the same as the domain used to create the cookie, or null/omitted if
//             no domain was specified when creating the cookie.
//
function DeleteCookie (name,path,domain) {
  if (GetCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}
