/**
 * Copyright (C) 2002-2003, CodeHouse.com. All rights reserved.
 * CodeHouse(TM) is a registered trademark.
 *
 * THIS SOURCE CODE MAY BE USED FREELY PROVIDED THAT
 * IT IS NOT MODIFIED OR DISTRIBUTED, AND IT IS USED
 * ON A PUBLICLY ACCESSIBLE INTERNET WEB SITE.
 * 
 * CodeHouse.com JavaScript Library Module: Cookie Utility Class
 *
 * You can obtain this script at http://www.codehouse.com
 */
function CJL_CookieUtil(name, duration, path, domain, secure)
{
   this.affix = "";
   
   if( duration )
   {   	  
      var date = new Date();
	  var curTime = new Date().getTime();

	  date.setTime(curTime + (1000 * 60 * duration));
	  this.affix = "; expires=" + date.toGMTString();
   }
   
   if( path )
   {
      this.affix += "; path=" + path;
   }
   
   if( domain )
   {
      this.affix += "; domain=" + domain;
   }

   if( secure )
   {
      this.affix += "; secure=" + secure;
   }
   
      
   function getValue()
   {
      var m = document.cookie.match(new RegExp("(" + name + "=[^;]*)(;|$)"));

      return m ? m[1] : null;   
   }
   
   this.cookieExists = function()
   {
      return getValue() ? true : false;
   }
      
   this.expire = function()
   {
      var date = new Date();
	  date.setFullYear(date.getYear() - 1);
	  document.cookie=name + "=noop; expires=" + date.toGMTString(); 
   }
        
   this.setSubValue = function(key, value)
   {
      var ck = getValue();

      if( /[;, ]/.test(value) )
      {
         //Mac IE doesn't support encodeURI
		 value = window.encodeURI ? encodeURI(value) : escape(value);
      }

      
      if( value )
      {
         var attrPair = "@" + key + value;

         if( ck )
         {
             if( new RegExp("@" + key).test(ck) )
	         {
		        document.cookie =
				   ck.replace(new RegExp("@" + key + "[^@;]*"), attrPair) + this.affix;
	         }
	         else
	         {
		        document.cookie =
				   ck.replace(new RegExp("(" + name + "=[^;]*)(;|$)"), "$1" + attrPair) + this.affix;
	         }
         }
         else
         {
	        document.cookie = name + "=" + attrPair + this.affix;
         }
      }
      else
      {      
	     if( new RegExp("@" + key).test(ck) )
	     {
	        document.cookie = ck.replace(new RegExp("@" + key + "[^@;]*"), "") + this.affix;
	     }
      }
   }


   this.getSubValue = function(key) {
       var ck = getValue();

       if (ck) {
           var m = ck.match(new RegExp("@" + key + "([^@;]*)"));

           if (m) {
               var value = m[1];

               if (value) {
                   //Mac IE doesn't support decodeURI
                   return window.decodeURI ? decodeURI(value) : unescape(value);
               }
           }
       }
       return "";
   }
}

// icee logic
function extract_get_var(_var_name)
{
	var	_var_val;
	_get = document.location.search;
	_pos=_get.indexOf(_var_name+"=");
	if (_pos!=-1)
	{
		_start=_pos+_var_name.length+1;		
		_end=_get.indexOf("&",_start);				
		if(_end<0) _end=_get.length;		
		_var_val=_get.substring(_start,_end);		
	}
	return _var_val;
}

//A uuid generator
Math.uuid = (function() {
  // Private array of chars to use
  var CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split(''); 

  return function (len, radix) {
    var chars = CHARS, uuid = [], rnd = Math.random;
    radix = radix || chars.length;

    if (len) {
      // Compact form
      for (var i = 0; i < len; i++) uuid[i] = chars[0 | rnd()*radix];
    } else {
      // rfc4122, version 4 form
      var r;

      // rfc4122 requires these characters
      uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
      uuid[14] = '4';

      // Fill in random data.  At i==19 set the high bits of clock sequence as
      // per rfc4122, sec. 4.1.5
      for (var i = 0; i < 36; i++) {
        if (!uuid[i]) {
          r = 0 | rnd()*16;
          uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r & 0xf];
        }
      }
    }

    return uuid.join('');
  };
})();


var icee_domain=".playcool.com";
ml_domain="ml.playcool.com";
_duration=60*24*720;
var ice_cookie=new CJL_CookieUtil("icee_data",_duration,"/",icee_domain);
_referrer=document.referrer;
_referrer=_referrer.substring(7);
_referrer=_referrer.substring(0,_referrer.indexOf("/"));	

if(_referrer && _referrer.indexOf(icee_domain)<0)
{
	ice_cookie.setSubValue("ice_referrer",_referrer);
}
_ad_id=extract_get_var("ad_id");
if(_ad_id)
{
	//refresh ad_id every time
		ice_cookie.setSubValue("ice_ad_id",decodeURIComponent(_ad_id));
}

//create a clientid
_client_id=ice_cookie.getSubValue("ice_client_id");
if(!_client_id)
{
	ice_cookie.setSubValue("ice_client_id",Math.uuid());
}

//google analytic customer var
var _customer = extract_get_var("customer");
if (_customer)
{
	try
	{pageTracker._setVar(decodeURIComponent(_customer));}
	catch(ex)
	{void(0);}
	
}	
	

