/* 
<ul class="stars">
{foreach:calificacion,c}
<li><a href="#"><img src="/img/star_{c[ok]}.png" width="17" height="17"></a></li>
{end:}
</ul>

calificar = new Calificar(servicio en /json.php, id del contenido o item a comentar);
jQuery('.stars li').bind('mouseout', function(event) { calificar.out(event); });
jQuery('.stars li').bind('mouseover', function(event) { calificar.over(event); });
jQuery('.stars li').bind('click', function(event) { calificar.click(event); });

el servicio debe aceptar el parámetro check y devolver { ok: true/false } si el usuario puede comentar o no en ese item
*/
Calificar = function(service, itemid, captcha)
{
	this.calif_desc = [
	"sin puntuaciones",
	"Deficiente",
	"Nada especial",
	"Vale la pena verlo",
	"Bastante bueno",
	"¡Impresionante!"
	];
	this.calif_clicked = null;
	this.calif_service = service;
	this.calif_itemid = itemid;
	this.done = false;
	this.callback = false;
	var obj = this;

		jQuery.getJSON("/json.php", {
		"service": this.calif_service,
		"index": this.calif_itemid,
		"check": true
		}, function(r) {
			if(!r.ok) {
				obj.done = true;
			}
		});

	this.click = function(event)
	{
		event.preventDefault();
		if(this.done) return;
		var chlds = jQuery(event.currentTarget).parent().eq(0).children();
		var index = chlds.index(event.currentTarget) + 1;
		this.calif_clicked = jQuery(event.currentTarget);
		var obj = this;


		jQuery.ajax({
		  url: "/json.php",
		  dataType: 'json',
		  context: this,
		  data: {
				"service": this.calif_service,
				"index": this.calif_itemid,
				"calif": index,
				"captcha": captcha
		  }, success: function(r) {
			jQuery(obj.calif_clicked).parent().eq(0).attr('chosen', r.calif);
			jQuery('#' + jQuery(obj.calif_clicked).parent().eq(0).attr('id') + '_msg').html(r.msg);
			jQuery(obj.calif_clicked).parent().eq(0).attr('detail', r.msg);
			if(this.callback){
					this.callback(this,r);	
			}
			obj.done = true;
		}});
		return false;
	}

	this.over = function(event)
	{
		if(this.done) return;
		var chlds = jQuery(event.currentTarget).parent().eq(0).children();
		var index = chlds.index(event.currentTarget);
		var i;
		if(typeof jQuery(event.currentTarget).parent().eq(0).attr('chosen') == 'undefined') {
			jQuery(event.currentTarget).parent().eq(0).attr('detail', jQuery(event.currentTarget).parent().find('span').html());
			jQuery(event.currentTarget).parent().eq(0).attr('chosen', 0);
			for(i = 0; i < chlds.length; i++) {
				if(chlds.eq(i).find('img').attr('src').match(/star_1.png/)) {
					jQuery(event.currentTarget).parent().eq(0).attr('chosen', i + 1);
				}
			}
		}
		jQuery('#' + jQuery(event.currentTarget).parent().eq(0).attr('id') + '_msg').html(this.calif_desc[index + 1]);
		for(i = 0; i <= index; i++) {
			chlds.eq(i).find('img').attr('src', '/img/star_1.png');
		}
		for(; i < chlds.length; i++) {
			chlds.eq(i).find('img').attr('src', '/img/star_0.png');
		}
	}
	this.out = function(event)
	{
		if(this.done) return;
		if(typeof jQuery(event.currentTarget).parent().eq(0).attr('chosen') == 'undefined') {
			return;
		}
		jQuery(event.currentTarget).parent().find('span').html(jQuery(event.currentTarget).parent().eq(0).attr('detail'));
		var chlds = jQuery(event.currentTarget).parent().eq(0).children();
		for(i = 0; i <= jQuery(event.currentTarget).parent().eq(0).attr('chosen') - 1; i++) {
			chlds.eq(i).find('img').attr('src', '/img/star_1.png');
		}
		for(; i < chlds.length; i++) {
			chlds.eq(i).find('img').attr('src', '/img/star_0.png');
		}
	}
}

