var votado=0;
var msg = new Array("<?php echo MSG_CRITIC_REGISTRADO ?>","<?php echo MSG_CRITIC_CORRECTO ?>","<?php echo MSG_CRITIC_VOTADO ?>");

var actual;
var timeSlider=10000;
var timeHSHD=7000;
var notin;
var moving=false;
var autoMove=6;
var speed=1250;

$(".btn").live("click",function(){
	if(votado==0){
	votado=1;
	var critic_id;
	var critic_film;
	var datos=new Array();
	if($(this).attr("class")=="btn okay"){
		vote=1;
	}
	if($(this).attr("class")=="btn no_way"){
		vote=0;
	}
	critic_id=$("#critic_id").val();
	jQuery.ajax({
	type: 	'POST',
	data: {'vote':vote,'critic':critic_id},
	url:	'ws/oknoway.php',
	success:  	function(code){
					var noway;
					var ok;

					datos=code.split("*");
					if(datos[0]==1){
					total=parseInt(datos[1])+parseInt(datos[2]);
					if(datos[1]>0 && total>0){
						ok=(datos[1]/total)*100;
					}else{
						ok=0;
					}
					if(datos[2]>0 && total>0){
						noway=(datos[2]/total)*100;
					}else{
						noway=0;
					}
					$("#numNoway").html(Math.round(noway)+"% No way");
					$("#numOk").html(Math.round(ok)+"% Okay");
					$("#numNoway").fadeIn("slow");
					$("#numOk").fadeIn("slow");
					}else{
						num=parseInt(datos[0]);
						if(num==0){
							$("#lb_log").click();
						}else{
							alert(msg[num]);
						}
						votado=0;
					}
				}
	});
	}
});
$(document).ready(function(){
	/*$("#slider").easySlider({
		auto: true,
		continuous: true,
		pause: 7000,
		speed: 1000
	});*/
	$(".next_critic").live("click",function(event){
	event.preventDefault();
	var critic_id=$("#critic_id").val();
	var film_id=$("#critic_film").val();
	jQuery.ajax({
		type: 	'POST',
		data: {'critic':critic_id,'film_id':film_id},
		url:	'ws/onecritic.php',
		success:  	function(code){
						$("#oneCritic").fadeOut("normal",function(){
							$("#oneCritic").html(code);
						});
						$("#oneCritic").fadeIn();
					}
		});
	});
	
	/*$("a").focus(function(){
		$(this).blur();
	});*/
	//intervalUpdate=setTimeout("update('.main_list_news')", timeHSHD);

	$("#nextBtn").live("click",function() {
		clearTimeout(intervalMove);
		autoMove=0;
		move(".avmSlider");
	});
	
	intervalMove=setTimeout("move('.avmSlider')", timeSlider);
	
});

function update(obj){
			jQuery.ajax({
				type: 	'POST',
				data: {'id':actual},
				url:	'/ws/hoySeHaDicho.php',
				success:  	function(code){
								var valores = new Array();
								valores=code.split("|");
								$(obj+" li:last").fadeOut("normal",function(){
								$(obj+" li:first").before(valores[1]);
								actual=valores[0];
								$(obj+" li:not(:first)").removeClass("update");
									$(obj+" li:first").fadeIn("normal",function(){
										$(obj+" li:last").remove();
										clearTimeout(intervalUpdate);
										intervalUpdate=setTimeout("update('.main_list_news')", timeHSHD);
									});
								});
								}
							});
								
}

function move(obj){
	//alert("a");
	if(moving==false){
		moving=true;
		$("#nextBtn").html('<a onclick="return false" title="" href="#">Siguiente estreno <img style="position:absolute;margin:-6px 0 0 -2px;" alt="" src="/img/ajaxL4.gif"></a>');
		jQuery.ajax({
			type: 	'POST',
			data: {'notin':notin},
			url:	'/ws/highlighteds.php',
			success:  	function(code){
				var valores = new Array();
				valores=code.split("|");
				if(valores[0]=="OK"){
					notin=valores[1];
					code=valores[2];
					$(obj).append(code);
					
					var objImagePreloader = new Image();
					objImagePreloader.visible=false;
					objImagePreloader.onerror = function() {
						clearTimeout(intervalMove);
						moving=false;
						$(obj+" li:last").remove();
						//$("#nextBtn").html('<a onclick="return false" title="siguiente pelicula" href="#">Siguiente estreno <img alt="" src="/img/next.png"></a>');
						//notin=valores[1];
						//valores=null;
						move(".avmSlider");
						//moving=false;
						return 0;
					}
					objImagePreloader.onload = function() {
					//$(obj+' li:eq(1)')
						$(obj).animate({
							left: '-=950'
						}, speed, function() {
							$(obj+" li:eq(0)").remove();
							$(obj+".avmSlider").css("left","0");
							moving=false;
							clearTimeout(intervalMove);
							if(autoMove>0){
								intervalMove=setTimeout("move('.avmSlider')", timeSlider);
								autoMove-=1;
							}
							$("#nextBtn").html('<a onclick="return false" title="siguiente pelicula" href="#">Siguiente estreno <img alt="" src="/img/next.png"></a>');
							$(".main_data_box").fadeIn("slow");
						});
					}
					objImagePreloader.src = $(obj+" .preload:last").attr("src");
				}
			}
		});
	}
}
	
	function delay(millis){
		var date = new Date();
		var curDate = null;

		do { curDate = new Date(); }
		while(curDate-date < millis);
	}
