jQuery(document).ready(function(){
	/**
	 * hover dos palestrantes na home
	 */
	jQuery('#palestrantes li').hover(
		function(){ jQuery( this ).find('.bio').fadeIn( 400 ); },
		function(){ jQuery( this ).find('.bio').fadeOut( 0 ); }
	);
	/**
	 * target=_blank
	 */
	jQuery("a[rel='externo']").click(function( e ){
		e.preventDefault();
		window.open( jQuery( this ).attr('href') );
	});
	
	/**
	 * validação formulário de contato
	 */
	if( jQuery("#f-contato").html() )
	{
		jQuery('#f-contato').hide();
		
		jQuery("input[name='continuar']").click(function(){
		
			var ja_inscreveu = jQuery("input[name='ja_inscreveu']:checked");
			if( jQuery( ja_inscreveu ).attr('checked') )
			{
				jQuery('#f_passo1-contato').hide();
				
				if( jQuery( ja_inscreveu ).attr('value')=='sim' )
					jQuery('#n_inscricao').append('<span>Número de Inscrição</span> <input name="n_inscricao" type="text" />');
				
				jQuery('#f-contato').show();
			}
			else
			{
				jQuery('label.error').remove();
				jQuery( this ).parent('label').parent('fieldset').append('<label class="error">Informe se você já se inscreveu.</label>');
			}
				
		});
	
		jQuery("input[name='telefone']").mask('(99) 9999-9999');
		jQuery("#f-contato").validate({
		rules:{
			motivo: {
				required: true
			},
			nome: {
				required: true, minlength: 2 },
			n_inscricao: {
				required: true, minlength: 2 },
			telefone: {
				required: true },
			email: {
				required: true, email: true },
			assunto: {
				required: true },
			mensagem: {
				required: true }
			},
		messages:{
			motivo: {
				required: 'Escolha o motivo'
			},
			nome: {
				required: "Digite seu nome",
				minlength: "O nome deve conter no minimo 2 caracteres" },
			n_inscricao: {
				required: "Informe o seu número de inscrição",
				minlength: "Informe corretamente" },
			telefone: {
				required: "Digite um telefone seu" },
			email: {
				required: "Digite seu e-mail",
				email: "Digite um e-mail valido" },
			assunto: {
				required: "Escreva o assunto da sua mensagem" },
			mensagem: {
				required: "Envie alguma mensagem" }
			},
			submitHandler: function( form ){
				var dados = jQuery( form ).serialize();
				
				jQuery.ajax({
					type: 		'POST',
					url: 		'/sendmail-contato-13elpi.php',
					data: 		dados,
					success: 	function( data ){
						if( data=='1' )
						{
							jQuery('#f-contato').hide();
							jQuery('#f-contato').html('<p class="destaque">Sua mensagem foi encaminhada com sucesso!</p><a href="contato.html">Voltar para o formulário.</a>');
							jQuery('#f-contato').fadeIn();
						}
					}
				});
				
				return false;
			}
		});
	}
	/**
	 * validação formulário de inscrição
	 */
	if( jQuery("#f-inscricao").html() )
	{
		jQuery("input[name='cpf']").mask('999.999.999-99');		
		jQuery("input[name='nascimento']").mask('99/99/9999');
		jQuery("input[name='telefone']").mask('(99) 9999-9999');
		jQuery("input[name='celular']").mask('(99) 9999-9999');
		jQuery("input[name='cep']").mask('99999-999');
		
		
		jQuery("#f-inscricao").validate({
		rules:{
			nome: {
				required: true, minlength: 2 },
			nome_cracha: {
				required: true, minlength: 2 },
			email: {
				required: true, email: true },
			telefone: {
				required: true },
				
			'evento[]': {
				required: true }
			},
		messages:{
			nome: {
				required: "Digite seu nome",
				minlength: "Deve conter no minimo 2 caracteres" },
			nome_cracha: {
				required: "Digite o nome que vai aparecer no teu crachá",
				minlength: "Deve conter no minimo 2 caracteres" },
			email: {
				required: "Digite seu e-mail",
				email: "Digite um e-mail valido" },
			telefone: {
				required: "Digite um telefone seu" },
				
			'evento[]': {
				required: "Escreva ao menos 1 evento" }
			}
		});
	}
	if( jQuery('#videos').html() )
	{
		var objects = jQuery('#videos object');
		objects.hide();
		
		var x = Math.floor(Math.random()*( objects.length )); 
	
		objects.eq( x ).show(); 
	}
	
	/**
	 *
	 */
	var href_data_local = jQuery('#data_local .atual h3 a').attr('href');
	jQuery('#data_local h3 a').click(function( e ){
		e.preventDefault();
		

		if( href_data_local == jQuery( this ).attr('href') )
		{
			ocultar_data_local();
			href_data_local = '';
		}
		else
		{
			ocultar_data_local();
			exibir_data_atual( jQuery( this ).parents('li') );	
			
			href_data_local = jQuery( this ).attr('href');
		}
	});

	
	if( jQuery('ul#slideshow').html() )
	{
	
		jQuery('ul#slideshow').cycle({
			fx: 		'fade',
			pager: 		'#pager',
			speed: 		2000,
			timeout: 	4000,
			pause: 		true,
			pauseOnPagerHover: true
		});
	
		jQuery('#slideshow').cycle('pause');	
	}
	if( jQuery('#carousel-sp').html() )
	{
		jQuery('#carousel-sp').cycle({
			fx:   		'scrollHorz',
			next: 		'#next',
			prev: 		'#prev',
			timeout: 	8000,
			pause: 		true
		});
	}
	if( jQuery('#yt_mini').html() )
	{
		jQuery('#yt_mini div').cycle({
			fx:   		'scrollHorz',
			next: 		'#yt_next',
			prev: 		'#yt_prev',
			timeout: 	8000,
			pause: 		true
		});
		
		
		jQuery('#yt_mini li').click(function(){
			var src = jQuery( this ).find('img').attr('src');
			var pieces = src.split('/');

			
			var obj = '<object height="390" width="640" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,124,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000">'+
					'<param name="wmode" value="transparent" />'+
					'<param name="quality" value="hight" />'+
					'<param name="src" value="http://www.youtube.com/v/'+pieces[4]+'&amp;hl=en_US&amp;fs=1&amp;" /><embed height="390" width="640" src="http://www.youtube.com/v/'+pieces[4]+'&amp;hl=en_US&amp;fs=1&amp;" quality="hight" wmode="transparent" type="application/x-shockwave-flash"></embed>'+
				'</object>';
				
			
			jQuery('#yt_destaque').html( obj );
		});
	}	

	jQuery('.curriculo').hide();
	var tb_pathToImage = "images/loadingAnimation.gif";

	/*!!!!!!!!!!!!!!!!! edit below this line at your own risk !!!!!!!!!!!!!!!!!!!!!!!*/

	//on page load call tb_init

	tb_init('a.prog_foto, a.thickbox');//pass where to apply thickbox
	imgLoader = new Image();// preload image
	imgLoader.src = tb_pathToImage;
	
	jQuery('.TB_modal h5 span').live('click',function(){		
		tb_remove();
	});
	
	
	
	/**
	 * colorir evento atual
	 */
	var date = new Date();
	
	var ano = jQuery.browser.msie ? 0 : 1900;
	var hoje = new Date( (date.getYear()+ano), date.getMonth(), date.getDate() );
	
	
	
	if( jQuery('#datas_locais').html() )
	{
		var arr_data = new Array();
		var arr_dif = new Array();
		var j = 0;
		jQuery('#datas_locais li').each(function( i ){
			var date_li = str2Date( jQuery( this ).attr('id') );
			

			if( date_li >= hoje )
			{
				arr_data[j] = date_li;
				
				arr_dif[j] = difDate( hoje, date_li );
				j++;
			}
		});
		
		
		jQuery('#datas_locais ul').mouseleave(function(){
			var id_li_atual = date2Str( arr_data[0] );
			jQuery( '#'+id_li_atual ).addClass('atual');
			
			brasil_span( jQuery("#mapabrasil span[rel='"+id_li_atual+"']"), 'left' );
		});
		
		var id_li_atual = date2Str( arr_data[0] );
		jQuery( '#'+id_li_atual ).addClass('atual');
		

		var encerradas = arr_dif[0]<=3 ? ' - Inscrições encerradas!' : '';
		var daquia = arr_dif[0]>1 ? 'Faltam '+arr_dif[0]+' dias ': 'Falta apenas '+arr_dif[0]+' dia';
		if( arr_dif[0]==0 ) daquia = 'É hoje!';
		
		jQuery( '#'+id_li_atual ).append('<span>'+daquia+encerradas+'</span>');
		brasil_span( jQuery("#mapabrasil span[rel='"+id_li_atual+"']"), 'left' );
	}
	

	if( jQuery('#data_local').html() )
	{
		var arr_data = new Array();
		var j = 0;
		jQuery('#data_local li').each(function( i ){
			var date_li = str2Date( jQuery( this ).attr('id') );
			

			if( date_li >= hoje )
			{
				arr_data[j] = date_li;
				j++;
			}
		});
		var id_li_atual = date2Str( arr_data[0] );
		
		var qs = window.location.href.split('#');
		
		if( qs[1]!=undefined )
			jQuery( '#'+qs[1] ).addClass('atual');
		else
			jQuery( '#'+id_li_atual ).addClass('atual');
			
			
		exibir_data_atual( jQuery('#data_local .atual') );
	}
	if( jQuery('#mapabrasil').html() )
	{
		jQuery('#mapabrasil span').hover(
			function(){
				reset_mapa();/* resetando */
				
				
				var $this = jQuery( this );
				var rel = $this.attr('rel');
				
				jQuery( '#'+rel ).addClass('atual');
				
				brasil_span( $this, 'left' );//sprite data vermelho
			},
			function(){
				var $this = jQuery( this );
				var rel = $this.attr('rel');
				
				jQuery( '#'+rel ).removeClass('atual');
				
				
				brasil_span( $this, '-56px' );//sprite data cinza
			}
		);
		
		jQuery('#datas_locais li').css('cursor','pointer');
		jQuery('#datas_locais li').hover(
			function(){
				reset_mapa();/* resetando */
				
				
				var $this = jQuery( this );
				brasil_span( jQuery("#mapabrasil span[rel='"+$this.attr('id')+"']"), 'left' );
				$this.addClass('atual');
			},
			function(){
				var $this = jQuery( this );
				brasil_span( jQuery("#mapabrasil span[rel='"+$this.attr('id')+"']"), '-56px' );
				$this.removeClass('atual');
			}
		)
		
		jQuery('#mapabrasil span').click(function(){
			window.location.href = '/encontro/data-e-local.html#'+jQuery( this ).attr('rel');
		});
		jQuery('#datas_locais li').click(function(){
			window.location.href = '/encontro/data-e-local.html#'+jQuery( this ).attr('id');
		});
	}
	

});

function reset_mapa()
{
	jQuery('#mapabrasil span').removeClass('atual');
	jQuery('#mapabrasil span').each(function(){
		brasil_span( jQuery( this ), '-56px' );//sprite data cinza
	});
	jQuery('#datas_locais li.atual').removeClass('atual');
}
function brasil_span( span, hz )
{
	var id = span.attr('id');

	switch( id )
	{
		case 'belo-horizonte':
			var pos = hz+' top'; break;
		case 'salvador':
			var pos = hz+' -24px'; break;
		case 'curitiba':
			var pos = hz+' -48px'; break;
		case 'porto-alegre':
			var pos = hz+' -72px'; break;
		case 'rio-de-janeiro':
			var pos = hz+' -96px'; break;
		case 'sao-paulo':
			var pos = hz+' -118px'; break;
	}
	span.css( 'backgroundPosition', pos );
}
function difDate( d1, d2 ) 
{
	return Math.ceil( (d2.getTime()-d1.getTime() )/1000/60/60/24);
}
function str2Date( str )
{
	var pieces = str.split('-');
	return new Date( pieces[0], (pieces[1]-1), pieces[2] );
}
function date2Str( date )
{
	var ano = jQuery.browser.msie ? 0 : 1900;
	return (date.getYear()+ano)+'-'+(date.getMonth()+1)+'-'+date.getDate();
}
function ocultar_data_local()
{
	if( is_ie6_7() )
		jQuery('#data_local > li').css({'height' : '45px'});
	else
		jQuery('#data_local > li').animate({'height' : '45px'});
		

	jQuery('#data_local h3 a').html('detalhes').css('backgroundPosition', 'right -62px');
	jQuery('#data_local h3').css('backgroundColor', '#FF9069');
}
function exibir_data_atual( li )
{
	if( is_ie6_7() )
		jQuery( li ).css({'height': '550px'});
	else
		jQuery( li ).animate({'height': '550px'});
		
		
	jQuery( li ).find('.data_local_content').show();
	
	jQuery( li ).find('h3').find('a').html( 'ocultar' ).css('backgroundPosition', 'right -12px');
	jQuery( li ).find('h3').css('backgroundColor', '#CC0300');
}
function is_ie6_7()
{
	return ( jQuery.browser.msie && ( parseInt(jQuery.browser.version)==6 || parseInt(jQuery.browser.version)==7 ) );
}
