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);
	}



//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;
document.location.href = redirect;

}

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 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 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', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=510,height=200,left = 150,top = 100');
	
}
function open_player(URL) {
    //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', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=510,height=550,left = 150,top = 100');
			}
		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.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', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=510,height=550,left = 150,top = 100');
		//playerWindow.document.getElementById('playlist').src='/view/playlist';
		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;
	


}

