var URL_ICONOS = new Array("pages/img/es/aplicaciones/estrella_s3.gif","pages/img/es/aplicaciones/estrella_s2.gif");
var URL_ICONO = "pages/img/es/aplicaciones/estrella_s3.gif";
var TIPO_VOTO_AJAX = 1;
var ESTILO_ICONO_COMPLETO = "llena";
var ESTILO_ICONO_MEDIO = "media";
var ESTILO_ICONO_VACIO = "vacia";
var UT_RATING_IMG_HOVER = "llena";
var UT_RATING_IMG_REMOVED = "";

var oAjax = null;
var oCapa = null;

/**
 * Crea el objeto xmlHttpRequest para las peticiones
 * 
 * @return Devuelve el objeto xmlHttpRequest
 */
function getXmlHttpRequest() {
	var httpRequest = null;
	try {
		httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
			httpRequest = null;
		}
	}
	if (!httpRequest && typeof XMLHttpRequest != "undefined") {
		httpRequest = new XMLHttpRequest();
	}
	return httpRequest;
}

/**
 * Reemplaza la coma decimal por el punto
 * 
 * @param valor  Valor para representar en decimal
 * @return Devuelve el valor decimal
 */
function getValorDecimal(valor) {
	return valor.replace(",", ".");
}

/**
 * Pinta la valoracion actual
 * 
 * @param nombreCapa  Capa en la que se representa la valoracion
 * @param valor  Valor que se representa
 */
function pintarValoracion(nombreCapa, valor, tipo) {
	oCapa = document.getElementById(nombreCapa);
	calcularValoracion(getValorDecimal(valor),tipo-1);
}

/**
 * Calcula la valoracion 
 * 
 * @param valor  Valor que se representa
 */
function calcularValoracion(valor, tipo) {
	var cotaSup = Math.ceil(valor);
	var decimal = valor - Math.floor(valor);
	var estilo = ESTILO_ICONO_VACIO;
	for (var i = 1; i <= 5; i++) {
		if ((i < cotaSup) || ((i == cotaSup) && (decimal == 0))) {
			estilo = ESTILO_ICONO_COMPLETO;
		} else if ((i == cotaSup) && (decimal >= 0.5)) {
			estilo = ESTILO_ICONO_MEDIO;
		} else {
			estilo = ESTILO_ICONO_VACIO;
		}
		addIcono(oCapa, estilo, tipo);
	}
	oCapa.innerHTML += "&nbsp;" + valor;
}

/**
 * Aniade un icono para votar
 * 
 * @param capa  Capa a la que se aniade el icono
 * @param estilo  Estilo CSS del icono
 */
function addIcono(capa, estilo, tipo) {
	oImagen = document.createElement("img");
	if (tipo > 0){
		oImagen.setAttribute("src", URL_ICONOS[tipo]);		
	}else{
		oImagen.setAttribute("src", URL_ICONO);
	}
	oImagen.setAttribute("class", estilo);
	capa.appendChild(oImagen);
	oImagen = null;
}

/**
 * Funcion que valora un video mediante AJAX.
 * 
 * @param idVideo  Identificador del video
 * @param valor  Valoracion del usuario acerca del video
 */
function valorarVideo(idVideo, valor) {
	oAjax = getXmlHttpRequest();
	if (!oAjax) {
		return;
	}
	oAjax.onreadystatechange = function() {
								if ((oAjax.readyState == 4) 
										&& (oAjax.status == 200)) {
									oCapa.innerHTML = "";
									calcularValoracion(parseFloat(getValorDecimal(oAjax.responseText)));
								}};
	oAjax.open("POST", "tratarVotoVideo.do?identificador=" + idVideo + "&voto=" + valor + "&tipoVoto=" + TIPO_VOTO_AJAX, true);
	document.getElementById("capaValoracion_" + idVideo).innerHTML = mensajeVoto;
	oAjax.send(null);
	destruirObjetos();
}

/**
 * Funcion que valora un video de la seccion videoteca mediante AJAX.
 * 
 * @param idVideo  Identificador del video
 * @param valor  Valoracion del usuario acerca del video
 */
function valorarVideoVideoteca(idVideo, valor) {
	oAjax = getXmlHttpRequest();
	if (!oAjax) {
		return;
	}
	oAjax.open("POST", "tratarVotoVideoVideoteca.do?identificador=" + idVideo + "&voto=" + valor + "&tipoVoto=" + TIPO_VOTO_AJAX, true);
	document.getElementById("capaValoracion_" + idVideo).innerHTML = mensajeVoto;
	oAjax.send(null);
	destruirObjetos();
}

/**
 * Destruye los objetos de memoria
 */
function destruirObjetos() {
	if (oCapa) {
		oCapa == null;
	}
	if (oAjax) {
		oAjax == null;
	}
}

function UTRating(ratingElementId, maxStars, objectName, formName, ratingMessageId, pointsMessageId, componentSuffix, size, iIdioma)
{
	this.ratingElementId = ratingElementId;
	this.maxStars = maxStars;
	this.objectName = objectName;
	this.formName = formName;
	this.ratingMessageId = ratingMessageId
	this.pointsMessageId = pointsMessageId
	this.componentSuffix = componentSuffix
	this.iIdioma = iIdioma

	this.starTimer = null;
	this.starCount = 0;
	
	// pre-fetch image
	(new Image()).src = ESTILO_ICONO_COMPLETO;
	(new Image()).src = ESTILO_ICONO_MEDIO;

	function showStars(starNum, skipMessageUpdate) {
		this.clearStarTimer();
		this.greyStars();
		this.colorStars(starNum);
		if(!skipMessageUpdate)
			this.setMessage(starNum);
	}

	function setMessage(starNum) {
		messages = new Array("<strong>Gracias por su voto</strong>", "<strong>Gracias por su voto</strong>", "<strong>Gracias por su voto</strong>", "<strong>Gracias por su voto</strong>", "<strong>Gracias por su voto</strong>", "<strong>Gracias por su voto</strong>");
		if (starNum == 0) {
			document.getElementById(this.pointsMessageId).innerHTML = "";
		} else {
			//document.getElementById(this.pointsMessageId).innerHTML = messages[starNum];
			document.getElementById(this.pointsMessageId).innerHTML = mensajeVoto;
		}
	}

	function colorStars(starNum) {
		for (var i=0; i < starNum; i++)
			document.getElementById('star_'  + this.componentSuffix + "_" + (i+1)).className = ESTILO_ICONO_COMPLETO;
	}

	function greyStars() {
		for (var i=0; i < this.maxStars; i++)
			document.getElementById('star_' + this.componentSuffix + "_"  + (i+1)).className = ESTILO_ICONO_VACIO;
	}

	function setStars(starNum) {
		this.starCount = starNum;
		this.drawStars(starNum);
		document.forms[this.formName]['voto'].value = this.starCount;
		capa = document.getElementById("valoracion")
		if(capa != null){
			capa.style.display="none";
		}
		var ratingElementId = this.ratingElementId;
		postForm(this.formName, true, function (req) { replaceDivContents(req, ratingElementId); });
	}


	function drawStars(starNum, skipMessageUpdate) {
		this.starCount=starNum;
		this.showStars(starNum, skipMessageUpdate);
	}

	function clearStars() {
		this.starTimer = setTimeout(this.objectName + ".resetStars()", 500);
	}

	function resetStars() {
		this.clearStarTimer();
		if (this.starCount)
			this.drawStars(this.starCount);
		else
			this.greyStars();
		this.setMessage(0);
	}

	function clearStarTimer() {
		if (this.starTimer) {
			clearTimeout(this.starTimer);
			this.starTimer = null;
		}
	}

	this.clearStars = clearStars;
	this.clearStarTimer = clearStarTimer;
	this.greyStars = greyStars;
	this.colorStars = colorStars;
	this.resetStars = resetStars;
	this.setStars = setStars;
	this.drawStars = drawStars;
	this.showStars = showStars;
	this.setMessage = setMessage;

}

/**
 * Abre la url en un popup
 *
 * @param sURL  URL de destino
 */
function abrirPopup(sURL) {
	window.open(sURL, null, "width=400,height=395,directories=no,location=no,menubar=no,resizable=yes,scrollbars=no,status=no,titlebar=no");
	var objetoFlash = document.getElementById("objetoflash");
	if ((objetoFlash != null) && (objetoFlash != 'undefined')) {
		objetoFlash.innerHTML = '';
	}
}