// JavaScript Document
var viewUpdate; //Boolean value returned to Flash regarding implementation of viewUpdate ad
	var timeout_id; //to call viewIncrement function
	var toPeriods = new Array(0,12,24); // day-part END times - 4pm=16, midnight=24

	var hour_extract = new Date();
	var current_hour = hour_extract.getHours();
	//getUTCHours would be 7 ghours ahead, everything else is UTC time
	var current_day = hour_extract.getUTCDay();
	//get the current hour to set our cookie expires Date value
	
	var cookie_expires = new Date();
	
	if(current_hour >= toPeriods[0] && current_hour < toPeriods[1])	{
		cookie_expires.setHours(toPeriods[1]);
		cookie_expires.setUTCMinutes(0);
		cookie_expires.setUTCSeconds(0);
		//if between midnight and 3am
		//alert("if between midnight and 3am cookie_expires: " + cookie_expires);
		//alert("1: current_hour: " + current_hour + ", " + cookie_expires.toUTCString());
	}
	else if(current_hour >= toPeriods[1] && current_hour < toPeriods[2])	{
		cookie_expires.setHours(toPeriods[0]);
		cookie_expires.setUTCMinutes(0);
		cookie_expires.setUTCSeconds(0);
		
		cookie_expires.setUTCDate(cookie_expires.getUTCDate() + 1);
		//increment the day 24 hours as above we have only adjusted the hour, minutes and seconds but no the day
		//if between 3am and 3pm
		//alert("if between 3am and 3pm cookie_expires: " + cookie_expires);
		//alert("2: current_hour: " + current_hour + ", " + cookie_expires.toUTCString());
	}
	
	//set our cookie_expires Date Object
	//document.write("<font color='#FFFFFF'>PST " + cookie_expires + ", GMT</font>");
	//document.write("<font color='#FFFFFF'>" + cookie_expires.toGMTString() + "</font>");

    var cookie_name = "CBS_view";
	var cookie_value_increment;
	var cookie_value_increment_max = 200; //make 2 if ad
	var cookie_arr;
	var cookie_written_arr;
	//initilise cookie array variables
	var increment_nmb;
	//counter
	
	// Create a cookie with the specified name and value.
	// The cookie expires at the end of the 21st century.	
	function SetCookie(sName, sValue)	{
	    date = new Date();
	    //document.cookie = sName + "=" + escape(sValue) + "; expires=Fri, 31 Dec 2099 23:59:59 GMT;";
		document.cookie = cookie_name + "=" + escape(sValue) + "; expires=" + cookie_expires.toUTCString() + ";";
		//alert(cookie_expires.toGMTString());
		//alert(document.cookie);
	}
	
	// Get the value of the cookie with the specified name.
	function GetCookie(cookie_name)	{
	  cookie_written_arr = document.cookie.split("; ");
	  //based on the setCookie function the cookie will be written on your system something like: "cookie_name = 0". We split it so we dont' get: "; expires=Fri, 31 Dec 2099 23:59:59 GMT;", ie: the expiry date. If there are more than one cookies, then the 1st element in the array may be name/value pair in a String: "cookie_name = 0", the 2nd element: "himym_takover = 2", etc

	  for (var i=0; i < cookie_written_arr.length; i++)	{
	  	  //the length is 1 in this case
	      // a name/value pair (a crumb) is separated by an equal sign
		  cookie_arr = cookie_written_arr[i].split("=");

		  //we make the name value pair: "cookie_name = 0" of the only element of array cookie_written_arr, 2 array elements in array cookie_arr: cookie_arr[0] = cookie_name, cookie_arr[1] = "1". So even numbers will contain cookie names and odd numbers will contain the cookie values in String format
		  
		  if (cookie_name == cookie_arr[0])	{
		  	 //if in the GetCookie(cookie_name); function call, the parameter equals cooke_arr element 0 which it does
		     return unescape(cookie_arr[1]);
			 //if parameter cookie_name equals the actual cookie name, return the acutal value which is in cookie_arr element 2
		  }
	  }
	
	  return null;
	  //if a cookie with the requested name does not exist, return null
	}
	
	// Delete the cookie with the specified name.
	// The value of the cookie is unnecessary.
	function DelCookie(cookie_name)	{
	  document.cookie = cookie_name + "=; expires=Fri, 21 Dec 1976 04:31:24 GMT;";
	  // we use an expiry date in the past so the cookie is automatically deleted
	}
	
	function viewIncrement() {
		//alert('viewIncrement has been called');
		//var geturl = window.location.toString();
		
		var ord_srt = ord + "";
		
		if(GetCookie(cookie_name) == null)	{
			SetCookie(cookie_name, 0);
		}
		else	{
			cookie_value_increment = parseInt(cookie_arr[1]) + 1;
			SetCookie(cookie_name, cookie_value_increment);
			
			if(cookie_value_increment < cookie_value_increment_max)	{
				viewUpdate = true;
				//return cookie_value_increment;
				//thisMovie("ad_light").getTakeover(takeOver, ord_srt); //ungrey if ad
			}
			else	{
				viewUpdate = false;
				//return cookie_value_increment;
				//thisMovie("ad_light").getTakeover(viewUpdate, ""); //ungrey if ad
			}
		}
		//alert(GetCookie(cookie_name));
		
		clearTimeout(timeout_id);
	}

	function thisMovie(movieName) {
        if (navigator.appName.indexOf("Microsoft") != -1) {
            return window[movieName]
        }
        else {
            return document[movieName]
        }
    }
                    
    /*function setTakeover()	{
        //alert('setTakeover has been called');
        timeout_id = setTimeout("viewIncrement()", 500);
    }
	
	setTimeout("viewIncrement()", 500);*/