var imagenRotativo=1;
var cantRotativo=4;
var sobreRotativo=false;

$().ready(function()
{	
//	window.open("/chat","chat");
	
	preloadPlantilla();
	
	$($(".BotonRotativo")[0]).addClass("RotativoSeleccionado");
	var current = 0;	
	var loaded  = 0;
	var mov = 0;
	for(var i = 1; i <=cantRotativo; ++i) {
		$('<img />').load(function(){
			++loaded;
			if(loaded == cantRotativo){
				$('.BotonRotativo').click(function(e){
					
					var $this = $(this);
					var indice=$(this).attr("indice");
					
					$(".RotativoSeleccionado").removeClass("RotativoSeleccionado");
					$(this).addClass("RotativoSeleccionado");					
					imagenRotativo=parseInt(indice)+2;					
					
					if(indice == current)
						return;

					var item = e.target.id;
					
					$('#menu > li').animate({backgroundPosition:"(975px 0)"},0).removeClass('bg1 bg2 bg3 bg4 bg5 bg6').addClass(item);
					move(mov++,item);
					if (mov == 2) {
						mov=0;
					}
						
					current = indice;
				});
			}	
		}).attr('src', 'rotativo/'+i+'b.jpg');
	}
	
	$('#controlesRotativo').mouseover(function(e){
		sobreRotativo=true;							   
	});
	
	$('#controlesRotativo').mouseout(function(e){
		sobreRotativo=false;							   
	});	
	
	pasarRotativo();
	
	$("#botonIngresar").click(function() {
		var email=$("#emailLogin").val();
		var pass=$("#passLogin").val();
		if (email != "") {
			if (pass != "") {
				var param="accion=inicio_sesion&email="+email+"&pass="+pass;
				$.ajax({
					type: "POST",
					url: "sesion.php",
					data: param,
					success: function(data) {
						if (data.indexOf("OK") == "0") {
							window.location.reload();
						} else {
							mostrarCartel(data);
							$("#emailLogin").focus();
							$("#emailLogin").select();							
						}
					}
				});		
			} else {
				$("#passLogin").focus();
			}
		} else {
			$("#emailLogin").focus();			
		}
	});
	
	$("#botonCerrar").click(function() {
		var param="accion=cerrar_sesion";
		$.ajax({
			type: "POST",
			url: "sesion.php",
			data: param,
			success: function(data) {
				if (data.indexOf("OK") == "0") {
					window.location.reload();
				} 
			}
		});		
	});	
	
	$("#emailLogin").keypress(function(e) {
		if (e.keyCode == 13) {
			$("#passLogin").focus();
		}
	});
	
	$("#passLogin").keypress(function(e) {
		if (e.keyCode == 13) {
			$("#botonIngresar").click();
		}
	});
	
	$("#aceptarTerminos").click(function() {
		var param="accion=terminos";
		$("#aceptarTerminos").hide();
		$.ajax({
			type: "POST",
			url: "cuenta_acciones.php",
			data: param,
			success: function(data) {
				if (data.indexOf("OK") == "0") {
					window.location="index.php";
				} else {
					$("#textoMensaje").html(datos);
				}
			}
		});	
	});
	
	$("#terminos").click(function() {
		var url="politicas_uso.html";
		$.fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'width'			: 750,
			'height'		: 550,					
			'href'			: url,
			'type'			: 'iframe',
		});										  
	});
	
	$("#terminosPagina").click(function() {
		var url="politicas_uso.html";
		$.fancybox({
			'padding'		: 0,
			'autoScale'		: false,
			'transitionIn'	: 'elastic',
			'transitionOut'	: 'elastic',
			'width'			: 750,
			'height'		: 550,					
			'href'			: url,
			'type'			: 'iframe',
		});										  
	});		
});

function mostrarCartel(texto) {
	url="ventana_mensaje.php?mensaje="+texto;
	$.fancybox({
		'padding'		: 0,
		'autoScale'		: false,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',	
		'width'			: 460,	
		'height'		: 155,	
		'href'			: url,
		'type'			: 'iframe',
	});			
}

function pasarRotativo() {
	if (imagenRotativo > cantRotativo) {
		imagenRotativo=1;
	}
	if (!sobreRotativo) {
		$("#bg"+imagenRotativo++).click();
	}
	setTimeout('pasarRotativo();',8000);
}

function move(dir,item){
	if(dir){
		$('.P1').stop().animate({backgroundPosition:"(0 0)"},200);
		$('.P2').stop().animate({backgroundPosition:"(-325px 0)"},300);
		$('.P3').stop().animate({backgroundPosition:"(-650px 0)"},400,function(){
			$('#menuWrapper').removeClass('bg1 bg2 bg3 bg4 bg5 bg6').addClass(item);
		});		
	}
	else{
		$('.P1').stop().animate({backgroundPosition:"(0 0)"},400,function(){
			$('#menuWrapper').removeClass('bg1 bg2 bg3 bg4 bg5 bg6').addClass(item);
		});
		$('.P2').stop().animate({backgroundPosition:"(-325px 0)"},300);
		$('.P3').stop().animate({backgroundPosition:"(-650px 0)"},200);
	}
}

function preloadPlantilla() {
	var _images = ["imagenes/plantilla/btn_cerrarsesion2.png","imagenes/plantilla/btn_inicio2.png","imagenes/plantilla/btn_play2.png","imagenes/plantilla/btn_registrarme2.png","imagenes/plantilla/btn_twitter2.png","imagenes/plantilla/btn_facebook2.png","imagenes/plantilla/btn1_la_ciudad_luz.png","imagenes/plantilla/btn2_guia_de_lugares_luz.png","imagenes/plantilla/btn3_chat_luz.png","imagenes/plantilla/btn4_la_chusma_luz.png","imagenes/plantilla/btn5_calendario_luz.png","imagenes/plantilla/btn6_noticias_luz.png","imagenes/plantilla/estado_2.png","imagenes/plantilla/btn_recuperar_pass_2.png"];
	var gotime = _images.length;
 
	$.each(_images,function(e) {
		$(new Image()).load(function() {
		}).attr('src',this);
	});	
}



