$(document).ready(function(){
													 
	//menu
	ajustaMenus();
	
	$('ul.lv1 > li').bind('mouseenter mouseleave',function() {
		$(this).children('ul.lv2').toggle();
		$(this).children('a').toggleClass('hover');
	});
	
	$('ul.lv2 > li').bind('mouseenter mouseleave',function() {
		$(this).children('a').toggleClass('hover');
	});
	
	$('ul.lv1 > li').last().css({border: 'none'});
	
	//função que troca as imagens dos banners da direita
	$('#banner').each(function(){
		var num = $(this).find('img').size();
		var time = $(this).attr('tempo');
		trocaBanners($(this),0,num,time);
	});
	
	//noticias
	$('.lista > ul > li').hover(function(){
		$(this).toggleClass('hover');
	});
	
	$('.programas > h4 > span').click(function(){
		$(this).parent().parent().toggleClass('open');
	});
	
});

function trocaBanners(obj,ind,num,time){
	//se só tiver um banner, ele não chama mais funções
	if (num>1){
		obj.find('img').eq(ind).stop('true','true').fadeOut(500);
		if (ind==num-1)
			ind = 0;
		else
			ind++;
		obj.find('img').eq(ind).stop('true','true').fadeIn(500);
		setTimeout(function() { trocaBanners(obj,ind,num,time); },time);
	}
	else {
		obj.find('img').show();
	}
	return;
}

function ajustaMenus(){
	$('ul.lv2').each(function(){
		var totalWidth = $(this).width();
		if (totalWidth < $(this).parent('li').width()){
			totalWidth = $(this).parent('li').width();
			if (( $.browser.msie ) && ($.browser.version == '6.0') ) {
				totalWidth +=  ($(this).width() - $(this).outerWidth()); //bug do IE6
			}
		}
		if (( $.browser.msie ) && ($.browser.version == '6.0') ) {}
		else {
			totalWidth +=  ($(this).width() - $(this).outerWidth()); //bordas
		}
		totalWidth -= parseInt($(this).children('li').children('a').css("padding-right")) + parseInt($(this).children('li').children('a').css("padding-left")); //padding
		$(this).children('li').children('a').each(function() {
			$(this).width(totalWidth);
		});
	});
}

function checkLogin() {
	var form = document.getElementById("login");
	if (form.usuario.value == "Usuário") {
		window.alert("Por favor, informe o usuario.");
		form.usuario.focus();
		return false
	}

	else if (form.senha.value == "senha") {
		window.alert("Por favor, informe a senha.");
		form.senha.focus();
		return false
	}

	return true;
}

$(function($) {
	$("#newsletter").submit(function() {
		var email = $("#news_email").val();
		var nome = $("#news_nome").val();
		if (nome == "nome"){
			alert("Por favor, informe nome.");
		}
		else if (!/[A-Za-z0-9_.-]+@([A-Za-z0-9_]+\.)+[A-Za-z]{2,4}/.test(email)){
				alert("Por favor, informe um e-mail válido.");
		}
		else{
			$.post('newsletter.php', {email: email,nome: nome}, function(resposta) {
				if (resposta != false) {
					alert(resposta);
				}
				else {
					alert("E-mail cadastrado com sucesso!");
					$("#nome").val("");
					$("#email").val("");
				}
			});
		}
	});
});
