var sel = -1;
var over = -1;
$(document).ready(function(){
	
	$(".juego").attr("style","cursor: pointer;");
	$(".juego").click(function(){
		aux = $(this).attr("id");
		aux = Number(aux.replace(/[^0-9]*/g,""));
		if(aux == sel)
			return;
		if(sel != -1)
			$("#j"+sel).attr("class","juego");
			
		sel = aux;
		$(this).attr("class","juego_selected");
		over = -1;
	});
	
	$(".juego").mouseover(function(){
		aux = $(this).attr("id"); 
		aux = Number(aux.replace(/[^0-9]*/g,""));
		if(over == aux || sel == aux)
			return;
		if(over != -1)
			$("#j"+over).attr("class","juego");
		
		over = aux;
		$(this).attr("class","juego_over");
	});
	
	$("#jugar_juego").click(function(){
		if(juego_seleccionado())
		{	return;	  }
		$.ajax(
			{
		        type: "POST",
		        url: uri+"library/ajax/jugar.ajax.php",
		        dataType: "text",
		        data: "p="+sel.toString(),
		        success: function(datos)
				{
					if(datos != "")
						location.href = datos;
					else
						alert("No se pudo acceder al juego");
		        }
			});
	});
	
	$("#quitar_juego").click(function(){
		if(juego_seleccionado())
		{	return;	  }
		$.ajax(
			{
		        type: "POST",
		        url: uri+"library/ajax/quitar.ajax.php",
		        dataType: "text",
		        data: "p="+sel.toString(),
		        success: function(datos)
				{
					if(datos == "OK")
						location.href = uri+"tusjuegos.php";
					else
						alert("No se pudo quitar el juego");
		        }
			});
	});
});
function juego_seleccionado()
{
	if(sel == -1)
	{
		alert("Debes seleccionar un juego primero! Haz click sobre el para seleccionarlo");
		return true;
	}
	return false;
}
