(function($) {
	// jQuery plugin definition
	$.fn.Baires = function() {
		
		$('#enviarse').bind('click', function() {
			var nombre = $("#nombres").val();
			if (nombre == " " || nombre == "Nombre y Apellido") {
			alert('Nombre no valido');
			$("#nombres").focus();
				return false;
			}

			function IsValidEmail(email) {
			var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
			return filter.test(email);
			}
			var email = $("#emails").val();
			if (!IsValidEmail(email)) {
			alert('Email no valido');
			$("#emails").focus();
				return false;
			}
			
			$('#suscripcion').fadeOut(400, function() {
				$('#suscripcion').text("DATOS RECIBIDOS. GRACIAS POR PREFERIR BAIRES!").css('color','#f90').fadeIn(700);}
				);   
			  
			});


		$('#enviar').bind('click', function() {
   		 //$("form").submit(function () { return false; }); // so it won't submit

		  var nombre = $("#nombre").val();
			if (nombre == " " || nombre == "Nombre") {
			alert('Nombre no valido');
			$("#nombre").focus();
		  return false;
		}
			function IsValidEmail(email) {
			var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
			return filter.test(email);
			}
			var email = $("#email").val();
			if (!IsValidEmail(email)) {
			alert('Email no valido');
			$("#email").focus();
				return false;
			}
			
			var mensaje = $("#mensaje").val();
			if (mensaje == " " || mensaje == "Escribe tu mensaje") {
			alert('Mensaje no valido');
		    $("#mensaje").focus();
		  return false;
		}
		  	$('#success').fadeIn();

			var dataString = 'nombre='+ nombre + '&email=' + email + '&mensaje=' + mensaje;
			//alert (dataString);return false;
			
			$.ajax({
		  type: "POST",
		  url: "mailer.php",
		  data: dataString,
			error:function(x,e){
			if(x.status==0){
			alert('You are offline!!\n Please Check Your Network.');
			}else if(x.status==404){
			alert('Requested URL not found.');
			}else if(x.status==500){
			alert('Internel Server Error.');
			}else if(e=='parsererror'){
			alert('Error.\nParsing JSON Request failed.');
			}else if(e=='timeout'){
			alert('Request Time out.');
			}else {
			alert('Unknow Error.\n'+x.responseText); }},
		  
		  success: function() {
			//alert(responseText);
		  	$('#success').html("<h5>Formulario enviado correctamente!</h5>").css('color','#c00');
			$('#success-loader').fadeOut(); 
			  }
		 });
		return false;
		});
		
		        $(function() {
        
                function filterPath(string) {
                        return string
                        .replace(/^\//,'')
                        .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
                        .replace(/\/$/,'');
                }
        
                var locationPath = filterPath(location.pathname);
                var scrollElem = scrollableElement('html', 'body');
        
                // Any links with hash tags in them (can't do ^= because of fully qualified URL potential)
                $('a[href*=#]').each(function() {
        
                        // Ensure it's a same-page link
                        var thisPath = filterPath(this.pathname) || locationPath;
                        if (  locationPath == thisPath
                                && (location.hostname == this.hostname || !this.hostname)
                                && this.hash.replace(/#/,'') ) {
        
                                        // Ensure target exists
                                        var $target = $(this.hash), target = this.hash;
                                        if (target) {
        
                                                // Find location of target
                                                var targetOffset = $target.offset().top;
                                                $(this).click(function(event) {
        
                                                        // Prevent jump-down
                                                        event.preventDefault();
        
                                                        // Animate to target
                                                        $(scrollElem).animate({scrollTop: targetOffset}, 400, function() {
        
                                                                // Set hash in URL after animation successful
                                                                location.hash = target;
        
                                                        });
                                                });
                                        }
                        }
        
                });
        
                // Use the first element that is "scrollable"  (cross-browser fix?)
                function scrollableElement(els) {
                        for (var i = 0, argLength = arguments.length; i <argLength; i++) {
                                var el = arguments[i],
                                $scrollElement = $(el);
                                if ($scrollElement.scrollTop()> 0) {
                                        return el;
                                } else {
                                        $scrollElement.scrollTop(1);
                                        var isScrollable = $scrollElement.scrollTop()> 0;
                                        $scrollElement.scrollTop(0);
                                        if (isScrollable) {
                                                return el;
                                        }
                                }
                        }
                        return [];
                }
        
        });
	};
})(jQuery);
