/**
 * @author maca
 */
jQuery.extend({
  random: function(X) {
      return Math.floor(X * (Math.random() % 1));
  },
  randomBetween: function(MinV, MaxV) {
    return MinV + jQuery.random(MaxV - MinV + 1);
  }
});
jQuery.fn.reset = function () {
  $(this).each (function() { this.reset(); });
}

$(document).ready(function() {
  $('#ContactoEnviar').click(function(){ 
              enviarComentario(); 
              return false;
  });
});

function enviarComentario(){
	
	var nombre, email, mensaje = null;
		
  if($('#ContactoNombre').val() != 'Tu nombre...' && $('#ContactoNombre').val() != ''){
    nombre = $('#ContactoNombre').val();
  }
  else {
    nombre = false;
  }

  if($('#ContactoMail').val() != 'Tu email...' && $('#ContactoMail').val() != ''){
    email = $('#ContactoMail').val();
  }
  else {
    email = false;
  }  

  if($('#ContactoMensaje').val() != 'Tu mensaje...' && $('#ContactoMensaje').val() != ''){
    mensaje = $('#ContactoMensaje').val();
  }
  else {
    mensaje = false;
  }
  
	var quien = 0;
  quien = $.random(2);
	var titulo = '';
	var texto = '';
	var image_status = null;
	var http_host = 'http://' + HOST;
	www_root = (WEBROOT=='/'?'':WEBROOT);
		
  if(nombre && email && mensaje && validateEmail(email)) { 
    $.ajax({
          type: 'POST',
          url: '/home/comentario/',
          data: 'data[Contactomensaje][mensaje]='+mensaje+'&data[Contactomensaje][email]='+email+'&data[Contactomensaje][nombre]='+nombre,
          dataType: 'json',
          success: function(data){
           
            titulo = data.mensaje;
            if(data.ok){
              if(quien == 0) {
                image_status = http_host + www_root +'/img/mok.jpg';
              }
              if(quien == 1) {
                image_status = http_host + www_root +'/img/nok.jpg';
              }
              texto = 'El mensaje se envió perfectamente, ni bien revisemos tu mensaje nos pondremos en contacto con vos para darte el feedback';
            }
            else {
              if(quien == 0) {
                image_status = http_host + www_root +'/img/mno.jpg';
              }
              if(quien == 1) {
                image_status = http_host + www_root +'/img/nno.jpg';
              }
              texto = 'Hubo algun problema al enviar el mensaje, intentá nuevamente o contactanos a <a href="mailto:hola@h1lab.com">hola@h1lab.com</a> directamente, hasta que revisemos este problema.';
            }
            
            // TODO: Resetear el form
            $("#contacto_form").reset();
						
						
						$.gritter.add({
							// (string | mandatory) the heading of the notification
							title: titulo,
							// (string | mandatory) the text inside the notification
							text: texto,
							// (string | optional) the image to display on the left
							image: image_status,
							// (bool | optional) if you want it to fade out on its own or just sit there
							sticky: false, 
							// (int | optional) the time you want it to be alive for before fading out
							time: ''
						});
                        
          }
    }); 
				
  }
  else {
      if(quien == 0) {
        image_status = http_host + www_root +'/img/mno.jpg';
      }
      if(quien == 1) {
        image_status = http_host + www_root +'/img/nno.jpg';
      }
      titulo = "Oops!, Problemas :/"
      texto = 'Te falta completar algun campo, o hubo algun problema con tus datos, intentá nuevamente o contactanos a <a href="mailto:hola@h1lab.com">hola@h1lab.com</a> directamente.';    
			
			$.gritter.add({
				// (string | mandatory) the heading of the notification
				title: titulo,
				// (string | mandatory) the text inside the notification
				text: texto,
				// (string | optional) the image to display on the left
				image: image_status,
				// (bool | optional) if you want it to fade out on its own or just sit there
				sticky: false, 
				// (int | optional) the time you want it to be alive for before fading out
				time: ''
			});
			
  }
	
	
function validateEmail(email) {
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	return emailReg.test( email );
}	
            
             
}
