/*
$.fn.clouds = function(settings) {
	settings = $.extend({
		clouds: [],
		folder: '/',
		speed: 2,
		opacityStep: 0.02,
		fader: true
	}, settings);
	return this.each(function(n, item) {
		var self = $(item);
		$.each(settings.clouds, function(c, cloud) {
			var cImg = new Image();
			$(cImg).load(function() {
				var cDiv = $('<div class="nuvem"></div>');
				cDiv.css({
					top: cloud.y+"px",
					left: cloud.x+"px",
					width: cImg.width+"px",
					height: cImg.height+"px",
					backgroundImage: 'url('+this.src+')',
					zIndex: 2
				});
				self.append(cDiv);
				var o = settings.opacityStep;
				cloud.currentO = Math.random();
				cDiv.css({opacity: cloud.currentO});
				window.setInterval(function() {
					if (settings.fader) {
						if (cloud.currentO > 1.0) o = -settings.opacityStep;
						if (cloud.currentO < 0.3) o = +settings.opacityStep;
						cloud.currentO += o;
						cDiv.css({opacity: cloud.currentO});
					}
					var currentLeft = parseInt(cDiv.css("left"));
					cDiv.css({left: (currentLeft - cloud.speedX)+'px'});
					if (cDiv.offset().left > $(document).width() - self.offset().left) {
						var s = -cImg.width;
						cDiv.css({left: s+"px"});
					}
				}, settings.speed);
			});
			cImg.src = settings.folder + cloud.src;
		});
	});
}	
*/
$(function() {
	$("#btmes, #btweek, #bthoje").click(function(){
		var tipo = $(this).attr("id");
		if(tipo== 'btmes')
			$('#usertxt').html('Usuários mais populares deste męs');
		if(tipo== 'btweek')
			$('#usertxt').html('Usuários mais populares desta semana');
		if(tipo== 'bthoje')
			$('#usertxt').html('Usuários mais populares de hoje');
												
		$.ajax({
			type:'POST',
			url: 'js/ajax/retornaPopular.php',
			data: 'tipo='+tipo,
			beforeSend: function(){
				$("#userPopular").html("<center style='padding-top:40px'><img src='imgs/ajaxload.gif' style='width:186px; border:0px'></center>");
			},
			success: function(result){
				$("#userPopular").html(result);
			}
		});
	});

	$("#btAtualizaUp").click(function(){
		$.ajax({
			type:'POST',
			url: 'js/ajax/montaPainelHome.php',
			data: 'tipo=ultimasp',
			beforeSend: function(){
				$("#btAtualizaUp").fadeOut(200);
				$("#ultimasPerg").fadeIn(400).html("<center style='padding-top:100px'><img src='imgs/ajaxload.gif' style='width:186px; border:0px'></center>");
			},
			success: function(result){
				$("#btAtualizaUp").fadeIn(200);				
				$("#ultimasPerg").fadeIn(400).html(result);
			}
		});
	});

	$("#txtUsuario").focus();
    $('#recadastraSenha').validate({
		submitHandler: function(form) {  
			$(form).ajaxSubmit({  
				url:       "js/ajax/recuperaSenha.php",
				type:      'post',
				beforeSubmit:    function() { 
					$(".recadastraSenha").fadeOut(200, function(){
						$(this).fadeIn(400).html("<center style='padding-top:10px'>Verificando o Login<br><img src='imgs/ajaxload.gif' style='width:186px; border:0px'></center>");
					});
					
				},			
				success:    function(result) { 
					$(".recadastraSenha").fadeOut(200, function(){
						$(this).fadeIn(400).html(result);
					});
				} 					
				
			});  
		},
		onblur: true,
        // define regras para os campos
        rules: {
            txtUsuarioEmailRecadastra: {
                required: true,
                minlength: 3,
				email: true
            }
        }
    });	

	$('#formLogin').validate({
		submitHandler: function(form) {  
			$(form).ajaxSubmit({  
				type:'POST',
				url: 'js/ajax/login.php',
				beforeSend: function(){
					$("#formLogin").html("<center style='padding-top:50px'>Verificando o Login<br><img src='imgs/ajaxload.gif' style='width:186px; border:0px'></center>");
				},
				success: function(result){
					if(result!='erro'){
						window.location='./painel/';
					}
					else{
						$("#formLogin").html("<p style='text-align:center; color:#ff0000;padding-top:50px'>Usuário ou senha inválidos</p>");
						setTimeout(function(){
							$("#formLogin").fadeIn(400).html('<div style="margin-top:10px; border-bottom:1px solid #eeeeee; padding-bottom:5px; overflow:auto""><label for="txtUsuarioNome" class="label">Login: </label> <input type="txt" name="txtUsuario" id="txtUsuario" class="inputtxt"/></div><div style="margin-top:5px; margin-bottom:10px; border-bottom:1px solid #eeeeee; padding-bottom:5px; overflow:auto"><label for="txtUsuarioSenha" class="label">Senha: </label> <input type="password" name="txtUsuarioSenha" id="txtUsuarioSenha" class="inputtxt"/></div><input type="submit" value="Entrar" id="btHomeLogin" class="button"/>');
							$("#txtUsuario").focus();
						},3000);
					}
				}
			});  
		},
		onblur: false,
        rules: {
            txtUsuario: {
                required: true,
                minlength: 3
            },
            txtUsuarioSenha: {
                required: true,
                minlength: 3
            }
        },
        messages: {
            txtUsuario: "* Preencha o login",
            txtUsuarioSenha: "* Preencha a senha"
        }
    });
	
	$("#btCadastro, .btCadastro").click(function(){
		window.location="http://www.meresponda.com.br/cadastro/";
	});
	
	$("#btAtualizaOnline").click(function(){
		$.ajax({
			type:'POST',
			url: 'js/ajax/montaPainelHome.php',
			data: 'tipo=usariosOnline',
			beforeSend: function(){
				$("#usariosOnline").fadeIn(400).html("<center style='padding-top:50px'><img src='imgs/ajaxload.gif' style='width:186px; border:0px'></center>");
			},
			success: function(result){
				$("#usariosOnline").fadeIn(400).html(result);
			}
		});
	});

	$("#btAtualizaSeguidos").click(function(){
		$.ajax({
			type:'POST',
			url: 'js/ajax/montaPainelHome.php',
			data: 'tipo=usariosSeguidos',
			beforeSend: function(){
				$("#usariosSeguidos").fadeIn(400).html("<center style='padding-top:50px'><img src='imgs/ajaxload.gif' style='width:186px; border:0px'></center>");
			},
			success: function(result){
				$("#usariosSeguidos").fadeIn(400).html(result);
			}
		});
	});
	$("#nuvens").clouds({
		clouds: [
			{src:'cloud_1.png',x:-1,y:20,speedX:-1},
			{src:'cloud_2.png',x:-250,y:100,speedX:-2},
			{src:'cloud_3.png',x:150,y:350,speedX:-3},
			{src:'cloud_4.png',x:320,y:250,speedX:-1},
			{src:'cloud_4.png',x:0,y:300,speedX:-1},
			{src:'cloud_5.png',x:820,y:210,speedX:-2.5},
			{src:'cloud_6.png',x:720,y:300,speedX:-1.5},
			{src:'cloud_7.png',x:1000,y:400,speedX:-1},
			{src:'cloud_7.png',x:-300,y:400,speedX:-1}
		],
		folder: 'imgs/',
		speed: 100,
		fader: !$.browser.msie
	});
	
/*
	$.ajax({
		type:'POST',
		url: 'js/ajax/montaPainelHome.php',
		data: 'tipo=ultimasp',
		beforeSend: function(){
			$("#ultimasPerg").fadeIn(400).html("<center style='padding-top:100px'><img src='imgs/ajaxload.gif' style='width:186px; border:0px'></center>");
		},
		success: function(result){
			$("#ultimasPerg").fadeIn(400).html(result);
		}
	});
	
	$.ajax({
		type:'POST',
		url: 'js/ajax/montaPainelHome.php',
		data: 'tipo=usariosSeguidos',
		beforeSend: function(){
			$("#usariosSeguidos").fadeIn(400).html("<center style='padding-top:50px'><img src='imgs/ajaxload.gif' style='width:186px; border:0px'></center>");
		},
		success: function(result){
			$("#usariosSeguidos").fadeIn(400).html(result);
		}
	});

	$.ajax({
		type:'POST',
		url: 'js/ajax/montaPainelHome.php',
		data: 'tipo=usariosOnline',
		beforeSend: function(){
			$("#usariosOnline").fadeIn(400).html("<center style='padding-top:50px'><img src='imgs/ajaxload.gif' style='width:186px; border:0px'></center>");
		},
		success: function(result){
			$("#usariosOnline").fadeIn(400).html(result);
		}
	});

	$.ajax({
		type:'POST',
		url: 'js/ajax/retornaPopular.php',
		data: 'tipo=bthoje',
		beforeSend: function(){
			$("#userPopular").html("<center style='padding-top:40px'><img src='imgs/ajaxload.gif' style='width:186px; border:0px'></center>");
		},
		success: function(result){
			$("#userPopular").html(result);
		}
	});
	*/
	
});