var nValoracion = 0;

window.onload = function()
{
	var aEstrellas;
	var oValoracion;

	if ( document.getElementById )
	{
		oValoracion = document.getElementById( 'valoracion' )
		if ( oValoracion )
		{
			aEstrellas = oValoracion.getElementsByTagName( 'div' );
			// Preparar las estrellas de la valoración
			for ( i = 0; i < aEstrellas.length; i++ )
			{
				if ( aEstrellas[ i ].id != 'comentarios' )
				{
					// Escuchadores de eventos
					aEstrellas[ i ].onclick = votar;
					aEstrellas[ i ].onmouseout = ocultar;
					aEstrellas[ i ].onmouseover = mostrar;
					// Estado
					aEstrellas[ i ].className = 'desmarcado' // nValoracion > i ? 'seleccionado' : 'desmarcado';
				}
			}
		}
	}
}

// Marcar las estrellas hasta la actual y ocultar las siguientes
function mostrar( event )
{
	for ( i = 1; i < 6; i++ )
		if ( document.getElementById( 'valor_' + i ) )
			document.getElementById( 'valor_' + i ).className = i <= this.id.split( '_' ).pop() ? 'marcado' : 'desmarcado' //( nValoracion >= i ? 'seleccionado' : 'desmarcado' );
		
}

// Ocultar todas las estrellas
function ocultar( event )
{
	for ( i = 1; i < 6; i++ )
		if ( document.getElementById( 'valor_' + i ) )
			document.getElementById( 'valor_' + i ).className = 'desmarcado' //( nValoracion >= i ? 'seleccionado' : 'desmarcado' );
}

// Anotar la votación
function votar( event )
{
	// Control de estrellas
	var aEstrellas = this.parentNode.getElementsByTagName( 'div' );
	for ( i = 0; i < aEstrellas.length; i++ )
	{
		// Eliminar los escuchadores de eventos
		aEstrellas[ i ].onclick = nada;
		aEstrellas[ i ].onmouseout = nada;
		aEstrellas[ i ].onmouseover = nada;
		// Dejar marcadas las estrellas pertinentes
		if ( aEstrellas[ i ].className == 'marcado' )
			aEstrellas[ i ].className = 'seleccionado';
		aEstrellas[ i ].cursor = 'default';
	}
	// Mostrar mensaje de agradecimiento
	if ( document.getElementById( 'mensaje' ) )
	{
		document.getElementById( 'mensaje' ).innerHTML = 'Gracias por su voto:';
		document.getElementById( 'mensaje' ).style.marginRight = 27 + 'px';
	}
	// Almacenar el voto
	var oPagina = false;
	if ( window.XMLHttpRequest )		// Firefox
		oPagina = new XMLHttpRequest ();
	else if ( window.ActiveXObject )	// IE
	{
		try
		{
			oPagina = new ActiveXObject ( 'Msxml2.XMLHTTP' );
		}
		catch ( e )
		{
		}
	}
	else
		return oPagina;

	//oPagina.onreadystatechange = function () { avisar( oPagina ); }
	if ( document.valoracion )
	{
		if ( document.valoracion.conferencia )
		{
			oPagina.open ( 'GET', 'votar.asp?conferencia=' + document.valoracion.conferencia.value + '&valoracion=' + this.id.split( '_' ).pop(), true ); // asignar los métodos open y send
			oPagina.send ( null );
		}
	}
}

function nada() {}

function avisar( oPagina )
{
	// ¿El mensaje de agradecimiento podría ir aquí?
	//if ( oPagina.readyState == 4 && ( oPagina.status == 200 || window.location.href.indexOf ("http") == - 1 ) )
	//	alert( oPagina.responseText )
}