
$(document).ready(function() {
	
	$('input[name=cpf]').mask('999.999.999-99');
	$('input[name=rg]').mask('9999999-9');
	$('input[name=telefone]').mask('(99) 9999-9999');
	
	$('#interna .form').submit(function(event) {
				
		$('.field').each(function() {
		
			var submitForm = false;
			
			id = $(this).attr('rel');
			
			if(id != 'undefined' || id  == NULL ||  !id ){
			
				// seta a array com os tipos de validação
				var rel 			= id.split('|');
				var total 			= rel.length;		
				var regExpEmail  	= new RegExp('^[a-zA-Z0-9\._-]+@[a-zA-Z0-9\._-]+.([a-zA-Z]{2,4})$', "" );
				var regExpLimit  	= new RegExp('^(limit)?(-)?[0-9]');
				var regExpMin  		= new RegExp('^(min)?(-)?[0-9]');
				var regExpNum  		= new RegExp('^[0-9]');
				var regExpLet  		= new RegExp('^[a-zA-Z]');
				
				for( i = 0; i <= total; i++ ){	
					
						if( rel[i] ==  'requerid'){
							if( !$(this).val())
							{								
								innerHtml( this , 'O Campo é requerido.');
								submitForm = true;
							}
						}
						else if( rel[i] == 'email'){
							if( !regExpEmail.test( $(this).val() ) )
							{
								innerHtml( this , 'O email parece não ser valido.');  
								submitForm = true;
							}
						}
						else if( regExpLimit.test(  rel[i] ) ){
							var limitExplode = rel[i].split('-');					
							if( $(this).val().length > limitExplode[1] )
							{
								innerHtml( this , 'O numero maximo de caracteres e '+limitExplode[1]+'.'); 
								submitForm = true; 
							}
						}
						else if( regExpMin.test(  rel[i] ) ){
							var minExplode = rel[i].split('-');					
							if( $(this).val().length < minExplode[1] )
							{
								innerHtml( this , 'O numero minimo de caracteres e '+minExplode[1]+'.');  
								submitForm = true;
							}
						}
						else if( rel[i] == 'number' ){					
							if( !regExpNum.test(  $(this).val() ) )
							{
								innerHtml( this , 'So é permitido numero');  
								submitForm = true;
							}
						}
						else if( rel[i] == 'letters' ){					
							if( !regExpLet.test(  $(this).val() ) )
							{
								innerHtml( this , 'So é permitido Letras');  
								submitForm = true;
							}
						}
					
				}
			
			}
		
			if(submitForm)
			{
				event.preventDefault();
			}
			
		})	
		
	});
	
	$('.field').focus(function() {
		removeAlert( this );	
	});	
	
	$('.field').blur(function() {	
		
		// seta a array com os tipos de validação
		var rel 			= $(this).attr('rel').split('|');		
		var total 			= rel.length;		
		var regExpEmail  	= new RegExp('^[a-zA-Z0-9\._-]+@[a-zA-Z0-9\._-]+.([a-zA-Z]{2,4})$', "" );
		var regExpLimit  	= new RegExp('^(limit)?(-)?[0-9]');
		var regExpMin  		= new RegExp('^(min)?(-)?[0-9]');
		var regExpNum  		= new RegExp('^[0-9]');
		var regExpLet  		= new RegExp('^[a-zA-Z]');
		
		for( i = 0; i <= total; i++ ){	
			
			if( rel[i] ==  'requerid'){
				
				if( !$(this).val())
				{
					innerHtml( this , 'O Campo é requerido.'); 
				}
			}
			else if( rel[i] == 'email'){
				if( !regExpEmail.test( $(this).val() ) )
				{
					innerHtml( this , 'O email parece não ser valido.');  
				}
			}
			else if( regExpLimit.test(  rel[i] ) ){
				var limitExplode = rel[i].split('-');					
				if( $(this).val().length > limitExplode[1] )
				{
					innerHtml( this , 'O numero maximo de caracteres e '+limitExplode[1]+'.');  
				}
			}
			else if( regExpMin.test(  rel[i] ) ){
				var minExplode = rel[i].split('-');					
				if( $(this).val().length < minExplode[1] )
				{
					innerHtml( this , 'O numero minimo de caracteres e '+minExplode[1]+'.');  
				}
			}
			else if( rel[i] == 'number' ){					
				if( !regExpNum.test(  $(this).val() ) )
				{
					innerHtml( this , 'So é permitido numero');  
				}
			}
			else if( rel[i] == 'letters' ){					
				if( !regExpLet.test(  $(this).val() ) )
				{
					innerHtml( this , 'So é permitido Letras');  
				}
			}
		}
		
	});
	
	$('a.fancy').fancybox({
		'opacity'		: true,
		'overlayShow'	: false,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'none'
	});
});


function mudarDestaque(target) {
	
	$linkfoto = $(target).attr('href');		
	$srcfoto = $(target).attr('rel');
	$('#link1').attr('href',$linkfoto);
	//$('#img1').attr('src',$srcfoto);
	
	$srcfoto = 'border-radius:0 0 0 130px; -webkit-border-bottom-left-radius: 130px; -moz-border-radius: 0 0 0 130px; background: url('+$srcfoto+'); width:424px; height:290px';
	$('#img1').attr('style',$srcfoto);
}

function innerHtml( id , msg )
{
	$(id).css('border' , 'solid 1px red');
	$(id).siblings('.alert').fadeIn();
	$(id).siblings('.alert').html(msg);	
}
function removeAlert( id ){
	$(id).css('border' , 'solid 1px silver');
	$(id).siblings('.alert').fadeOut();
}

function openJornal( url )
{
	window.open(url, "jornal", "menubar=yes,resizable=yes,width=820,height=620");
}

function abrirTexto( id , val)
{
	for($i = 1; $i<15; $i++)
	{
		if('#titulo'+$i+'' != '#'+id+'')
		{
			$('#titulo'+$i+'').css('overflow' , 'hidden').animate({height: val }, 1000 );
		}
	}
	 
	
    if($('#'+id+'').css('height') == val+'px')
	{
        $('#'+id+'').css('overflow' , 'hidden').css('height' , 'auto');           
    }
	else
	{
        $('#'+id+'').css('overflow' , 'hidden').animate({height: val }, 1000 );
	}
        
    return false; 
}

function abrirLegislacao( id , val)
{
	for($i = 1; $i<10; $i++)
	{
		if('#titulo'+$i+'' != '#'+id+'')
		{
			$('#titulo'+$i+'').css('overflow' , 'hidden').animate({height: val }, 1000 );
		}
	}
	 
	
    if($('#'+id+'').css('height') == val+'px')
	{
        $('#'+id+'').css('overflow' , 'hidden').css('height' , 'auto');           
    }
	else
	{
        $('#'+id+'').css('overflow' , 'hidden').animate({height: val }, 1000 );
	}
        
    return false; 
}

function abrirLegislacaoInternas( id , val)
{
	for($i = 1; $i<6; $i++)
	{
		if('#int'+$i+'' != '#'+id+'')
		{
			$('#int'+$i+'').css('overflow' , 'hidden').animate({height: val }, 1000 );
		}
	}
	 
	
    if($('#'+id+'').css('height') == val+'px')
	{
        $('#'+id+'').css('overflow' , 'hidden').css('height' , 'auto');           
    }
	else
	{
        $('#'+id+'').css('overflow' , 'hidden').animate({height: val }, 1000 );
	}
        
    return false; 
}

function abrirConvenios( id , val)
{
	for($i = 1; $i<14; $i++)
	{
		if('#titulo'+$i+'' != '#'+id+'')
		{
			$('#titulo'+$i+'').css('overflow' , 'hidden').animate({height: val }, 1000 );
		}
	}
	 
	
    if($('#'+id+'').css('height') == val+'px')
	{
        $('#'+id+'').css('overflow' , 'hidden').css('height' , 'auto');           
    }
	else
	{
        $('#'+id+'').css('overflow' , 'hidden').animate({height: val }, 1000 );
	}
        
    return false; 
}

function abrirDenuncias( id , val)
{
	for($i = 1; $i<4; $i++)
	{
		if('#titulo'+$i+'' != '#'+id+'')
		{
			$('#titulo'+$i+'').css('overflow' , 'hidden').animate({height: val }, 1000 );
		}
	}
	 
	
    if($('#'+id+'').css('height') == val+'px')
	{
        $('#'+id+'').css('overflow' , 'hidden').css('height' , 'auto');           
    }
	else
	{
        $('#'+id+'').css('overflow' , 'hidden').animate({height: val }, 1000 );
	}
        
    return false; 
}
