function validaEmail (email){
	var er = /^[a-zA-Z0-9._-]+@(\.?[a-zA-Z0-9-]+){1,3}$/;
	if(er.exec(email)){
		return true;
	} else {
		return false;
	}
}
function criaCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function exibeCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function apagaCookie(name) {
	criaCookie(name,"",-1);
}
$(document).ready(function() {
	//Geral
	$('div.paginacao .atual').removeAttr('href');
	$('div.fvCategoria:odd').css('backgroundColor','#e5e5e5');
	$('div.listagemQuadros').hover(
		function() {
			$(this).css('backgroundColor','#ccc');
		}, function() {
			$(this).css('backgroundColor','#e5e5e5');
		}
	);
	//Menu / Submenu
	$('#menu ul li ul').parent().hover(
		function () {
			$(this).children('ul').slideDown('fast');
		},
		function () {
			$(this).children('ul').slideUp('fast');
		}
	);
	//Radio
	$('.radio').bind('click',function(){
		window.open(URI+'radio/','RadioJJ','width=400,height=400,top='+(window.screen.height/2-400/2)+',left='+(window.screen.width/2-400/2)+',scrollbars=no,toolbar=no,menubar=no,status=no,location=no,resizable=no');
		return false;
	});
	//SlideShow
	$('#slideshow ul li a img').hide();
	jQuery.fn.extend({ prepara:
		function(a) {
			$('#slideshow ul li a').fadeOut(400);
			$('#slideshow ul li a img').fadeOut(500);
			var k = $('#slideshow ul li').length;
			if (a == k) {var b = 0;} else {var b = a;}
			$(document).exibe(b);
		},exibe: function(i) {
			$('#slideshow ul li a img:eq('+i+')').fadeIn(500,function() {
				var k = parseFloat(i) + 1;
				var t1 = setTimeout("$(document).prepara('"+k+"');",6000);
			});
		}
	});
	$(window).bind('load', function() {
		$('#slideshow').fadeIn(600,function() {
			$(document).exibe(0);
		});
	});
	//FadeBox
	$('a.q').click(function() {
		//$(window).scrollTop(0);
		var link = $(this).attr('href');
		$('div#fadebox').fadeIn('fast');
		$('div#fadebox div#q').css('width','300px').fadeIn('fast');
		$('div#fadebox div#q #conteudo').css('text-align','center').html('<br /><br /><img src="'+URI+'img/loader.gif" alt="" /> Carregando...<br /><br /><br />').fadeIn('fast');
		$.get(URI+'inc/util/ajaxer.php', {id: link}, function(data){
			//alert(data);
			var z = data.substr(0,3);
			$('div#fadebox div#q').animate({width:z+'px'},500,function() {
				$('div#fadebox div#q #conteudo').hide().css('text-align','left').html(data.substring(3)).fadeIn('fast');
				$('div#fadebox div#q div#fechar').fadeIn('fast');
			});
		});
		return false;
	});
	//FadeBox Alertas
	$('#tableForm').submit(function() {
		$('.verifica').each(function() {
			var valor = $(this).val();
			var email = $(this).attr('name');
			if (valor=='' || (email=='email' && !validaEmail(valor))) {
				$(window).scrollTop(0);
				$('div#fadebox').fadeIn('fast');
				$('div#fadebox div#q').css('width','300px').fadeIn('fast');
				$('div#fadebox div#q #conteudo').css('text-align','center').html('Você deve preencher todos os dados corretamente.').fadeIn('fast');
				$('div#fadebox div#q div#fechar').fadeIn('fast');
				erro = 1;
				return false;
			}
		});
		if (erro==1) {
			erro = 0;
			return false;
		}
	});
	//FadeBox Fechar
	$('div#fadebox div#q #fechar a').click(function() {
		$('div#fadebox div#q div#fechar').slideUp('fast',function(){
			$('div#fadebox div#q div#conteudo').fadeOut('fast');
			$('div#fadebox').fadeOut('fast');
			$('div#fadebox div#q div#conteudo *').remove();
		});
		return false;
	});
	//Recados
	$('#recados li:odd').css({backgroundColor:'#e5e5e5',color:'#000'});
	$('#recados li b:odd').css({color:'#000'});
	$('#recados li').hover(function() {
		//var i = $('#recados li').index(this);
		//$('span.data:eq('+i+')').fadeIn('fast');
		$(this).css('backgroundColor','#d0d0d0');
	},function() {
		var i = $('#recados li').index(this);
		//$('span.data:eq('+i+')').fadeOut('fast');
		if (i%2==0) {
			$(this).css('backgroundColor','#fff');
		} else {
			$(this).css('backgroundColor','#e5e5e5');
		}
	});
	//Agenda
	$('#ver-compromisso').hide();
	$('a.visualizar').click(function() {
		var link = $(this).attr('href');
		$('#ver-compromisso').fadeIn('fast');
		$('#ver-compromisso').html('<img src="' + URI + 'img/loader.gif" alt="" /> Carregando...');
		$.get(URI + 'inc/util/agenda.php', {id: link}, function(data){
			if (data != 'erro') {
				$('#ver-compromisso').fadeOut('fast', function() {
					$(this).html(data);
					$(this).fadeIn('slow');
				});
			}
		}, 'html');
		return false;
	});
	/* Botão-X */
	$('.botaox').append('<span class="hover"></span>').each(function () {
  		var $span = $('> span.hover', this).css('opacity', 0);
  		$(this).hover(function () {
    		$span.stop().fadeTo(300, 1);
 		}, function () {
   			$span.stop().fadeTo(300, 0);
  		});
	});
	/* Containers Show-Hide */
	var cookie = exibeCookie('containers');
	if (cookie) {
		var container = cookie.split('');
		$.each(container, function(i,x) {
			$('.sh:eq('+x+')').removeClass('show').addClass('hide');
			$('.container_centro:eq('+x+')').hide();
		});
	} else {
		criaCookie('containers', '9', 30);
	}
	$('.sh').click(function() {
		var cookie = exibeCookie('containers');
		var container = $('.sh').index(this);
		if (cookie.indexOf(container) != -1) {
			criaCookie('containers', cookie.replace(container, ''), 30);
		} else {
			criaCookie('containers', cookie+container, 30);
		}
		if ($(this).hasClass('show')) {
			$(this).removeClass('show').addClass('hide');
			$(this).parent().next('.container_centro').slideUp();
		} else {
			$(this).removeClass('hide').addClass('show');
			$(this).parent().next('.container_centro').slideDown();
		}
		return false;
	});
	/* ToolTip */
	$('div.fvCategoria.relativo').hover(function(){
		$(this).children('.tooltip').fadeTo('fast', 0.9);
	}, function () {
		$(this).children('.tooltip').fadeOut('fast');
	});
	$('a[rel="nova"]').attr('target','_blank');
});
