function PlaylistsManager()
{
	this.playlistObjetos = new ObjectoManager();
	
	this.agregarPlaylist = function(etiqueta)
	{
		var playlistObj = this.playlistObjetos.traer(etiqueta);
		
		if(null == playlistObj)
		{
			var playlistObj = new Playlist()
			playlistObj.id = etiqueta;
			
			this.playlistObjetos.agregar(etiqueta, playlistObj);
		}
		
		return playlistObj;
	}
	
	this.pintarFavoritas = false;
}

function Playlist()
{
	this.id = null;
	this.nombre = "";
	this.descripcion;
	this.contenedor;
	this.esEditable = false;
	this.primeraVez = true;
	this.play = false;
	
	this.canciones = new ObjectoManager();
	
	this.agregarCancion = function(etiqueta)
	{
		var cancionObj = this.canciones.traer(etiqueta);
		
		if(null == cancionObj)
		{
			var cancionObj = new CancionObj()
			cancionObj.id = etiqueta;			

			this.canciones.agregar(etiqueta, cancionObj);
		}
		
		return cancionObj;
	}	

	this.titulos = [];

	this.agregarTitulo = function(id)
	{
		var tituloObj = new TituloObj();
		tituloObj.id = id;
		
		this.titulos.push(tituloObj)
		
		return tituloObj;
	}
	
	this.crearTitulos = function()
	{
		var titulo = this.agregarTitulo("no");
		titulo.nombre = "";
		titulo.width = "80px";
		titulo.onclick = function()
		{
			//alert("no");	
		}
		
		var titulo = this.agregarTitulo("Nombre");
		titulo.nombre = masterConf.idiomaManager.nombre;
		titulo.width = "300px";
		titulo.onclick = function()
		{
			//alert("nombre");	
		}
		
		var titulo = this.agregarTitulo("Artista");
		titulo.nombre = masterConf.idiomaManager.artista;
		titulo.width = "200px";
		titulo.onclick = function()
		{
			//alert("Artista");	
		}
		
		var titulo = this.agregarTitulo("Album");
		titulo.nombre = masterConf.idiomaManager.album;
		titulo.width = "300px";
		titulo.onclick = function()
		{
			//alert("Album");	
		}				
	}
}

Playlist.prototype = new PlaylistBase();

function PlaylistBase()
{
	this.idSeleccionado;
	this.colorSeleccionado = "#8a9bbd";
	this.colorMouseover = "#c2d1e3";
	this.cancionesAltura = 22;
	this.ccHeight = null; 
	
	this.infoContenedor;
	this.cancionesContenedor; 
	
	this.quitarCancion = function()
	{
		
	}
	
	this.reorganizar = function()
	{
		
	}	
	
	this.crearContenedores = function(parentEl)
	{
		this.parentEl = parentEl;
		var div = document.createElement("div");
		div.style.paddingLeft = 15;
		div.style.paddingTop = 2;
		div.style.paddingBottom = 3;
		div.style.paddingRight = 4;
		div.style.cursor = "pointer";
		this.contenedor = div;
		
		var tbody = crearTabla(div);
		
		var tr = cE("tr", tbody);
		
		var td = cE("td", tr);
		td.vAlign = "top";
		
		var img = cE("img", td);
		img.src = "/../player/img/play_list6.png";
		
		var td = cE("td", tr);
		td.vAlign = "top";
		td.style.paddingLeft = 8;
		td.style.paddingTop = 3;
		
		if(this.nombre.length > 28)
		{
			this.nombre = this.nombre.substring(0, 25) + "...";	
		}
		
		var span = cE("span", td);
		span.innerHTML = this.nombre;
		span.className = "playlist_titulos";
		span.style.cursor = "pointer";
		this.span = span;
		
		this.adjuntarPlaylistOnclick();
		
		this.adjuntarPlaylistOnmouseover()
	}
	
	this.adjuntarPlaylistOnclick = function()
	{
		var thisObj = this;
		
		var onclickFx = function()
		{
			masterPlayer.playlistActual.contenedor.style.backgroundColor = "#dadadb";
			masterPlayer.playlistActual.span.style.color = "#404040";
			masterPlayer.playlistActual = thisObj;
			masterPlayer.playlistSeleccionado = thisObj.id;
			masterPlayer.playlistActual.contenedor.style.backgroundColor = thisObj.colorSeleccionado;
			masterPlayer.playlistActual.span.style.color = "white";
			
			if(thisObj.esEditable)
			{
				masterPlayer.pintandoFavoritas = true;
				
				if(thisObj.primeraVez)
				{
					masterPlayer.bajarCancionesDelPlaylist();
				}
				else
				{
					masterPlayer.playlistActual.construirListado();
				}
			}
			else
			{	
				masterPlayer.pintandoFavoritas = false;
				masterPlayer.bajarCancionesDelPlaylist();				
			}
		}
		
		eh_attachEvent("onclick", this.contenedor, onclickFx);
	};
	
	this.adjuntarPlaylistOnmouseover = function()
	{	
		var thisObj = this;
		
		var onmouseoverFx = function()
		{
			thisObj.contenedor.style.backgroundColor = thisObj.colorMouseover;
			
			var onmouseoutFx = function()
			{
				thisObj.contenedor.style.backgroundColor = "white";
				if(masterPlayer.playlistActual == thisObj)
				{				
					thisObj.contenedor.style.backgroundColor = thisObj.colorSeleccionado;
				}
				else
				{
					thisObj.contenedor.style.backgroundColor = "#dadadb";
				}
			}
			
			eh_attachEvent("onmouseout", thisObj.contenedor, onmouseoutFx);
		}
		
		eh_attachEvent("onmouseover", thisObj.contenedor, onmouseoverFx);	
	};
	
	this.construir = function()
	{
		if(masterPlayer.playlistSeleccionado == this.id)			
		{	
			masterPlayer.playlistActual = this;
			if(null == masterPlayer.playlistSonando)
			{
				masterPlayer.playlistSonando = this;
			}
			this.contenedor.style.backgroundColor = this.colorSeleccionado;
			this.span.style.color = "white";
		}
		this.parentEl.appendChild(this.contenedor);	
	}	
	
	this.construirListado = function()
	{
		this.infoContenedor.innerHTML = "";
		
		var divI = cE("div", this.infoContenedor);
		divI.style.paddingTop = 8;
		
		var div = cE("div", divI);
		div.style.paddingLeft = 6;
		div.style.paddingBottom = 6;
		div.className = "playlist_titulo";
		div.innerHTML = this.info.nombre;	
		
		this.cancionesContenedor.innerHTML = "";	
		
		this.dibujarLista();	
	}
	
	this.dibujarLista = function()
	{		
		var divT = cE("div", this.cancionesContenedor);
		divT.className = "canciones_titulo_bg";
		
		var tbody = crearTabla(divT);
		
		var tr = cE("tr", tbody);
		
		for(var i=0; i<this.titulos.length; i++)
		{
			tituloObj = this.titulos[i];
			
			var td = cE("td", tr);
			td.className = "canciones_titulo";
			td.style.paddingLeft = "6px";
			td.style.padding = "2px";
			td.style.cursor = "pointer";
			td.innerHTML = tituloObj.nombre;
			td.style.width = tituloObj.width;
			
			eh_attachEvent("onclick", td, tituloObj.onclick);
		}
		
		this.ccHeight = masterPlayer.ccHeight - 20;
		
		var divC = cE("div", this.cancionesContenedor);	
		divC.style.height = this.ccHeight;
		
		var numeroDeFilas = Math.floor(this.ccHeight/22);
		divC.style.backgroundColor = (i%2 > 0) ? "#ffffff" : "#f0f4f8";
		
		var numeroDeFilasFinal = (numeroDeFilas > this.canciones.length) ? numeroDeFilas : this.canciones.length;
		
		for(var i=0; i<numeroDeFilasFinal; i++)
		{		
			var backgroundColor = (i%2 > 0) ? "#ffffff" : "#f0f4f8";
					
			var div2 = cE("div", divC);
			div2.style.height = this.cancionesAltura;

			var cancionObj = this.canciones.traerNo(i);	

			if(null != cancionObj)
			{			
				cancionObj.indice = i+1;
				cancionObj.backgroundColor = backgroundColor;								
				cancionObj.construir(div2)				
			}
			else
			{
				div2.innerHTML = "&nbsp;";
				div2.style.backgroundColor = backgroundColor;
			}
		}		
	}	
}

function CancionObj()
{
	this.id;
	this.indice;
	this.contenedor;
	this.contenedorAgregar;
	this.contenedorAgregarColor = "#373232";
	this.colorSeleccionado = "#8a9bbd";
	this.colorMouseover = "#c2d1e3";
	this.agregada = false;
	this.nombre;
	this.artista;
	this.album;
	this.sBackgroundColor = "#8a9bbd";
	this.path = null;
	
	this.numeroC;
	this.nombreC;
	this.artistaC;
	this.albumC;
	
	this.estaSonando = false;
	
	this.construir = function(contenedor)
	{
		contenedor.style.backgroundColor = (this.estaSonando) ? this.sBackgroundColor : this.backgroundColor;
		
		this.contenedor = contenedor;
		
		var tbody = crearTabla(contenedor);
		tbody.parentNode.style.height = "100%";
		
		var tr = cE("tr", tbody);
		
		for(var j=0; j<masterPlayer.playlistActual.titulos.length; j++)
		{	
			var tituloObj = masterPlayer.playlistActual.titulos[j];
			
			var td = cE("td", tr);
			td.style.paddingLeft = "6px";
			td.style.padding = "2px";
			td.className = "canciones";
			td.style.width = tituloObj.width;	
			
			if(j==0)
			{
				this.adjuntarNo(td)
			}
			else if(j==1)
			{
				var span = cE("span", td);
				span.innerHTML = this.nombre;
				this.nombreC = span;
			}
			else if(j==2)
			{
				var span = cE("span", td);
				span.innerHTML = this.artista;
				this.artistaC = span;
			}
			else if(j==3)
			{
				var span = cE("span", td);
				span.innerHTML = this.album;
				this.albumC = span;
			}
		}		
		
		this.adjuntarEventos();
		
		if(masterPlayer.cancionSeleccionada == this.id || masterPlayer.cancionActual == this)
		{				
			masterPlayer.cancionActual.estaSonando = true;					
			masterPlayer.cancionActual.contenedor.style.backgroundColor = this.sBackgroundColor;			
			masterPlayer.cancionActual.numeroC.style.color = "#ffffff";
			masterPlayer.cancionActual.nombreC.style.color = "#ffffff";
			masterPlayer.cancionActual.artistaC.style.color = "#ffffff";
			masterPlayer.cancionActual.albumC.style.color = "#ffffff";
		}	
	}
	
	this.adjuntarNo = function(contenedor)
	{			
		var tbody = crearTabla(contenedor, "100%");
		
		var tr = cE("tr", tbody);
		
		var td = cE("td", tr);
		td.style.paddingLeft = "8px";
		
		var div = cE("div", td);
		div.className = "canciones";
		div.innerHTML = this.indice;
		this.numeroC = div;		
			
		var td = cE("td", tr);
		td.style.paddingRight = 5;
		td.style.width = "15px";
		td.align = "right"
		
		if(playlistManager.pintarFavoritas)
		{		
			var div = cE("div", td);
			div.align = "center";
			div.className = "canciones";	
			div.style.color = "#ffffff";
			
			if(!masterPlayer.pintandoFavoritas)
			{		
				if(this.agregada)
				{
					div.innerHTML = "";		
					div.style.cursor = "default";
					div.style.display = "block";
					
					var img = cE("img", div);
					img.src = "/../player/img/chulo.png";
				}
				else
				{
					div.style.display = "none";
					div.style.cursor = "pointer";		
					div.innerHTML = "+";
				}
				
				var thisObj = this;
				
				var onclick = function()
				{
					if(!thisObj.agregada)
					{
						div.innerHTML = "";		
						div.style.cursor = "default";
						div.style.display = "block";
						
						var img = cE("img", div);
						img.src = "/../player/img/chulo.png"
						
						thisObj.agregada = true;
						
						var cancionObjS = masterPlayer.favoritas.agregarCancion(thisObj.id);
						cancionObjS.nombre = thisObj.nombre;
						cancionObjS.artista = thisObj.artista;
						cancionObjS.album = thisObj.album;
						cancionObjS.path = thisObj.path;
						
						thisObj.contenedorAgregar.onmouseover = function(e)
						{
							masterPlayer.popupInfo.mensaje = "La canción ha sido agregada";
							masterPlayer.popupInfo.mostrar(e);	
						}
						
						thisObj.contenedorAgregar.onmouseout = function(e)
						{
							masterPlayer.popupInfo.esconder();	
						}
					}
				}
				
				eh_attachEvent("onclick", div, onclick);
			}
			else
			{
				var thisObj = this;
				
				div.innerHTML = "";		
				div.style.cursor = "pointer";
				div.style.display = "block";
				div.innerHTML = "-";
				
				var onclick = function()
				{
					masterPlayer.favoritas.canciones.eliminar(thisObj.id);
					
					masterPlayer.playlistActual.construirListado();
					
					masterPlayer.popupInfo.esconder();	
				}
				
				eh_attachEvent("onclick", div, onclick);
			}
			
			this.contenedorAgregar = div;
		}
	}	
	
	this.adjuntarEventos = function()
	{
		var thisObj = this;
		
		var onMouseFx = function()
		{
			thisObj.contenedor.style.backgroundColor = thisObj.colorMouseover;
			
			if(playlistManager.pintarFavoritas)
			{
				thisObj.contenedorAgregar.style.backgroundColor = thisObj.colorMouseover;
				
				if(!thisObj.agregada)
				{							
					thisObj.contenedorAgregar.style.display = "block";
					thisObj.contenedorAgregar.style.backgroundColor = thisObj.contenedorAgregarColor;
					
					thisObj.contenedorAgregar.onmouseover = function(e)
					{
						masterPlayer.popupInfo.mensaje = (masterPlayer.pintandoFavoritas) ? "Eliminar" : "Agregar a favoritas";
						masterPlayer.popupInfo.mostrar(e);	
					}
					
					thisObj.contenedorAgregar.onmouseout = function(e)
					{
						masterPlayer.popupInfo.esconder();	
					}
				}
			}
			
			var onmouseoutFx = function()
			{
				thisObj.contenedor.style.backgroundColor = (thisObj.estaSonando) ? thisObj.sBackgroundColor : thisObj.backgroundColor;
				
				if(playlistManager.pintarFavoritas)
				{
					thisObj.contenedorAgregar.style.backgroundColor = (thisObj.estaSonando) ? thisObj.sBackgroundColor : thisObj.backgroundColor;
					if(!thisObj.agregada)
					{
						thisObj.contenedorAgregar.style.display = "none";
					}
				}
			}
			
			eh_attachEvent("onmouseout", thisObj.contenedor, onmouseoutFx);
		}
		
		eh_attachEvent("onmouseover", thisObj.contenedor, onMouseFx);	
		
		var onclickFx = function()
		{
			masterPlayer.playlistSonando = masterPlayer.playlistActual;
			
			if(masterPlayer.cancionActual)
			{
				if(playlistManager.pintarFavoritas)
				{
					masterPlayer.cancionActual.contenedorAgregar.style.backgroundColor = masterPlayer.cancionActual.backgroundColor;
				}
				
				masterPlayer.cancionActual.contenedor.style.backgroundColor = masterPlayer.cancionActual.backgroundColor;
				masterPlayer.cancionActual.numeroC.style.color = "#000000";
				masterPlayer.cancionActual.nombreC.style.color = "#000000";
				masterPlayer.cancionActual.artistaC.style.color = "#000000";
				masterPlayer.cancionActual.albumC.style.color = "#000000";			
				masterPlayer.cancionActual.estaSonando = false;
			}
			
			masterPlayer.cancionActual = thisObj;			
			masterPlayer.cancionActual.estaSonando = true;			
			masterPlayer.cancionActual.contenedor.style.backgroundColor = thisObj.sBackgroundColor;			
			masterPlayer.cancionActual.numeroC.style.color = "#ffffff";
			masterPlayer.cancionActual.nombreC.style.color = "#ffffff";
			masterPlayer.cancionActual.artistaC.style.color = "#ffffff";
			masterPlayer.cancionActual.albumC.style.color = "#ffffff";
			
			var nombre = (masterPlayer.cancionActual.nombre.length > 28) ? masterPlayer.cancionActual.nombre.substring(0, 25) + ".." : masterPlayer.cancionActual.nombre;
			
			cancion = 
			{
				nombre : nombre + " - " + masterPlayer.cancionActual.artista,
				path : masterPlayer.cancionActual.path
			}
	
			flashPlayer.index = masterPlayer.cancionActual.indice - 1;			
			flashPlayer.song = cancion;
			flashPlayer.play();
			
			if(masterPlayer.objetoExt)
			{			
				if(masterPlayer.playlistActual.id == masterPlayer.objetoExt.thisPlaylistId)
				{
					var objExt = masterPlayer.objetoExt;
					
					if(objExt.thisCancion)
					{
						objExt.thisCancion.contenedorG.style.backgroundColor = objExt.thisCancion.color;	
						objExt.thisCancion.contenedor.style.color = objExt.colorNormal;	
					}
					objExt.thisCancion = objExt.canciones.traer(thisObj.id);
					objExt.thisCancion.contenedorG.style.backgroundColor = objExt.backgroundSeleccionado;
					objExt.thisCancion.contenedor.style.color = objExt.colorSeleccionado;
				}
				else
				{
					var objExt = masterPlayer.objetoExt;
					
					if(objExt.thisCancion)
					{					
						objExt.thisCancion.contenedorG.style.backgroundColor = objExt.thisCancion.color;	
						objExt.thisCancion.contenedor.style.color = objExt.colorNormal;	
					}
				}
			}
		}
		
		eh_attachEvent("onclick", thisObj.contenedor, onclickFx);
	}
}

function TituloObj()
{
	this.id;
	this.width;
	this.nombre = "";
	this.onclick = null;
}

