$(function(){

	$("#imgFechar").click(function(){
		$(".aviso").hide();
	});

	var kidA = parseInt($(document).height()) / 2 - 150;
	var kidB = parseInt($(document).height()) / 2 - 250;
	var kidC = parseInt($(document).height()) / 2 - 200 ;
				
	var boxContato = parseInt($(document).height()) / 2 + 160;	

	if( $(window).height() < 600 ){
		$("#box-principal").height( 600+"px" );
	} else {
		$("#box-principal").height( $(document).height() + "px" );
	}

	$(window).resize(function(){


		var kidA = parseInt($(document).height()) / 2 - 150;
		var kidB = parseInt($(document).height()) / 2 - 250;
		var kidC = parseInt($(document).height()) / 2 - 200 ;
				
		var boxContato = parseInt($(document).height()) / 2 + 160;	

		if( $(window).height() < 600 ){
			$("#box-principal").height( 600+"px" );
		} else {
			$("#box-principal").height( $(document).height() + "px" );
		}		

	});

	
	if( $("#box-conteudo").length > 0 ){
		var x = $(window).height() / 2 - $("#box-conteudo").height() / 2;
		if( $(document).height() < 700 ) x = 0;
		$("#box-conteudo").animate({ "top":x+"px" }, 500);	
	}

	if( config.pageId == "kid-beeruta-home" ){
		
		$("#kid-beeruta-jardim, #kid-beeruta-do-campo, #kid-beeruta-lounge").height($(window).height());
		
		$(window).resize(function(){
			$("#kid-beeruta-jardim, #kid-beeruta-do-campo, #kid-beeruta-lounge").height($(window).height());
		});		
		
	} else {
		
		$("#kid-beeruta-jardim, #kid-beeruta-do-campo, #kid-beeruta-lounge").hide().height($("body").height());
		$("#box-conteudo").css({ "opacity":0 }).hide();
		
		window.setTimeout(function(){
			
			$("#"+config.pageId).css({ "left":"0px", "opacity":0 }).show().animate({ "opacity":1 }, 1000, function(){
						
				if( $(".collection .mini").length == 1 )
					$(".collection .mini").hide();
				
				if( $(".collection .mini").length > 4 )
					$("#mini-left, #mini-right").show();
				
				if( $(".collection .mini").length > 0 )
					$("#fancybox a").fancybox();
				
				$("#box-conteudo").show().animate({ "opacity":1 }, 1000, function(){
					mini.ini();
				});
				
				$("#kid-a, #kid-b, #kid-c").unbind("click").bind("click", function(){
					$("#"+ config.pageId +", #box-conteudo").animate({ "opacity":0 }, 1000, function(){
						$("#kid-beeruta-jardim, #box-conteudo").hide();
						window.setTimeout(function(){
							location.href = "/";
						}, 500);
					});
				});
				
			});
		
		}, 300);
		
	}
	
	var a = $("#kid-a");
	var aanimate = 0;
	a.animate({ "top": kidA +"px" }, 2000, function(){
		a.mouseover(function(){
			if( aanimate == 0 ){
				aanimate = 1;
				if( $(".a-subiu").length == 0 ){
					var yA = a.offset().top - 50;
					a.addClass("a-subiu");
				} else {
					var yA = a.offset().top + 50;
					a.removeClass("a-subiu");
				}
				
				a.animate({ "top": yA +"px" }, 1000, function(){
					aanimate = 0;
				});
			}
			
		});
	});
	
	var b = $("#kid-b");
	var banimate = 0;
	b.animate({ "top": kidB +"px" }, 2000, function(){
		b.mouseover(function(){
			if( banimate == 0 ){
				banimate = 1;
				if( $(".b-subiu").length == 0 ){
					var yB = b.offset().top - 50;
					b.addClass("b-subiu");
				} else {
					var yB = b.offset().top + 50;
					b.removeClass("b-subiu");
				}
				
				b.animate({ "top": yB +"px" }, 1000, function(){
					banimate =0;
				});
			}
		});
	});
	
	var c = $("#kid-c");
	var canimate = 0;
	c.animate({ "top": kidC +"px" }, 2000, function(){
		c.mouseover(function(){
			if( canimate == 0 ){
				canimate = 1;
				if( $(".c-subiu").length == 0 ){
					var yC = c.offset().top - 50;
					c.addClass("c-subiu");
				} else {
					var yC = c.offset().top + 50;
					c.removeClass("c-subiu");
				}
				c.animate({ "top": yC +"px" }, 1000,function(){
					canimate = 0;
				});
			}
		});
		
		if( config.pageId == "kid-beeruta-home" ){
		
			$("#abelha, #contato").css("opacity",0);
			$("#box-contato").css( "top", boxContato+"px").show();
			$("#abelha").animate({ "opacity":1 }, 500, function(){
				
				var contato = $("#contato");
				contato.animate({ "opacity":1 }, 300, function(){
					$("#overlay").css({ "opacity":0.5, "width":$(document).width()+"px", "height":$(document).height()+"px" });
					
					var posX = ($(document).width() / 2 - $("#para-contato").width() / 2);
					var posY = ($(document).height() / 2 - $("#para-contato").height() / 2);
					
					$("#para-contato").css({ "opacity":0.0, "top":posY+"px", "left":posX+"px" });
									
					contato.unbind("click").bind("click", function(){
						$("#overlay").show();
						$("#para-contato").show();
						
						$("#para-contato").animate({ "opacity":1 }, 1000, function(){
						
							$("#btn-fechar").unbind("click").bind("click", function(e){
							
								e.stopPropagation();
								e.preventDefault();
								
								$(".erro").removeClass("erro");
								$("#para-contato").animate({ "opacity":0.0 }, 1000, function(){
									$("#para-contato").hide();
									$("#overlay").hide();
									$("#nome, #email, #mensagem").val("");
									$(".msg-success").remove();
									$(".action").show();
								});													
							});
							
						});
					});
					
					var form = $("#mandar-contato");
					form.unbind("submit").bind("submit", function(){
										
						var formData = form.serializeArray();
						
						$(".erro").removeClass("erro");
						
						$.each(formData, function(i, field){
							
							var it = $("#"+field.name);
							if( field.value == "" ) it.addClass("erro");
							
						});
						
						if( $(".erro").length == 0 ){
							
							$(".action").hide();
							$(".action").after("<p class='msg-sending'>Enviando mensagem...</p>");
							
							$.ajax({
								url: form.attr("action"),
								type: "post",
								data: formData,
								success: function( retorno ){
									
									$(".msg-sending").remove();
									if( retorno == 1){
										$("#nome, #email, #mensagem").val("");
										$(".action").after("<p class='msg-success'>Mensagem enviada com sucesso!</p>");
									}
								}
							});
						}
						
						return false;
					});
					
				});
				
			});
		}
	});
		
	a.unbind("click").bind("click", function(){
		if( config.itemOpened == false ){
			$("#box-contato").hide();
			$("#kid-beeruta-do-campo, #kid-beeruta-lounge").animate({ "opacity":0 }, 1000, function(){
				window.setTimeout(function(){
					location.href = "kid-beeruta-jardim.html";
				}, 500);
			});		
		}
	});
	
	b.unbind("click").bind("click", function(){
		if( config.itemOpened == false ){
			$("#box-contato").hide();
			$("#kid-beeruta-jardim, #kid-beeruta-lounge").animate({ "opacity":0 }, 1000, function(){
				window.setTimeout(function(){
					location.href = "do-campo/novidades.html";
				}, 500);	
			});
		}
	});	
	
	c.unbind("click").bind("click", function(){
		if( config.itemOpened == false ){
			$("#box-contato").hide();
			$("#kid-beeruta-jardim, #kid-beeruta-do-campo").animate({ "opacity":0 }, 1000, function(){
				window.setTimeout(function(){
					location.href = "kid-beeruta-lounge.html";
				}, 500);
			});
		}
	});		
	
	$("#menu ul li a.submenu").unbind("click").bind("click", function(e){
		e.preventDefault();
		e.stopPropagation();
	});
	
	$("#menu ul li a.submenu").mouseover(function(){
		
		var it = $(this);
		var rel = it.attr("rel");
		if( rel ) $("#"+rel).show();
			
		$(".sub").mouseover(function(){
			$(this).show();
			it.addClass("over");
		}).mouseout(function(){
			if( !$(".open").length > 0 )
				$(this).hide();
			it.removeClass("over");
		});
		
	}).mouseout(function(){
		
		var it = $(this);
		var rel = it.attr("rel");
		
		if( rel )
			if( !$(".open").length > 0 )
				$("#"+rel).hide();
		
		$(".sub").mouseover(function(){
			$(this).show();
			it.addClass("over");
		}).mouseout(function(){
			if( !$(".open").length > 0 )
				$(this).hide();
			it.removeClass("over");
		});
	
	});	
		
});

var mini = {

	totalSize : 0,
	
	position: 0,
	
	duration: 200,
	
	animating: false,
	
	pages: 0,
	
	ini: function(){
		mini.pages = Math.round( $(".collection .mini").length / 4 );
		mini.totalSize = (($(".collection .mini").width() * $(".collection .mini").length) / mini.pages);		
		mini.go();
	},
	
	get: function(){
		mini.position = parseInt($(".collection").css("left").replace("px",""));	
	},
	
	go: function(){
		
		$("#mini-left").unbind("click").bind("click", function(){
			
			mini.get();
			
			if( mini.animating == false && mini.position < 0 ){
				mini.animating = true;
				mini.position += 48;
				$(".collection").animate({ "left":mini.position+"px" }, mini.duration, function(){
					mini.animating = false;
				});
			}
		});
	
		$("#mini-right").unbind("click").bind("click", function(){
			
			mini.get();
			
			if( mini.animating == false && (-1 * mini.totalSize) < mini.position ){
				mini.animating = true;
				mini.position += 48 * -1;
				
				$(".collection").animate({ "left":mini.position+"px" }, mini.duration, function(){
					mini.animating = false;
				});
			}
		});	
		
		$(".mini").unbind("click").bind("click", function(){
		
			var $_this = $(this);
			var newIndex = $_this.index();
			var oldIndex = $(".foto img.first").index();
		
			if( mini.animating == false ){
				mini.animating = true;
				$(".foto img").eq(oldIndex).animate({ "opacity":0 }, 500, function(){
					$(".foto img").eq(oldIndex).css({ "opacity":0 }).removeClass("first");
					$(".foto img").eq(newIndex).addClass("first").animate({ "opacity":1 }, 500);
					$("#fancybox a").hide();
					$("#fancybox a").eq(newIndex).show();
					mini.animating = false;
				});
			}
		});
		
	}

}

