var star = 0;
var comentario = 0;
var swap_detalle = "";

$(document).ready(function(){
	if(votFixed == 0)
	{
		$("#btn_votar").attr("style","cursor: pointer;");
		$("#e1, #e2, #e3, #e4, #e5").mouseover(function(){
			if(votFixed) return;
			star = /\d/i.exec($(this).attr("id"));
			for(i = 1; i <= star; i++)
				$("#e"+i).attr("src","../images/t1/estrellaamarilla.png");
		});
		$("#e1, #e2, #e3, #e4, #e5").mouseout(function(){
			if(votFixed) return;
			for(i = 5; i; i--)
				$("#e"+i).attr("src","../images/t1/estrellablanca.png");	
			star = 0;
		});
		$("#e1, #e2, #e3, #e4, #e5").click(function(){
			votFixed = (votFixed)? 0:1;
			if(votFixed)
				alert("Apreta el boton Votame para guardar tu voto, o click en las estrellas para corregir");
		});
		$("#btn_votar").click(function(){
			if(votFixed == 1)
			{
				$("#estrellas").html("<img src='../images/t1/loader.gif' width='16' alt=''/>&nbsp;<i>Guardando...</i>");
				$("#btn_votar").attr("src","../images/t1/votame_neg.gif");
				$("#btn_votar").removeAttr("style");
				votFixed = 2;
				$.ajax({
			        type: "POST",
			        url: uri+"library/ajax/votar.ajax.php",
			        dataType: "text",
			        data: "s="+star+"&id="+gameid,
			        success: function(datos)
					{
						$("#estrellas").html("<i>Voto Guardado</i>");
			        }
				});
				
			}
		});
		
	}
	else
	{
		$("#btn_votar").attr("src","../images/t1/votame_neg.gif");
	}
	
	/* Comentarios */
	$("#comments_cambiar").click(function(){
    	if($.browser.opera)
			$("#image_table").show();
		else
			$("#image_table").slideDown("slow");
		
		$("#comments_cambiar").html("");
    });
    
    $("#c0, #c1, #c2, #c3, #c4, #c5, #c6, #c7, #c8, #c9, #c10, #c11, #c12, #c13, #c14, #c15, #c16, #c17, #c18, #c19, #c20").click(function(){
       if($.browser.opera)
	   		$("#image_table").hide();
	   else
			$("#image_table").slideUp("slow");	
	   $("#comments_img").attr("src",$(this).attr("src"));
	   $("#comments_cambiar").html("MOSTRAR IMAGENES");
    });
    
    $("#c0, #c1, #c2, #c3, #c4, #c5, #c6, #c7, #c8, #c9, #c10, #c11, #c12, #c13, #c14, #c15, #c16, #c17, #c18, #c19, #c20").mouseover(function(){
    	$(this).removeAttr("style");
    	$(this).attr("style","background-color:	white; border: 1px dotted black;")
    });
    $("#c0, #c1, #c2, #c3, #c4, #c5, #c6, #c7, #c8, #c9, #c10, #c11, #c12, #c13, #c14, #c15, #c16, #c17, #c18, #c19, #c20").mouseout(function(){
    	$(this).removeAttr("style");
    	$(this).attr("style","border: 1px dotted white;")
    });
    
    $("textarea").focus(function(){
    	if(comentario == 0)
    		$(this).val("");
    	comentario = 1;
    });
    
    $("#comments_nombre").focus(function(){
    	$(this).select();
    });
    
    $("#comments_enviar").click(function(){
    	if($("#comments_pais option:selected").val() == 0)
    	{
    		alert("Por favor elige tu pais");
    		return;
    	}
    	if(comentario == 0 || $("textarea").val() == "")
    	{
    		alert("Por favor agrega un comentario");
    		return;
    	}
    	
    	comments_img_selected = /\d+/i.exec($("#comments_img").attr("src"));
    	$("#comments_submit").html("<img src='../images/t1/loader.gif' width='16' alt=''/>&nbsp;<i>Guardando...</i>");
    	$.ajax(
		{
	        type: "POST",
	        url: uri+"library/ajax/comentario.ajax.php",
	        dataType: "text",
	        data: "n="+$("#comments_nombre").val()+"&i="+comments_img_selected
	        	  +"&m="+$("textarea").val()+"&r="+$("input[name='comments_type']:checked").val()
	        	  +"&p="+$("#comments_pais option:selected").val()+"&id="+gameid,
	        success: function(datos)
			{
				if(datos == 0)
					$("#comments_submit").html("<i>Comentario guardado</i>");
				else
					$("#comments_submit").html("<i>No se pudo guardar tu comentario</i>");
	        }
		});
    });
    
    /*	Agregar en tu web */
    swap_detalle = $("#data").html();
    
    $("#md_desc").click(function (){
    	$("#data").html(swap_detalle);
    });
    
    $("#md_web").click(function(){
    	if($("#en_tu_web").html() == "")
    	{
    		$.ajax(
			{
		        type: "POST",
		        url: uri+"library/ajax/forweb.ajax.php",
		        dataType: "text",
		        data: "id="+gameid,
		        success: function(datos)
				{
					if(datos != "")
					{
						$("#en_tu_web").html(datos);
						$("#data").html(datos);
						forweb();
					}
		        }
			});
			return;
    	}
		$("#data").html($("#en_tu_web").html());
		$("#content_opc_fondo_sample").attr("style","width: 10px; background-color: "+$("#content_opc_fondo").val());
   		$("#content_opc_borde_sample").attr("style","width: 10px; background-color: "+$("#content_opc_borde").val());
   		$("#content_codigo :input").val($("#content_juego").html());
   		forweb();
	});	
	/*		Guardar juego		*/
	$("#guardar_juego").click(function(){
		$.ajax(
			{
		        type: "POST",
		        url: uri+"library/ajax/guardar.ajax.php",
		        dataType: "text",
		        data: "id="+gameid,
		        success: function(datos)
				{
					if(datos == "FAIL")
					{	
						alert("El juego no pudo ser guardado!");
						return ;
					}	
					if(datos == "OK")
						alert("El juego fue guardado! Buscalo en TUS JUEGOS");
					else
						if(datos == "MAX")
							alert("No puedes guardar mas juegos. Quita alguno de TUS JUEGOS");
						else
							alert("El juego no pudo ser guardado");
		        }
			});
	});
});

function forweb() {
 	$("#content_opc_fondo_sample").attr("style","width: 10px; background-color: "+$("#content_opc_fondo").val());
   	$("#content_opc_borde_sample").attr("style","width: 10px; background-color: "+$("#content_opc_borde").val());
   	$("#content_codigo :input").val($("#content_juego").html());
   
    $("#content_codigo :input").focus(function(){
    	$(this).select();
    });
    $("#content_opc_fondo").focus(function(){
    	showColorGrid2('content_opc_fondo','content_opc_fondo_sample');
    });
    $("#content_opc_borde").focus(function(){
    	showColorGrid2('content_opc_borde','content_opc_borde_sample');
    });
    $("#content_opc_cambiar").click(function(){
    	estilo = "";
    	if($("#content_opc_borde").val() != "")
    		estilo = estilo + "border: 1px dotted " + $("#content_opc_borde").val() + ";";
    	if($("#content_opc_fondo").val() != "")
    		estilo = estilo + "background-color: " + $("#content_opc_fondo").val() + ";";
    		
    	if(estilo != "")
    		$("#tabla_del_juego").attr("style",estilo);
    	else
    		$("#tabla_del_juego").removeAttr("style");
    		
    	$("#content_codigo :input").val($("#content_juego").html());
    });
}

function get_country()
{
	if(document.getElementById("comments_pais").length > 1)
		return;
	document.getElementById("comments_pais").options[0].text = "Cargando...";
	$.getJSON(uri+"library/ajax/paises.ajax.php",
			  function(data)
			  {  
       			document.getElementById("comments_pais").remove(0);
			  	$.each(data.items, function(i,item)
       			{  
				   document.getElementById("comments_pais").options[i] = new Option(item.text,item.value);  
       			});  
     		  });
}