/**
 * @author Marcelo "Ganryu" Ramos
 */



$(document).ready(function(){

	(function($) {
	  var cache = [];
	  // Arguments are image paths relative to the current page.
	  $.preLoadImages = function() {
	    var args_len = arguments.length;
	    for (var i = args_len; i--;) {
	      var cacheImage = document.createElement('img');
	      cacheImage.src = arguments[i];
	      cache.push(cacheImage);
	    }
	  }
	})(jQuery)
	
	jQuery.preLoadImages("img/img_clientes.gif");
	
	var divNumber = 1;
	var readyToStack = true;
	var stopStacking = false;
	
	var hypnoPag = true;
	var clientesPag = false;
	var faleconoscoPag = false;

	
	var totalNum = 7;
	var rndNum = Math.floor(Math.random() * totalNum);
	

	var arrayTab = [0,1,2,3,4,5,6,7,8];
	var atualTab = arrayTab;
	atualTab.sort(function() {return 0.5 - Math.random()});
	var numBIndex = 0;
	
	$("body").css("background-image","url(img/background_0" + rndNum  + ".gif)");
	
	$("#box0").load("tabela.html table#tabela", function(){
		$('a',this).css('display','block');
		/*$('a',this).hover(function() {
			$('p',this).slideDown('fast');
		},function() {
			$('p',this).slideUp('fast');
		});*/
	});
	
	$('.btHypno').click(function() {
		$("a.active").removeClass('active');
		$(this).addClass('active');
		$('html, body').animate({scrollTop:0}, 'slow');
		$(".formError").fadeOut(150,function(){
			$(this).remove();
		});
		$(".ajaxSubmit").fadeOut(150,function(){
			$(this).remove();
		});
		if(clientesPag){
			$('#clienteBox').slideUp('slow', function(){
				$(this).remove();
			});
			clientesPag = false;
		}
		if(faleconoscoPag){
			$('#formBox').slideUp('slow', function(){
				$(this).remove();
			});
			faleconoscoPag = false;
		}
		pageTracker._trackPageview('index.html');
	});
	$('.btClientes').click(function() {
		$("a.active").removeClass('active');
		$(this).addClass('active');
		$('html, body').animate({scrollTop:0}, 'slow');
		$(".formError").fadeOut(150,function(){
			$(this).remove();
		});
		$(".ajaxSubmit").fadeOut(150,function(){
			$(this).remove();
		});
		if(!clientesPag){
			
			if(faleconoscoPag){
				$('#formBox').slideUp('slow', function(){
					$(this).remove();
				});
			}
			$('<div id="clienteBox"></div>').insertBefore($('#mainContentBox'));
			$('#clienteBox').hide().load("clientes.html div.clientesBox", function(){
				$(this).slideDown('slow');
			});
			faleconoscoPag = false;
			clientesPag = true;
		}
		pageTracker._trackPageview('clientes.html');
	});
	$('.btCases').click(function() {
		$("a.active").removeClass('active');
		$(this).addClass('active');
		$('html, body').animate({scrollTop:0}, 'slow');
		if(!faleconoscoPag){
			
			if(clientesPag){
				$('#clienteBox').slideUp('slow', function(){
					$(this).remove();
				});
			}
			$('<div id="formBox"></div>').insertBefore($('#mainContentBox'));
			$('#formBox').hide().load("form.html div.formBox", function(){
				$(this).slideDown('slow');
				$("#form").validationEngine({
					ajaxSubmit: true,
					ajaxSubmitFile: "contato.php",
					ajaxSubmitMessage: "Obrigado pelo seu contato!",
					success :  function() {
						$("#formBox").animate({ 
							height: 82
						}, 200 );
					},
					failure : function() {}
				});

			});
			faleconoscoPag = true;
			clientesPag = false;
		}
		pageTracker._trackPageview('form.html');
		
	});
	
	$(window).scrollTop(0,0);
	
	$(window).bind('scroll', function(event){
		//if($("#mainContentBox").height()<='3000'){
			var totalDiv = $('#mainContentBox').outerHeight();
			
			if (($(document).height() - $(window).height()) - $(window).scrollTop() <= 60 && readyToStack) {
				readyToStack = false;
				setTimeout(function(){ 
					//$('#mainContentBox').animate({ 
					//	height: (totalDiv+10)
					//}, 1000, function(event){
						
						var atualBox = $('<div class="innerBox" id="box'+divNumber+'">'+divNumber+'</div>').insertAfter($('#box'+(divNumber-1)+''));
						var rndNumTab = atualTab[numBIndex];
						$('#box'+divNumber+'').fadeOut(0).load("tabela_menor_"+rndNumTab+".html table#tabela", function(){
							$('#mainContentBox').animate({ 
								height: ($('#mainContentBox').height()+($(atualBox).height()))
							}, 200, function(event){
								$(atualBox).fadeIn('slow', function(){
									$('a',this).css('display','block');
									/*$('a',this).hover(function() {
										$('p',this).slideDown('fast');
									},function() {
										$('p',this).slideUp('fast');
									});*/
								});
							} );
						});
						if( numBIndex == arrayTab.length-1 ){
							numBIndex = 0;
						}
						
						divNumber++;
						readyToStack = true;
						
					//} );
				}, 500);
				numBIndex++;
			}
		//}
		
	});
	
});

/*
setInterval(function(){ 
	aa = $(".borderContent");
	aa.find('a').mouseout();
	eeq = Math.floor(Math.random() * aa.length);
	aa.eq(eeq).find("a").mouseover();
	$('.infoSide').html(eeq);
	}, 
	1000); */


