var DOM = YAHOO.util.Dom;
var REG = YAHOO.util.Region;

function changeLIPosition(){
	try{
	
		DOM.getElementsByClassName('ultopo', 'ul', document.body, function(obj){
		
			var childs = DOM.getChildrenBy(obj, function(objc){
							return (objc.nodeName == 'LI' && (objc.className == 'sel' || objc.className == 'unsel')) ? true : false;
						});
					
			var totWidthLI = 0;
			
			for(var i = 0; i <= childs.length - 1; i++){
					
					//Variável que guarda o comprimento total das lis com link e que será passada à função widthFirstLI
					totWidthLI = totWidthLI + REG.getRegion(childs[i]).width + 1;
					
					var xCoord = 0;
		
					//Verificar a posição X da li
					if (childs[i].offsetParent){
						xCoord = childs[i].offsetLeft;
					}
					
					if (xCoord >= 478){
					
						var lichilds = DOM.getElementsBy(function(objlink){
						
											return (objlink.nodeName == 'LI') ? true : false;
											
										}, 'li', childs[i]);
						
						var ulpai = DOM.getElementBy(function(objul){
										return (objul.nodeName == 'UL') ? true : false;
									}, 'ul', childs[i]);
						
						var liToAppend = new Array();
						
						for(var j = lichilds.length - 1; j >= 0; j--){
							
							//Criar o novo elemento e atribuir-lhe as propriedades necessárias
							var linew = document.createElement('li');
							
							linew.innerHTML = lichilds[j].innerHTML;
							
							
							linew.style.cssFloat = 'right';
							linew.style.styleFloat = 'right'; //For IE
						
							linew.style.marginLeft = '0px';
							linew.style.marginRight = '28px';
							
							//Adicionar novo elemento ao array
							liToAppend.push(linew);
							
							//Renover elemento actual
							ulpai.removeChild(lichilds[j]);
							
						}
						
						//Adicionar elementos novos à lista
						for(var k = 0; k <= liToAppend.length - 1; k++){
							ulpai.appendChild(liToAppend[k]);
						}

					}	
				
			}
			
			widthFirstLI(totWidthLI);
			
		});
	}
	catch(err){
	}
}

function widthPositionSpan(){
	try{
		
		DOM.getElementsByClassName('ultopo', 'ul', document.body, function(obj){
		
			DOM.getElementsBy(function(objli){
			
				return (objli.nodeName == 'LI') ? true : false;
				
			}, 'li', obj, function(objli){
			
				var divChild = DOM.getFirstChildBy(objli, function(objd){
									return (objd.nodeName == 'DIV') ? true : false;
							   });
							   
				if(divChild){
				
					var spanDiv = DOM.getFirstChild(divChild);
					
					spanDiv.style.width = REG.getRegion(objli).width + 'px';
					
					var xCoord = 0;
		
					//Verificar a posição X da li
					if (objli.offsetParent){
						xCoord = objli.offsetLeft;
					}
					
					spanDiv.style.marginLeft = xCoord + 'px';
					
				}
				
			});
		
		});
		
	}
	catch(err){
	}
}

function setTopSize(){
	try{
		
		DOM.getElementsByClassName('ultopo', 'ul', document.body, function(obj){
		
			var heightULTop = 0;
			var childUL = new Boolean(false);
			var childDIV = new Boolean(false);
			
			DOM.getElementsBy(function(objli){
				
				return (objli.className == 'sel') ? true : false;
				
			}, 'li', obj, function(objli){

				child = DOM.getFirstChildBy(objli, function(objn){
							return (objn.nodeName == 'UL' || objn.nodeName == 'DIV') ? true : false;
						});
				
				if(child.nodeName == 'UL'){
					childUL = true;
					heightULTop = REG.getRegion(child).height - 10;
				}
				else if(child.nodeName == 'DIV'){
					childDIV = true;
					heightULTop = 25 - 10;
				}
				
			});
			
			var ctheader = DOM.getElementBy(function(obj){
						return (obj.className == 'containerheader') ? true : false;
					}, 'div', document.body);
					
			var barnav = DOM.getElementBy(function(obj){
							return (obj.className == 'barnav') ? true : false;
						}, 'div', document.body);
			
			if (childUL == true){
				barnav.style.paddingBottom = '31px';
			}
			else if(childDIV == true){
				barnav.style.paddingBottom = '51px';
			}
			else{
				heightULTop = 0;
				barnav.style.paddingBottom = '51px';
			}
			
			ctheader.style.height = (130 + heightULTop) + 'px';
			
		});
	
	}
	catch(err){
	}
}

function hoverInputImage(){
	try{
	
		DOM.getElementsBy(function(obj){
		
			return (obj.type == 'image') ? true : false;
			
		}, 'input', document.body, function(obj){
		
			obj.onmouseover = function(){
				this.src = 'sysimages/btpesqhome_hover.png';
			};
			
			obj.onmouseout = function(){
				this.src = 'sysimages/btpesqhome.png';
			};
			
		});
		
	}
	catch(err){
	}
}

function addEventImgLoja(){
	try{
		
		DOM.getElementsByClassName('ulimgs', 'ul', document.body, function(obj){
		
			DOM.getElementsBy(function(objli){
			
				return (objli.nodeName == 'LI') ? true : false;
				
			}, 'li', obj, function(objli){
			
				objli.onclick = function(){
				
					var sel = DOM.getElementBy(function(objli2){
								return (objli2.className == 'sel') ? true : false;
							}, 'li', obj);
					
					if(sel){
						sel.className = 'unsel';
						sel.getElementsByTagName('span')[0].style.visibility = 'hidden';
					}
					
					var imgBig = DOM.getElementBy(function(objd){
									return (objd.className == 'imgback') ? true : false;
								}, 'div', document.body);
								
					if(imgBig){
						imgBig.getElementsByTagName('img')[0].src = this.getElementsByTagName('img')[0].src;
					}
					
					this.className = 'sel';
					this.getElementsByTagName('span')[0].style.visibility = 'visible';
				};
				
			});
			
		});
		
	}
	catch(err){
	}
}


YAHOO.util.Event.onDOMReady(function(){
	try{
		changeLIPosition();
		widthPositionSpan();
		setTopSize();
		hoverInputImage();
		addEventImgLoja();
	}
	catch(err){
	}
}, document.body, true);


function widthFirstLI(totW){
	try{
		
		var liFirst = DOM.getElementBy(function(obj){
						return (obj.className == 'lifirst') ? true : false;
					}, 'li', document.body);
					
		if(liFirst){
			liFirst.style.width = (935 - totW) + 'px';
		}
		
	}
	catch(err){
	}
}

function RenderTTF()
{
  DATAFUNCS.RenderFonts('tit_h1',{
   fontSize         : '18',
   backgroundColor  : 'FFFFFF',
   color            : '333333',
   fontFile         : 'fonts/gilb.TTF',
   transparent      : '1'
  });
   DATAFUNCS.RenderFonts('tit_h1_bloco',{
   fontSize         : '18',
   backgroundColor  : 'FFFFFF',
   color            : '333333',
   fontFile         : 'fonts/gil.TTF',
   transparent      : '1'
  });
  DATAFUNCS.RenderFonts('tit_h2',{
   fontSize         : '13',
   backgroundColor  : 'FFFFFF',
   color            : '000000',
   fontFile         : 'fonts/gil.TTF',
   transparent      : '1'
  });
  DATAFUNCS.RenderFonts('tit_h2_ext',{
   fontSize         : '11.25',
   backgroundColor  : 'FFFFFF',
   color            : '333333',
   fontFile         : 'fonts/gil.TTF',
   transparent      : '1'
  });
  DATAFUNCS.RenderFonts('tit_h2_l',{
   fontSize         : '13',
   backgroundColor  : 'FFFFFF',
   color            : '000000',
   fontFile         : 'fonts/gil.TTF',
   transparent      : '1'
  });
} 
function ver_pesquisa(p){
    var _p = document.getElementById(p);
    if(_p.value.length<3 || _p.value=="Pesquisa..."){
      alert("Deve preencher a sua pesquisa com no minimo 3 letras");
      return false;
    } else {
      return true;
    } 
  
}


var DOM = YAHOO.util.Dom;
var EVT = YAHOO.util.Event;

function validate_form(_obj,lg){
  var errors = false;
  var maily = false;
	DOM.getElementsByClassName(
		'required',
		null,
		_obj,
		function(e){
  			var _tag  = e.tagName;
  			var _type = e.getAttribute('type');
  			var _emai = e.getAttribute('email');
  			var _fich = e.getAttribute('fich');
        var _sel  = e.getAttribute('selecta');
        
        
  			if(_tag == 'TEXTAREA' || _type == 'text' || _type == 'password'){
  				if ((e.value == '') || (e.value == 'Preenchimento obrigatório.')){
            var _varid = e.getAttribute('id');
            e.style.border = '1px solid #D20000';
  					e.style.color  = '#D20000';

  					
  					if(errors == false) errors = true;
  					
  				} else {
  				  var idinput =  e.getAttribute('id');
  				  if (idinput=='confpassword') {
  				    var pass1 = document.getElementById('password').value;
  
  				    if (pass1 != e.value){

                
                var _varid = e.getAttribute('id');
                e.style.border = '1px solid #D20000';
      					e.style.color  = '#D20000';

      					
      					document.getElementById('password').style.border = '1px solid #D20000';
      					document.getElementById('password').style.color  = '#D20000';

      					
      					maily = true;
              } else {
                var _varid = e.getAttribute('id');
      				  e.style.border = '1px solid #6889bb';
      					e.style.color  = '#666666';
      					
      					document.getElementById('password').style.border = '1px solid #6889bb';
      					document.getElementById('password').style.color  = '#666666';
      					maily = false;
              }
  				  
            } else {
    				  var _varid = e.getAttribute('id');
    				  e.style.border = '1px solid #6889bb';
    					e.style.color  = '#666666';
    				}
  				}
  				
  				if(_emai == 1){
  					if(verify_email(e.value) == false){
  						var _varid = e.getAttribute('id');
              e.style.border = '1px solid #D20000';
    					e.style.color  = '#D20000';

  						
  						maily = true;
  						
  					} else {
  					  var _varid = e.getAttribute('id');
      				e.style.border = '1px solid #6889bb';
    					e.style.color  = '#666666';
    					maily = false;
  					}
  				}
  			} else if(_fich == 1){
  			  if(e.value == ''){
            var _varid = e.getAttribute('id');
            e.style.border = '1px solid #D20000';
  					e.style.color  = '#D20000';

  					if(errors == false) errors = true;
          } else {
  					var _varid = e.getAttribute('id');
  				  e.style.border = '1px solid #6889bb';
  					e.style.color  = '#666666';
  				}
  			} else if(_sel == 1) {
          if ((e.value=="") || (e.value==0)){
            var _varid = e.getAttribute('id');
            e.style.border = '1px solid #D20000';
  					e.style.color  = '#D20000';

						if(errors == false) errors = true;
          } else {
            var _varid = e.getAttribute('id');
  				  e.style.border = '1px solid #6889bb';
  					e.style.color  = '#666666';
          }
  			} else if(_type == 'checkbox') {
  				if(e.checked == false){

  					if(errors == false) errors = true;
  				}
  			}
  		
		}
	);
	if(maily == true && errors == false){
     alert("Email inválido.");
    return false;
  }else{
  	if(errors == true){
       alert("Verifique os campos obrigatórios");
  		return false;
  	} else {
  		return true;
  	}
	}
}


function verify_email(_mail){
	
  var status = false;     
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	
  if (_mail.search(emailRegEx) == -1) {
		status = false;
	} else {
		status = true;
	}
	return status;
}

function valida_email(email){
  var e = email.value;
  var erro = 0;
  if (e!=''){
    if (verify_email(e) == false){

      erro=1;
    }
  } else {

    erro=1;
  }
  if(erro==1){  
    return false;
  } else {
    return true;
  }
}

function verify_number(field){
  if (isNaN(field.value)) 
  {
    alert("Só pode introduzir caracteres numéricos.");
    field.value='';
    field.focus();
  }
}
function muda_location(este){
  location.href=este.value;
}


function validate_news(_obj,lg){
  var errors = false;

	DOM.getElementsByClassName(
		'required',
		null,
		_obj,
		function(e){
  			var _tag  = e.tagName;
  			var _type = e.getAttribute('type');
  			var _emai = e.getAttribute('email');
  			var _fich = e.getAttribute('fich');
        var _sel  = e.getAttribute('selecta');
        
        
  			if(_tag == 'TEXTAREA' || _type == 'text' || _type == 'password'){
  				if ((e.value == '') || (e.value == 'Preenchimento obrigatório.')){
            var _varid = e.getAttribute('id');
            

  					
  					if(errors == false) errors = true;
  					
  				} else {
  				  var idinput =  e.getAttribute('id');
  				  if (idinput=='confpassword') {
  				    var pass1 = document.getElementById('password').value;
  
  				    if (pass1 != e.value){

                
                var _varid = e.getAttribute('id');
                
      					maily = true;
              } else {
                var _varid = e.getAttribute('id');
      				  
      					maily = false;
              }
  				  
            } else {
    				  var _varid = e.getAttribute('id');
    				  
    				}
  				}
  				
  				if(_emai == 1){
  					if(verify_email(e.value) == false){
  						var _varid = e.getAttribute('id');
              
  						maily = true;
  						
  					} else {
  					  var _varid = e.getAttribute('id');
      				
    					maily = false;
  					}
  				}
  			} else if(_fich == 1){
  			  if(e.value == ''){
            var _varid = e.getAttribute('id');
            

  					if(errors == false) errors = true;
          } else {
  					var _varid = e.getAttribute('id');
  				  
  				}
  			} else if(_sel == 1) {
          if ((e.value=="") || (e.value==0)){
            var _varid = e.getAttribute('id');
            

						if(errors == false) errors = true;
          } else {
            var _varid = e.getAttribute('id');
  				  
          }
  			} else if(_type == 'checkbox') {
  				if(e.checked == false){

  					if(errors == false) errors = true;
  				}
  			}
  		
		}
	);
	if(maily == true && errors == false){
     alert("Email inválido.");
    return false;
  }else{
  	if(errors == true){
       alert("Verifique os campos obrigatórios");
  		return false;
  	} else {
  		return true;
  	}
	}
}

