//
//  public/js/fucntions.php
//  criado por Peewee em 30/05/2011
//	modificado por Peewee em 30/05/2011
//
//
(function($){
	
	var URL = "/Angelino/site/";
	var _total = 0;
	var _time = null;
	var _atual = 0;
	 
				
	$.fn.extend({
		//fade in imagens
		loadImageFade: function ()
		{
			return this.each(function ()
			{
				var el = $(this);
				el.load(function ()
				{
					$(this).fadeIn (200);
				});
			})
		},
		//bt voltar
		btVoltar: function ()
		{
			return this.each(function ()
			{
				var el = $(this);
				el.hover(function ()
				{
					$(this).animate({
						marginLeft	: -45
					}, 300);
				}, function ()
				{
					$(this).animate({
						marginLeft	: -33
					}, 300)
				});
			})
		},
		//moviementa as nuvens
		moveClouds: function ()
		{
			var el = $(this);
			
			el.mousemove (function (e)
			{
				var center = $(this).width()/2;
				var mouseX = e.pageX;
				var dif = center - (mouseX - center) / 10;
				$("div[id='main']").css("backgroundPosition",dif+"px 0px");
				//descomente próxima linha para verificar os valores
				// $("#main").text (center+" / "+ mouseX +" / "+dif);
			});
		},
		//limpa input
		cleanInput: function ()
		{
			return this.each(function()
			{
				var el = $(this);
				var text = el.val();
				el.focusin (function ()
				{
					var t = $(this).val();
					if (t == text)
					{
						$(this).val("");
					}
				});
				
				el.focusout (function ()
				{
					var t = $(this).val();
					if (t == "")
					{
						$(this).val(text);
					}
				});
			})
		},
		//navegacao da vitrine
		vitrineCarousel: function ()
		{
			//ajsute para não dar erro
			_timer = setTimeout("$(this).nextDestaque()", 9999);
			//
			var el = $(this);
			var first = el.children("ul").children("li:first") 
			//ativa primeiro bt
			first.addClass("active");
			first.setDestaque();
			//cria click
			el.children("ul").children("li").each(function (i)
			{
				_total = i;
				var li = $(this);
				li.click (function ()
				{
					$(this).setDestaque();
				});
			});
		},
		//troca o destaque
		setDestaque: function ()
		{
			clearTimeout(_timer);
			//ajusta botoes
			$(this).parent("ul").children("li").removeClass("active");
			$(this).addClass("active");
			_atual = $(this).prevAll("li").length;
			//
			$("div[class='container']").animate({ 
				marginLeft: -(890*_atual) 
			},500);
			//ative timer
			_timer = setTimeout("$(this).nextDestaque()", 12000);
		},
		//ativa proximo destaque
		nextDestaque: function ()
		{   	
			//
		    var _li = $(this);
		    var _next = _atual + 1;
		    if (_next > _total){
		        _next = 0;
		    }
		   	_li.removeClass("active");
		    
		    $dest = $("div[id='vitrine'] ul li:eq("+_next+")")
		    $dest.addClass("active");
		       
		    $dest.setDestaque ();
		},
		//mostra brincadeira
		showBrincadeira: function ()
		{
			
			return this.each(function ()
			{
				var el = $(this);
				
				/*el.click (function ()
				{
					
					$("div[class='item']:visible").slideUp(200);
					$(this).parent().children("div[class='item']:first").stop().slideDown(500);
					
				});*/
			});
			
		},
		//mostra texto colunistas
		scrollColunista: function ()
		{
			return this.each(function()
			{
				var el = $(this);
				
				// abaixa todos os info
				var target = el.parent().children("div[class='info']");
				var altura = target.height();
				el.attr("h",altura);
				target.css({height: "70px"});
				//

				el.click(function ()
				{
					//remove abertura antiga
					var atual = $(this).hasClass("active")
					var ativo = $("a[class*='scroll_colunista'][class*='active']");
					if (ativo && !atual)
					{
						ativo.removeClass("active");
						var target = ativo.parent().children("div[class='info']");
						target.animate(
							{
								height: "70px"
							},500);
					}				
					//ativa abertura atual
					target = $(this).parent().children("div[class='info']");
					if (atual)
					{
						var altura =  70;	
						$(this).removeClass("active");
					}
					else
					{
						altura = $(this).attr("h")
						$(this).addClass("active");
					}
					
					target.animate(
						{
							height: altura+"px"
						},500);
					return false;
				});				
			});
		},
		//troca formulario de contato
		menuContato: function ()
		{		
			
			//ativa primerio form
			$("div[class='form']:first").addClass("active").show();
				
			return this.each(function()
			{

				var el = $(this);
				
				el.click (function ()
				{
					//limpa retorno
					$("div[class='retorno']").text("");
					//limpa fields vermelhos
					$("input[class='empty'], textarea[class='empty']").removeClass("empty");
					//antigo
					$("div[class*='form'][class*='active']").removeClass("active").hide();
					$("ul[class*='menu_contato'] li[class*='active']").removeClass("active");
					//novo
					$(this).addClass("active");
					var p = $(this).prevAll("li").length;
					$("div[class='form']:eq("+(p)+")").addClass("active").fadeIn(500);
					
				});
			});
		},
		//envia o contato
		enviaContato: function ()
		{
			return this.each(function()
			{
				var el = $(this);
				
				el.submit (function (){
					//esconde erro
					$("div[class='retorno']").hide();
					//erros iniciais
					var error = false;
					//captura inputs
					var fields = el.serializeArray();
					//verifica campos
					jQuery.each(fields, function(i, field){
						//captura imput
						campo = el.children("div[class='campo']").children("input[name='"+field.name+"'], textarea[name='"+field.name+"']");
						//remove vermelho dos campos 
						campo.removeClass("empty");
						//verifica
				        if (field.value == "")
				        {
				        	$("div[class='retorno']").html("preencha todos os campos").fadeIn(200);
				        	campo.addClass("empty");
				        	error = true;
				        }
				    });
				    
				    if (error == false)
				    {
				    	return true;
				    }
				    
				    return false;
				});
			});
		}
	});
})(jQuery);
