/*--------------------------------------------------------------------
JAVASCRIPT "usados ao longo do site MRG"

Version: 	2.0 - 2009/2010
author: 	Burocratik (alexandre gomes)
email: 		alex@burocratik.com
website: 	http://www.burocratik.com
-----------------------------------------------------------------------*/
var W3CDOM = (document.getElementsByTagName && document.createElement);
if (W3CDOM){
	var link = document.createElement('link');
	link.setAttribute("rel", "stylesheet");
	link.setAttribute("type", "text/css");
	link.setAttribute("href", "/scripts/javascript-corpo.css");
	link.setAttribute("media", "screen");
	document.getElementsByTagName("head")[0].appendChild(link);
}

/* =Open new window e Mandar Imprimir + IE6
-----------------------------------------------------------------------*/
$(document).ready(function(){
  //IE6
  if($.browser.msie && $.browser.version<"7.0"){window.top.location = "http://burocratik.com/error/ie6-please-no.html";}
  //Open new window
  $("a[rel=external]").click(function(){
    var linkExterno = window.open($(this).attr("href"));
    return linkExterno.closed;
  });
	//Mandar Imprimir
  $("a[rel=print]").click(function(){
    var imprimir = window.print();
    return false;
  });
})
/*-----------------------------------------------------------------------
 =LIMPAR FORMULARIOS
So limpa nos forms os Input Text com class txtLimpar
-----------------------------------------------------------------------*/
$(document).ready(function() {
  $('input[type=text]').each(function() {
    if($(this).hasClass("textLimpar")!=true) return; //se nao tem a class sai
		var defeito = this.defaultValue; //this pois defaultValue é propriedade de javascript e nao jquery
		$(this).focus(function() {
			if($(this).val() == defeito) {$(this).val("")}
		});
		$(this).blur(function() {
			if($(this).val() == "") {$(this).val(defeito)
			}
		});

	});
});
/*-----------------------------------------------------------------------
=Inserir E-mails correctos: Obrigatorio class="email", e [-at-]
-----------------------------------------------------------------------*/
$(document).ready(function(){
   $("a.email").each(function(){
     var mailReal = $(this).text().replace("[-at-]","@");
		 $(this).text(mailReal);
     $(this).attr("href", "mailto:"+mailReal);
	 })
})
/*-----------------------------------------------------------------------
=MAIN Menu de Navegação
-----------------------------------------------------------------------*/
$(document).ready(function(){
  $("ul#navMain span").css("left","0");
	if($.browser.msie && $.browser.version<"9.0"){
  	$("ul#navMain span").hide();
		$("ul#navMain a").hover(function () {
			$(this).children("span").show();
			},function () {
				$(this).children("span").hide();
		});
	}else{
 		$("ul#navMain span").css("opacity","0"); //todos os span com opacidade zero
		$("ul#navMain span").hover(function () {
			$(this).stop().animate({opacity: 1}, "normal");
			},function () {
				$(this).stop().animate({opacity: 0}, "slow");
		});
	}
 })
 /*-----------------------------------------------------------------------
=NAVEGACAO SECUNDARIA  (needs jqueryColor.js )
-----------------------------------------------------------------------*/
 $(document).ready(function(){
  $("ul#navSec a").hover(function () {
    if($(this).parent().hasClass("on")) return false;
		$(this).stop().animate({backgroundColor: "#058ba7"}, 400);
		},function () {
		  if($(this).parent().hasClass("on")) return false;
			$(this).stop().animate({backgroundColor: "#c9e6ed"}, 600);
		});
 })

/*-----------------------------------------------------------------------
=Acordeao tipo read more (desaparece ao clicar e nao tens como fechar)
-----------------------------------------------------------------------*/
 $(document).ready(function(){
   $(".txtLerMais").hide();
   $(".lerMais").click(function(){
     $(this).parent().hide();
   	 $(this).parent().next().slideToggle("normal");
   	 return false;
   });
 })

/*-----------------------------------------------------------------------
=Acordeao Normal = slide
-----------------------------------------------------------------------*/
 $(document).ready(function(){
   $(".slide").each(function(){
   	$(this).next().hide().css({overflow:"auto"});
		$(this).next().addClass("txtslide"); // class que puxa o txt a dta
   	if($(this).next().hasClass("show")){$(this).next().show(); $(this).addClass("slideon");}
	 });
   $(".slide").click(function(){
   	 $(this).next().slideToggle("normal");
		 $(this).toggleClass("slideon");
   	 return false;
   });
 })

 /*-----------------------------------------------------------------------
 =NUVENS e BALOES
Random integer between min and max: Math.floor(Math.random() * (max - min + 1)) + min;
-----------------------------------------------------------------------*/
//NUVENS
var dx=Math.floor(Math.random()*(500));
function animNuvens(){
 	 $("#nuvens").css({backgroundPosition:""+dx+"px 0"});
   dx -= 1;
}
var nuvens_meuSetInterval = setInterval("animNuvens()" ,100);
//BALOES
var xspeed1=-1; var yspeed1=-1; var dyy1=0; var dxx1=Math.floor(Math.random()*(740+1));
var xspeed2=2; var yspeed2=1; var dyy2=0; var dxx2=Math.floor(Math.random()*(840+1));
function animBalao1(){
 	dyy1 += yspeed1; dxx1 += xspeed1;
	$("#balao1").css({left:""+dxx1+"px", top:""+dyy1+"px"});
 	var x = $("#balao1").css("left").replace("px","");
	var y = $("#balao1").css("top").replace("px","");
	if( y > 80) yspeed1 = -yspeed1;
 	if( y < -70) yspeed1 = -yspeed1;
	if( x > 845) xspeed1 = -xspeed1;
 	if( x < -10) xspeed1= -xspeed1;
}
function animBalao2(){
 	dyy2 += yspeed2; dxx2 += xspeed2;
	$("#balao2").css({left:""+dxx2+"px", top:""+dyy2+"px"});
 	var x = $("#balao2").css("left").replace("px","");
	var y = $("#balao2").css("top").replace("px","");
	if( y > 180) yspeed2 = -yspeed2;
 	if( y < -90) yspeed2 = -yspeed2;
	if( x > 845) xspeed2 = -xspeed2;
 	if( x < -10) xspeed2= -xspeed2;
}
//Consuante os elementos chama dada animação
$(document).ready(function(){
  if(document.getElementById("balao1")) {var baloes1_meuSetInterval = setInterval('animBalao1()',110);}
	if(document.getElementById("balao2")) {var baloes2_meuSetInterval = setInterval('animBalao2()',90);}
});

/*-----------------------------------------------------------------------
 =OVERLAY - LIGHTBOX
 NOTAS: tentei usar png transp mas flipa np ie7
-----------------------------------------------------------------------*/
//Função que determina a altura do Documento
(function($){
	$.getDocHeight = function(){
    return Math.max($(document).height(),$(window).height());
	};
})(jQuery);
//Função que determina o comprimento do Documento
(function($){
	$.getDocWidth = function(){
    return Math.max($(document).width(),$(window).width());
	};
})(jQuery);
//Funcao que posiciona segundo X a overlay box
function fPosicaoX(destino){
	var largStage = $.getDocWidth();
	var posicaoX = largStage/2-($(".overlayBox").width()/2);
	if($.browser.msie && $.browser.version<"9.0"){ /* IE-jQuery Cleartype glitch by Ben Novakovic*/
		$(destino).css({display:"block", left:posicaoX}).fadeTo(300,1,function() {this.style.removeAttribute('filter');});
	}else{
   	$(destino).css({display:"block", left:posicaoX}).fadeTo(300,1);
	}
}
//Funcao que tira os elementos do stage
function fTira(){
	$(".overlay").fadeTo(0,0).hide();
  $(".overlayBox").fadeTo(0,0).hide();
  $(window).unbind("resize"); //tirar o evento do resize
}
/////////
$(document).ready(function(){
  //iniciar objectos
 	$(".overlay").fadeTo(0,0).hide();
  $(".overlayBox").fadeTo(0,0).hide();
	// Botoes
  $(".btOverlay").click(function(){
    var qualID = $(this).attr("href").replace("#","");//replace is javascript method;
		var destino = $("div#"+qualID+"");
		//fundo
		var alturaStage = $.getDocHeight();
		$(".overlay").css({height:alturaStage,display:"block"}).fadeTo(300,.95)
    //Interior e resize da janela
    fPosicaoX(destino);
    $(window).bind("resize", function(){
  		fPosicaoX(destino);
		});
  })
  $(".overlay").click(function(){fTira();})
  $(".btClose").click(function(){fTira();})
})

/*-----------------------------------------------------------------------
 =OVERLAY - LIGHTBOX Fotos (uso 2 imgs (loading e holder) para nao ter flicker)
-----------------------------------------------------------------------*/
function fPosicaoIni(){
	var largStage = $.getDocWidth();
	var posicaoX = largStage/2-($(".overlayBoxP").width()/2);
 	$(".overlayBoxP").css({display:"block", left:posicaoX}).fadeTo(200,1);
}
//Funcao que tira os elementos do stage
function fTiraP(){
  $(".overlay").fadeTo(0,0).hide();
 	$(".overlayBoxP").fadeTo(0,0).css({width:"150px", height:"150px", display:"none"});
	$("#placeholder").fadeTo(0,0);
}
$(document).ready(function(){
  //iniciar objectos
 	//$(".overlay").fadeTo(0,0).hide();  //ja feito em cima
  $(".overlayBoxP").fadeTo(0,0).hide();
	$("#placeholder").fadeTo(0,0);
	var picLoad = $("#placeholder").attr("src");
  // Botoes
  $(".btPic").click(function(){
		var picNova = $(this).attr("href");
		//fundo
		var alturaStage = $.getDocHeight();
		$(".overlay").css({height:alturaStage,display:"block"}).fadeTo(300,.9);
    fPosicaoIni()
    //LOADING novas fotos
   	var lerImg = new Image();
		lerImg.onload = function(){
		 lerImg.onload = null;
		 $("#placeholder").attr("src", lerImg.src);
		 var comP = $("#placeholder").width();
		 var altP = $("#placeholder").height();
     var posicaoXnew = $.getDocWidth()/2-(comP/2);
     $(".overlayBoxP").css({width:comP, height:altP, left:posicaoXnew});
		 $("#placeholder").fadeTo(250,1);
		}
		lerImg.src = picNova;
		return false;
  })
 	$(".overlay").click(function(){fTiraP();})
 	$(".btClose").click(function(){fTiraP();})
})

/*-----------------------------------------------------------------------
 =OVERLAY - LIGHTBOX VIDEO
 NOTAS: tentei usar png transp mas flipa np ie7
-----------------------------------------------------------------------*/
//Funcao que tira os elementos do stage
function fTiraV(){
	$(".overlay").fadeTo(0,0).hide();
  $(".overlayBoxV").fadeTo(0,0).hide();
}
/////////
$(document).ready(function(){
  //iniciar objectos
 	//$(".overlay").fadeTo(0,0).hide();  //ja feito em cima
  $(".overlayBoxV").fadeTo(0,0).hide();
	// Botoes
  $(".btVideo").click(function(){
    var qualID = $(this).attr("href").replace("#","");//replace is javascript method;
		var destino = $("div#"+qualID+"");
		//fundo
		var alturaStage = $.getDocHeight();
		$(".overlay").css({height:alturaStage,display:"block"}).fadeTo(300,.9);
    //Interior e resize da janela
    var comV = $("#video").width();
		var altV = $("#video").height();
    var posicaoXnew = $.getDocWidth()/2-(comV/2);
    $(".overlayBoxV").css({width:comV, height:altV, left:posicaoXnew}).fadeTo(0,1);
	})
  $(".overlay").click(function(){fTiraV();})
  $(".btClose").click(function(){fTiraV();})
})
/*-----------------------------------------------------------------------
 =NEWSLETTER VALIDACAO: based on: Adrian Gondelle & Ivan Castro
-----------------------------------------------------------------------*/
$(document).ready(function(){
 	$(".erroMail").fadeTo(0,0).hide();
	//
	var form = $("#formNewsletter");
	var email = $("#dhurli-dhurli");
	//
	email.blur(validateEmail);
	//On Submitting
	form.submit(function(){
		if(validateEmail())
			return true;
		else
			return false;
	});
	//
	function validateEmail(){
		var a = $("#dhurli-dhurli").val();
		var filter = /^[a-zA-Z0-9]+[a-zA-Z0-9_.-]+[a-zA-Z0-9_-]+@[a-zA-Z0-9]+[a-zA-Z0-9.-]+[a-zA-Z0-9]+.[a-z]{2,4}$/;
		if(filter.test(a)){
			$(".erroMail").fadeTo(200,0);
			email.removeClass("erro");
			return true;
		}
		else{
			$(".erroMail").fadeTo(300,1);
			email.addClass("erro");
			return false;
		}
	}
})