/* Manejo de la botonera superior */

	var categoria_ids = [ "DOCUMENTAL", "PROGRAMA", "EVENTO", "OTROS" ];
	var categoria_nombres = [ "Documania", "Magazines", "Programas", "Eventos" ];
	var selected_categoria = 0;
	
	var imgs_on;
	var imgs_off;
	
	function switchcategorias( ncat, language )
	{
		imgs_on = [ 
			"images/documania_on_" + language + ".gif", 
			"images/magazine_on_" + language + ".gif", 
			"images/programas_on_" + language + ".gif", 
			"images/eventos_on_" + language + ".gif" 
		];
	
		imgs_off = [ 
			"images/documania_off_" + language + ".gif", 
			"images/magazine_off_" + language + ".gif", 
			"images/programas_off_" + language + ".gif", 
			"images/eventos_off_" + language + ".gif" 
		];
		
		verNavegador( );
		
		var selector_documentales = document.getElementById( "btn_selector_documentales" );
		var selector_programas = document.getElementById( "btn_selector_programas" );
		var selector_eventos = document.getElementById( "btn_selector_eventos" );
		var selector_otros = document.getElementById( "btn_selector_otros" );	

		selector_documentales.src = imgs_off[ 0 ];
		selector_programas.src = imgs_off[ 1 ];
		selector_eventos.src = imgs_off[ 2 ];
		selector_otros.src = imgs_off[ 3 ];
		
		switch( ncat )
		{
			case 1: 
				selector_documentales.src = imgs_on[ 0 ];
				setNombreCategoria( categoria_nombres[ 0 ] );
				loadSubcategorias( categoria_ids[ 0 ] );
				selected_categoria = 0;
				break;
			case 2: 
				selector_programas.src = imgs_on[ 1 ];
				setNombreCategoria( categoria_nombres[ 1 ] );
				loadSubcategorias( categoria_ids[ 1 ] );
				// loadVideos( categoria_ids[ 1 ], null );
				selected_categoria = 1;
				break;
			case 3: 
				selector_eventos.src = imgs_on[ 2 ];
				setNombreCategoria( categoria_nombres[ 2 ] );
				loadSubcategorias( categoria_ids[ 2 ] );
				// loadVideos( categoria_ids[ 2 ], null );
				selected_categoria = 2;
				break;
			case 4: 
				selector_otros.src = imgs_on[ 3 ];
				setNombreCategoria( categoria_nombres[ 3 ] );
				loadSubcategorias( categoria_ids[ 3 ] );
				// loadVideos( categoria_ids[ 3 ], null );
				selected_categoria = 3;
				break;
		}		
	}
	
	function setNombreCategoria( nombre )
	{
		var menu = document.getElementById( "documania_menu" );
		if ( menu != null && nombre != null )
		{
			clearchilds( menu );
			// menu.appendChild( document.createTextNode( nombre ) );
		}
	}
	
	function loadSubcategorias( idcat )
	{
		if ( ajax != null && !onrequest )
		{
			var query = 'documania_data.php?type=sc&cid=' + idcat + '&time=' + new Date( ).getTime( );
			ajax.open( 'GET', query, true );
			ajax.onreadystatechange = subcategoriasLoaded;
			ajax.send( '' );
			onrequest = true;
		}
	}
	
	function subcategoriasLoaded( )
	{
		try
		{
			if ( ajax != null && ajax.readyState == 4 )
			{
				if ( ajax.status == 200 )
				{
					var menu = document.getElementById( "documania_menu" );
					if ( menu != null )
					{
						var subcategorias = json_parse( ajax.responseText );
						menu.appendChild( document.createElement( 'br' ) );
						var idsc0 = null;
						var idcat = subcategorias.id;
						var nsubs = subcategorias.subcategorias.length;
						for ( var i = 0; i < nsubs; i++ )
						{
							if ( idsc0 == null ) idsc0 = subcategorias.subcategorias[i].id;
							var p = document.createElement('p');
							p.eventHook = true;
							if ( p.addEventListener )
							{
								p.addEventListener( "mouseover", function( event ) { overSubcategoria( event ); }, false );
								p.addEventListener( "mouseout", function( event ) { outSubcategoria( event ); }, false );
							}
							else if ( p.attachEvent )
							{
								p.attachEvent( "onmouseover", function( event ) { overSubcategoria( event ); } );
								p.attachEvent( "onmouseout", function( event ) { outSubcategoria( event ); } );
							}
							var a = document.createElement('a');
							var texto = subcategorias.subcategorias[ i ].nombre;
							a.appendChild( document.createTextNode( texto ) );
							a.setAttribute( "href", "javascript:loadVideos('" + 
								idcat + "','" + subcategorias.subcategorias[i].id + "');" );
							p.appendChild( a );
							var img = document.createElement( 'img' );
							img.src = 'images/arrow.gif';
							p.appendChild( img );
							menu.appendChild( p );
						}
						
						onrequest = false;						
						if ( idsc0 != null )
						{
							loadVideos( idcat, idsc0 );
						}
						else
						{
							loadVideos( idcat );
						}
					}
				}
			}
		}
		catch( e )
		{
			alert( "Error = " + e.name + "\nMessage = " + e.message + "\nAt = " + e.at + "\nText = " + e.text );
		}
		onrequest = false;
	}
	
	function overSubcategoria( event )
	{
		var src;
		if ( event.target )
		{
			src = event.target;
		}
		else if ( event.srcElement )
		{
			src = event.srcElement;
		}
		while ( src != null && !src.eventHook ) src = src.parentNode;
		src.className = 'over';
	}
	
	function outSubcategoria( event )
	{
		var src;
		if ( event.target )
		{
			src = event.target;
		}
		else if ( event.srcElement )
		{
			src = event.srcElement;
		}
		while ( src != null && !src.eventHook ) src = src.parentNode;
		src.className = '';
	}
	
	function loadVideos( idcat, idsubcat )
	{
		if ( ajax != null && !onrequest )
		{
			var query = 'documania_data.php?type=v';
			if ( idcat != null )
			{
				query = query + "&cid=" + idcat;
				if ( idsubcat != null )
				{
					query = query + "&scid=" + idsubcat;
				}
			}
			query = query + '&time=' + new Date( ).getTime( );
			ajax.open( 'GET', query, true );
			ajax.onreadystatechange = videosLoaded;
			ajax.send( '' );
			onrequest = true;
		}
	}
	
	var PAGE_SIZE = 10;
	var current_loaded_videos;
	
	function videosLoaded( )
	{
		try
		{
			if ( ajax != null && ajax.readyState == 4 )
			{
				if ( ajax.status == 200 )
				{
					current_loaded_videos = json_parse( ajax.responseText );					
					gotoVideosPage( 1 );
				}
			}
		}
		catch( e )
		{
			alert( "Error = " + e.name + "\nMessage = " + e.message + "\nAt = " + e.at + "\nText = " + e.text );
		}
		onrequest = false;
	}
	
	function gotoVideosPage( page )
	{
		try 
		{		
			var videos = current_loaded_videos;
			var nvideos = videos.videos.length;
			var maxPages = Math.floor( nvideos / PAGE_SIZE );
			if ( nvideos % PAGE_SIZE > 0 ) maxPages++;
			if ( maxPages >= 1 )
			{
				page = ( page <= 1 ) ? 1 : page;
				page = ( page >= maxPages ) ? maxPages : page ;
				var firstVideo = ( page - 1 ) * PAGE_SIZE;
				var lastVideo = ( ( ( page * PAGE_SIZE) - 1 ) < nvideos ) ? ( ( page * PAGE_SIZE) - 1 ) : nvideos - 1;  
				drawVideosPaginationInfo( nvideos, page, maxPages );
				drawVideosPage( videos, firstVideo, lastVideo );
			}
			else
			{
				drawVideosPaginationInfo( 0, -1, -1 );
				drawVideosPage( videos, 0, -1 );
			}
		}
		catch( e )
		{
			alert( "Error = " + e.name + "\nMessage = " + e.message + "\nAt = " + e.at + "\nText = " + e.text );
		}
	}
	
	function drawVideosPaginationInfo( nvideos, currentPage, maxPages )
	{
		var paginador = document.getElementById( 'documania_paginador' );
		if ( paginador != null )
		{
			clearchilds( paginador );
			
			if ( currentPage > 0 )
			{
				var prevPage = ( currentPage - 1 < 1 ) ? -1 : currentPage - 1;
				var postPage = ( currentPage * PAGE_SIZE >= nvideos ) ? -1 : currentPage + 1;  
	
				paginador.appendChild( document.createTextNode( '[' ) );
				if ( prevPage > 0 )
				{
					var aBefore = document.createElement( 'a' );
					aBefore.href = 'javascript:gotoVideosPage(' + 1 + ');';
					aBefore.appendChild( document.createTextNode( '<<' ) );
					paginador.appendChild( aBefore );
				}
				else
				{
					paginador.appendChild( document.createTextNode( '<<' ) );
				}
				paginador.appendChild( document.createTextNode( '] [' ) );
				if ( prevPage > 0 )
				{
					var aBefore = document.createElement( 'a' );
					aBefore.href = 'javascript:gotoVideosPage(' + prevPage + ');';
					aBefore.appendChild( document.createTextNode( '<' ) );
					paginador.appendChild( aBefore );
				}
				else
				{
					paginador.appendChild( document.createTextNode( '<' ) );
				}
				paginador.appendChild( document.createTextNode( '] Pagina ' + currentPage + ' de ' + maxPages + ' [' ) );
				if ( postPage > 0 )
				{
					var aAfter = document.createElement( 'a' );
					aAfter.href = 'javascript:gotoVideosPage(' + postPage + ');';
					aAfter.appendChild( document.createTextNode( '>' ) );
					paginador.appendChild( aAfter );
				}
				else
				{
					paginador.appendChild( document.createTextNode( '>' ) );
				}
				paginador.appendChild( document.createTextNode( '] [' ) );
				if ( postPage > 0 )
				{
					var aAfter = document.createElement( 'a' );
					aAfter.href = 'javascript:gotoVideosPage(' + maxPages + ');';
					aAfter.appendChild( document.createTextNode( '>>' ) );
					paginador.appendChild( aAfter );
				}
				else
				{
					paginador.appendChild( document.createTextNode( '>>' ) );
				}
				paginador.appendChild( document.createTextNode( ']' ) );
			}
		}
	}
	
	function drawVideosPage( videos, firstVideo, lastVideo )
	{
		var contenido = document.getElementById( "documania_contenido" );
		if ( contenido != null )
		{
			clearchilds( contenido );
			for ( var i = firstVideo; i <= lastVideo; i++ )
			{
				var table = document.createElement( 'table' );
				table.className = ( i%2==0 ) ? 'filapar' : 'filaimpar';
				var tr = table.insertRow( 0 );

				var tdImg = document.createElement( 'td' );
				tr.appendChild( tdImg );
				tdImg.className = 'td_video_arrow';
				
				var pVideo = document.createElement( 'p' );
				pVideo.className = 'video';
				tdImg.appendChild( pVideo );

				var aVideo = document.createElement( 'a' );
				aVideo.href = "javascript:showVideo( '" + videos.videos[i].videofile + "');";
				pVideo.appendChild( aVideo );

				var imgVideo = document.createElement( 'img' );
				imgVideo.src = videos.videos[i].imagen;
				aVideo.appendChild( imgVideo );
				
				var imgArrow = document.createElement( 'img' );
				imgArrow.src = 'images/arrow.gif';
				imgArrow.className = 'arrow';
				aVideo.appendChild( imgArrow );
				
				var tdTexto = document.createElement( 'td' );
				tr.appendChild( tdTexto );
				tdTexto.className = 'td_video_texto';
				
				var spantitulo = document.createElement( 'span' );
				spantitulo.className = "titulo";
				spantitulo.appendChild( document.createTextNode( videos.videos[i].nombre ) );
				tdTexto.appendChild( spantitulo );
				tdTexto.appendChild( document.createElement( 'br' ) );
				tdTexto.appendChild( document.createTextNode( videos.videos[i].descripcion ) );
				
				var tdCheck = document.createElement( 'td' );
				tr.appendChild( tdCheck );
				tdCheck.className = 'td_video_check';
				
				var pCheck = document.createElement( 'p' );
				tdCheck.appendChild( pCheck );
				pCheck.className = 'video';
				
				var aCheck = document.createElement( "a" );
				pCheck.appendChild( aCheck );
				aCheck.id = "documania_checkbox_" + i;
				aCheck.video = videos.videos[i];
											
				configureCheckbox( videos.videos[i].id, videos.videos[i].videofile, aCheck );							 

				contenido.appendChild( table );
			}
		}
	}
	
	function configureCheckbox( idvideo, videofile, a )
	{
		clearchilds( a );
		var img = document.createElement( 'img' );
		img.setAttribute( "galleryimg", "no" );
		img.src = ( a.video.playlist ) ? "images/check_on.gif" : "images/check_off.gif";
		img.className = "checkbox";
		var playlist = document.createElement( "img" );
		playlist.src = "images/playlist.gif";
		playlist.className = "playlist";
		a.appendChild( img );
		a.appendChild( playlist );
		if ( a.video.playlist )
		{
			a.href = "javascript:removeVideoFromPlayList('" +
				idvideo + "','" + videofile + "','" + a.id + "');";
		}
		else
		{
			a.href = "javascript:addVideoToPlayList('" +
				idvideo + "','" + videofile + "','" + a.id + "');";
		}
	}

	function loadPlayList( )
	{
		if ( ajax != null && !onrequest )
		{
			var query = 'documania_data.php?type=p&time=' + new Date( ).getTime( );
			
			ajax.open( 'GET', query, true );
			ajax.onreadystatechange = playListLoaded;
			ajax.send( '' );
			onrequest = true;
		}
	}
	
	var current_loaded_playlist;

	function playListLoaded( )
	{
		try
		{
			if ( ajax != null && ajax.readyState == 4 )
			{
				if ( ajax.status == 200 )
				{
					current_loaded_playlist = json_parse( ajax.responseText );
					gotoPlayListPage( 1 );
				}
			}
		}
		catch( e )
		{
			alert( "Error = " + e.name + "\nMessage = " + e.message + "\nAt = " + e.at + "\nText = " + e.text );
		}
		onrequest = false;
	}
	
	function gotoPlayListPage( page )
	{
		try 
		{		
			var videos = current_loaded_playlist;
			var nvideos = videos.playlist.length;
			var maxPages = Math.floor( nvideos / PAGE_SIZE );
			if ( nvideos % PAGE_SIZE > 0 ) maxPages++;
			if ( maxPages >= 1 )
			{
				page = ( page <= 1 ) ? 1 : page;
				page = ( page >= maxPages ) ? maxPages : page ;
				var firstVideo = ( page - 1 ) * PAGE_SIZE;
				var lastVideo = ( ( ( page * PAGE_SIZE) - 1 ) < nvideos ) ? ( ( page * PAGE_SIZE) - 1 ) : nvideos - 1;  
				drawPlayListPaginationInfo( nvideos, page, maxPages );
				drawPlayListPage( videos, firstVideo, lastVideo );
			}
			else
			{
				drawPlayListPaginationInfo( 0, -1, -1 );
				drawPlayListPage( videos, 0, -1 );
			}
		}
		catch( e )
		{
			alert( "Error = " + e.name + "\nMessage = " + e.message + "\nAt = " + e.at + "\nText = " + e.text );
		}
	}

	
	function drawPlayListPaginationInfo( nvideos, currentPage, maxPages )
	{
		var paginador = document.getElementById( 'documania_paginador' );
		if ( paginador != null )
		{
			clearchilds( paginador );
			
			if ( currentPage > 0 )
			{
				var prevPage = ( currentPage - 1 < 1 ) ? -1 : currentPage - 1;
				var postPage = ( currentPage * PAGE_SIZE >= nvideos ) ? -1 : currentPage + 1;  
	
				paginador.appendChild( document.createTextNode( '[' ) );
				if ( prevPage > 0 )
				{
					var aBefore = document.createElement( 'a' );
					aBefore.href = 'javascript:gotoPlayListPage(' + 1 + ');';
					aBefore.appendChild( document.createTextNode( '<<' ) );
					paginador.appendChild( aBefore );
				}
				else
				{
					paginador.appendChild( document.createTextNode( '<<' ) );
				}
				paginador.appendChild( document.createTextNode( '] [' ) );
				if ( prevPage > 0 )
				{
					var aBefore = document.createElement( 'a' );
					aBefore.href = 'javascript:gotoPlayListPage(' + prevPage + ');';
					aBefore.appendChild( document.createTextNode( '<' ) );
					paginador.appendChild( aBefore );
				}
				else
				{
					paginador.appendChild( document.createTextNode( '<' ) );
				}
				paginador.appendChild( document.createTextNode( '] Pagina ' + currentPage + ' de ' + maxPages + ' [' ) );
				if ( postPage > 0 )
				{
					var aAfter = document.createElement( 'a' );
					aAfter.href = 'javascript:gotoPlayListPage(' + postPage + ');';
					aAfter.appendChild( document.createTextNode( '>' ) );
					paginador.appendChild( aAfter );
				}
				else
				{
					paginador.appendChild( document.createTextNode( '>' ) );
				}
				paginador.appendChild( document.createTextNode( '] [' ) );
				if ( postPage > 0 )
				{
					var aAfter = document.createElement( 'a' );
					aAfter.href = 'javascript:gotoPlayListPage(' + maxPages + ');';
					aAfter.appendChild( document.createTextNode( '>>' ) );
					paginador.appendChild( aAfter );
				}
				else
				{
					paginador.appendChild( document.createTextNode( '>>' ) );
				}
				paginador.appendChild( document.createTextNode( ']' ) );
			}
		}
	}
	
	function drawPlayListPage( videos, firstVideo, lastVideo )
	{
		var playlist = document.getElementById( "documania_playlist" );
		if ( playlist != null )
		{
			clearchilds( playlist );
			for ( var i = firstVideo; i <= lastVideo; i++ )
			{
				var table = document.createElement( 'table' );
				table.className = ( i%2==0 ) ? 'filapar' : 'filaimpar';
				var tr = table.insertRow( 0 );

				var tdImg = document.createElement( 'td' );
				tr.appendChild( tdImg );
				tdImg.className = 'td_video_arrow';
				
				var pVideo = document.createElement( 'p' );
				pVideo.className = 'video';
				tdImg.appendChild( pVideo );

				var aVideo = document.createElement( 'a' );
				aVideo.href = "javascript:showVideo( '" + videos.playlist[i].videofile + "');";
				pVideo.appendChild( aVideo );

				var imgVideo = document.createElement( 'img' );
				imgVideo.src = videos.playlist[i].imagen;
				aVideo.appendChild( imgVideo );
				
				var imgArrow = document.createElement( 'img' );
				imgArrow.src = 'images/arrow.gif';
				imgArrow.className = 'arrow';
				aVideo.appendChild( imgArrow );
				
				var tdTexto = document.createElement( 'td' );
				tr.appendChild( tdTexto );
				tdTexto.className = 'td_video_texto';
				
				var spantitulo = document.createElement( 'span' );
				spantitulo.className = "titulo";
				spantitulo.appendChild( document.createTextNode( videos.playlist[i].nombre ) );
				tdTexto.appendChild( spantitulo );
				tdTexto.appendChild( document.createElement( 'br' ) );
				tdTexto.appendChild( document.createTextNode( videos.playlist[i].descripcion ) );
				
				playlist.appendChild( table );
			}
		}
	}

	
	function doBusqueda( )
	{
		if ( ajax != null && !onrequest )
		{
			var input = document.getElementById( "documania_buscador_input" );
			var tosearch = input.value;
			input.value = '';
			// Computar la query adecuada
			var cid = categoria_ids[ selected_categoria ];
			var query = 'documania_data.php?type=v&scid=' + cid + '&s=' + escape( tosearch ) + '&time=' + new Date( ).getTime( );
			ajax.open( 'GET', query, true );
			ajax.onreadystatechange = videosLoaded;
			ajax.send( '' );
			onrequest = true;
		}
	}
	
	function addVideoToPlayList( idvideo, videofile, id )
	{
		if ( ajax != null && !onrequest )
		{
			var query = 'documania_data.php?action=add&vid=' + idvideo + 
				'&vf=' + videofile + '&time=' + new Date( ).getTime( );
			
			ajax.open( 'GET', query, true );
			ajax.onreadystatechange = success;
			ajax.send( '' );
			onrequest = true;
			
			var a = document.getElementById( id );
			a.video.playlist = true;
			configureCheckbox( idvideo, videofile, a );
		}
	}
	
	function removeVideoFromPlayList( idvideo, videofile, id )
	{
		if ( ajax != null && !onrequest )
		{
			var query = 'documania_data.php?action=del&vid=' + idvideo + '&time=' + new Date( ).getTime( );
			
			ajax.open( 'GET', query, true );
			ajax.onreadystatechange = success;
			ajax.send( '' );
			onrequest = true;
			
			var a = document.getElementById( id );
			a.video.playlist = false;
			configureCheckbox( idvideo, videofile, a );
		}
	}
	
	function success( )
	{
		onrequest = false;
	}
	

/* Obtencion del XMLHttpRequest */

var ajax = createAJAXObject( );
var onrequest = false;

function createAJAXObject( urltogo )
{  
	var obj;
	if( window.XMLHttpRequest ) // no es IE
	{ 	  
		obj = new XMLHttpRequest( );
	}
	else // Es IE o no tiene el objeto 
	{ 	  
		try 
		{   
			obj = new ActiveXObject("Microsoft.XMLHTTP");  
		}
		catch ( e )
		{  
			alert( 'El navegador utilizado no está soportado' );
			if ( urltogo != null )
			{
				window.location = urltogo;
			}
		}  
	}  
	return obj;  
}

function clearchilds( toclear )
{
	var childs = toclear.childNodes;
	if ( childs != null )
	{
		var lc = childs.length;
		var i;
		for ( i = 0; i < lc; i++ )
		{
			toclear.removeChild( childs.item( 0 ) );
		}
	}
}

function verPlayList( )
{
	var menu = document.getElementById( "documania_menu" );
	var contenido = document.getElementById( "documania_contenido" );
	var playlist = document.getElementById( "documania_playlist" );
	var tabvernav = document.getElementById( "documania_vernavegador" );
	var tabverpla = document.getElementById( "documania_verplaylist" );
	var sep_nav_pla = document.getElementById( "sep_nav_pla" );
	var sep_pla_voi = document.getElementById( "sep_pla_voi" );
	var buscador = document.getElementById( "documania_buscador" );
	var reproducir = document.getElementById( "documania_cabecera_reproducir" );
	
	buscador.className = 'hidden';
	reproducir.className = '';
	sep_nav_pla.src = "images/sep_navoff_plaon.gif";
	sep_pla_voi.src = "images/sep_plaon_voi.gif";
	menu.className = "hidden";
	contenido.className = "hidden";
	playlist.className = "playlist";
	tabvernav.className = "tab_off";
	tabverpla.className = "tab_on";
	
	loadPlayList( );
}

function verNavegador( )
{
	var menu = document.getElementById( "documania_menu" );
	var contenido = document.getElementById( "documania_contenido" );
	var playlist = document.getElementById( "documania_playlist" );
	var tabvernav = document.getElementById( "documania_vernavegador" );
	var tabverpla = document.getElementById( "documania_verplaylist" );
	var sep_nav_pla = document.getElementById( "sep_nav_pla" );
	var sep_pla_voi = document.getElementById( "sep_pla_voi" );
	var buscador = document.getElementById( "documania_buscador" );
	var reproducir = document.getElementById( "documania_cabecera_reproducir" );
	
	buscador.className = '';
	reproducir.className = 'hidden';
	sep_nav_pla.src = "images/sep_navon_plaoff.gif";
	sep_pla_voi.src = "images/sep_plaoff_voi.gif";
	menu.className = "menu";
	contenido.className = "contenido";
	playlist.className = "hidden";
	tabvernav.className = "tab_on";
	tabverpla.className = "tab_off";
}

function launchPlayList( )
{
	var reproductor = document.getElementById( 'reproductor' );
	reproductor.src = "showdocuvideo.php?pl=1&time=" + new Date( ).getTime( );
}

function showVideo( video )
{
	var reproductor = document.getElementById( 'reproductor' );
	var src = "showdocuvideo.php?vf=" + escape(video);
	reproductor.src = src;
}

function stopVideo( )
{
	var reproductor = document.getElementById( 'reproductor' );
	reproductor.src = "showdocuvideo.php?vf=null";
}

