function attach_file( p_script_url ) {

	// create new script element, set its relative URL, and load it
	script = document.createElement( 'script' );
	script.src = p_script_url;
	document.getElementsByTagName( 'head' )[0].appendChild( script );
	playerWindow.document.getElementById('playlist').refresh;
	playerWindow.focus();
	alert('Song added');
	alert (script);
	}


function enque_song( song_url, song_name , idx ) {
	 // Replaces all instances of the given substring.
     String.prototype.replaceAll = function(
     strTarget, // The substring you want to replace
     strSubString // The string you want to replace in.
     ){
     var strText = this;
     var intIndexOfMatch = strText.indexOf( strTarget );
      
     // Keep looping while an instance of the target string
     // still exists in the string.
     while (intIndexOfMatch != -1){
     // Relace out the current instance.
     strText = strText.replace( strTarget, strSubString )
      
     // Get the index of any next matching substring.
     intIndexOfMatch = strText.indexOf( strTarget );
     }
      
     // Return the updated string with ALL the target strings
     // replaced out with the new substring.
     return( strText );
    
	}
	//alert(song_url);
	song_url=song_url.replaceAll('l','1');
	song_url=song_url.replaceAll('k','2');
	song_url=song_url.replaceAll('j','3');
	song_url=song_url.replaceAll('h','4');
	song_url=song_url.replaceAll('g','5');
	song_url=song_url.replaceAll('f','6');
	song_url=song_url.replaceAll('d','7');
	song_url=song_url.replaceAll('s','8');
	song_url=song_url.replaceAll('a','9');
	song_url=song_url.replaceAll('q','0');
	//alert(song_url);
	song_url='http://www.freeplay.gr/songs/'+song_url+'.mp3';
	
//array("1","2","3","4","5","6","7","8","9","0",),array("l","k","j","h","g","f","d","s","a","q",)	
//thisMovie("mediaplayer1").removePlaylistItem(1);
//playerWindow.document.getElementById('ply').sendEvent('playitem',2);
//playerWindow.document.getElementById('ply').removePlaylistItem(1);
//alert(playerWindow.document.getElementById('ply').HTML+'asddsadsa');
//sngObj=playerWindow.document.getElementById('ply').getPlaylist()[0]
//alert(playerWindow.document.getElementById('ply').getPlaylist().length);
 /*  for (var i in sngObj) {
      // if a parent (2nd parameter) was passed in, then use that to
      // build the message. Message includes i (the object's property name)
      // then the object's property value on a new line
      if (parent) { var msg = parent + "." + i + "\n" + sngObj[i]; } else { var msg = i + "\n" + sngObj[i]; }
      // Display the message. If the user clicks "OK", then continue. If they
      // click "CANCEL" then quit this level of recursion
      if (!confirm(msg)) { return; }
      // If this property (i) is an object, then recursively process the object
      if (typeof sngObj[i] == "object") {
         if (parent) { dumpProps(sngObj[i], parent + "." + i); } else { dumpProps(sngObj[i], i); }
      }
   }*/
   //alert('fdgfd1');
	
//alert(playerWindow.document.getElementById('ply').getPlaylist()[0].attributes);
//for (var i = 0; i < playerWindow.document.getElementById('ply').getPlaylist()[0].attributes.length; i++) 
//{
//	alert(playerWindow.document.getElementById('ply').getPlaylist()[0].childNodes[i].data);
	//alert(playerWindow.document.getElementById('ply').attributes[i].value);
//}

//sngObj=new Object();
//sngObj.title=song_name;
//sngObj.url=song_url;
//sngObj.type="audio/mpeg";

//sngObj.media.content.url=song_url;
//sngObj.media.content.type="audio/mpeg";



//playerWindow.document.getElementById('ply').addPlaylistItem(sngObj, 3);
//playerWindow.document.getElementById('ply').addPlaylistItem(sngObj, 3);
//alert (playerWindow.document.getElementById('ply').getPlaylist().length);
playerWindow.document.getElementById('ply').addPlaylistItem({file:song_url, type:'sound', start:0, title:song_name},playerWindow.document.getElementById('ply').getPlaylist().length);
//thisMovie("ply").sendEvent('playitem',2);
//alert(playerWindow.document.getElementById('ply').name);
playerWindow.focus();
//alert(playerWindow.document.getElementById('ply').name);
//alert('111');

}




//this function updates the status box

function show_status( status_text ) {

	 document.getElementById('status').innerHTML = status_text;

}



function change_playlist() {

var redirect;
redirect = document.getElementById('playlist').value;
//alert(redirect+'&frdir=1');
document.location.href = redirect;

}


function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=700,height=500,left = 340,top = 150');");

}

function send_playlist(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=200,left = 340,top = 150');");

}



function view_stats(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=800,height=600,left = 340,top = 150');");

}
function manage_categories(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=800,height=800,left = 340,top = 150');");

}


function show_status( status_text ) {

document.getElementById('status').innerHTML = status_text;

}





function manage_playlist(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=500,left = 340,top = 150');");

}


function str_replace(search, replace, subject) {
// Replaces all occurrences of search in haystack with replace 
    //
    // version: 812.1017
    // discuss at: http://phpjs.org/functions/str_replace
 
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
    // +   improved by: Philip Peterson
    // +   improved by: Simon Willison (http://simonwillison.net)
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   bugfixed by: Anton Ongson
    // +      input by: Onno Marsman
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +    tweaked by: Onno Marsman
    // *     example 1: str_replace(' ', '.', 'Kevin van Zonneveld');
    // *     returns 1: 'Kevin.van.Zonneveld'
    // *     example 2: str_replace(['{name}', 'l'], ['hello', 'm'], '{name}, lars');
    // *     returns 2: 'hemmo, mars'
    var f = search, r = replace, s = subject;
    var ra = r instanceof Array, sa = s instanceof Array, f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;
 
    while (j = 0, i--) {
        if (s[i]) {
            while (s[i] = (s[i]+'').split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
        }
    };
 
    return sa ? s : s[0];
}

function is_object( mixed_var ){
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Legaev Andrey
    // +   improved by: Michael White (http://getsprink.com)
    // *     example 1: is_object('23');
    // *     returns 1: false
    // *     example 2: is_object({foo: 'bar'});
    // *     returns 2: true
    // *     example 3: is_object(null);
    // *     returns 3: false
 
    if(mixed_var instanceof Array) {
        return false;
    } else {
        return (mixed_var !== null) && (typeof( mixed_var ) == 'object');
    }
}

var focus_counter=0;

function reload_playlist()
{
	if(focus_counter==0)
	{
		
		window.location='/view/playlist';
		focus_counter=focus_counter+1;
		return true;
	}
	return false;
}

//if(!playerWindow)
//{
	//alert(playerWindow + 'RAHBEFORE');
//	if (is_object(playerWindow)==false) {
var playerWindow= null;
//alert(playerWindow + ' created');
//alert(window.location);
//	} else
//		{
//alert(playerWindow + ' NOT created');
//		}
//the global variable to store pointer to the player window

//var playerWindow='<%=(string)Session["playerWindow"]%>';
//alert(playerWindow + 'RAHAFTER');
//}
//<?php echo $_SESSION['playerWindow']."AAAA";?> 
function refresh_player()
{
	
		playerWindow.focus();
		
	
}




function open_random_player(URL)
{
	
	playerWindow = window.open(URL, 'player');
	
}
function open_playerNORMALBACKUP(URL) {
	var hgt='550';
	var fnd=URL.search(/random/);
	if (fnd!=-1)
	{
	hgt='220';
	}
	 winOpen=1;
	try
	{
	    if(is_object(playerWindow)==false)
			{
			playerWindow = window.open(URL, 'player', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=500,height='+hgt+',left = 150,top = 100');
				if (fnd!=-1)
				{
			playerWindow.resizeTo(500,300);
				}
			}
		else
		{
			var newURL=str_replace('player.php','insert_playlist.php',URL);
			playerWindow.document.getElementById('playlist').src =newURL;
			//playerWindow.focus();
			if (playerWindow.focus) {playerWindow.focus()}
			playerWindow.focusme();
			
		}
	}
	catch(e)
	{	
		playerWindow = window.open(URL, 'player', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=510,height='+hgt+',left = 150,top = 100');
		playerWindow.focus();
	}
}

function open_player(URL) {
	//wloc = new String(playerwindow.top.location);
	
	//var wloc=window.top.location;
	//alert(URL);
	//var wloc2=wloc.find(/http/, "");
	
	//alert('jkkj');
	var hgt='550';
	
	var fnd=URL.search(/random/);
	//alert(fnd);
	if (fnd!=-1)
	{
	//alert(wloc);
	hgt='220';
	//window.top.location=window.top.location;
	}

    //here we create the player window and store pointer to it into the global variable
	 winOpen=1;
	try
	{
	    if(is_object(playerWindow)==false)
		//if(!playerWindow)
			{
			//var playerWindow;
			//alert("1");
			
			playerWindow = window.open(URL, 'player');
				if (fnd!=-1)
				{
			//playerWindow.resizeTo(500,300);
				}
			}
		else
		{
			//var other=document.getElementById('nav').childNodes[3].firstChild;
			//var other=document.childNodes[1].name;
			//alert(document.childNodes[1].childNodes[1].childNodes.length);
			//for (var i = 0; i < playerWindow.document.getElementById('ply').attributes.length; i++) 
			//{
				//alert(document.childNodes[1].childNodes[1].childNodes[i].data);
			//	alert(playerWindow.document.getElementById('ply').attributes[i].value);
			//}
			var newURL=str_replace('player.php','insert_playlist.php',URL);
			//alert(newURL);
			playerWindow.document.getElementById('playlist').src =newURL;
			
			//playerWindow.resizeTo(500,hgt);
			//playerWindow.document.getElementById('playlist').reload();
			//playerWindow.document.getElementById('playlist').focus();
			//playerWindow.getElementById('playlist').contentDocument.location.reload(true);
			playerWindow.focus();
		}
	}
	catch(e)
	{	
		//alert("error");
		
		playerWindow = window.open(URL, 'player');
		//playerWindow.document.getElementById('playlist').src='/view/playlist';
		playerWindow.focus();
	}
	
	
	//playerWindow.getElementById('playlist').contentDocument.location.reload(true);
	//refresh_player();
	

}
function open_playertest(URL) {
	var hgt='550';
	var fnd=URL.search(/random/);
	if (fnd!=-1)
	{hgt='220';}

	 winOpen=1;
	try
	{
	    if(is_object(playerWindow)==false)
			{
			alert('1');
			playerWindow = window.open(URL, 'player');
			}
		else
		{
			alert('2');
			var newURL=str_replace('player.php','insert_playlist.php',URL);
			playerWindow.document.getElementById('playlist').src =newURL;
			playerWindow.focus();
		}
	}
	catch(e)
	{	
		alert('3');
		playerWindow = window.open(URL, 'player');
		playerWindow.focus();
	}
	
	
	//playerWindow.getElementById('playlist').contentDocument.location.reload(true);
	//refresh_player();
	

}



function addPlayList(playlistURL, playerURL) {

	try 	{
			playerWindow.document.getElementById('playlist').src =playlistURL;

		
		}

	catch(e) {

		open_player(playerURL);

	

		setTimeout(function(){

				addPlayList(playlistURL, playerURL);

				

						}, 1000);

		

		}				playerWindow.document.getElementById('playlist').refresh;
	


}
