$(document).ready(function(){

	//remove class
	function removerClasses(){
		content.removeClass("box-sobre");
		content.removeClass("box-escritorios");
		content.removeClass("box-localizacao");
		content.removeClass("box-atuacao");
		content.removeClass("box-contato");
	}
	function atribuirSelecao(itemSelecionado){
		sections.removeClass("selecionado");
		$("#menu li#"+itemSelecionado).addClass("selecionado");
	}
	
	//References
	var sections = $("#menu li");
	var loading = $("#loading");
	var content = $("#blocotexto");
	
	//click home
	$("h1#home").click(function(){
		showLoading();
		atribuirSelecao(sections + "#casa");
		content.slideUp();
		removerClasses();
		content.addClass("box-home");
		content.load("/internal/home.html #section_home", hideLoading);
		content.slideDown();
	});
	//start loading content
	content.load("/internal/home.html #section_home", hideLoading);
	atribuirSelecao(sections + "#casa");
	
	//Manage click events
	sections.click(function(){
		//alert(this.id);
		//show the loading bar
		showLoading();
		//load selected section
		switch(this.id){
			case "casa":
				atribuirSelecao(this.id);
				content.slideUp();
				removerClasses();
				content.addClass("box-home");
				content.load("/internal/home.html #section_home", hideLoading);
				content.slideDown();
				break;
			case "sobre":
				atribuirSelecao(this.id);
				content.slideUp();
				removerClasses();
				content.addClass("box-sobre");
				content.load("/internal/sobre.html #section_sobre", hideLoading);
				content.slideDown();
				break;
			case "servicos":
				atribuirSelecao(this.id);
				content.slideUp();
				removerClasses();
				content.addClass("box-servicos");
				content.load("/internal/servicos.html #section_servicos", startAccordion);
				content.slideDown();
				break;
			case "parceiros":
				atribuirSelecao(this.id);
				content.slideUp();
				removerClasses();
				content.addClass("box-parceiros");
				content.load("/internal/parceiros.html #section_parceiros", hideLoading);
				content.slideDown();
				break;
			case "clientes":
				atribuirSelecao(this.id);
				content.slideUp();
				removerClasses();
				content.addClass("box-clientes");
				content.load("/internal/clientes.html #section_clientes", hideLoading);
				content.slideDown();
				break;
			case "contato":
				atribuirSelecao(this.id);
				content.slideUp();
				removerClasses();
				content.addClass("box-contato");
				content.load("/internal/contato.html #section_contato", hideLoading);
				content.slideDown();
				break;
			default:
				//hide loading bar if there is no selected section
				hideLoading();
				break;
		}
	});

	function startAccordion(){
		hideLoading();
		$("#accordion").accordion();
	}
	
	//show loading bar
	function showLoading(){
		loading
			.css({visibility:"visible"})
			.css({opacity:"1"})
			.css({display:"block"})
		;
	}
	//hide loading bar
	function hideLoading(){
		loading.fadeTo(1000, 0);
	};

	$('#menu li')
	.css( {backgroundPosition: "0px 0px"} )
	.mouseover(function(){
		$(this).stop().animate({backgroundPosition:"(0px -20px)"}, {duration:500})
	})
	.mouseout(function(){
		$(this).stop().animate({backgroundPosition:"(0px -20px)"}, {duration:200, complete:function(){
			$(this).css({backgroundPosition: "0px 0px"})
		}})
	})

	// NOTICIAS
	//cache the ticker
	var ticker = $(".ticker");
	//wrap dt:dd pairs in divs
	ticker.children().filter("dt").each(function() {
		var dt = $(this),
		container = $("<div>");
		dt.next().appendTo(container);
		dt.prependTo(container);
		container.appendTo(ticker);
	});	
	//hide the scrollbar
	ticker.css("overflow", "hidden");
	//animator function
	function animator(currentItem) {
	  //work out new anim duration
	  var distance = currentItem.height();
		duration = (distance + parseInt(currentItem.css("marginTop"))) / 0.025;
	    //animate the first child of the ticker
	    currentItem.animate({ marginTop: -distance }, duration, "linear", function() {
		//move current item to the bottom
		currentItem.appendTo(currentItem.parent()).css("marginTop", 0);
		//recurse
		animator(currentItem.parent().children(":first"));
	  }); 
	};
	//start the ticker
	animator(ticker.children(":first"));	
	//set mouseenter
	ticker.mouseenter(function() {
	  //stop current animation
	  ticker.children().stop();
	});
	//set mouseleave
	ticker.mouseleave(function() {    
      //resume animation
	  animator(ticker.children(":first"));
	});

	
	// DICAS
	//cache the ticker
	var tips = $(".tips");
	//wrap dt:dd pairs in divs
	tips.children().filter("dt").each(function() {
		var dt = $(this),
		container = $("<div>");
		dt.next().appendTo(container);
		dt.prependTo(container);
		container.appendTo(tips);
	});	
	//hide the scrollbar
	tips.css("overflow", "hidden");
	//animator function
	function animator(currentItem) {
	  //work out new anim duration
	  var distance = currentItem.height();
		duration = (distance + parseInt(currentItem.css("marginTop"))) / 0.025;
	    //animate the first child of the ticker
	    currentItem.animate({ marginTop: -distance }, duration, "linear", function() {
		//move current item to the bottom
		currentItem.appendTo(currentItem.parent()).css("marginTop", 0);
		//recurse
		animator(currentItem.parent().children(":first"));
	  }); 
	};
	//start the ticker
	animator(tips.children(":first"));	
	//set mouseenter
	tips.mouseenter(function() {
	  //stop current animation
		tips.children().stop();
	});
	//set mouseleave
	tips.mouseleave(function() {    
      //resume animation
	  animator(tips.children(":first"));
	});

});

// Formulario de contato
function enviaForm(){
    var dataString = $("#contact").serialize();
    $.ajax({
    	type: "POST",
    	url: "/bin/send.php",
    	data: dataString,
    	success: function() {
    	//Mensagem para o usuario
    		$('#contact').html("<div id='message'></div>");
    		$('#message').html("<h3>Seus dados foram enviados com Sucesso</h3>")
    		.append("<p>Nós entraremos em contato em breve.</p>")
    		.hide()
			.fadeIn(1500, function() {
				$('#message').css({'background':'#D1FFBF url(/img/ico/accept.png) 10px center no-repeat', 'padding-left':'20px 20px 20px 50px'});
			});
    	}
    });
    return false;
}