/**********************************************************************************/
/* 07/10/2010 NeoMobile Group - Hilari Peña - JSWMAPlayer over WMP Plugin and YUI */
/**********************************************************************************/

var wmpplaylist = Array();
var wmpplaylistpos = 0;
// size of the text for the playlist
var wmpplaylist_title_length = 11;
var wmpplaylist_album_length = 9;
var wmpcoverX = 85;
var wmpcoverY = 85;
var wmpsongPos = 0;
var Dom   = YAHOO.util.Dom;
var Event = YAHOO.util.Event;
var lang  = YAHOO.lang;
var tcentinela = null;
var tcentinelaspeed = 200;

function WMPCentinela()
{
	if (document.Player.playState == 1) 
		{
		//alert('Next Song!');
		WMPNextSong();
		}
	tcentinela = setTimeout("WMPCentinela()",tcentinelaspeed);
}

function WMPGoToSound(id_song)
{
	WMPStopPlayer();
	var pos = WMPPosSong(id_song);
	wmpplaylistpos = pos;
	WMPPlaySong();
}

function WMPPosSong(id_song)
{
	var i = wmpplaylist.length;
	var pos = 0;
	var x = 0;
	for (x=0;x<i;x++)
	{
		if(id_song == wmpplaylist[x][0]) pos = x;
	}
	return pos;
}

function WMPAddSong(id_song, title, album, urlcover, urlmp3)
{
// Nomes deixem afegir cançons quan la plana est`a carregada
if (planacarregada)
	{
	var i = wmpplaylist.length;
	// Search for repeated song
	var songid = id_song;
	if(WMPSameSong(songid))
		{
		//Same Song!
		//alert("¡La canción ya esta en la playlist!");
		}
	else
		{
		// Add trak
		wmpplaylist[i] = new Array(id_song, title, album, urlcover, urlmp3);
		// Afegim al playlist intern del WMP (no ho fem servir pq requerix confitmacio acces usuari)
		//var wmpmusic = urlmp3;
		//var mediaItem = Player.newMedia(wmpmusic);
		//Player.currentPlayList.appendItem(mediaItem);
		// Recarregem la visual del playlist
		WMPReloadDomPlaylist();
		// First Song on the playlist? Runs the player
		if (wmpplaylist.length == 1) 
			{
			WMPStartPlayer();
			}
		if (document.Player.playState == 1)
			{
			WMPGoToSound(id_song);
			}
		}
	}
}

function WMPAddSongMultiple(id_song, title, album, urlcover, urlmp3)
{
// Nomes deixem afegir cançons quan la plana est`a carregada
if (planacarregada)
	{
	var i = wmpplaylist.length;
	// Search for repeated song
	var songid = id_song;
	if(WMPSameSong(songid))
		{
		//Same Song!
		//alert("¡La canción ya esta en la playlist!");
		}
	else
		{
		// Add trak
		wmpplaylist[i] = new Array(id_song, title, album, urlcover, urlmp3);
		// Afegim al playlist intern del WMP (no ho fem servir pq requerix confitmacio acces usuari)
		//var wmpmusic = urlmp3;
		//var mediaItem = Player.newMedia(wmpmusic);
		//Player.currentPlayList.appendItem(mediaItem);
		// Recarregem la visual del playlist
		//WMPReloadDomPlaylist();
		// First Song on the playlist? Runs the player
		/*
		if (wmpplaylist.length == 1) 
			{
			WMPStartPlayer();
			}
		if (document.Player.playState == 1)
			{
			WMPGoToSound(id_song);
			}
		*/
		}
	}
}



// Start the Player
function WMPStartPlayer()
{
	var i = wmpplaylist.length;
	wmpplaylistpos = 0;
	if (i > 0) WMPPlaySong();
}

// Play songs with the flash player
function WMPPlaySong()
{
	if (planacarregada)
		{
		n = wmpplaylistpos;
		if (n == wmpplaylist.length) {n = 0; wmpplaylistpos = 0;}
		if (wmpplaylist.length > 0 && wmpplaylist[n][4] != null)
			{
			//alert(wmpplaylist[n][4]);
			document.Player.URL = wmpplaylist[n][4];
			if (tcentinela != null) clearTimeout(tcentinela);
			WMPCentinela();
			WMPMarcaPistaActual();
			var namesong = ""+wmpplaylist[n][1]+" - "+wmpplaylist[n][2]+"";
			var zonenm = Dom.get('plysongname');
			zonenm.innerHTML = namesong;
			document.forms.fply.iply.value = namesong;
			// Encenem el ticker del titol de la canço
			if (parent.tmot != null) clearTimeout(parent.tmot);
			PlayerTicker();
			}
		}
}

function WMPSameSong(songid)
{
	var i = wmpplaylist.length;
	var songdtmp = "";
	var same = 0;
	for (x=0;x<i;x++)
		{
		songidtmp = wmpplaylist[x][0];
		if (songid == songidtmp) same++;
		}
	if (same == 0) return false;
	else return true;
}

function WMPMarcaPistaActual()
{
	var i = wmpplaylist.length;
	var x = 0;
	for (x=0;x<i;x++)
	{
		var pistaactual = Dom.get('music'+wmpplaylist[x][0]);
		if (x == wmpplaylistpos)
			{
			YAHOO.util.Dom.setStyle(pistaactual, "background-color", "#B5FFC8");
			}
		else 
			{
			YAHOO.util.Dom.setStyle(pistaactual, "background-color", "#f0f0f0");
			}
	}
}

function WMPReloadDomPlaylist()
{
	var zonepl = Dom.get('plylsongs');
	var html = "";
	var i = wmpplaylist.length;
	var stitle = '';
	var salbum = '';
	html = "";
	html += "<div class=\"head\"><img src=\"images/txt_a-seguir.gif\" alt=\"A seguir\" /></div>\n";
	for (x=0;x<i;x++)
	{
		var sufix = '';
		stitle = wmpplaylist[x][1];
		if (stitle.length > wmpplaylist_title_length) sufix = '...';
		else sufix = '';
		stitle = stitle.substr(0, wmpplaylist_title_length) + sufix;
		salbum = wmpplaylist[x][2];
		if (salbum.length > wmpplaylist_album_length) sufix = '...';
		else sufix = '';
		salbum = salbum.substr(0, wmpplaylist_album_length) + sufix;
		html += "<div class=\"playListArea\" id=\"music"+wmpplaylist[x][0]+"\">\n";
		html += "<ul>\n";
		html += "<li class=\"nameMusic\"><a href=\"javascript:WMPGoToSound("+wmpplaylist[x][0]+");\">"+stitle+" <span>- "+salbum+"</span></a></li>\n";
		html += "<li><a href=\"javascript:DownFromPl("+wmpplaylist[x][0]+");\"><img src=\"images/icon_download.gif\" width=\"16\" height=\"16\" id=\"lipd"+wmpplaylist[x][0]+"\" alt=\"download\" /></a></li>\n";
		html += "<li><a href=\"javascript:PlayerSMSPush("+wmpplaylist[x][0]+")\"><img src=\"images/icon_download-mobile.gif\" width=\"16\" height=\"16\" alt=\"mobile\" id=\"lipe"+wmpplaylist[x][0]+"\" /></a></li>\n";
		html += "<li><a href=\"javascript:PutOnPlayList("+wmpplaylist[x][0]+");\"><img src=\"images/icon_put-on-playlist.gif\" width=\"16\" height=\"17\" alt=\"putpl\" id=\"lipa"+wmpplaylist[x][0]+"\" /></a></li>\n";
		html += "<li><a href=\"javascript:WMPRemoveSong("+wmpplaylist[x][0]+");\"><img src=\"images/icon_delete-music.gif\" width=\"16\" height=\"16\" alt=\"apagar\"  /></a></li>\n";
		html += "</ul>\n";
		html += "<div class=\"clear\"></div>\n";
		html += "</div>\n";
	}
	if (i>0) html += "<DIV class=\"apagartodas\" id=\"apagartodas\"><img src=\"./images/blank.gif\" height=\"2\" width=\"2\"><BR><a href=\"javascript:WMPRemoveAllSongs();\" class=\"apagartodaslink\">Apagar todas</a><BR><img src=\"./images/blank.gif\" height=\"5\" width=\"5\"></DIV>";
	zonepl.innerHTML = html;
	WMPMarcaPistaActual();
	WMPTooltipsPlayer();
}

function WMPTooltipsPlayer()
{
	var i = wmpplaylist.length;
	var z = 0;
	var el1 = '';
	var el2 = '';
	var el3 = '';
	var url = '';
	for (z=0;z<i;z++)
	{
	//url = wmpplaylist[z][4];
	el1 = 'lipd'+wmpplaylist[z][0]+'';
	el2 = 'lipe'+wmpplaylist[z][0]+'';
	el3 = 'lipa'+wmpplaylist[z][0]+'';
	AddTooltip(el1,'Download','');
	AddTooltip(el2,'Enviar para telemóvel','');
	AddTooltip(el3,'Adiccionar à playlist','');
	//alert(el1 + ' ' + el2 + ' ' + el3);
	//alert(url);
	}
	GeneraTooltips('');
}

function WMPRemoveAllSongs()
{
	var i = wmpplaylist.length;
	var id_song = 0;
	for (x=0;x<i;x++)
		{
		var id_song = wmpplaylist[x][0];
		var pistaactual = Dom.get('music'+id_song);
		pistaactual.innerHTML = '';
		}
	WMPStopPlayer();
	if (parent.tmot != null) clearTimeout(parent.tmot);
	var zonenm = Dom.get('plysongname');
	zonenm.innerHTML = '';
	var zoneat = Dom.get('apagartodas');
	zoneat.innerHTML = '';
	wmpplaylist = new Array();
}

// Remove Song for the playlist matrix
function WMPRemoveSong(id_song)
{
	var i = wmpplaylist.length;
	var pos = WMPPosSong(id_song);
	//alert ('l: '+i+' p: '+wmpplaylistpos);
	var flag = 0;
	if (wmpplaylist[wmpplaylistpos][0] == id_song)
		{
		if (i >= 1)
			{ 
			flag = 1; 
			}
		else flag = 2;
		}
	if (i > 0)
		{
		var tmpplaylist = new Array();
		var z = 0;
		for (x=0;x<i;x++)
			{
			if (wmpplaylist[x][0] != id_song) 
				{
				tmpplaylist[z] = wmpplaylist[x];
				z++;
				}
			}
		wmpplaylist = new Array();
		wmpplaylist = tmpplaylist;
		//WMPReloadDomPlaylist();
		var pistaactual = Dom.get('music'+id_song);
		pistaactual.innerHTML = '';
		if (pos < wmpplaylistpos) wmpplaylistpos--;
		if (wmpplaylistpos < 0) wmpplaylistpos = 0;
		}
	if (flag == 1) 
		{
		WMPPlaySong();
		}
	if (flag == 2 || wmpplaylist.length == 0) 
		{
		WMPStopPlayer();
		if (parent.tmot != null) clearTimeout(parent.tmot);
		wmpplaylist = new Array();
		var zonenm = Dom.get('plysongname');
		zonenm.innerHTML = '';
		// Aqui s'ha d'esborrar la canço del player
		}
}

function WMPNextSong()
{
	var i = wmpplaylist.length;
	var n = wmpplaylistpos + 1;
	//alert ('i:' +i+'n:'+n);
	if (i > n)
		{
		wmpplaylistpos = n;
		WMPPlaySong();
		}
	else
	{
	//End of playlist
	if (i > 0)
		{
		//alert(tcentinela);
		//WMPPlaySong();
		if (tcentinela != null) clearTimeout(tcentinela);
		WMPStopPlayer();
		//wmpplaylistpos = 0;
		}
	}
}

function WMPBackSong()
{
	var i = wmpplaylist.length;
	if (i > 0)
		{
		var n =  wmpplaylistpos - 1;
		if (n < 0)
			{
			wmpplaylistpos = (i-1);
			WMPPlaySong();
			}
		else
		{
		wmpplaylistpos = n;
		WMPPlaySong();
		}
	}
}

function WMPStopPlayer()
{
	document.Player.controls.stop();
}

function WMPPlayPause()
{
	//alert(x);
	var p = Dom.get('playpause');
	if (document.Player.playState == 3)
		{
		document.Player.controls.pause();
		p.innerHTML = '<a href=\"javascript:WMPPlayPause();\"><img src=\"images/icon_pause.gif\" width=\"16\" height=\"16\" alt=\"pause\" /></a>';
		}
	else
		{
		if (document.Player.playState == 2)
			{
			p.innerHTML = '<a href=\"javascript:WMPPlayPause();\"><img src=\"images/icon_play-black.gif\" width=\"16\" height=\"16\" alt=\"play\" /></a>';
			document.Player.controls.play();
			}
		}
	if (document.Player.playState == 1)
		{
		WMPStartPlayer();
		}
}


function WMPAddAllPl()
{
	var i = wmpplaylist.length;
	if (parent.usuarilogat)
		{
		if (i > 0)
			{
			// Agafem les pistes del playlist
			var pistas = '';
			var z = 0;
			for (z=0;z<i;z++)
				{
				pistas = pistas + wmpplaylist[z][0] + ",";
				}
			//alert(pistas);
			// Obrim l'aplicacio
			window.open('pista2playlist.php?pistas='+pistas,'popl','width=430, height=250, left=460, top=500, resizable=0, scrollbars=yes');
			}
		else
			{
			alert('Adicione música ao leitor.');
			}
		}
	else 
		{
		down_alta('');
		}
}
