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

Version: 	2.0 - 2009/2010
author: 	Burocratik (alexandre gomes)
email: 		alex@burocratik.com
website: 	http://www.burocratik.com
-----------------------------------------------------------------------*/
/* =Open new window e Mandar Imprimir
-----------------------------------------------------------------------*/
$(document).ready(function(){
  //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() {
    var aux = $(this).attr("class");
		if(aux != "textLimpar") 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);
	 })
 })

/*-----------------------------------------------------------------------
=Acordeao para lista definicao dl
-----------------------------------------------------------------------*/
$(document).ready(function(){
	$(".acordeao dd").each(function(){
  	$(this).hide().css({overflow:"auto"});
    if($(this).hasClass("show")){$(this).show(); $(this).prev().addClass("on");}
	});
  $(".acordeao dt").click(function(){
 	 $(this).next().slideToggle("normal");
	 $(this).toggleClass("on");
 	 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;
   });
 })

/*-----------------------------------------------------------------------
=Acordeao tipo read more (abre e fecha o tag a frente)
NOTA: o txt tem de estar dentro de um tag (por exemplo span dentro de um p) e o proximo elemento tem de ter class open more
-----------------------------------------------------------------------*/
 $(document).ready(function(){
   $(".txtMore").hide();
   $(".openMore").click(function(){
   	 $(this).parent().next().slideToggle("normal");
		 $(this).toggleClass("on");
     $(this).hasClass("on") ?  $(this).text("[-]") : $(this).text("[+]");
   	 return false;
   });
 })

 /*-----------------------------------------------------------------------
=TRAY Lista Downloads
-----------------------------------------------------------------------*/
 $(document).ready(function(){
   $(".download ul li:even").css("backgroundColor","#f4faff");
	 $(".download ul li").children("p.botao").css("left","300px");
   if($.browser.msie && $.browser.version<"9.0"){
    $(".download ul li").children("p.botao").hide();
	 	//
	 	$(".download ul li").hover(function () {
			$(this).children("p.botao").show();
	 	},
			function () {
			$(this).children("p.botao").hide();
		});
	 }else{
	 	$(".download ul li").children("p.botao").css("opacity","0");
   	//
	 	$(".download ul li").hover(function () {
				$(this).children("p.botao").stop().animate({opacity: 1}, "fast");
	 	},
			function () {
				$(this).children("p.botao").stop().animate({opacity: 0}, "slow");
		});
	 }
 })

/*-----------------------------------------------------------------------
 =MENU TRAY , BUY , SUPPORT
-----------------------------------------------------------------------*/
$(document).ready(function(){
  $("#navTry li a").each(function(){
    var qualID = $(this).parent("li").attr("id");
  	if($(this).hasClass("on")){
			switch(qualID){
  			case "navT1":
        	$(this).parent("li").css("backgroundPosition","0px -76px");
					break;
  			case "navT2":
        	$(this).parent("li").css("backgroundPosition","-57px -76px");
					break;
        case "navT3":
        	$(this).parent("li").css("backgroundPosition","-118px -76px");
					break;
        default:
				break;
			}//end switch
		 //se nao tem a classe on
     }else{
  		$(this).mouseover(function(){
				var aux = fBotoesTry($(this));
			  $(this).parent("li").css("backgroundPosition",""+aux+"px -38px");
  		})
      $(this).mouseout(function(){
         var aux = fBotoesTry($(this));
         $(this).parent("li").css("backgroundPosition",""+aux+"px 0px");
  		})
     }//end if
  })

  //FUNCAO QUAL É O FUNDO
	function fBotoesTry(este){
    var qualID = este.parent("li").attr("id");
		switch(qualID){
  		case "navT1":
     		valorX = 0;
				 break;
  	  case "navT2":
       	valorX = -57;
				break;
      case "navT3":
       	valorX = -118;
				break;
      default:
				break;
			}
		return valorX
	}

})

/*-----------------------------------------------------------------------
 =OVERLAY - LIGHTBOX
 NOTAS: tentei usar png transp mas flipa np ie7
-----------------------------------------------------------------------*/
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);
}
//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);
  $(".overlayBox").fadeTo(0,0);
 //	$(".overlayBox").style.removeAttribute('filter');
	// 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,.9)
    //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(){
  $(".overlayP").fadeTo(0,0).css("display", "none");
 	$(".overlayBoxP").fadeTo(0,0).css({width:"150px", height:"150px", display:"none"});
	$("#placeholder").fadeTo(0,0);
  $(".overlayBoxP .btClose span").css("display", "none");
}
$(document).ready(function(){
  //iniciar objectos
 	$(".overlayP").fadeTo(0,0);
  $(".overlayBoxP").fadeTo(0,0);
	$("#placeholder").fadeTo(0,0);
	var picLoad = $("#placeholder").attr("src");
  // Botoes
  $(".btPic").click(function(){
		var picNova = $(this).attr("href");
		//fundo
		var alturaStage = $.getDocHeight();
		$(".overlayP").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()+25;
     var posicaoXnew = $.getDocWidth()/2-(comP/2);
     $(".overlayBoxP").css({width:comP, height:altP, left:posicaoXnew});
		 $("#placeholder").fadeTo(250,1);
		 $(".overlayBoxP .btClose span").css("display", "block");
		}
		lerImg.src = picNova;
		return false;
  })
 	$(".overlayP").click(function(){fTiraP();})
 	$(".overlayBoxP .btClose").click(function(){fTiraP();})
})
