/**
 * @author Willy Barro
 */
jQuery(function() {
	// Logo e bg
	swfobject.embedSWF('_swf/home.swf?t='+ (Math.random()), 'content_home_holder', jQuery(window).width(), jQuery(window).height(), '9.0.0', false,
	{
		file: '_services/home.php',
		transition: 'fade',
		screencolor: '0xFFFFFF',
		repeat: true,
		shownavigation: false,
		showicons: false,
		overstretch: true,
		rotatetime: 3
	}, {wmode: 'transparent', scale: 'exactFit', align: "left"});
	
	// Resize
	window.setTimeout(resize, 1);
	window.onresize = resize;
	
	// Menu
	topMenu.init();
	
	// Topo
	topLogo.init();
	
	// Máscara de telefone
	if(jQuery.mask != undefined) jQuery('.maskTel input').mask('(99) 9999-999?9', {placeholder: '  '});
	
});


var topLogo = {
	props: {
		totalItens: 0,
		itemAtual: 0,
		tempo: 3
	},
	init: function() {
		topLogo.props.totalItens = jQuery('#top ul li').length;
		
		window.setInterval( topLogo.rotate, topLogo.props.tempo * 1000 );
		
		topLogo.ativa( 0 );
	},
	rotate: function() {
		if( (topLogo.props.itemAtual+1) < topLogo.props.totalItens) {
			var ativaItem = topLogo.props.itemAtual + 1;
		} else {
			var ativaItem = 0;
		}
		
		topLogo.ativa( ativaItem );
	},
	ativa: function(n) {
		jQuery('#top ul li').eq( topLogo.props.itemAtual ).fadeOut('medium', function() {
			jQuery('#top ul li').eq( n ).show();
		});
		topLogo.props.itemAtual = n;
	}
	
}

var isIE = jQuery.browser.msie;
ieTimeout = null;
var topMenu = {
	init: function() {
		jQuery('#menu > ul').addClass('princ');
		
		jQuery('#menu ul ul, #menu ul ul ul').hide();
		jQuery('#menu').show();
		var ulHeight = jQuery('#menu ul').height();
		
		jQuery('#menu .princ > li > a').bind('mouseenter', function() {
			jQuery('#menu *').removeClass('active');
			
			
			jQuery(this).addClass('active');
			jQuery(this.parentNode).addClass('active')
			jQuery('ul', this.parentNode).addClass('active');
		});
		
		jQuery('#menu ul').bind('mouseleave', function() { jQuery('#menu *').removeClass('active');  })
		
		jQuery('#menu .sectionheader')
		.bind('mouseenter', function() {
			if(ieTimeout) window.clearInterval(ieTimeout);
			var totUL = jQuery(this).parents().filter('ul').length;
			var topPX = ulHeight + 'px';
			var menuFilho = jQuery('ul:eq(0)', this);
			var paiMeio = (parseInt(jQuery(this).offset().left) + parseInt( (jQuery(this).width()/2) )) - jQuery('#menu').offset().left;
			
			
			menuFilho
			.css('marginTop', menuFilho.height()*-1)
			.css('visibility', 'hidden')
			.show();
			
			if(totUL == 1) {
				var liWidth = 0;
				jQuery('> li', menuFilho).each(function() { if(jQuery(this).width() != 990) { liWidth += parseInt( jQuery(this).width() ); } });
				
				var filhoLeft = paiMeio - (liWidth/2);
				
				// Se for ie, tira um pouco do left
				if(isIE) filhoLeft += 42;
				
				// Nao deixa o menu sumir
				if(filhoLeft < 0) {
					filhoLeft = 0;
				} else
				if( (filhoLeft + liWidth) >= 990 ) {
					filhoLeft = 990 - liWidth;
				}
				
				filhoLeft = filhoLeft + 15;
			}
			
			menuFilho
			jQuery('ul:eq(0)', this)
			.css('visibility', 'visible')
			.css('left', filhoLeft);
			
		})
		.bind('mouseleave', function() {
			
			jQuery('#menu .princ > li > a').removeClass('active');
			
			var sHeader = jQuery(this);
			jQuery('ul:eq(0)', sHeader).hide();
		});
		
	}
};


function resize() {
	var height = parseFloat( jQuery(window).height() );
	
	jQuery('#content_interna .content, #content_interna .content .dir, #estados').height( height - 160 - 170 );
	jQuery('#estados').height( height - 160 - 220 );
	jQuery('#content_home_holder').height( height );
}