// JavaScript Document
var currentState = "IDLE";
var popWin = null;
var muscol_player_module_loaded = true;
var module_jwplayer;


function addListenersModule() {
	if (module_jwplayer) { 
		module_jwplayer.addModelListener("STATE", "stateListener");
	} else {
		setTimeout("addListenersModule()",100);
	}
}

function addPopupListeners() {
	if (popWin.module_jwplayer) { 
		popWin.module_jwplayer.addModelListener("STATE", "stateListener");
	} else {
		setTimeout("addPopupListeners()",100);
	}
}

function stateListener(obj) { //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
	currentState = obj.newstate; 
	previousState = obj.oldstate; 

	if (new_items) {
		reload_if_possible(); 
	}
}

function deletePlayer(theWrapper, thePlayerId) { 
        //s_jwplayer.removeSWF(thePlayerId);
        var tmp=document.getElementById(theWrapper);
        if (tmp) { tmp.innerHTML = ""; }
        module_jwplayer = null;
}

function createWindow(theUrl) {  
	if (module_jwplayer) { 
		module_jwplayer.sendEvent("STOP"); 
		deletePlayer('jwplayer_wrapper', 'module_jwplayer');
	}

	popWin = window.open(theUrl, "popup_window_jwplayer", "width="+popup_width+",height="+popup_height+",left=0,top=0,resizable=yes");
	addPopupListeners();
	save_popup_status(1);
	show_restore_button();
	popWin.focus();
}

function restore_player(){
	close_popup_player();
	current_playlist_url = current_encoded_playlist_url;
	initPlayer();
	current_playlist_url = not_encoded_playlist_url;
	show_popup_button();
}

function show_popup_button(){
	document.getElementById("show_popup_button").style.display = "inline" ;
	document.getElementById("restore_player_button").style.display = "none" ;
}
function show_restore_button(){
	document.getElementById("show_popup_button").style.display = "none" ;
	document.getElementById("restore_player_button").style.display = "inline" ;
}

function initialize_popup_variables(theUrl){
	deletePlayer('jwplayer_wrapper', 'module_jwplayer');
	popWin = window.open(theUrl, "popup_window_jwplayer", "width="+popup_width+",height="+popup_height+",left=0,top=0,resizable=yes");
	addPopupListeners();
	show_restore_button();
}

function close_popup_player(){
	if(popWin) popWin.close();
	else{
		popWin = window.open("/", "popup_window_jwplayer");
		popWin.close();
	}
	save_popup_status(0);
}

function save_popup_status(status){
	save_popup_status_on_session(status);
	if(status) popup_active = true;
	else popup_active = false;
}

function save_popup_status_on_session(popup_active){
	var xmlHttp;
	try
	  {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	  }
	catch (e)
	  {
	  // Internet Explorer
	  try
		{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
	  catch (e)
		{
		try
		  {
		  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		catch (e)
		  {
		  alert("Your browser does not support AJAX!");
		  return false;
		  }
		}
	  }
	  xmlHttp.onreadystatechange=function()
		{
		if(xmlHttp.readyState==4)
		  {
	
		
		  }
		}
	  //xmlHttp.open("GET","puntua.php?points="+points+"&album_id="+album_id,true);
	  xmlHttp.open("GET","index2.php?option=com_muscol&task=save_popup_status_on_session&popup_active=" + popup_active,true);
	  xmlHttp.send(null);
}

