google.load("swfobject", "2.1"); 

var volLevel = 0 ;
var windWidth = 600;
var windHeight = 400;
var cntr = 0;
var playListCount = 0;
var playerSizeCurrent = 2;
var playListEntries = new Array();

    function LoadIt()
    {
        
        var cookieValue = readCookie("playerState");
        if(cookieValue != null)
        {
            cntr = cookieValue;
            cntr = cntr * 1;
        }
       
        cookieValue = readCookie("playerWidth");
        if(cookieValue != null)
        {
            playerWidth = cookieValue;
        }
        
        cookieValue = readCookie("playerHeigth");
        if(cookieValue != null)
        {
            playerHeigth = cookieValue;
        }
        
        cookieValue = readCookie("playerSizeCurrent");
        if(cookieValue != null)
        {
            playerSizeCurrent = cookieValue;
        }
        
//        if(cntr > playListCount + 1)
//            cntr = 0;

        var i = 0;
        var list = getPlayList(playlistFile);
        var rowsArry = new Array();
        rowsArry = list.split("\r");
        for( i == 0; i < rowsArry.length; i++)
        {
            insertPlayListEntry(rowsArry[i].toString(), i);
        }
        playListCount = playListEntries.length;

        var query = window.location.search.substring(1);
        if(query != null && query.length > 0)
        {
            query = query * 1;
            
            if(query >= 0 & query < playListCount)
            {
                cntr  = query;
                createCookie("playerState", cntr.toString(), 1);
            }
            window.location = window.location.pathname;
        }
        
        
        
    }
    
    function insertPlayListEntry(arrRowData, iIndex)
    {
        var rowCount = playListEntries.length ;
        var columns = new Array();
        columns = arrRowData.split("|");
        
        playListEntries[iIndex] = new Array();
        
        playListEntries[iIndex][0] = columns[0];
        playListEntries[iIndex][1] = columns[1];
        playListEntries[iIndex][2] = columns[2];  // Volume level
        
    }

    function getPlayList(url) 
    { 
        var playList;
        var connection = new ActiveXObject("Microsoft.XMLHTTP"); 
        try 
        { 
            connection.open("GET", url, false); 
            connection.send(); 
            if(connection.readyState == 4) playList = connection.responseText; 
        } 
        catch(e) 
        { 
            alert("ERROR: The remote host could not be found or the connection was refused."); 
            return ""; 
        } 
        return playList;
    }

    function onError(errCode)
    {
        
        alert("Error: " ); // + errCode
    }

    function playerUnstuck()
    {
        if(ytplayer.getPlayerState() == 2)
        {
           ytplayer.playVideo();
        }
    }
        
    function onytplayerStateChange(newState)
    {
    
        if(newState == 0 )
        {
            ytplayer.clearVideo();
            cntr = cntr + 1;
            if(cntr > playListCount - 1)
            {
                cntr = 0;
            }          
            
            ytplayer.loadVideoById(playListEntries[cntr][0], 0, "default");
            
            volLevel = playListEntries[cntr][2];
            if(volLevel > 0 && volLevel <= 100) 
                ytplayer.setVolume(volLevel);
                
            //ytplayer.playVideo();
            createCookie("playerState", cntr.toString(), 1);
            document.location = document.location;
        }
        // This is to make sure the first song volume gets adjusted as well.
        if(newState == 5)
        {
            volLevel = playListEntries[cntr][2];
            if(volLevel > 0 && volLevel <= 100) 
                ytplayer.setVolume(volLevel);
        }
        
//        updateStatusText("State: " + newState.toString() + " |Playing: " + cntr + " of " +
//            playListCount + "  Video ID: " + playListEntries[cntr][0] + "| Vol: " + volLevel);
            
        updateStatusText(playListEntries[cntr][1] + " (" + cntr + " / " + playListCount + ")");
            
        createCookie("playerState", cntr.toString(), 1);

    }
    
    function onYouTubePlayerReady(id) 
    {   
        ytplayer = document.getElementById("myytplayer");
        ytplayer.addEventListener("onStateChange", "onytplayerStateChange");

        volLevel = playListEntries[cntr][2];
        if(volLevel > 0 && volLevel <= 100) 
            ytplayer.setVolume(volLevel);
    }
    
    function updateStatusText(strTxt)
    {
        var x = document.getElementById("output");
        if(x != null)
            x.value = strTxt;
    }
    
    
    function goNext()
    {
        ytplayer.stopVideo();
        ytplayer.setVolume(10);
        setTimeout('ContinueAfterPause()',1000);
    }
    

    function ContinueAfterPause()
    {
        onytplayerStateChange(0);
    }
    
    function goBack()
    {
        // ytplayer.pauseVideo();
        ytplayer.stopVideo();
        ytplayer.setVolume(10);

        if(cntr > 1)
        {   
            cntr = cntr -2;
        }
        else if(cntr == 1)
        {
            cntr = -1;
        }
        else if(cntr == 0)
        {
            cntr = playListCount -2;
        }
        setTimeout('ContinueAfterPause()',1000);
    }
    
    function larger()
    {
        if(playerSizeCurrent < 5)
            playerSizeCurrent++;
            
        setSizes(playerSizeCurrent);   
    }
    
    function smaller()
    {
        if(playerSizeCurrent > 1)
            playerSizeCurrent--;
            
        setSizes(playerSizeCurrent);   
    }
    
    
    function setSizes(newSize)
    {
        createCookie("playerSizeCurrent", newSize.toString(), 1);
        switch(newSize)
        {
            case 1:
                playerHeigth = 260;
                playerWidth = 360;
                
                break;
            case 2:
                playerHeigth = 360;
                playerWidth = 460;
                
                break;
            case 3:
                playerHeigth = 560;
                playerWidth = 700;
                
                break;
            case 4:
                playerHeigth = 660;
                playerWidth = 900;
                
                break;
            case 5:
                playerHeigth = 1200;
                playerWidth = 1600;
                
                break;
        }
        
        createCookie("playerWidth", playerWidth.toString(), 1);
        createCookie("playerHeigth", playerHeigth.toString(), 1);
        document.location = document.location;        
    }
    
    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=/";
    }

    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 eraseCookie(name) {
	    createCookie(name,"",-1);
    }
        
    function EmbitPlayer()
    {   
        swfobject.embedSWF("http://www.youtube.com/v/" + playListEntries[cntr][0] + "?hl=en_US&autoplay=1&enablejsapi=1&playerapiid=ytplayer&fs=1",  "ytapiplayer", playerWidth, playerHeigth, "10", null, null, params, atts);
    }


