// Notes:
/*
When hiding the <select> boxes depending on how well formed the HTML is, IE5.5+ may hide more than just the selects.  This is a browser render issue.  The solution is to look thru at the effected elements and apply position: relative to the highest parent (ie: table).
*/




// Detect which browser to play flash or not 

// Global vars
hideableElements = document.getElementsByTagName("");
var flashShow = 0; /********** New Var ************/
function movieHandler(){
    //loads the flash movie if capable browser detected.
    var x = this;
    x.isCompatibleBrowser = false;
    
    x.checkBrowser = function(){
        //put code in here to check if browser ok, if so set x.isCompatibleBrowser = true
        if (is_nav6up){
            //alert("Sorry, no movie for you");
            x.isCompatibleBrowser = true;
        }
        else if (is_ie5_5up || is_gecko || is_safari){
            //alert("Watch the movie: IE 5.5+, Moz, Safari");
            x.isCompatibleBrowser = true;
        }
        else
        { 
            //alert("Sorry, no movie for you");
            x.isCompatibleBrowser = false;
        }
    }
    
    x.createDiv = function() {
        dv = document.createElement('div');
        dv.setAttribute('id',"flash-movie");     
        //set the inner styling of the div tag 
        with(dv){
            style.position="absolute";       
            style.left='0px';
            style.top='120px';
            style.margin='0px auto';
            style.width='99%';
            //style.border='1px solid red';
            style.zIndex="1000";
        }
        
        //set the html content inside the div tag
        dv.innerHTML='<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="752" height="400" id="snowman" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="/eCommerceWeb/tme/flash/ghost/alb_goldCM10b_week9.swf" /><param name="quality" value="best" /><param name="wmode" value="transparent" /><param name="bgcolor" value="#ffffff" /><embed src="/eCommerceWeb/tme/flash/ghost/alb_goldCM10b_week9.swf" quality="best" wmode="transparent" bgcolor="#ffffff" width="752" height="400" name="snowman" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
        
        // Find a spot to place the div
        var main = document.getElementsByTagName("body").item(0);
        main.appendChild(dv);
    }
    
    x.init=function(){
        x.checkBrowser();
        //Run in select browsers
        if(x.isCompatibleBrowser){
            for(var i=0; i<hideableElements.length; i++){
                // Hide Form Elements
                //alert(hideableElements[i].nodeName);
                hideableElements[i].style.visibility = "hidden";
            }
           /*******************
            Begin New Code
            *******************/
            var isVisitedOnce = readCookie("movieViewedDecorate");
            
            if(isVisitedOnce == 1) {
                if(flashShow == 0) {
                    replayButton();
                    flashShow = 1;
                    //Need to make sure Form elements are displayed!
                    for(var i=0; i<hideableElements.length; i++){
                        // Show Form Elements
                        hideableElements[i].style.visibility = "";
                    }
                }else {
                    x.createDiv();
                }
            }else {
                x.createDiv();
                createCookie("movieViewedDecorate",1,30);
                flashShow = 1;
            }
            /*******************
            End New Code
            *******************/

            
            /**************
            Begin Old Code
            ***************/
            //x.createDiv();
            /*************
            End Old Code
            ***************/
            //alert('Loaded Movie and Hidden selects');
            //x.createDiv();
            //alert('Loaded Movie and Hidden selects');
        }
    }
}

//Load and place Replay Button
function replayButton() {
    dv = document.createElement('div');
    dv.setAttribute('id',"replay-button");     
    //set the inner styling of the div tag 
    with(dv){
        style.position="absolute";       
        style.left='0px';
        style.top='120px';
        style.margin='0px auto';
        style.width='99%';
        style.zIndex="999";
    }
    
    //set the html content inside the div tag
    dv.innerHTML='<div style="width: 720px;margin: 0 auto;" align="right"><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"  width="103" height="25" id="snowman" align="right"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="/eCommerceWeb/tme/flash/ghost/Button_ABS_10b_week9.swf" /><param name="quality" value="best" /><param name="wmode" value="transparent" /><param name="bgcolor" value="#ffffff" /><embed src="/eCommerceWeb/tme/flash/ghost/Button_ABS_10b_week9.swf" quality="best" wmode="transparent" bgcolor="#ffffff" width="103" height="25" name="snowman" align="right" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object></div>';
    
    // Find a spot to place the div
    //var main = document.getElementsByTagName("div").item(0);
    var main = document.getElementsByTagName("body").item(0);
    main.appendChild(dv);
}


// Remove an Element by Id
function removeById(id){
    var toRemove;
    if((toRemove=document.getElementById(id)) && toRemove.parentNode){
        toRemove.parentNode.removeChild(toRemove);
    }
}

//Close,Remove Flash Movie, Load Replay button
function closeFlash(){
    removeById('flash-movie');
    for(var i=0; i<hideableElements.length; i++){
        // Show Form Elements
        hideableElements[i].style.visibility = "";
    }
    // Place Replay Button
    replayButton();
}

// reloadFlash called from Flash movie
function reloadFlash(){
    oMovieHandler.init();
    removeById('replay-button');
}

/********************
Begin new stuff
********************/
function readCookie(name)
{
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++)
    {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function createCookie(name,value,days)
{
    if (days)
    {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}
/********************
End new stuff
********************/
// INIT
var oMovieHandler = new movieHandler();
oMovieHandler.init();
