$( document ).ready(
	function() {
		
		$( "#sidebar #associados ul li a, #content .galeria ul li a" )
		.mouseover(
			function() {
				var top = $( this ).height();
				$( this ).children( "span" ).css( "top", top );
				$( this ).children( "span" ).css( "display", "block" );
			}
		).mouseout(
			function() {
				$( this ).children( "span" ).slideUp( 150 );
			}
		);
		
		// Hacks CSS para IE6
		$( "#content form input[type='checkbox']" ).css( { width: "auto", border: 0 } );
		$( "#menu-primario li" ).mouseover(
			function () {
				$( this ).children( "ul" ).css( "display", "block" );
			}
		);
		$( "#menu-primario li" ).mouseout(
			function () {
				$( this ).children( "ul" ).css( "display", "none" );
			}
		);
		
		// Travando botao direito do mouse (IE7, FF e Chrome. Opera não)
		$( "img" ).bind( "contextmenu", 
			function( e ) {
			return false;
			}
		);
		
		/*$( "#menu-primario li.maiores" )
		.mouseover( 
			function() {
				$( "#content #galeria_associados img" ).hide();
				$( "#content #galeria_imagens img" ).hide();
				$( "#content object" ).hide();
			}
		).mouseout(
			function() {
				$( "#content #galeria_associados img" ).show();
				$( "#content #galeria_imagens img" ).show();
				$( "#content object" ).show();
			}
		);*/
	}
);

function trocaNoticia( el ) {
	
	// Envia o conteudo do atributo href do link clicado para a pagina news.php
	$.post( 'news.php', { url: $( el ).attr( 'href' ) }, 
		function( retorno ) {
			$( '#content #noticias' ).html( retorno );
		}
	);

	return false;
}

function trocaData( valor ) {
	
	// Envia o conteudo do atributo href do link clicado para a pagina news.php
	$.post( 'data.php', { anuario: valor }, 
		function( retorno ) {
			$( '#data_entrega' ).html( retorno );
		}
	);
	return false;
}

function accordion( el ) {
	
	id = $( el ).attr( "id" );
	
	if( $( el ).attr( "visible" ) != "visible" ) {
		// Trocando as classes (imagens + e - )
		$( el ).parent().children( "span.accordion_aberto" ).attr( "class", "accordion_fechado" );
		$( el ).attr( "class", "accordion_aberto" );
		
		$( el ).parent().children( "div" ).hide(); // Escondendo divs abertos 
		$( el ).parent().children( "div#div_" + id ).show(); // Mostrando o clicado
	}
	
}

function limpaCampo ( campo, valor ) {
	if ( campo.val() == valor ) {
		campo.val( "" );
	}
	campo.blur(
		function () {
			if ( campo.val() == "" ) {
				campo.val( valor );
			}
		}
	);
}

function popup( url, largura, altura ) {
	window.open( url, 'materiais', 'width=' + largura + ',height=' + altura );
}

function confirma() {
	return confirm( "Tem certeza?" );
}

function mascaraData( val, id ) {
	
	document.getElementById( id ).maxLength = 10;
	
	size  = val.length;
	antes = size - 1;
	letra = val.substr( antes, size );
	
	if( letra >= '0' && letra <= '9' && letra != '/') {
	    if( size == 2 && letra != "/" )
	       	document.getElementById( id ).value = val.substr( 0, size ) + '/';
	    else if( size == 5 && letra != "/" )
	       	document.getElementById( id ).value = val.substr( 0, size ) + '/';
	} else {
		document.getElementById( id ).value = val.substring( 0, size );
		return false;
	}
}

function mascaraTel( val, id ) {
	
	document.getElementById( id ).maxLength = 13;
	
	size  = val.length;
	antes = size - 1;
	letra = val.substr( antes, size ); 

	if( letra >= '0' && letra <= '9' && letra != '(' && letra != ')' && letra != '-') {
	    if( size == 1 && letra != "(" )
	       	document.getElementById( id ).value = '(' + val.substr( 0, antes ) + letra;
	    else if( size == 4 && letra != ")" )
	       	document.getElementById( id ).value = val.substr( 0, antes ) + ')' + letra;
	    else if( size == 9 && letra != "-" )
	    	document.getElementById( id ).value = val.substr( 0, antes ) + '-' + letra;
	} else {
		document.getElementById( id ).value = val.substring( 0, antes );
		return false;
	}
}

function mascaraCEP(val,id)
{
	
	document.getElementById( id ).maxLength = 9;
	
	size  = val.length;
	antes = size - 1;
	letra = val.substr(antes,size);
	
	if( letra >= '0' && letra <= '9' && letra != '-')
	{
	    if( size == 5 && letra != "-" )
	       	document.getElementById( id ).value = val.substr( 0, size ) + '-';
	}
	else
	{
		document.getElementById( id ).value = val.substring( 0, size );
		return false;
	}
}

function mascaraCPF( val,id )
{
	
	document.getElementById( id ).maxLength = 14;
	
	size  = val.length;
	antes = size - 1;
	letra = val.substr( antes, size );
	
	if( letra >= '0' && letra <= '9' && letra != '.' && letra != '-' ) {
	    if( size == 3 && letra != "." )
	       	document.getElementById( id ).value = val.substr( 0, size ) + '.';
	    else if( size == 7 && letra != "." )
	       	document.getElementById( id ).value = val.substr( 0, size )+ '.';
		else if( size == 11 && letra != "-")
			document.getElementById( id ).value = val.substr( 0, size )+ '-';
	} else {
		document.getElementById( id ).value = val.substring( 0, size );
		return false;
	}
	return true;
}


function validaData( data ) {

	dia = data.substring( 0, 2 );
	mes = data.substring( 3, 5 );
	ano = data.substring( 6, 9 );
		
	fev = ( ( ano % 100 ) % 4 == 0 ) ? 29 : 28;
	diasMes = Array( 31, fev, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 );
	
	if( mes < 0 || mes > 12 ) {
		return false;
	} else if( dia > diasMes[ mes - 1 ] || dia < 1 ) {
		return false;
	} else if( ano < 0 ) {
		return false;
	}
	
	return true;
}

function validaCPF( CPF )
{
	if( CPF.length > 14 ) {
		return false;
	}
	
	CPF = CPF.replace( ".", "" );
	CPF = CPF.replace( ".", "" );
	CPF = CPF.replace( "-", "" );

	a = Array();
	b = 0;
	c = Array( 11, 10, 9, 8, 7, 6, 5, 4, 3, 2 );
	
	for ( i = 0; i < 9; i++ ) {
		a[ i ] = CPF.charAt( i );
		b += a[ i ] * c[ i + 1 ];
	}
	x = b % 11;
	a[ 9 ] = x < 2 ? 0 : 11 - x;
	
	b = 0;
	for ( y = 0; y < 10; y++ ){
		b += ( a[ y ] * c[ y ] );
	}
	
	x = b % 11
	a[ 10 ] = x < 2 ? 0 : 11 - x;
	 
	if ( ( CPF.charAt( 9 ) != a[ 9 ] ) || ( CPF.charAt( 10 ) != a[ 10 ] ) ){
		return false;
	}
	
	return true;
}
