/*
-----------------------------------------------
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 getCookie(c_name)
{
    if (document.cookie.length>0)
    {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1)
        {
            c_start=c_start + c_name.length+1;
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return "";
}

function setCookie(c_name,value,expiredays)
{
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function flash_ready()
{
    CB_Cookie.init(cookie_id);
    //our code
    sc_cookie_name = 'wid';
    
    if( !CB_Cookie.get(sc_cookie_name) && getCookie(sc_cookie_name) ){
        CB_Cookie.set(sc_cookie_name,getCookie(sc_cookie_name));
    }else
    if( CB_Cookie.get(sc_cookie_name) && !getCookie(sc_cookie_name) ) {
        setCookie(sc_cookie_name,CB_Cookie.get(sc_cookie_name),271);
    } else
        if( CB_Cookie.get(sc_cookie_name) && getCookie(sc_cookie_name)
            && CB_Cookie.get(sc_cookie_name) != getCookie(sc_cookie_name) ) {
            setCookie(sc_cookie_name,CB_Cookie.get(sc_cookie_name),271);
        }
}

