function CategoriaTab()
{
	this.id = null;
	this.nombre = "";
	this.ordenarPor = null;
	this.ordenDireccion = null;
	this.onclick = null;
};

function CategoriasTab()
{
	this.contenedor = null;
	this.categoriasParent;
	this.idSeleccionado = null;
	this.nombreSeleccionado = null;
	this.indice = 0;
	
	this.categorias = new ObjectoManager();
	
	this.agregarTab= function(etiqueta)
	{
		var categoria = this.categorias.traer(etiqueta);
		
		if(null == categoria)
		{
			var categoria = new CategoriaTab()
			categoria.id = etiqueta;
			
			this.categorias.agregar(etiqueta, categoria);
		}
		
		return categoria;
	}
	
	this.obtenerIndice = function()
	{
		if(!this.idSeleccionado)
		{
			var tab = this.categorias.traerNo(0);
			
			this.idSeleccionado =  tab.id;
			this.nombreSeleccionado = tab.nombre
			
			return 0;
		};
		
		for(var i=0; i<this.categorias.length; i++)
		{
			var tab = this.categorias.traerNo(i);
			
			if(this.idSeleccionado == tab.id)
			{
				this.nombreSeleccionado = tab.nombre;
				var indice = i - 1;
				
				return indice;
			};
		};
		
		return 0;
	};
	
	this.construir = function()
	{	
		var thisObj = this;
		this.contenedor.innerHTML = "";
		
		var tbody = crearTabla(this.contenedor);
		tbody.parentNode.style.height = "100%";
		
		var tr = cE("tr", tbody);
		
		var td = cE("td", tr);
		
		var img = cE("img", td);
		img.style.cursor = "pointer";
		img.src = "/../player/img/anterior_categoria.png";
		
		var onclickFx = function()
		{			
			thisObj.indice--;
			thisObj.dibujarCategorias();
		};
		
		eh_attachEvent("onclick", td, onclickFx)
		
		this.indice = this.obtenerIndice();
		
		var td = cE("td", tr);
		td.style.paddingLeft = 6;
		td.style.paddingRight = 6;
		td.style.width = 216;
		
		this.categoriasParent = td;
		
		this.dibujarCategorias();
		
		var td = cE("td", tr);
		
		var img = cE("img", td);
		img.style.cursor = "pointer";
		img.src = "/../player/img/siguiente_categoria.png";
		
		var onclickFx = function()
		{
			thisObj.indice++;
			thisObj.dibujarCategorias();
		};
		
		eh_attachEvent("onclick", td, onclickFx);
	}

	this.dibujarCategorias = function()
	{
		if(this.indice == this.categorias.length - 1)
		{
			this.indice--;	
		}
		else if(this.indice < 0)
		{
			this.indice = 0;	
		};
		
		if(this.indice < 0)
		{
			this.indice = 0;	
		}
		
		var parentEl = this.categoriasParent;
		parentEl.innerHTML = "";
		
		var tbody = crearTabla(parentEl);
		tbody.parentNode.style.height = "100%";
		
		var tr = cE("tr", tbody);
	
		for(var i=this.indice; i<this.indice+3; i++)
		{
			if( i < this.categorias.length)
			{
				var categoriaTab = this.categorias.traerNo(i);
				
				var td = cE("td", tr);
				td.style.width = 64;
				
				var div = cE("div", td)
				div.style.cursor = "pointer";
				div.align = "center";
				div.style.paddingTop = 3;
				if(categoriaTab.id == this.idSeleccionado)
				{
					div.style.backgroundColor = "#525559";
				};
				
				var span = cE("span", div);
				span.className = "categoria_titulos";
				span.style.color = "white";
				span.innerHTML = categoriaTab.nombre;
				span.style.cursor = "pointer";	
				div.style.width = (span.offsetWidth > 0) ? span.offsetWidth + 15 : 40;
				div.style.height = (span.offsetHeight > 0) ? span.offsetHeight + 2 : 15;
				
				adjuntarOnclickCategoria(td, this, categoriaTab);
			};
		};	
	};
};

function adjuntarOnclickCategoria(td, categoriasTabObj, categoriaTab)
{
	var onclickFx = function()
	{
		masterPlayer.vieneDeExplorar = false;
		
		categoriasTabObj.idSeleccionado = categoriaTab.id;
		categoriasTabObj.construir();
		
		masterPlayer.playlistContenedor.innerHTML = "";	
		masterPlayer.categoriaActual = categoriaTab.nombre;		
		masterPlayer.categoriaSeleccionada = categoriaTab.id;
		masterPlayer.bajarPlaylistsDeLaCategoria();	
	};
	
	eh_attachEvent("onclick", td, onclickFx);
};

function formatearPlaylistSeleccionado(obj)
{
	masterPlayer.playlists = [];
	
	for(var i=0; i<obj.length; i++)
	{		
		var lista = obj[i];
		var playlist = masterPlayer.agregarPlaylist(lista.id);
		playlist.nombre = lista.nombre;
	};	
	
	masterPlayer.dibujarCategoriaPlaylists();
};
