//////////////////////////////////////////////
//////////////////////////////////////////////
function xmlhttpPost(str) {
    var xmlHttpReq = false;
    var self = this;
    var strURL = "index.phtml";
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    alert("1");
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
        	alert("2");
            updatepage(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send(str);
}

function updatepage(str){
    document.getElementById("Div-LOGIN").innerHTML = str;
}

function Login(){
	var form     = document.forms['login'];
    var usuario = form.usuario.value;
    var password = form.clave.value;
    qstr = 'usuario=' + usuario + '&clave=' + password;  // NOTE: no '?' before querystring
    xmlhttpPost(qstr);
}
//////////////////////////////////////////////
//////////////////////////////////////////////

var titulos          = new Array(0);
var usuarios         = new Array(0);
var categorias       = new Array(0);
var duraciones       = new Array(0);
var votoss           = new Array(0);
var imagenes         = new Array(0);
var tituloUrls       = new Array(0);
var razonSelecciones = new Array(0);
var razonTextoImgs   = new Array(0);

var pagina = 1;
var numPaginas;
var par;

var resumen;
var tags;
var categorias;
var usuarios;

var videosParaTop;
var minimenu;

var votosVisualizaciones;
var video;
var codVideos = new Array(0);
var votoss    = new Array(0);
var vistass   = new Array(0);

var i;
var imagenes       = new Array("/img/izqsinflecha.gif", "/img/izqconflecha.gif", "/img/derechasinflecha.gif", "/img/derechaconflecha.gif");
var lista_imagenes = new Array();

function init(verTop, obtenerVideosTop, numPaginas, mini) {
	minimenu = mini
	// PRECARGA DE IMAGENES
	if (verTop == 1){// EJECUTAMOS EL JAVASCRIPT NECESARIO.
		// PINTAMOS EL RESUMEN DE ARRIBA
		pintarResumenTop();
		// PRECARGA DE IMAGENES PARA PAGINACION
		preCargaImagenes();
		pintarPaginacion(pagina, numPaginas, obtenerVideosTop);
	}// SI NO, NO LO EJECUTAMOS. PERO SI LOS VOTOS Y LAS VISUALIZACIONES.
	//cargaVotosVisualizaciones();
}

function pintarResumenTop(){
	getXMLResumenTop();
}

function preCargaImagenes(){

   for(i = 0; i < 4; i++){
     lista_imagenes[i]     = new Image();
     lista_imagenes[i].src = imagenes[i];
	}
}

function pintarPaginacion(pag, numPaginas, obtenerVideosTop){
	pagina = pag;
	if (numPaginas == 1){// SI SOLO HAY UNA PAGINA (4 VIDEOS)
		document.getElementById('paginacionIzq').innerHTML = '<img src="'+lista_imagenes[0].src+'" alt="Mas Videos" width="37" height="147" border="0" />';
		document.getElementById('paginacionDer').innerHTML = '<img src="'+lista_imagenes[2].src+'" alt="Mas Videos" width="38" height="147" border="0" />';
	}else{
		if (pagina == 1){// PRIMERA PAGINA
			document.getElementById('paginacionIzq').innerHTML = '<img src="'+lista_imagenes[0].src+'" alt="Mas Videos" width="37" height="147" border="0" />';
			document.getElementById('paginacionDer').innerHTML = '<a href="javascript:pintarPaginacion('+(pagina+1)+','+numPaginas+',0);"><img src="'+lista_imagenes[3].src+'" alt="Mas Videos" width="38" height="147" border="0" /></a>';
		
		}else if (pagina == numPaginas){// ULTIMA PAGINA
			document.getElementById('paginacionIzq').innerHTML = '<a href="javascript:pintarPaginacion('+(pagina-1)+','+numPaginas+',0);"><img src="'+lista_imagenes[1].src+'" alt="Mas Videos" width="37" height="147" border="0" /></a>';
			document.getElementById('paginacionDer').innerHTML = '<img src="'+lista_imagenes[2].src+'" alt="Mas Videos" width="38" height="147" border="0" />';
	
		}else{ // EN MEDIO
			document.getElementById('paginacionIzq').innerHTML = '<a href="javascript:pintarPaginacion('+(pagina-1)+','+numPaginas+',0);"><img src="'+lista_imagenes[1].src+'" alt="Mas Videos" width="37" height="147" border="0" /></a>';
			document.getElementById('paginacionDer').innerHTML = '<a href="javascript:pintarPaginacion('+(pagina+1)+','+numPaginas+',0);"><img src="'+lista_imagenes[3].src+'" alt="Mas Videos" width="38" height="147" border="0" /></a>';
		}
	}
	getXML(obtenerVideosTop);
	
}

function cargaVotosVisualizaciones(){

	getXMLVotosVisualizaciones(minimenu);
	setTimeout("getXMLVotosVisualizaciones("+minimenu+")", 10000);
}

function getXML(obtenerVideosTop) {

	var url = "videosTop.phtml";
	var querystring = crearQueryString(obtenerVideosTop);
	var myAjax = null;

	myAjax = new Ajax.Request(
	url,
	{
		method: 'get',
		asynchronous: true,
		parameters: querystring,
		onComplete: processXML
	});
}

function getXMLResumenTop() {

	var url = "resumenTop.phtml";
	var querystring = crearQueryStringResumenTop();
	var myAjax = null;

	myAjax = new Ajax.Request(
	url,
	{
		method: 'get',
		asynchronous: true,
		parameters: querystring,
		onComplete: processXMLResumenTop
	});
}

function getXMLVotosVisualizaciones() {

	var url         = "votosVisualizaciones.phtml";
	var querystring = crearQueryStringVotosVisualizaciones();
	var myAjax      = null;

	myAjax = new Ajax.Request(
	url,
	{
		method: 'get',
		asynchronous: true,
		parameters: querystring,
		onComplete: processXMLVotosVisualizaciones
	});
}
function crearQueryString(obtenerVideosTop){
	var str;
	str="pag="+pagina+"&obtenerVideosTop="+obtenerVideosTop;
	return str;
}

function crearQueryStringResumenTop(){
	var str;
	str="par="+par;
	return str;
}

function crearQueryStringVotosVisualizaciones(){
	//alert('en query minimenu: '+minimenu);
	var str;
	str="minimenu="+minimenu;
	return str;
}

function processXML(request) {

	var videosParaTop;
	var video;

	try {
		varVideosParaTop = request.responseXML.getElementsByTagName("videosParaTop")[0];
        video = varVideosParaTop.getElementsByTagName("video");
        numPaginas = varVideosParaTop.getElementsByTagName("numPaginas")[0].firstChild.nodeValue;
	}
	catch (e) {
		setTimeout("getXML()", 1000);
		return;
	}
	
	for (i = 0; i < 4; i++){ // VACIAMOS
		titulos[i] 		    = '';
		usuarios[i] 		= '';
		duraciones[i] 		= '';
		categorias[i] 		= '';
		votoss[i] 			= '';
		imagenes[i] 		= '';
		tituloUrls[i] 		= '';
		razonSelecciones[i] = '';
		razonTextoImgs[i]   = '';
	}
	
	for (i = 0; i < video.length; i++) {
		titulos[i] 		    = video[i].getElementsByTagName("titulo")[0].firstChild.nodeValue;
		usuarios[i] 		= video[i].getElementsByTagName("usuario")[0].firstChild.nodeValue;
		duraciones[i] 		= video[i].getElementsByTagName("duracion")[0].firstChild.nodeValue;
		categorias[i] 		= video[i].getElementsByTagName("categoria")[0].firstChild.nodeValue;
		votoss[i] 			= video[i].getElementsByTagName("votos")[0].firstChild.nodeValue;
		imagenes[i] 		= video[i].getElementsByTagName("imagen")[0].firstChild.nodeValue;
		tituloUrls[i] 		= video[i].getElementsByTagName("tituloUrl")[0].firstChild.nodeValue;
		razonSelecciones[i] = video[i].getElementsByTagName("razonSeleccion")[0].firstChild.nodeValue;
		razonTextoImgs[i]   = video[i].getElementsByTagName("razonTextoImg")[0].firstChild.nodeValue;
	}
	for (j = i; j < 4; j++){// RELLENAMOS LOS VACIOS
		titulos[j] 		    = '';
		usuarios[j] 		= '';
		duraciones[j] 		= '';
		categorias[j] 		= '';
		votoss[j] 			= '';
		imagenes[j] 		= '/t/r/transparente.gif';
		tituloUrls[j] 		= '';
		razonSelecciones[j] = '';
		razonTextoImgs[j]   = '';
	}
	
	if (pagina <= numPaginas){// SI HAY MAS ELEMENTOS LOS PINTAMOS
		push();
	}else{
		pagina = numPaginas;
	}
}

function processXMLResumenTop(request){

	resumen    = request.responseXML.getElementsByTagName("resumenTop")[0];
	tags       = resumen.getElementsByTagName("tags")[0].firstChild.nodeValue;
	categorias = resumen.getElementsByTagName("categorias")[0].firstChild.nodeValue;
	usuarios   = resumen.getElementsByTagName("usuarios")[0].firstChild.nodeValue;
	pushResumenTop();
}

function processXMLVotosVisualizaciones(request){

	var votosVisualizaciones;
	var video;
	
	votosVisualizaciones = request.responseXML.getElementsByTagName("votosVisualizaciones")[0];
	video                = votosVisualizaciones.getElementsByTagName("video");
	
	for (i = 0; i < video.length; i++) {

		codVideos[i] = video[i].getElementsByTagName("codVideo")[0].firstChild.nodeValue;
		votoss[i]    = video[i].getElementsByTagName("votos")[0].firstChild.nodeValue;
		vistass[i]   = video[i].getElementsByTagName("vistas")[0].firstChild.nodeValue;
	
	}
	pushVotosVisualizaciones();
}

function push() {
	
	var text;
	text  = '<div class="bagon">';
	text += '<ul id="bagonlist">';
	for(i=0;i<titulos.length;i++){ // CON LA LONGITUD DE TITULOS (U OTRO) SE EL NUMERO DE VUELTAS
		text += razonTextoImgs[i]; 
		text += tituloUrls[i];
		text += '<img src="/imagenes/videos/'+ imagenes[i] +'"alt="'+titulos[i]+'" width="100" height="75"  border="0"/><br />';
		text += titulos[i]+ '</a>' + '<br />'; 
		text += '<span class="tiempo">'+duraciones[i]+'</span><br / > ';
		text += razonSelecciones[i]+' <br / > ';
		text += '</li>';
	
	}
	text += '</ul>';
	text += '</div>';
	
	document.getElementById("DIV-topSuscripcion").innerHTML =	text;
}

function pushResumenTop(){

	document.getElementById("DIV-resumenTop").innerHTML = '<strong>Mejores videos de tus suscripciones:</strong> tags('+tags+'), usuarios('+usuarios+'), categorias('+categorias+') <a href="/user/mySubscriptions/">Editar suscripciones</a>';
}

function pushVotosVisualizaciones(){
	var capa;
	var bd;
	var j;
	for(i = 1; i < 13; i++){
		j = i-1;
		// COMPARAR, SI ES DIFERENTE PINTAR.
		// QUE HAY EN LA CAPA
		capa = document.getElementById("votos-"+i).innerHTML
		//alert(capa);
		// QUE HAY EN LA BD (COMPLETADO CON LO QUE LLEVA LA CAPA)
		bd = votoss[j] + ' votos';
		//alert(bd);
		if (capa != bd){
			document.getElementById("votos-"+i).innerHTML = votoss[j] + ' votos';
		}
		//document.getElementById("votos-"+i).innerHTML = '6';
		//document.getElementById("visualizaciones-"+i).innerHTML = 'visualizaciones_A';
	}
	//getXMLVotosVisualizaciones();
}


function countDown()
{
var tiempo = document.getElementById('contador');

	toSecond=toSecond-1;
	if(toSecond<0)
	{
		toSecond=59;
		toMinute=toMinute-1;
	}

	
	if(toMinute<0)
	{
		toMinute=59;
		toHour=toHour-1;
	}

	if(toHour<0)
	{
		toHour = 0;
		toMinute = 0;
		toSecond = 0;
		tiempo.innerHTML = toHour+'0:'+toMinute+'0:0'+toSecond;
	}else{
		setTimeout("countDown()",1000);
	}
	
	tiempo.innerHTML = toHour+':'+toMinute+':'+toSecond;
}

