var timerId = 0;
	
$(document).ready(function () {
	
	startSlideShow('home');
	// background 100% hack ie7/8
	if ($.browser.msie  && (parseInt($.browser.version) == 8 || parseInt($.browser.version) == 7)) {
		$(".fadeIn .bg").each( function() {
			
			var url = $(this).attr("title");
			var bgImg = $('<img id="tmpImg" src="'+ url +'"/>');
			$('body').append(bgImg);
			bgImg.hide().load();
			width = $("#tmpImg").width();
			height = $("#tmpImg").height();
			$("#tmpImg").remove();
			
			var w_width = $(window).width();
			var w_height = (height * w_width)/width;
			
			$(this).css( {
		  		"filter": "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+ $(this).attr("title") +"', sizingMethod='scale');",
				"-ms-filter": "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+ $(this).attr("title") +"', sizingMethod='scale');",
				"height":  w_height + "px",
				"margin-top" : "-103px"
			});
		});
	}

	// menu interaction
	$("#ft .menu li span").click( function() {
		$("#ft .menu li ul").hide();
		$("#ft .menu li").removeClass("active");
		$(this).next("ul").show();
		$(this).parent('li').addClass("active");
		
		// hide legend and slideshow
		$("#bd .label").hide();
		$(".fadeIn").hide();
		clearInterval(timerId);
		$(".body_links .block").removeClass("selected");
		$(".body_links div.block div + div").hide();
		
		// sets module background
		$("#bg_world").hide();
		$("#bd .mapa").hide();
		$("#bg_flash").hide().html('');
		$("#bg_module").show().css({ "background-image" : "url('docs_folder/site_background_imgs/"+ $(this).attr('alt') +"')" });
		
		// background 100% hack ie7/8
		if ($.browser.msie  && (parseInt($.browser.version) == 8 || parseInt($.browser.version) == 7)) {
			
			var url = "docs_folder/site_background_imgs/" + $(this).attr("alt");
			var bgImg = $('<img id="tmpImg" src="'+ url +'"/>');
			$('body').append(bgImg);
			width = $("#tmpImg").width();
			height = $("#tmpImg").height();
			$("#tmpImg").remove();
			
			var w_width = $(window).width();
			var w_height = (height * w_width)/width;
			
		  $("#bg_module").css( {
		  		"filter": "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='docs_folder/site_background_imgs/"+ $(this).attr('alt') +"', sizingMethod='scale');",
				"-ms-filter": "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='docs_folder/site_background_imgs/"+ $(this).attr('alt') +"', sizingMethod='scale');",
				"height":  w_height + "px",
				"margin-top" : "-103px",
		  });
		}

	});
	
	// load home module
	$("#ft .menu li.home").click( function() {
		$(".module").hide();
		$("#bd .label").removeClass('fact').show().find("p").html($(this).attr("title"))
		$("#bg_module").hide();
		$("#bg_world").hide();
		$("#bd .mapa").hide();
		$("#bg_flash").hide().html('');
		$(".menu li ul").hide();
		$(".body_links .block").removeClass("selected");
		$(".body_links div.block div + div").hide();
		$(".fadeIn.home").show();
		startSlideShow('home');
	});
	
	// show search module
	$(".footer_icons .search").click( function() {
		showSearch();
	});
	
	// show world module
	$("#bd .world").click( function() {
		// hide legend and slideshow
		$(".module").hide();
		$("#bd .label").removeClass('fact').hide().find("p").html($(this).attr("title"));
		$(".fadeIn").hide();
		$("#bg_module").hide();
		$("#bg_flash").hide().html('');
		$(".menu li ul").hide();
		clearInterval(timerId);
		
		// sets module background
		$("#bd .mapa").show();
		$("#bg_world").show().css({ 
			"width" : "90%", 
			"height": "90%", 
			"opacity" : "0", 
			"margin-left" : ($("#bg_module").width() - ($("#bg_module").width()/1.1))/2 + "px", 
			"margin-top" : ($("#bg_module").height() - ($("#bg_module").height()/1.1))/2 + "px"}
		 )
		.stop(true,true).animate({ "width" : "110%", "height" : "110%", "margin-left" : "-5%", "margin-top" : "-3%", "opacity" : "1" }, 1000, function() {
			$("#bd .label").css({ "right": "-770px"}).show().stop(true,true).animate({ "right": "0px"}, 3000);
		});
		
		$(".body_links .block").removeClass("selected");
		$(".body_links div.block div + div").hide();
		$(this).addClass("selected");
	});
	
	// show biomind module
	$("#bd .biomind").click( function() {
		// hide legend and slideshow
		
		$(".module").hide();
		$("#bd .label").hide().find("p").html($(this).attr("title"));
		$(".fadeIn").hide();
		$("#bg_module").hide();
		$("#bg .world").hide();
		$("#bd .mapa").hide();
		$(".menu li ul").hide();
		clearInterval(timerId);
		
		$("#bg_flash").show();	
		flashembed("#bg_flash", {
			src: 'docs_folder/flash/'+ $("#bg_flash").attr("alt"),
			allowfullscreen: true,
			width: $(window).width(),
			height: $(window).height() + 275, 
			wmode: "transparent"
		});
		
		$(".body_links .block").removeClass("selected");
		$(".body_links div.block div + div").show();
		$(this).addClass("selected");
	});
	
	// show factory module
	$("#bd .fact").click( function() {
		// hide legend and slideshow
		$(".module").hide();
		$(".fadeIn.home").hide();
		$("#bg_world").hide();
		$("#bd .mapa").hide();
		$("#bg_flash").hide().html('');
		$(".menu li ul").hide();
		$("#bg_module").hide();
		clearInterval(timerId);
		
		$("#bd .label").addClass('fact').stop(true,true).show().find("p").html('EMPRESA');
		startSlideShow('factory');
		$(".module.factory").slideDown(500);
		
		$(".body_links .block").removeClass("selected");
		$(".body_links div.block div + div").hide();
		$(this).addClass("selected");
	});
	
	// show factory images
	$(".fact_boxes .box").click( function() {
		$(".fadeIn.factory").hide();
		clearInterval(timerId);
		
		$("#bg_module").show().css({ "background-image" : "url('docs_folder/site_imgs/"+ $(this).attr("alt") +"')" });
		$("#bd .label p").html($(this).attr("title"));
		
		// background 100% hack ie7/8
		if ($.browser.msie  && (parseInt($.browser.version) == 8 || parseInt($.browser.version) == 7)) {
			
			var url = "docs_folder/site_imgs/" + $(this).attr("alt");
			var bgImg = $('<img id="tmpImg" src="'+ url +'"/>');
			$('body').append(bgImg);
			bgImg.hide();
			width = $("#tmpImg").width();
			height = $("#tmpImg").height();
			$("#tmpImg").remove();
			
		 	var w_width = $(window).width();
		 	var w_height = (height * w_width)/width;
			
		  $("#bg_module").css( {
		  		"filter": "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='docs_folder/site_imgs/"+ $(this).attr('alt') +"', sizingMethod='scale');",
				"-ms-filter": "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='docs_folder/site_imgs/"+ $(this).attr('alt') +"', sizingMethod='scale');",
				"height":  w_height + "px",
				"margin-top" : "-103px"
		  });
		}
		
		//setTimeout( function() {  $("#bg_module").fadeOut('slow'); startSlideShow('factory') }, 5000);
	});
	
	// header links interaction
	$(".top_links div").click( function() {
		if ($(this).attr("title") != "") {
			// hide legend and slideshow
			clearInterval(timerId);
			$(".fadeIn").hide();
			$("#bg_world").hide();
			$("#bd .mapa").hide();
			$("#bg_flash").hide().html('');
			$(".menu li ul").hide();
			$("#bd .label").hide();
			$(".body_links .block").removeClass("selected");
			$(".body_links div.block div + div").hide();
			$("#ft .menu li").removeClass("active");
			
			$("#bg_module").show().css({
				"background-image": "url('components/img/site/" + $(this).attr("title") + "')"
			});
		}
	});
	
	// logo redirect
	$(".logo").click( function() { document.location.href = 'index.php' });
	
	// clear pre-filled fields
	$('.autoclear').focus( function() { 
		if ($(this).attr('alt') == '') $(this).attr('alt', $(this).val());
		
		if($(this).attr('alt') == $(this).val()) $(this).val('');
	});
	
	$('.autoclear').blur( function() { if($(this).val() == '') $(this).val( $(this).attr('alt') ); });
	
	// register newsletter
	$(".newsletter .ok").click( function() {
		
		valid = true;
		
		if($(this).hasClass("back")) {
			$(".newsletter .ok").removeClass("back");
			$(".msg").hide();
			$("input[name=nl_email]").val('').show();
		}		
		else {
			if($("input[name=nl_email]").val() == '' || $("input[name=nl_email]").val() == $("input[name=nl_email]").attr("title")) {
				$(".newsletter .ok").addClass("back");
				$("input[name=nl_email]").hide();
				$(".msg.invalid").show();
				valid = false;
			}
	
			var reg = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i;
	
			if($("input[name=nl_email]").val() != "") {
		    	if(reg.test($("input[name=nl_email]").val()) == false) {
		    	    $(".newsletter .ok").addClass("back");
					$("input[name=nl_email]").hide();
					$(".msg.invalid").show();
		    	    valid = false;
		        }
			}
			
			if(valid === true) {
				$.post("components/inc/ajax_posts.php",  {
					formID : 1,
					security_token: $("input[name=token]").val(),
					email: $("input[name=nl_email]").val()
				},
				function(data) {
					$(".newsletter .ok").addClass("back");
					$("input[name=nl_email]").hide();
					
					if(data == 1)
						$(".msg.success").show();
					else
						$(".msg.error").show();
				});
			}
		}
	});
	
	//get search
	$(".search_box .ok").click( function() {
		getSearchResults();
	});
	
	$("input[name=search_input]").keypress( function(e) {
		var code = (e.keyCode ? e.keyCode : e.which);
		 if(code == 13) {
		 	getSearchResults();
		 }
	});
	
});

// start background slideshow
function startSlideShow(cls) {
	$('.fadeIn.'+ cls).show();
	$('.fadeIn.'+cls+' .bg:first-child').show(0, function() { 
		$("#bd .label p").html($(this).attr("alt")); 
	});
	
	// homepage background slideshow
	timerId = setInterval(function(){
	  	$('.fadeIn.'+cls+' .bg:first-child').fadeOut(2000).next('.bg').show(0, function() { 
			$("#bd .label p").html($(this).attr("alt")); 
		}).end().appendTo('.fadeIn.'+cls);
	}, 6000);
}

// load module
function showModule(obj, mod, page, type){

	// set active state
	$("#ft .menu li ul li").removeClass("selected");
	obj.addClass("selected");
	
	// hide legend and slideshow
	$("#bd .label").hide();
	$(".fadeIn").hide();
	clearInterval(timerId);
	
	// shows module
	$(".module").hide();
	
	$.get('load.php', {
		page: page + '.php',
		type: type,
		mod: mod
	}, function(data){
		$(".module." + mod).html(data);
		
		// specific module css settings
		if (type == "missao") 
			$("#bd .module.first .wrap").css("height", "73px");
		else if (type == "producao") 
			$("#bd .module.first .wrap").css("height", "200px");
		else if (type == "recursos") 
			$("#bd .module.first .wrap").css("height", "235px");
		else if (type == "investigacao") 
			$("#bd .module.first .wrap").css("height", "200px");
		else if (type == "empresas") {
			$("#bd .module.second .wrap").css({ "height" : "310px", "width":"699px", "padding-top": "8px", "padding-left": "8px", "padding-right" : "3px" });
			$("#bd .module.second").css("width","710px");
		}
		else if (type == "automovel")
			$("#bd .module.third .wrap").css("height", "157px");
		else if (type == "calcado")
			$("#bd .module.third .wrap").css("height", "215px");
		else if (type == "construcao")
			$("#bd .module.third .wrap").css("height", "215px");
		else if (type == "construcao")
			$("#bd .module.third .wrap").css("height", "215px");
		else if (type == "outras")
			$("#bd .module.third .wrap").css("height", "157px");
		else if (type == "novos_produtos")
			$("#bd .module.third .wrap").css("height", "340px");
		else if (type == "higiene")
			$("#bd .module.fourth .wrap").css("height", "160px");
		else if (type == "ambiente")
			$("#bd .module.fourth .wrap").css("height", "160px");
		else {
			// reset settings
			$("#bd .module.first .wrap").css("height", "273px");
			$("#bd .module.second .wrap").css({ "height" : "197px", "width": "448px", "padding-top": "35px", "padding-left": "16px", "padding-right" : "15px"});
			$("#bd .module.second").css("width","480px");
			$("#bd .module.third .wrap").css("height", "290px");
			$("#bd .module.fourth .wrap").css("height", "224px");
		}
		
		$(".module." + mod).slideDown(500, function(){
			if (mod == "first") mCustomScrollbars(1);
			if (mod == "second") mCustomScrollbars(2);
			if (mod == "third") mCustomScrollbars(3);
			if (mod == "fourth") mCustomScrollbars(4);
			if (mod == "fifth") mCustomScrollbars(5);
			if (mod == "sixth") mCustomScrollbars(6);
		});
	});
}

function showSiteMap()
{
	$("#ft .menu li ul li").removeClass("selected");
	
	// hide legend and slideshow
	$(".body_links .block").removeClass("selected");
	$(".body_links div.block div + div").hide();
	
	$(".module").hide();
	$("#bd .label").hide();
	$("#bg_module").hide();
	$("#bg_world").hide();
	$("#bd .mapa").hide();
	$("#bg_flash").hide().html('');
	$(".body_links .block").removeClass("selected");
	$(".menu li ul").hide();
	$(".fadeIn").hide();
	$(".fadeIn.home").show();
	clearInterval(timerId);
		
	// sets module background
	$("#bg_world").hide();
	$("#bd .mapa").hide();
	$("#bg_flash").hide().html('');
	$("#bg_module").show().css({ "background-image" : "url('docs_folder/site_background_imgs/"+ $("#cabopol").attr('alt') +"')" });
	
	$(".module.sitemap").slideDown();
}

function showHome() {
	$(".module").hide();
	$("#bd .label").removeClass('fact').show().find("p").html($("#home").attr("title"))
	$("#bg_module").hide();
	$("#bg_world").hide();
	$("#bd .mapa").hide();
	$("#bg_flash").hide().html('');
	$(".menu li ul").hide();
	$(".body_links .block").removeClass("selected");
	$(".body_links div.block div + div").hide();
	$(".fadeIn.home").show();
	startSlideShow('home');
}

function loadModule(menu,obj,mod,page,type) {
	
	$("#ft .menu li ul").hide();
	$("#ft .menu li").removeClass("active");
	$(menu).next("ul").show();
	$(menu).parent('li').addClass("active");
		
	// hide legend and slideshow
	$("#bd .label").hide();
	$(".fadeIn").hide();
	clearInterval(timerId);
	$(".body_links .block").removeClass("selected");
	$(".body_links div.block div + div").hide();
		
	// sets module background
	$("#bg_world").hide();
	$("#bd .mapa").hide();
	$("#bg_flash").hide().html('');
	$("#bg_module").show().css({ "background-image" : "url('docs_folder/site_background_imgs/"+ $(menu).attr('alt') +"')" });
	
	showModule(obj,mod,page,type)
}

// show search module
function showSearch() {
	$(".module").hide();
	$("#bd .label").hide();
	$("#bg_module").hide();
	$("#bg_world").hide();
	$("#bd .mapa").hide();
	$("#bg_flash").hide().html('');
	$(".body_links .block").removeClass("selected");
	$(".menu li ul").hide();
	$(".fadeIn").hide();
	$(".fadeIn.home").show();
	clearInterval(timerId);
	
	$(".body_links .block").removeClass("selected");
	$(".body_links div.block div + div").hide();
	$("#ft .menu li").removeClass("active");
	
	$("input[name=search_input]").val('');
	$(".module.search .content").html('');
	$(".module.search").fadeIn(500);
}

// get search term results
function getSearchResults() {
	$(".msg.found").css("visibility","hidden");
		
	if($("input[name=search_input]").val().length < 2) {
		$(".module.search .content").html($(".msg.short").html());
	}
	else {
		$.get('load.php', { page: 'search.php', term: $("input[name=search_input]").val() }, function(data) {
			if (data != '') {
				$(".module.search .content").html(data);
				$(".msg.found").show();
			}
			else {
				$(".module.search .content").html($(".msg.no_results").html()+$("input[name=search_input]").val());
			}
			
			$("#mcs_container7").mCustomScrollbar("vertical", 400, "easeOutCirc", 1.05, "fixed", "yes", "yes", 0);
		});
	}
}

// load custom scrollbars
function mCustomScrollbars(mod){
	/* 
	malihu custom scrollbar function parameters: 
	1) scroll type (values: "vertical" or "horizontal")
	2) scroll easing amount (0 for no easing) 
	3) scroll easing type 
	4) extra bottom scrolling space for vertical scroll type only (minimum value: 1)
	5) scrollbar height/width adjustment (values: "auto" or "fixed")
	6) mouse-wheel support (values: "yes" or "no")
	7) scrolling via buttons support (values: "yes" or "no")
	8) buttons scrolling speed (values: 1-20, 1 being the slowest)
	*/
	$("#mcs_container"+mod).mCustomScrollbar("vertical",200,"easeOutCirc",1.05,"fixed","yes","yes",0);
}

/* function to fix the -10000 pixel limit of jquery.animate */
$.fx.prototype.cur = function(){
    if ( this.elem[this.prop] != null && (!this.elem.style || this.elem.style[this.prop] == null) ) {
      return this.elem[ this.prop ];
    }
    var r = parseFloat( jQuery.css( this.elem, this.prop ) );
    return typeof r == 'undefined' ? 0 : r;
}

