/*
 //
 //source: http://tech.irt.org/articles/js064/index.htm
 //
 
 
To embed the cookie.js file into a page use the following: 
 
<SCRIPT SRC="js/cookie.js"><!--
    var src_loaded = false;
//--></SCRIPT>


To later create the Master Cookie: 

<SCRIPT LANGUAGE="JavaScript"><!--
if (src_loaded) storeMasterCookie();
//--></SCRIPT>


To store an Intelligent Cookie: 

<SCRIPT LANGUAGE="JavaScript"><!--
if (src_loaded) storeIntelligentCookie('test','cookie value');
//--></SCRIPT>

 
*/

function Get_Cookie(name) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
}

function Set_Cookie(name,value,expires,path,domain,secure) {
    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") + 
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}

function Delete_Cookie(name,path,domain) {
    if (Get_Cookie(name)) document.cookie = name + "=" +
       ( (path) ? ";path=" + path : "") +
       ( (domain) ? ";domain=" + domain : "") +
       ";expires=Thu, 01-Jan-70 00:00:01 GMT";
}

var today = new Date();
var zero_date = new Date(0,0,0);
today.setTime(today.getTime() - zero_date.getTime());

var todays_date = new Date(today.getYear(),today.getMonth(),today.getDate(),0,0,0);
var expires_date = new Date(todays_date.getTime() + (999999 * 8 * 7 * 86400000));

function storeMasterCookie() {
    if (!Get_Cookie('MasterCookie'))
        Set_Cookie('MasterCookie','MasterCookie');
}

function storeIntelligentCookie(name,value) {
    if (Get_Cookie('MasterCookie')) {
        var IntelligentCookie = Get_Cookie(name);
        if ((!IntelligentCookie) || (IntelligentCookie != value)) {
            Set_Cookie(name,value,expires_date);
            var IntelligentCookie = Get_Cookie(name);
            if ((!IntelligentCookie) || (IntelligentCookie != value))
                Delete_Cookie('MasterCookie');
        }
    }
}

var src_loaded = true;


function WriteFlashPlayOnce(flashObjName, width, height, flashdiv) {
    if (src_loaded) storeMasterCookie();
    
    var flashHtml = "";
    var amDebugging = false;  //set to false when done debugging
    var debugStr = "";
    
    if (Get_Cookie(flashObjName)==flashObjName) {
        debugStr = flashObjName + " alreadyshowed=true";        
        // build flash - first time
        
        //assert: dont play automatically
        flashHtml = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + width + '" height="' + height + '" id="' + flashObjName + '" align="middle"><param name="allowScriptAccess" value="sameDomain" />';
        flashHtml += '<param name="movie" value="/templates/audio/' + flashObjName + '.swf?alreadyshowed=true" />';
        flashHtml += '<embed play="false" src="/templates/audio/' + flashObjName + '.swf?alreadyshowed=true" quality="high" bgcolor="#ffffff" width="' + width + '" height="' + height + '" name="' + flashObjName + '" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object>';
            
    } else {
    
        debugStr = flashObjName + ' alreadyshowed=false';   
        //debugStr += ' cookie [' + flashObjName + ']:=' + Get_Cookie(flashObjName); 
        // build flash - 2nd time
        
        //assert: not set cookie yet - should play
        flashHtml = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="' + width + '" height="' + height + '" id="' + flashObjName + '" align="middle"><param name="allowScriptAccess" value="sameDomain" />';
        flashHtml += '<param name="movie" value="/templates/audio/' + flashObjName + '.swf?alreadyshowed=false" />';
        flashHtml += '<embed play="true" src="/templates/audio/' + flashObjName + '.swf?alreadyshowed=false" quality="high" bgcolor="#ffffff" width="' + width + '" height="' + height + '" name="' + flashObjName + '" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object>';
  
        //storeIntelligentCookie(flashObjName,"loaded");
        storeIntelligentCookie(flashObjName,flashObjName);
    }

    if (document.getElementById) {
       document.getElementById(flashdiv).innerHTML = flashHtml;             
    } else {
       document.all[flashdiv].innerHTML = flashHtml; 
    }
    if (amDebugging) {
       alert(debugStr);
       //alert(flashHtml);
    }
    
    return true;
}
