var playerWindow;
var windowWidth = 280;
var windowHeight = 446;	 

function playTrack(playlist_pos)
{
    // check to see if cookie has been set
    playerCookie = getCookie("player");
    // if it has then, all being well with the pinger, we should focus it 
    if(playerCookie == "true") {
        // get our focus
        if (playerWindow) {
				if(playerWindow.location.pathname == "/radio_player/player.php") {
					playerWindow.location.href = "/audio_player/player.php?pp=" + playlist_pos;
				} else {
            	playerWindow.playerFrame.location.href = "/audio_player/audio_player.php?pp=" + playlist_pos;
				}
        } else {
            // this will force the window forward. Hopefully without a reload.
	        	playerWindow = window.open("/audio_player/player.php?pp=" + playlist_pos,"player","status,width="+windowWidth+",height=" + windowHeight);
        }
    } else {
        // open window and start playing playlist at last postion or top based upon session settings;
        playerWindow = window.open("/audio_player/player.php?pp=" + playlist_pos,"player","status,width="+windowWidth+",height=" + windowHeight);
    }
    if(!playerWindow.opener) {
        playerWindow.opener = this;
    }
    playerWindow.focus();
}

function startPlaying(playlist_pos) 
{
	playerFrame.location.href = "/audio_player/audio_player.php?pp=" + playlist_pos;
}
	 	 	    
function launchPlayer(tid, aid, rid, lid)
{
    // add our track to the playlist;
    // check to see if cookie has been set
    playerCookie = getCookie("player");
    // if it has then, all being well with the pinger, we should focus it 
    if(playerCookie == "true") {
        // get our focus
        if (playerWindow) {
				if(playerWindow.location.pathname == "/radio_player/player.php") {
					playerWindow.location.href = "/audio_player/player.php?t=" + tid + "&a=" + aid + "&r=" + rid + "&l=" + lid;
				} else {
            	playerWindow.focus();
            	addTrack(tid, aid, rid, lid); 
				}
        } else {
             // this will force the window forward. Hopefully without a reload.
             playerWindow = window.open("","player","status,width="+windowWidth+",height=" + windowHeight);
             addTrack(tid, aid, rid, lid); 
        }
    } else {
        // open window and start playing playlist at last postion or top based upon session settings;
        playerWindow = window.open("/audio_player/player.php?t=" + tid + "&a=" + aid + "&r=" + rid + "&l=" + lid,"player","status,width="+windowWidth+",height=" + windowHeight);
    }
    if(!playerWindow.opener) {
        playerWindow.opener = this;
    }
}	    

function launchPlaylist(playlistid)
{
   // add our track to the playlist;
   // check to see if cookie has been set
   playerCookie = getCookie("player");
   // if it has then, all being well with the pinger, we should focus it 
   if(playerCookie == "true") {
       // get our focus
       if (playerWindow) {
				if(playerWindow.location.pathname == "/radio_player/player.php") {
					playerWindow.location.href = "/audio_player/player.php?pl=" + playlistid;
				} else {
					playerWindow.location.href = "/audio_player/player.php?pl=" + playlistid;
           		playerWindow.focus();
				}
       } else {
            // this will force the window forward. Hopefully without a reload.
            playerWindow = window.open("/audio_player/player.php?pl=" + playlistid,"player","status,width="+windowWidth+",height=" + windowHeight);
       }
   } else {
       // open window and start playing playlist at last postion or top based upon session settings;
       playerWindow = window.open("/audio_player/player.php?pl=" + playlistid,"player","status,width="+windowWidth+",height=" + windowHeight);
   }
   if(!playerWindow.opener) {
       playerWindow.opener = this;
   }	
}

function checkPlaying() {
    playing = isPlaying();
    if(!playing) {
        playerWindow.playerFrame.location.href = "/audio_player/audio_player.php?id=9999";
    }
}


function addTrack(tid, aid, rid, lid) 
{   
   if(playerWindow && playerWindow.coverImage) {
    	playerWindow.coverImage.location.href = "/audio_player/playlist.php?t=" + tid + "&a=" + aid + "&r=" + rid + "&l=" + lid;
	} else if (playerWindow) {
      playerWindow.setTimeout("addTrack(" + tid + "," + aid + "," + rid + "," + lid + ")",1000);
	} else {
		setTimeout("addTrack(" + tid + "," + aid + "," + rid + "," + lid + ")",1000);
	}
}

function pingPlayer()
{
    // if we can see a player window
    if(playerWindow) {
        // ping that window
        playerWindow.setTimeout("pingParent()",1000);
    }
}

function updatePlaying() {
    parent.playerDetail.location.href = "/audio_player/now_playing.php";
    if(parent.coverImage && parent.coverImage.location) {
        coverImageLocation = parent.coverImage.location.pathname;
    }
    if(coverImageLocation != "/audio_player/cover.php" && coverImageLocation != "/audio_player/playlist.php") {
        coverImageLocation = "/audio_player/cover.php";
    }
    parent.coverImage.location.href = coverImageLocation;
}

function showPage(pageID) {
    parent.opener.location.href = "/" + pageID;
}

function registerPlayer()
{
    // set a cookie to say that the player is open
    document.cookie = "player=true; path=/";
    playerWindow = this;
    opener.playerWindow = this;
}	    

function setPlaying(value) {
    document.cookie = "playing=" + value + "; path=/";
}

function isPlaying() {
    playing = getCookie("playing");
    if(playing == "true") {
        return true;
    } else {
        return false;
    }
}
	
function unregisterPlayer()
{
    setPlaying("false");
    document.cookie = "player=false; path=/";
}
	 
function pingParent() 
{
    // if the source window still exists
    if(opener && opener.playerWindow != this) {
        // function that tells parent window where to find this window
        opener.playerWindow = this;
        // if we've not managed to set the variable for some reason.
        if(opener.playerWindow != this) {
            // ping after another second
            playerWindow.setTimeout("pingParent()",1000);
        }
    }
}

function getCookie(cookieName)
{
    var allcookies = document.cookie;
    // Look for the start of the cookie named "version"
    var pos = allcookies.indexOf(cookieName + "=");

    // If we find a cookie by that name, extract and use its value
    if (pos != -1) {
        var start = pos + cookieName.length + 1;                       // Start of cookie value
        var end = allcookies.indexOf(";", start);  // End of cookie value
        if (end == -1) end = allcookies.length;
        var value = allcookies.substring(start, end);  // Extract the value
        value = unescape(value);                       // Decode it
        return value;
    } else {
        return "";
    }
}