/*
-----------------------------------------------
Daniel Bulli
http://www.nuff-respec.com/technology/cross-browser-cookies-with-flash
----------------------------------------------- */

//set this to name of your flash object/embed
var cookie_id  = 'CBCookie';

var CB_Cookie =
{
	init: function(cookie_id){
		this.cookie_id  		= cookie_id;
		this.flash_cookie_ready = false;
		this.flash_cookie_able  = false;
		this.flash_cookie  		= null;
		this.flash_alert  		= false;

		this.flash_is_ready();
	},

	flash_is_ready: function(){
		if(!document.getElementById || !document.getElementById(this.cookie_id)) return;
		if(!this.get_movie(this.cookie_id)) return;

		this.flash_cookie_ready = true;
		this.flash_cookie_able  = this.flash_cookie.f_cookie_able();
	},

	is_able: function(){
		if(!this.flash_alert && !(this.flash_cookie_ready && this.flash_cookie_able)){
			alert("CB_Cookie not initialized correctly.");
			this.flash_alert = true;
		}
		return (this.flash_cookie_ready && this.flash_cookie_able);
	},

	del: function(key){
		if(!this.is_able()) return;
		this.flash_cookie.f_delete_cookie(key);
	},

	get: function(key){
		if(!this.is_able()) return;
		var ret = this.flash_cookie.f_get_cookie(key);
		return ((ret == 'null') ? '' : ret);
	},

	set: function(key,val){
		if(!this.is_able()) return;
		this.flash_cookie.f_set_cookie(key,val);
	},

	get_movie: function(){
    	if (navigator.appName.indexOf("Microsoft") != -1){
    		this.flash_cookie = window[this.cookie_id];
    	}else{
    		this.flash_cookie =  document[this.cookie_id];
    	}

    	return ((this.flash_cookie) ? true : false);
	}

};

function flash_ready(){
	// when the swf is ready, initialize the flash cookie store obj,
	// then check cookie support and set/reset browser/flash cookies as necessary
	CB_Cookie.init(cookie_id);
	checkCookie();
}

//browser cookie functions
function setCookie(cName, cValue, cExpireDays){
    var exDate = new Date();
    exDate.setDate(exDate.getDate()+cExpireDays);
		cookie_str = cName + "=" + escape(cValue) +
											((cExpireDays == null) ? "" : "; expires=" + 
											exDate.toGMTString()) + "; path=/";

    document.cookie = cookie_str
  }

function getCookie(cName){
  if (document.cookie.length > 0){
  cStart = document.cookie.indexOf(cName + "=");
  if(cStart != -1){ 
  	cStart 	= cStart + cName.length + 1; 
    cEnd 		= document.cookie.indexOf(";", cStart);
    if(cEnd == -1) cEnd = document.cookie.length;
    	return unescape(document.cookie.substring(cStart, cEnd));
    } 
  }
  return null;
}

function deleteCookie(cName){
  setCookie(cName, '', 0);  
}

