$(document).ready(function() {
	//fade da página + load
	$('div[id="bb-load"]').fadeOut(200);
	$('div[id="bb-main"]').fadeIn(400);

	//GET HASH
	$hash = document.location.hash;
	if($hash) {
		$hash = $hash.replace('#', '');
		$('ul[class="bb-header-menu"] a[id="bb-menu-'+$hash+'"]').addClass('active');
	}

	//BOX ERROR
	$('div[id="bb-erro"] div[class*="bb-erro-box"] a').each(function() {
		$e = $(this);
		$e.hover(function() {
			$(this).css({
				marginTop : '-132px'
			});
		}, function() {
			$(this).css({
				marginTop : '0px'
			});
		});
	});
	//MENU
	$('div[class="bb-header-marca"] a').click(function() {
		/*$('.bb-header-menu a.active').removeClass('active');
		 $('html, body').animate({scrollTop : 0}, 500);*/
	});
	$('ul[class="bb-header-menu"] a[href*="#"]').each(function() {
		$e = $(this);
		$e.click(function() {
			$home = $('ul[class="bb-header-menu"]').attr('home');

			$('.bb-header-menu a.active').removeClass('active');
			$(this).addClass('active');
			$valor = $(this).text();
			$valor = $valor.replace(/ /g, "-");
			$posicao = $('#'+$valor).position();
			$posicao.top += 30;
			$('html, body').animate({
				scrollTop : $posicao.top
			}, 500);

		});
	});
	//O QUE FAZEMOS
	$('div[class="bb-home-box"] div[class="bb-home-box-texto"] p a[href*="#"]').each(function() {
		$e = $(this);
		$e.click(function() {
			$('.bb-header-menu a.active').removeClass('active');
			$(this).addClass('active');
			$valor = $(this).text();
			$valor = $valor.replace(/ /g, "-");
			$posicao = $('#'+$valor).position()
			$('html, body').animate({
				scrollTop : $posicao.top + 40
			}, 500);

			$('ul[class="bb-header-menu"] a[id="bb-menu-'+$valor+'"]').addClass('active');
		});
	});
	//FALE COM A GENTE
	$('a#bb-mapa').fancybox({
		'width' : '75%',
		'height' : '75%',
		'autoScale' : false,
		'transitionIn' : 'elastic',
		'transitionOut' : 'elastic',
		'speedIn' : 400,
		'speedOut' : 200,
		'overlayShow' : true,
		'overlayColor' : '#000000',
		'type' : 'iframe'
	});
	$('form[id="formulario"]').submit(function() {
		$.post('content/home/email.php', $('form').serialize(), function(data) {
			$('div[id="retorno"]').text(data).slideToggle(200).delay(7000).slideToggle(200);
			if(data == 'Muito obrigado, responderemos o mais breve possível.') {
				$('form input[name="nome"], form input[name="empresa"], form input[name="url"], form input[name="pais"], form input[name="telefone"], form input[name="email"], form input[name="assunto"], form input[name="mensagem"], form input[name="checkbox"]').val('');
				$('form textarea[name="mensagem"]').text('');
			}
		});
		return false;
	});
	//BLOG
	$('div[class="bb-home-box"] div[class="bb-nav-blog"] a').each(function() {
		$e = $(this);
		$e.click(function() {
			$('div[class="bb-home-box"] div[class="bb-nav-blog"] a.active').removeClass('active');
			$(this).addClass('active');
			pos = -$(this).prevAll().length * 810;
			$('div[class="bb-home-box"] div[class="bb-blog-container"]').animate({
				marginLeft : pos + 'px'
			}, 400);
			return false;
		});
	});
	//PROJETOS
	$('div[class*="bb-projeto"] a').each(function() {
		$e = $(this);
		$e.hover(function() {
			$(this).children('div[class="bb-projeto-info"]').fadeIn(400, function() {
				if(!$.support.opacity) {
					this.style.removeAttribute('filter');
				}
			});
		}, function() {
			$(this).children('div[class="bb-projeto-info"]').fadeOut(400, function() {
				if(!$.support.opacity) {
					this.style.removeAttribute('filter');
				}
			});
		});
	});
	//PESSOAS
	$('div[class*="bb-time-pessoa-box"] a').each(function() {
		$e = $(this);
		$e.hover(function() {
			//info pessoa
			$(this).children('div[class="bb-pessoa-info"]').fadeIn(200, function() {
				if(!$.support.opacity) {
					this.style.removeAttribute('filter');
				}
			});
			$('div[class="bb-titulo"] h2').hide().fadeIn(400);
			$('div[class="bb-titulo"] cite').hide().fadeIn(400);

			$('div[class="bb-titulo"] h2').children('#frase').html($(this).attr('frase'));
			$('div[class="bb-titulo"] cite').html("<span>//</span> " + $(this).attr('nome') + " <span>/</span> " + $(this).attr('cargo'));
		}, function() {
			//pessoa
			$(this).children('div[class="bb-pessoa-info"]').fadeOut(200, function() {
				if(!$.support.opacity) {
					this.style.removeAttribute('filter');
				}
			});
			//caixa

			$('div[class="bb-titulo"] h2').hide().fadeIn(400);
			$('div[class="bb-titulo"] cite').hide().fadeIn(400);

			$('div[class="bb-titulo"] h2').children('#frase').html("Várias cabeças, uma missão.");
			$('div[class="bb-titulo"] cite').html('');
		});
	});
	//CASA
	$('div[class*="bb-casa-box"] a').each(function() {
		$e = $(this);
		$e.hover(function() {
			$(this).children('div[class*="bb-preview-over"]').fadeIn(400);
		}, function() {
			$(this).children('div[class*="bb-preview-over"]').fadeOut(400);
		});
	});
	//CASA LINKS
	$('a[rel="casa"]').fancybox({
		'transitionIn' : 'elastic',
		'transitionOut' : 'elastic',
		'speedIn' : 400,
		'speedOut' : 200,
		'titlePosition' : 'inside',
		'overlayShow' : true,
		'overlayColor' : '#000000'
	});
	//TROFEU
	function formatTitle(title) {
		if(title != 'null' && title != 'NULL') {
			return '<a href="' + title + '" class="bb-link" target="_blank">+ saiba mais</a>';
		}
	}

	$('a[rel="trofeu"]').fancybox({
		'transitionIn' : 'elastic',
		'transitionOut' : 'elastic',
		'speedIn' : 400,
		'speedOut' : 200,
		'titlePosition' : 'inside',
		'overlayShow' : true,
		'overlayColor' : '#000000',
		'titleFormat' : formatTitle
	});
	$('div[class*="bb-trofeu"] a').each(function() {
		$e = $(this);
		$e.hover(function() {
			$(this).children('div[class="bb-trofeu-info"]').fadeIn(400, function() {
				if(!$.support.opacity) {
					this.style.removeAttribute('filter');
				}
			});
		}, function() {
			$(this).children('div[class="bb-trofeu-info"]').fadeOut(400, function() {
				if(!$.support.opacity) {
					this.style.removeAttribute('filter');
				}
			});
		});
	});
	//FORA DA CAIXA
	$("a[class*='bb-caixa-item-saibamais']").each(function() {
		$e = $(this);
		$e.click(function() {
			$link = $(this).attr("rel");
			$(".bb-caixa-geral").hide();
			$(".bb-caixa-view").html("").show().addClass("load");
			$.post('/content/home/foradacaixa_item.php', {evento:$link}, function(data) {
				$(".bb-caixa-view").removeClass("load");
				$("div[class='bb-caixa-view']").html(data);
			});
			return false;
		});
	});
	var $total;

	$("div[class='bb-caixa-nav'] a").each(function(i) {
		var $e = $(this);
		$total = i - 1;
		$("div[class='bb-caixa-nav'] a:eq(1)").addClass("active");

		$e.click(function() {
			$valor = $(this).attr("rel");
			$atual = $("div[class='bb-caixa-nav'] a[class='active']").attr("rel");
			if($valor == "proximo") {
				$valor = parseInt($atual) + 1;
				if($valor > $total) {
					$valor = $total;
				}
			} else if($valor == "anterior") {
				$valor =  parseInt($atual) - 1;
				if($valor == 0) {
					$valor = 1;
				}
			}
			_rollCaixa($valor);
			return false;
		});
		function _rollCaixa($p) {
			$("div[class='bb-caixa-nav'] a").removeClass();
			$("div[class='bb-caixa-nav'] a:eq("+$p+")").addClass("active");
			//
			$final = 810 * $p;
			$size = $("div[class='bb-caixa-content']").width();
			$dif = $final > $size ? $final - $size : 0;
			$pos = (810 * ( $p - 1)) - $dif;
			$("div[class='bb-caixa-content']").animate({
				left : -$pos + 'px'
			});
		}
	});
});
function backpage() {
	history.back(-1);
	return false();
}
