$(document).ready(function(){				
		$('a[name=modal]').click(function(e) {
				$('#titulo').html('<span class="resaltado_blue">Mostrando:</span> '+$(this).attr('rel')).show();
				$('#tabla').html('');
				//Hacer la peticion AJax
				$('#dialog').mask('cargando distribuidores');
				$.post('model/ajax.php',
					   {fn:'getDealer',state:$(this).attr('rel')},
						function(data){
						//$('#c_mensaje').html(data);
						$('#tabla').html(data);
						$('#dialog').unmask();
				});
		
				//Cancel the link behavior
				e.preventDefault();
				
				//Get the A tag
				var id = $(this).attr('href');
			
				//Get the screen height and width
				var maskHeight = $(document).height();
				var maskWidth = $(window).width();
			
				//Set heigth and width to mask to fill up the whole screen
				$('#mask').css({'width':maskWidth,'height':maskHeight});
				
				//transition effect		
				$('#mask').fadeIn(1000);	
				$('#mask').fadeTo("slow",0.8);	
			
				//Get the window height and width
				var winH = $(window).height();
				var winW = $(window).width();
					  
				//Set the popup window to center
				$(id).css('top',  winH/2-$(id).height()/2);
				$(id).css('left', winW/2-$(id).width()/2);
				//transition effect
				$(id).fadeIn(2000);
						
		});
		
		//if close button is clicked
		$('.window .close').click(function (e) {
			//Cancel the link behavior
			e.preventDefault();
			
			$('#mask').hide();
			$('.window').hide();
		});		
		
		//if mask is clicked
		$('#mask').click(function () {
			$(this).hide();
			$('.window').hide();
		});
		
		$('#btn_enviar').click(function(){
		$('#mensaje_error').hide();									
		var mensaje='';
		if($('#nombre').val()=='')
			mensaje = 'El Nombre no puede ir vacio.';
		else
			if($('#asunto').val()=='')
				mensaje = 'El Asunto no puede ir vacio.';
			else
				if($('#email').val()=='')
					mensaje = 'El Email no puede ir vacio.';
				else
					if(!validaMail($('#email').val()))
						mensaje = 'Introduce un e-mail v&aacute;lido.';
					else		
						if(nicEditors.findEditor('mensaje').getContent()=='<br>')
							mensaje = 'El Mensaje no puede ir vacio.';
		
		if(mensaje!='')
		{			
			$('#mensaje_error').fadeIn(500,function(){
						}).html(mensaje);			

		}
		else
		{
			//Procesar ajax de envio
			 $('#mensaje_error').show('slow').html('Su mensaje se esta enviando.');
			$.post('model/ajax.php',
				  {
					  fn: 'sendMail',
					  nombre:$('#nombre').val(),
					  asunto:$('#asunto').val(),
					  email:$('#email').val(),
  					  destinatario: $('#target').val(),
					  mensaje:nicEditors.findEditor('mensaje').getContent()
				   },
				   function(data){
					   $('#mensaje_error').html(data);
					   //Limpiamos los campos
 		   			   $('#nombre').val('');
					   $('#asunto').val('');
					   nicEditors.findEditor('mensaje').setContent('');
					   $('#email').val('');
				   });
			
		}
//		else  PRoceder a enviar el mail
			
		return false;
	})

//Page Flip on hover
	$("#pageflip").hover(function() {
		$("#pageflip img , .msg_block").stop()
			.animate({
				width: '307px', 
				height: '319px'
			}, 500); 
		} , function() {
		$("#pageflip img").stop() 
			.animate({
				width: '50px', 
				height: '52px'
			}, 220);
		$(".msg_block").stop() 
			.animate({
				width: '50px', 
				height: '50px'
			}, 200);
	});

$("#imgs_menu img[title]").tooltip({
		   offset: [10, 2],
		   effect: 'slide'
		}).dynamic({ bottom: { direction: 'down', bounce: true } });
	
$("#states img[title]").tooltip({
		   offset: [10, 2],
		   effect: 'slide',
		   tipClass: 'tooltip_blue'
		}).dynamic({ bottom: { direction: 'down', bounce: true } });
	
});

function validaMail(mail)
        {
            if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(mail))
                return (true);
            else
                return (false);
        }


