// JavaScript Document
jQuery.fn.ForceNumericOnly =
function()
{
    return this.each(function()
    {
        $(this).keydown(function(e)
        {
            var key = e.charCode || e.keyCode || 0;
            // allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
            return (
                key == 8 || 
                key == 9 ||
                key == 46 ||
                (key >= 37 && key <= 40) ||
                (key >= 48 && key <= 57) ||
                (key >= 96 && key <= 105));
        })
    })
};
$(document).ready(function(){
	//simple gallery

	
	$('.gallery').cycle({ 
		next:   '.gallery',
		timeout: 5000 
	});
	
		$('.lagal').cycle({ 
		next:   '.lagal',
		timeout: 5000 
	});
	
	//IMG Protect
	$(document).bind("contextmenu",function(e){  
		return false;  
	});
	//$('.gallery').protectImage();
	
	//gallery plus
	/*
	$("ul.thumb li").hover(function() {
	$(this).css({'z-index' : '10'}); //Add a higher z-index value so this image stays on top 
	$(this).find('img').addClass("hover").stop() //Add class of "hover", then stop animation queue buildup
		.animate({
			marginTop: '-110px', //The next 4 lines will vertically align this image 
			marginLeft: '-110px',
			top: '50%',
			left: '50%',
			width: '174px', // Set new width 
			height: '174px', // Set new height 
			padding: '20px'
		}, 200); //this value of "200" is the speed of how fast/slow this hover animates 

	} , function() {
	$(this).css({'z-index' : '0'}); // Set z-index back to 0 
	$(this).find('img').removeClass("hover").stop()  // Remove the "hover" class , then stop animation queue buildup
		.animate({
			marginTop: '0', // Set alignment back to default 
			marginLeft: '0',
			top: '0',
			left: '0',
			width: '100px', // Set width back to default 
			height: '100px', // Set height back to default 
			padding: '5px'
		}, 400);
	});
	$("a[rel=gallery]").fancybox({
		'titleShow'     : false,
		'easingIn'      : 'easeOutBack',
		'easingOut'     : 'easeInBack',
		'overlayOpacity': 0.7,
		'overlayColor'	: '#FFFFFF',
	});
	*/
	
	/**** Send Mail ****/
	$("#phoneC").ForceNumericOnly();
	$("#submitcontact").click(function(){
		$(".error").hide();
		var hasError = false;
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,6})?$/;

		var emailFromVal = $("#emailFromC").val();
		if(emailFromVal == '') {
			$("#emailFromC").before('<span class="error">Ingresa tu email.</span>');
			hasError = true;
		} else if(!emailReg.test(emailFromVal)) {
			$("#emailFromC").before('<span class="error">Ingresa un email valido.</span>');
			hasError = true;
		}

		var subjectVal = $("#subjectC").val();
		if(subjectVal == '') {
			$("#subjectC").before('<span class="error">Ingresa un asunto.</span>');
			hasError = true;
		}

		var nameVal = $("#nameC").val();
		if(nameVal == '') {
			$("#nameC").before('<span class="error">Ingresa tu nombre.</span>');
			hasError = true;
		}

		var messageVal = $("#messageC").val();
		if(messageVal == '') {
			$("#messageC").before('<span class="error">Ingresa un mensaje.</span>');
			hasError = true;
		}
		
		var programaVal = $("#programaC").val();
		
		var telVal = $("#phoneC").val();
		if(telVal == '' || telVal.length < 7) {
			$("#phoneC").before('<span class="error">Ingrese un n&uacute; telef&oacute;nico valido (m&iacute;nimo 7 caracteres).</span>');
			hasError = true;
		}
		
		if(hasError == false) {
			$(this).hide();
			$('#tellafriend_form').slideUp();
			$("#contactenos").append('<img src="images/loader.gif" alt="Enviando..." id="cargando" />');
			
			$.post("js/contactMail.php",
   				{ name: nameVal, emailFrom: emailFromVal, subject: subjectVal, message: messageVal, programa: programaVal, phone: telVal },
				function(data){
						if(data == 'true')
						{
							$("#contactenos").before('<div class="msgSent"><h3>Tu mensaje se envi&oacute; satisfactoriamente</h3><p>Gracias por contactarnos.</p></div>'); 
						}
						else
						{
							$("#contactenos").before('<div class="msgSent error"><h3>Hay un error enviando su mensaje</h3><p>Disculpe los incomvenientes, por favor intente de nuevo mas tarde.</p></div>');
						}
						//$("#contactenos").before('<div class="msgSent"><p>'+data+'</p></div>');
						$('#cargando').fadeOut('fast',function()
						{
							$('.msgSent').fadeIn();
						});
			});
		}
		
		return false;
	});

});
