var GLOBAL_EASING = 'easeInOutQuad';
var opacitySupport = $.support.opacity; //this is so we can do something else besides fades in IE cuz of it's abysmal handling of fading pngs

// --- IWC NAV 2: ELECTRIC BOOGALOO
var IwcNav2 = function(opts){
	var surrogate = this;
	this.defaults = {
		navRef : null,
		dur : 400,
		easingMethod : GLOBAL_EASING
	};
	this.nav = $('#pageHeader').find('.nav');
	this.dds = this.nav.find('.dd');
	this.ddLs = this.dds.parent();
	this.drawer = $('<div id="ddDrawer"></div>');
	this.indicatorTrack = $('<div class="indicatorTrack"></div>');
	this.indicator = $('<div class="indicator"></div>');
	this.indicatorLeft = $('<div class="indicatorLeft"></div><div class="indicatorRight"></div><div class="setame"></div>');
	this.isDrawerDown = false;
	this.iMoving = false;
	this.isOverLink = false;
	this.options = $.extend(this.defaults, opts);
	this.nav = $('#pageHeader').find('.nav');
	this.searchForm = this.nav.find('li.navSearch');
	this.searchField = this.searchForm.find('input.text');
	this.sfWidth = 140;
	this.optTrigger = this.searchForm.find('.optionTrigger');
	this.searchDD = this.searchForm.find('.searchOptions');
	this.optsList = this.searchDD.find('ul');
	this.isDrawerDown = false;
	this.isOverLink = false;
	
	// MAIN SETUP
	this.indicator.append(this.indicatorLeft);
	this.indicatorTrack.append(this.indicator);
	this.drawer.append(this.indicatorTrack).insertAfter(this.nav);
	
	this.drawer.bind('mouseleave', function(){
		if(!this.isOverLink){
			surrogate.hideDrawer();
		}
	});
	
	//highlight the proper nav element
	var loc = document.location.pathname.split('/')[1];
	if(loc == 'news' || loc =='library'){
		loc = 'about'
	}else if(loc == 'service'){
		loc = 'retail'
	}
	$('#topNav > li > a').not('[href="/' + loc +'/"]').parent().removeClass('active').end().end().filter('[href="/' + loc +'/"]').parent().addClass('active');
	
	// DD SETUPS
	var hoverConfig = {
		interval:this.options.dur/2,
		timeout:this.options.dur,
		over:function(){ddOver(this)},
		out:function(){ddOff(this)}
	}
	var ddOver = function(elem){
		var elem = $(elem);
		var theDD = $(elem).children('.dd');
		var theA = elem.children('a:first-child');
		var drawerMode = surrogate.isDrawerDown == true ? 'fade' : 'slide';
		if(!opacitySupport){//Why IE?? WHY YOU SO BAD AT ALPHA TRANSPARENCY??
			drawerMode = 'ie';
		}
				
		surrogate.showDropDown(theDD, { mode: drawerMode });
		var theHeight =  theDD.children().outerHeight();
		// if(theDD.hasClass('collection')){
		// 	theHeight = 330;
		// }
		
		//var targX = theA.outerWidth()/2 + elem.position().left + 4;
		var targX = elem.position().left - 15;
		var targWidth = theA.outerWidth();
		// console.log(theA.offset().left);
		//$('.shadow').show();
		//alert($(elem).find('a').css('color'));
		
		
		$('.nav').find('a').css('color', '#7B8693');
		$(elem).find('a').css('color', '#fff');
		//alert(targWidth);
		surrogate.showDrawer(theHeight, targX, targWidth);
	}
	var ddOff = function(elem){
		surrogate.hideDropDown($(elem).children('.dd'));
		if(!surrogate.isOverLink){
			surrogate.hideDrawer();$('.nav').find('a').css('color', '#ccc');
		}	

	}
	this.ddLs.each(function(){
		$(this).hoverIntent(hoverConfig);
	}).addClass('ddLink').bind("mouseenter", function(){
		surrogate.isOverLink = true;
	}).bind("mouseleave", function(){
		surrogate.isOverLink = false;
	});
	
	this.dds.each(function(){
		var t = $(this);
		if(t.hasClass('collection')){
			t.children().wrapAll('<div class="inner"/>');
		}
		
		t.css({visibility:'visible'}).children().css({marginTop: -t.height()});
		
		if(t.hasClass('userInfo')){
			t.css({left: -t.parent().position().left, top:t.parent().position().top + t.parent().height()});
		}
	});
	
	// SEARCH SETUP
	var hoverConfigSearch = {
		interval:100,
		timeout:400,
		over:function(){surrogate.searchOver()},
		out:function(){surrogate.searchOut()}
	}
	this.searchForm.hoverIntent(hoverConfigSearch);
	this.searchField.width(0);
	this.optsList.show().css({marginTop: -(this.optsList.height() + 22) }).delegate('a', 'click', function(e){
		surrogate.searchOptionsClick(e);
		return false;
	}).hide();
	this.optTrigger.css({right:5}).bind('click', function(){
		$(this).addClass('active');
		surrogate.optsList.show().animate({marginTop:0}, {duration:surrogate.options.dur, easing:surrogate.options.easingMethod});
		return false;
	});
}
IwcNav2.prototype.showDropDown = function(dd, opts){
	var surrogate = this;
	var defaults = { mode : 'slide' };
	var opts = $.extend(defaults, opts);
	var theCont = dd.children();
	
	if(opts.mode == 'fade'){
		this.dds.children().not(theCont).each(function(){
			var t = $(this);
			t.fadeTo(surrogate.options.dur/3, 0, function(){t.hide()});
		});
		theCont.hide().delay(surrogate.options.dur).css({marginTop:0}).fadeTo(surrogate.options.dur/2, 1).addClass('active');
	}else if(opts.mode =="ie"){//sigh...
		this.dds.children().not(theCont).each(function(){
			var t = $(this);
			t.animate({marginTop: -t.outerHeight()}, {duration:surrogate.options.dur, easing:surrogate.options.easingMethod, complete:function(){t.hide()}});
		});
		theCont.show().css({marginTop:-theCont.outerHeight()}).animate({marginTop: 0}, {duration:surrogate.options.dur, easing:surrogate.options.easingMethod}).addClass('active');
	}else{
		theCont.show().fadeTo(0,1).css({marginTop:-theCont.outerHeight()}).animate({marginTop: 0}, {duration:surrogate.options.dur, easing:surrogate.options.easingMethod}).addClass('active');
	}
	
	// TODO:fix bg arrow for IE
	if(opts.mode !="ie"){
		dd.siblings('a').animate({backgroundPosition : '50% 50px'}, {duration:surrogate.options.dur/2, easing:surrogate.options.easingMethod});
	}
}

IwcNav2.prototype.hideDropDown = function(dd, opts){
	var surrogate = this;
	var defaults = { mode : 'slide' };
	var opts = $.extend(defaults, opts);
	
	// TODO:fix bg for IE
	if(opacitySupport){
		dd.siblings('a').stop().delay(surrogate.options.dur/2).animate({backgroundPosition : '50% 36px'}, {duration:surrogate.options.dur/2, easing:surrogate.options.easingMethod});
	}
}
IwcNav2.prototype.showDrawer = function(ht, indicatorX, wt){	
	var surrogate = this;
	var widthMe = wt + "px";
	if(!this.isDrawerDown){this.indicator.css({top:-10,left:indicatorX	});}
	this.indicator.css("width", widthMe);
	this.indicator.animate({top:0, left: indicatorX}, {duration:surrogate.options.dur/1.5, easing:surrogate.options.easingMethod});
	this.drawer.stop().animate({height:ht}, {duration:surrogate.options.dur, easing:surrogate.options.easingMethod, complete: function(){ surrogate.isDrawerDown = true; }});
	
}
IwcNav2.prototype.hideDrawer = function(){
	var surrogate = this;
	this.indicator.stop().animate({top:-10}, {duration:surrogate.options.dur/1.5, easing:surrogate.options.easingMethod});
	this.drawer.stop().animate({height:0}, {duration:surrogate.options.dur, easing:surrogate.options.easingMethod, complete: function(){ surrogate.isDrawerDown = false;}});
	this.dds.children().each(function(){
		$(this).animate({marginTop:-$(this).outerHeight()}, {duration:surrogate.options.dur, easing:surrogate.options.easingMethod, queue:false});
	});
}

IwcNav2.prototype.searchOver = function(){
	this.searchField.animate({width:140}, {duration:this.options.dur, easing:this.options.easingMethod}).focus();
	this.optTrigger.animate({right:175}, {duration:this.options.dur, easing:this.options.easingMethod});
}
IwcNav2.prototype.searchOut = function(){
	var surrogate = this;
	this.optsList.animate({marginTop: -(surrogate.optsList.height() + 22) }, {duration:surrogate.options.dur, easing:surrogate.options.easingMethod, complete:function(){
		surrogate.searchField.animate({width:0}, {duration:surrogate.options.dur, easing:surrogate.options.easingMethod}).blur();
		surrogate.optTrigger.animate({right:0}, {duration:surrogate.options.dur, easing:surrogate.options.easingMethod}).removeClass('active');
		surrogate.optsList.hide();
	}});
	
}
IwcNav2.prototype.searchOptionsClick = function(e){
	var t = $(e.target);
	t.addClass('selected').parent().siblings().find('a').removeClass('selected');
	$('#id_filter').val(t.attr('href').substring(1));
	
	if(this.searchField.val() != ''){
		this.searchForm.find('form').find('.submit').click();
	}
}

var setupL10n = function(){
	var link = $('#topNav li#l10n');
	var dd = link.find('ul');
	dd.show();
	var ht = dd.outerHeight();
	dd.hide().css({'marginTop':-ht}).show();
	
	link.hoverIntent({
		over: function(){
			dd.stop().animate({'marginTop':0}, 300);
		},
		out: function(){
			dd.stop().animate({'marginTop':-ht}, 300);
		},
		timeout:200
	});
}

$(document).ready(function() {
	var nav = new IwcNav2();
	setupL10n();
});



//Menu Vertical - talent.abatista@bvsistemas.com.br

var menuids=["sidebarmenu1"] //adicionar o ID do menu, pode ser varios separados por virgula.

function initsidebarmenu(){
	$("#sidebarmenu1").find(".folder").mouseover(function() {
		$(this).find(".sub li").last().css("background","none");
		$(this).find(".sub").show();
		$(this).find(".subInfolder").addClass("subfolderstyle");
		$(this).find(".subInfolder .on").show();
		$(this).find(".subInfolder .off").hide();
	}).mouseout(function(){
    	$(this).find(".sub").hide();
    	$(this).find(".subInfolder").removeClass("subfolderstyle");	
    	$(this).find(".subInfolder .on").hide();
		$(this).find(".subInfolder .off").show();
 	});
	
	// Menu Interno.
	$("#sidebarmenu2").find(".nivel1").mouseover(function() {
		//$(this).addClass("ChangeFundo");
		$(this).find(".sub li").last().css("background","none");
		$(this).find(".sub").show();
		$(this).find("a:first").addClass("subfolderstyle");
		//$(this).find(".subInfolder .on").show();
		//$(this).find(".subInfolder .off").hide();
	}).mouseout(function(){
		//$(this).removeClass("ChangeFundo");
    	$(this).find(".sub").hide();
    	$(this).find("a:first").removeClass("subfolderstyle");	
    	//$(this).find(".subInfolder .on").hide();
		//$(this).find(".subInfolder .off").show();
 	});
 	
 	$("#sidebarmenu2").find(".nivel1:last").css("background","transparent");
 	
 	
 	// Menu Private
 	$('#sidebarmenu3 > .folder').each(function(){
 		
		var qtdUl = $(this).find('ul').size();	

		 if (qtdUl <= 1){
				
			$(this).mouseover(function() {
				$(this).find(".sub li").last().css("background","none");
				$(this).find(".sub").show();
				$(this).find(".subInfolder").addClass("subfolderstyle");
				$(this).find(".subInfolder .on").show();
				$(this).find(".subInfolder .off").hide();
			}).mouseout(function(){
		    	$(this).find(".sub").hide();
		    	$(this).find(".subInfolder").removeClass("subfolderstyle");	
		    	$(this).find(".subInfolder .on").hide();
				$(this).find(".subInfolder .off").show();
		 	});	
		
		}else{
		
			$(this).find('ul:first').removeClass('sub');
			$(this).find('ul:first').addClass('vertical');
			$('.sub2').parent().css("background","none");
			$('.vertical').parent().find('a:first').css("cursor","text");
			
		 	$('.sub1').parent().mouseover(function() {
			
				$(this).find("li").last().css("background","none");
				$(this).find('.sub1').show();
				$(this).find(".subInfolder").addClass("subfolderstyle");
				$(this).find(".subInfolder .on").show();
				$(this).find(".subInfolder .off").hide();
			}).mouseout(function(){
		    	$(this).find('.sub1').hide();
		    	$(this).find(".subInfolder").removeClass("subfolderstyle");	
		    	$(this).find(".subInfolder .on").hide();
				$(this).find(".subInfolder .off").show();
		 	});
		
		}		
	});	
	
	// Menu VAM
 	$('#sidebarmenu4 > .folder').each(function(){
 		
		var qtdUl = $(this).find('ul').size();	
		 if (qtdUl <= 1){
				
			$(this).mouseover(function() {
				$(this).find(".sub li").last().css("background","none");
				$(this).find(".sub").show();
				$(this).find(".subInfolder").addClass("subfolderstyle");
				$(this).find(".subInfolder .on").show();
				$(this).find(".subInfolder .off").hide();
			}).mouseout(function(){
		    	$(this).find(".sub").hide();
		    	$(this).find(".subInfolder").removeClass("subfolderstyle");	
		    	$(this).find(".subInfolder .on").hide();
				$(this).find(".subInfolder .off").show();
		 	});	
		
		}else{
		
			$(this).find('ul:first').removeClass('sub');
			$(this).find('ul:first').addClass('vertical');
			//$('.sub2').parent().css("background","none");
			$('.vertical').parent().find('a:first').css("cursor","text");
			
		 	$('.sub1').parent().mouseover(function() {
			
				$(this).find("li").last().css("background","none");
				$(this).find('.sub1').show();
				$(this).find(".subInfolder").addClass("subfolderstyle");
				$(this).find(".subInfolder .on").show();
				$(this).find(".subInfolder .off").hide();
			}).mouseout(function(){
		    	$(this).find('.sub1').hide();
		    	$(this).find(".subInfolder").removeClass("subfolderstyle");	
		    	$(this).find(".subInfolder .on").hide();
				$(this).find(".subInfolder .off").show();
		 	});
		
		}		
	});	
		

 	
 	
}
// carrega o menu
if (window.addEventListener)
window.addEventListener("load", initsidebarmenu, false)
else if (window.attachEvent)
window.attachEvent("onload", initsidebarmenu)
//Menu Vertical - talent.abatista@bvsistemas.com.br

// Menu IB - Valida CPF
function validaCPF (link){ 
	var form = document.getElementById("ib");
	var cpf = new String(form.txtCpf.value);
	cpf = cpf.replace(".", "");
	cpf = cpf.replace(".", "");
	cpf = cpf.replace("-", "");
	//alert(cpf);
	
	if (cpf.length != 11 
		|| cpf == "00000000000" 
		|| cpf == "11111111111" 
		|| cpf == "22222222222" 
		|| cpf == "33333333333" 
		|| cpf == "44444444444" 
		|| cpf == "55555555555" 
		|| cpf == "66666666666" 
		|| cpf == "77777777777" 
		|| cpf == "88888888888" 
		|| cpf == "99999999999") {
		
			$.colorbox({opacity:0.6,html:'<div class="meAlert"><br/><br/><p>CPF inválido. Por favor, informe novamente.</p><br/><p><a href="#" onclick="$.colorbox.close();" class="bt_okModal">OK</a></p></div>'});
			return false;
	}
	soma = 0;
	for (i=0; i < 9; i ++)
		soma += parseInt(cpf.charAt(i)) * (10 - i);
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		resto = 0;
	if (resto != parseInt(cpf.charAt(9))) {
	
		$.colorbox({opacity:0.6,html:'<div class="meAlert"><br/><br/><p>CPF inválido. Por favor, informe novamente.</p><br/><p><a href="#" onclick="$.colorbox.close();" class="bt_okModal">OK</a></p></div>'});
		return false;
	}
	soma = 0;
	for (i = 0; i < 10; i ++)
		soma += parseInt(cpf.charAt(i)) * (11 - i);
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		resto = 0;
	if (resto != parseInt(cpf.charAt(10))) {
		
		$.colorbox({opacity:0.6,html:'<div class="meAlert"><br/><br/><p>CPF inválido. Por favor, informe novamente.</p><br/><p><a href="#" onclick="$.colorbox.close();" class="bt_okModal">OK</a></p></div>'});
		return false;
	}
	//alert('CPF correto.');
	form.txtCpf.value = cpf;
	form.action = link + cpf;
	form.submit();
}
// Menu IB - Valida CPF


// Reseta a fonte para o tamanho padrão 
function resetFont() {
 var originalFontSize = $('.text').css('font-size');	 
   $('.text, .text-detalhes').css('font-size', '0.875em'); 
   
   Cufon.replace('h1', { fontFamily: 'Formata' });
   Cufon.replace('h2', { fontFamily: 'Formata2' });
   Cufon.replace('.list-title', { fontFamily: 'Formata2' });	
}
// Aumenta a fonte
function increaseFont(){	 
   var currentFontSize = $('.text').css('font-size');
   var currentFontSizeNum = parseFloat(currentFontSize, 10);
   var newFontSize = currentFontSizeNum*1.2;
   if(newFontSize < 26.4){
   	$('.text, .text-detalhes').css('font-size', newFontSize);  		
  		Cufon.replace('h1', { fontFamily: 'Formata' });
		Cufon.replace('h2', { fontFamily: 'Formata2' });
		Cufon.replace('.list-title', { fontFamily: 'Formata2' });	
   }   
   return false;	  
}
// Diminui a fonte
function decreaseFont(){

   var currentFontSize = $('.text').css('font-size');
   var currentFontSizeNum = parseFloat(currentFontSize, 10);
   var newFontSize = currentFontSizeNum*0.8;
   if(newFontSize > 8){
   	$('.text, .text-detalhes').css('font-size', newFontSize);
   	Cufon.replace('h1', { fontFamily: 'Formata' });
	Cufon.replace('h2', { fontFamily: 'Formata2' });
	Cufon.replace('.list-title', { fontFamily: 'Formata2' });	
   }   
   return false;	
}

// Mascara Nova para CPF

function formataCPF(campo){
	campo.value = campo.value.replace('/','');
	var retorno = aplicarMascara(campo,'999.999.999-99');
	campo.maxLength=15;
	return retorno;
}	

function aplicarMascara(campo, mascara) {
	campo.maxLength = mascara.length;
	mascara = mascara.toUpperCase()
	var valor = campo.value;
	var resultado = "";
	// Remove todo caractere que nao seja numero e letras
	for(i = 0 ; i < mascara.length ; i++){
	               if(mascara.substr(i,1) != 'X' && mascara.substr(i,1) != '9'){
	                               valor = valor.replace(mascara.substr(i,1),'');
	               }
	}
	// Valor contem os dados em branco
	var iValor = 0;
	for(i = 0 ; i < mascara.length; i++){
     var itemMascara = mascara.substr(i,1);
     var itemValor = valor.substr(iValor,1);
     if(itemValor != ''){
                     if(itemMascara != 'X' && itemMascara != '9'){
                                     // Se a mascara nao disser que e numero ou letra
                                     // Atualiza com o conteudo da mascara
                                     resultado = resultado + itemMascara;
                                     iValor--;
                     }
                     if(itemMascara == 'X'  && getTipo(itemValor)  == 2){
                                     // Codigo correto para o campo
                                     // Atualiza no resultado
                                     resultado = resultado + itemValor
                                     
                     }else if(itemMascara == '9' && getTipo(itemValor) == 1){
                                     // Codigo correto para numero
                                     // Atualiza
                                     resultado = resultado + itemValor;
                                     //iValor++;
                     }
                     iValor++;
     }
	
	}
	if(campo.length < mascara.length){
	               campoCompleto = false;
	}else{
	               campoCompleto = true;
	}
	campo.value = resultado;
	return false;
}
	
function getTipo(valor){ 
	var codigo = valor.charCodeAt(0);
 	// codigos numeros
 	if(codigo >= 48 && codigo <= 57){
  		return 1;
 	}
 	// Codigos alfanumericos
 	if((codigo >= 97 && codigo <= 122) || (codigo >= 65 && codigo <= 90)){
  		return 2;
 	}
 	// Codigos de controle
 	if (codigo >= 0 && codigo < 48){
  		return 3;
 	}
 return 0;
}

