$(document).ready(function() {

	/* Formulários */
	$(".validar").validate();
	
	/* Máscaras */
	$(".telefone").mask("(99) 9999-9999");
   	$(".cep").mask("99999-999");
   	$(".cnpj").mask("99.999.999/9999-99");
   	$(".cpf").mask("999.999.999-99");
   	$(".data").mask("99/99/9999");

	//MENU
	function megaHoverOver(){
		$(this).find(".sub").stop().fadeTo('fast', 1).show();
			
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {					
					rowWidth += $(this).width(); 
				});	
			};
		})(jQuery); 
		
		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
			
		} else { //If row does not exist...
			
			$(this).calcSubWidth();
			//Set Width
			$(this).find(".sub").css({'width' : rowWidth});
			
		}
	}
	
	function megaHoverOut(){ 
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  });
	}
 
 
	var config = {    
		 sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 100, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 100, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};
 
	$("ul#menu_links li .sub").css({'opacity':'0'});
	$("ul#menu_links li").hoverIntent(config);
	
	//SLIDE LINHA DE PRODUTOS
	var obj_width = 0;
	var obj_height = 0;
	var total = 0;
	var atual = 0;
	var last = $('.categoria_slider').size()-1;
	
	$('#slider img').live('mouseenter', function(){
		obj_width = $(this).attr('width');
		obj_height = $(this).attr('height');
		$(this).stop().animate({height: '+='+$(this).height()*0.4, width: '+='+$(this).width()*0.4}, 70);
	});
	
	$('#slider img').live('mouseleave', function(){
		$(this).stop().animate({height: obj_height, width: obj_width}, 100);
	});
	
	$('.categoria_slider').click(function(){
		go_slider($(this).index());
	});
	
	$('#prevBtn').live('click', function(){
		atual--;
		$('#slider ul').animate({left: '+='+$('#container').width()}, 800);
		controle();
	});
	
	$('#nextBtn').live('click', function(){
		atual++;
		$('#slider ul').animate({left: '-='+$('#container').width()}, 800);
		controle();
	});
	
	go_slider(last);
	
	function go_slider(pos){
		atual = 0;
		total = $('#oculto .span').eq(pos).find('li').size();
		$('.categoria_slider').css('color','#ee3224');
		$('.categoria_slider').css('background','#FFF');
		$('.categoria_slider').eq(pos).css('color','#FFF');
		$('.categoria_slider').eq(pos).css('background','#ee3224');
		$('#slider ul').slideUp('normal', 'easeOutQuart', function(){
			var novo = $('#oculto .span').eq(pos).clone();
        	$('#slider ul').html(novo).animate({opacity: 1}, 500);
			$('#slider ul').css('width',(total * $('#container').width()));
			$('#slider ul').animate({left: '0'}, 0);
			$('#slider ul').slideDown('fast');
		});
		
		controle();
	}
	
	function controle(){
		if (atual == 0) {
			$('#prevBtn').hide();
			$('#nextBtn').show();
		} else if (atual < total-1) {
			$('#prevBtn').show();
			$('#nextBtn').show();
		} else {
			$('#prevBtn').show();
			$('#nextBtn').hide();
		}
	}
	
	//DOWNLOADS
	$('#form_login #login, #senha').focus(function(){
		$(this).css('background','#ee3324')
		$(this).css('color','#FFF')
	});
	
	$('#form_busca_download #buscar').focus(function(){
		if($(this).val() == "Nome do arquivo ou palavra-chave"){
			$(this).val("");
		}
	});
	
	$('#form_busca_download #buscar').blur(function(){
		if($(this).val() == ""){
			$(this).val("Nome do arquivo ou palavra-chave");
		}
	});
	
	$(".iframe").fancybox({
		'width'				: 300,
		'height'			: 210,
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'type'				: 'iframe'
	});

	
	//RELATORIO ANUAL
	$('#accordion').accordion({
		autoheight:false
	});
 
 	//BUSCA GERAL
	$('#form_busca #busca').focus(function(){
		if($(this).val() == "O que você procura?"){
			$(this).val("");
		}
	});
	
	$('#form_busca #busca').blur(function(){
		if($(this).val() == ""){
			$(this).val("O que você procura?");
		}
	});
	
	//NO MUNDO
	$(function(){
		$('.mundo').mobilymap({
			position: '480 440',
			popupClass: 'bubble',
			markerClass: 'point',
			popup: true,
			cookies: false,
			caption: true,
			setCenter: true,
			navigation: true,
			navSpeed: 1000,
			navBtnClass: 'navBtn',
			outsideButtons: '.map_buttons a',
			onMarkerClick: function(){},
			onPopupClose: function(){},
			onMapLoad: function(){}
		});
	});
	
	//BUSCA E CATALOGO
	$('#form_busca_catalogo #buscar').focus(function(){
		if($(this).val() == "Nome do produto ou palavra-chave"){
			$(this).val("");
		}
	});
	
	$('#form_busca_catalogo #buscar').blur(function(){
		if($(this).val() == ""){
			$(this).val("Nome do produto ou palavra-chave");
		}
	});
	
	$('#fechar_detalhar').click(function(){
		$(this).parent().parent().fadeOut('normal');
	});
	
});

//COLUNAS MAXH
$(window).load(function() {
	var sizes = [];
	$('#columns .box_chamada').each(function(){
		 sizes.push($(this).height()); 
	});
	var max = Math.max.apply(null, sizes);
	$('#columns').height($('#columns .box_chamada').height(max+$('#columns ul').height()));
});

//DOWNLOADS
function logar(){
	if (!document.form_login.login.value){
		document.form_login.login.focus();
		return false;
	} else if (!document.form_login.senha.value){
		document.form_login.senha.focus();
		return false;
	} else {
		document.form_login.submit();
	}
}
