$(document).ready( function(){
	/***
	*	:: Centralizar Objeto ::
	*	:: Diego Marques - Agência DMK ::
	*	:: 2007 ::
	*
	*	Função para centralizar verticalmente ou horizontalmente.
	*	ex:
	*		$("#container").centro(); -> Centraliza Vertical e Horizontal;
	*		$("#footer").centroV(); -> Centraliza Verticalmente;
	*		$("#menu").centroH(); -> Centraliza Horizontalmente;
	*
	*	OBS: Nescessário plugin Dimension.
	***/
	jQuery.fn.extend({
		centroH: function(){
			var dmObj = $(this).parent().is("body") ? $(window) : $(this).parent();
			if($(dmObj).width() > $(this).width()) // Só aplica o alinhamento se o elemento pai for maior que o objeto alvo;
				$(this).css({left: "0px", marginLeft: Math.round((parseInt($(dmObj).width())-parseInt($(this).width()))/2)+"px"});
			else
				$(this).css({left: "0px", marginLeft: "0px"});
			return this;
		},
		centroV: function(){
			var dmObj = $(this).parent().is("body") ? $(window) : $(this).parent().css({overflow:"hidden"});
			if($(dmObj).height() > $(this).height()) // Só aplica o alinhamento se o elemento pai for maior que o objeto alvo;
				$(this).css({top: "0px", marginTop: Math.round((parseInt($(dmObj).height())-parseInt($(this).height()))/2)+"px"});
			else
				$(this).css({top: "0px", marginTop: "0px"});
			return this;
		},
		centro: function(){
			$(this).centroH().centroV();
			return this;
		}
	});
	
	/***
	*	:: Log para Debug ::
	*	:: Diego Marques - Agência DMK ::
	*	:: 2007 ::
	*	ex:
	*		l("testando");
	*
	*	OBS: Funciona apenas no Firefox com o plugin Firebug.
	***/
	l = function(x){ if(window.console) console.log(x); }
	
	/***
	*	:: Exibe div de bloqueio ::
	*	:: Diego Marques - Agência DMK ::
	*	:: 2007 ::
	*
	*	Função que abre uma div cobrindo toda a tela bloqueando o site. Caso não seja passado o id da div ele procura com padrão pelo id #bloqueio. com função de callback.
	*	ex:
	*		bloqueia("div_de_bloqueio", function(){ alert('tá bloqueado'); });
	*		bloqueia("div_de_bloqueio", function(){ alert('finalizando bloqueio'); },"fim");
	*
	*	OBS: Nescessário plugin Dimension.
	***/
	bloqueia = function(a,b,c){
		a = a == undefined || a == "" ? "bloqueio" : a;
		b = b == undefined || b == "" ? function(){} : eval(b);
		d = $(document).height() > $(window).height() ? $(document).height() : $(window).height();
		if(c != "fim"){
			$("#"+a).css({background:"#fff",height:d,left:"0",opacity:"0.5",position:"absolute",top:"0",width:"100%",zIndex:"10"}).fadeIn("normal", function(){ b(); });
			$(window).resize(function(){
				d = $(document).height() > $(window).height() ? $(document).height() : $(window).height();
				$("#"+a).css({height:d});
			});
		}else{
			$("#"+a).fadeOut("normal", function(){ b(); });
		}
	}
	
	/************************************************** GERAL **************************************************/
	$("#testeira").append( $("#migalha")[0] );
	ajustes = function(){
		$("#geral").centroV();
		//$("#rodape a").centroH();
		if($(window).width() > $("#conteudo").width()){
			$("#testeira").centroH();
			$("#conteudo").centroH();
			if($("#menu_auxiliar").attr("id") == "menu_auxiliar"){ $("#menu_auxiliar").centroH(); }
			bgPos = parseInt($("#geral").css("margin-top"))+608+"px";
			$("body").css("background-position","0 "+bgPos);
			/* Index */
			if($("#flash").attr("id") == "flash"){
				if(parseInt($("#conteudo").css("margin-left")) >= 79){
					$(".index #testeira").css({marginLeft: (parseInt($(".index #testeira").css("margin-left"))+78)+"px"});
					$(".index #conteudo").css({marginLeft: (parseInt($(".index #conteudo").css("margin-left"))+78)+"px"});
					$(".index #flash").css({left: (parseInt($(".index #conteudo").css("margin-left"))-157)+"px"});
				}else{
					$(".index #testeira").css({marginLeft: "157px"});
					$(".index #conteudo").css({marginLeft: "157px"});
					$(".index #flash").css({left: "0"});
				}
			}
		}else if($("#flash").attr("id") == "flash"){
			$(".index #testeira").css({marginLeft: "157px"});
			$(".index #conteudo").css({marginLeft: "157px"});
			$(".index #flash").css({left: "0"});
		}else{
			$(".index #testeira").css({marginLeft: "0px"});
			$(".index #conteudo").css({marginLeft: "0px"});
		}
	}
	ajustes();
	$(window).resize(function(){ ajustes(); });
	
	/* Fisheyer Menu */
	$("#menu_auxiliar").Fisheye(
		{
			maxWidth: 80,
			items: 'a',
			itemsText: 'span',
			container: '.menu_conteudo',
			itemWidth: 66,
			proximity: 100,
			alignment : 'left',
			valign: 'bottom',
			halign : 'center'
		}
	)
	setTimeout(function(){
		$("#menu_auxiliar div a div").css({width:"70%", height:"auto"});
		$("#menu_auxiliar div a div img").css({width:"100%", height:"auto"});
		if(document.all && !window.opera){
			$("#menu_auxiliar div a div img.reflected").next().css("height","50px").parent().parent().parent().parent().css("bottom","0px");
		}
		$("#menu_auxiliar").css({visibility:"visible"});
	},500);
	
	/* abrir imagem */
	$(".abreImgRel").click( function(){
		amAlvo = $(this).attr("rel").split("|");
		if(amAlvo != ""){
			$(".linksgaleria").children("a").attr("rel","imagebox-cabral");
			$(".linksgaleria").children("a[@href="+amAlvo[1]+"]").attr("rel","");
			$("#conteudo_conteudo").append("<img id=\"imgCarregar\" src=\"imagens/carregando.gif\" alt=\"Carregando\" border=\"0\" />");
			$("#imgCarregar").css({position:"absolute",zIndex:"99999",top:"0",left:"0",opacity:0.8}).centro();
			imgAbre = new Image();
			imgAbre.onload = function(){
				$("#conteudo_conteudo img").attr({src:amAlvo[0]}).parent().attr({href:amAlvo[1]});
				$("#imgCarregar").remove();
			}
			imgAbre.src = amAlvo[0];
		}
	});

	/* Quadradinhos do logo */
	$("#testeira .pequeno").mouseover( function(){
		$(this).parent().addClass("qdAberto").css("z-index","3");
		$(this).hide().next().show();
		setTimeout(function(){
			dmkclasses = $("#testeira .qdAberto").children(".grande").attr("class");
			if(dmkclasses != "" && dmkclasses != undefined && dmkclasses.indexOf("grandeAberto") == -1){
				obj = $("#testeira .qdAberto").children(".grande");
				fechaquadradinho(obj);
			}
		},500);
	});
	$("#testeira .grande").mouseover( function(){
		$(this).addClass("grandeAberto");
	});
	$("#testeira .grande").mouseout( function(){
		$(this).removeClass("grandeAberto");
		fechaquadradinho(this);
	});
	fechaquadradinho = function(obj){
		$(obj).parent().removeClass("qdAberto").css("z-index","1");
		$(obj).hide().prev().show();
	}
	
	// Bug Ies
	$(".menu_conteudo img").click(function(){
		location.href = $(this).parent().parent().attr("href");
	});
	
	/***
	*	:: Links para sites Otimizados ::
	*	:: Diego Marques - Agência DMK ::
	*	:: 2008 ::
	*
	*	Faz a correção do target dos links para sites otimizados
	***/
	$("a").attr("target","_parent");
	
});