var urlVotar = "/procesar/votaciones.pag";
var votacion = new Rater("Votacion");
var valorVotado = 0;
var identificador;
var descripcion;
var titulo;
var seccion;

function iniciar(idVideo, tit, desc, secc) {
	identificador = idVideo;

	titulo = tit;
	descripcion = desc;
	seccion = secc;
	votacion.onRate = votar;		
	calcularMedia(idVideo, tit, desc, secc);
}

function reset()
{
    votacion.value = 0;
    votacion.draw(true);
}

/****************************************************************************** VOTAR **********************************************************************/
function votar(value) {	
	var param = new Object();
	param.accion = "votar";
	param.votacion = value;
	param.id = identificador;
	param.titulo = titulo;
	param.descripcion = descripcion;
	param.seccion = seccion;
	valorVotado = value;
	var opciones = {
			method: 'get', 
			parameters: param,
			onComplete: recuperarVoto
	};
	new Ajax.Request(urlVotar, opciones); 
}

function recuperarVoto(respuesta){
	var data = eval("(" + respuesta.responseText + ")");
	debugger;
	if(!data.error){		
		votacion.settings.initialValue = valorVotado;
		votacion.draw(false);
		$('spanInfo').innerHTML = data.mensaje;	
		borrarTablas();
		calcularMasVotados(donde);
	}
}

/****************************************************************************** MEDIA **********************************************************************/
function calcularMedia(idVideo, tit, desc, secc){
	var param = new Object();
	identificador = idVideo;
	titulo = tit;
	descripcion = desc;
	seccion = secc;
	param.accion = "calcularMedia";
	param.id = idVideo;
	var opciones = {
			method: 'get', 
			asynchronous: false, 
			parameters: param,
			onComplete: recuperarCalcularMedia
	};
	new Ajax.Request(urlVotar, opciones);
}

function recuperarCalcularMedia(respuesta){
	var data = eval("(" + respuesta.responseText + ")");
	if(!data.error){		
		votacion.settings.initialValue = data.votacion;;
		votacion.draw(true);
		$('spanInfo').innerHTML = data.totalVotos;		
	}
}

/****************************************************************************** TOP 3 **********************************************************************/
function calcularMasVotados(secc) {	
	var param = new Object();
	param.accion = "recuperarMasVotados";
	param.seccion = donde;
	var opciones = {
			method: 'get', 
			parameters: param,
			onComplete: recuperarMasVotados
	};
	new Ajax.Request(urlVotar, opciones); 
}

function recuperarMasVotados(respuesta){
	var data = eval("(" + respuesta.responseText + ")");
	if(!data.error) {
		if (data.top.size() != 0) {
			dibujarTablas(data.top);
		}
	}
}

function borrarTablas() {
	for(i=0; i<3; i++) {
		var tabla = $('top' + i);			
		if (tabla.rows.length != 0) {
			tabla.deleteRow(2);
			tabla.deleteRow(1);
			tabla.deleteRow(0);
		}
	}
}




function dibujarTablas(top) {
	// Aquí dibujo los 3 destacados.
	for (i=0; i<top.size(); i++) {
		var tabla = $('top' + i);	
		var row = tabla.insertRow(tabla.rows.length);
		row.style.cursor = 'pointer';
		// La primera fila es donde va la imagen y el enlace
		var celda0 = row.insertCell(0);	
		celda0.vAlign = 'top';
		// Creamos la imagen.
		var imagen = document.createElement("img");
		imagen.src = "fotos/" + top[i].idVideo + ".gif";
		imagen.width = "60";
		imagen.height = "39";
		imagen.border = "0";
		imagen.align="left";
		// Creamos la parte en negrita para meter la imagen dentro
		var bold = document.createElement("b");
		bold.innerHTML = i+1 + 'º ';	
		// Creamos el enlace
		var enlace = document.createElement('a');
		// Añadimos al enlace la imagen, la negrita y el titulo
		imagen.name = top[i].idVideo + "|_|" + top[i].titulo + "|_|" + top[i].descripcion + "|_|" + top[i].seccion;
		enlace.appendChild(imagen);
		enlace.appendChild(bold);
		enlace.appendChild(document.createTextNode(top[i].titulo));
		if (donde == "") {		
			enlace.setAttribute('href', 'javascript:void(0);');
			enlace.className = 'entraditis';
//			enlace.setAttribute('class', 'entraditis');
			enlace.id = 'enlace' + i;
			enlace.name = top[i].idVideo + "|_|" + top[i].titulo + "|_|" + top[i].descripcion + "|_|" + top[i].seccion;
			enlace.onclick = onClickTop;
/*			Event.observe($(enlace.id), 'click', onClickTop.bindAsEventListener(enlace));
			if (enlace.addEventListener)  // standard W3C DOM
				enlace.addEventListener("click", onClickTop, false);
			else if (enlace.attachEvent) { // IE cabron DOM
				enlace.attachEvent("onclick", onClickTop);
			}*/
		} else {
			enlace.href = top[i].idVideo + '.htm?height=335&width=735';
			enlace.className = 'thickbox';
		}
		// Y ahora metemos todo eso dentro de la celda
		celda0.appendChild(enlace);
		// La segunda fila es donde van las estrellas
		var row1 = tabla.insertRow(tabla.rows.length);
		var celda1 = row1.insertCell(0);	
		celda1.vAlign = 'top';
		celda1.setAttribute('class', 'estrellas');
		var div = document.createElement('div');
		var a = document.createElement('a');
		var span = document.createElement('span');
		
		a.id = 'votado' + i;
		span.id = 'spanVotado' + i;		
		div.appendChild(a);
		div.appendChild(span);
		celda1.appendChild(div);
		var votado = new Rater(a.id);	
		votado.onRate = votar;	
		votado.settings.initialValue = top[i].puntuacion/top[i].votantes;		
		votado.draw(false);
		$('spanVotado' + i).innerHTML = "(" + top[i].puntuacion + ")";	
		// La tercera fila es donde va un pixel transparente
		var row2 = tabla.insertRow(tabla.rows.length);
		var celda2 = row2.insertCell(0);	
		celda2.height="10";		
		// Creamos la imagen.
		var imagenPixel = document.createElement("img");
		imagenPixel.src = "imagenes/pixel_trans.gif";
		imagenPixel.width = "1";
		imagenPixel.height = "1";
		imagenPixel.longdesc = "pixel transparente";
		celda2.appendChild(imagenPixel);
	}
}

function onClickTop(event) {
	var keys = this.name.split("|_|");
	calcularMedia(keys[0], keys[1], keys[2], keys[3]);
	choix1('http://ondemand.telemadrid.ondemand.flumotion.com/telemadrid/ondemand/' + keys[0] + '.f4v');
	cambiar2(keys[1]);
	cambiar(keys[2]);
}

