/**
 * Background image size, used to scale it.
 */
var IMAGE = {
  width: 1100,
  height: 630
};
var MIN_WIDTH = 1024;
var MIN_HEIGHT = 618;

/**
 * Changes the background size, taking into acount the size of the viewport
 * and keeping its aspect ratio.
 */
function changeBackgroundSize() {
  var windowHeight = $(window).height();
  var windowWidth = $(window).width();
  if (windowHeight < MIN_HEIGHT) {
    windowHeight = MIN_HEIGHT;
    $("#body").height(windowHeight);
  } else {
    $("#body").height('');
  }
  if (windowWidth < MIN_WIDTH) {
    windowWidth = MIN_WIDTH;
    $("#body").width(windowWidth);
  } else {
    $("#body").width('');
  }
  if (IMAGE.width/IMAGE.height < windowWidth/windowHeight) {
    var margins = (windowWidth - windowHeight * (IMAGE.width/IMAGE.height))/2
    $(".background-container").css({
      top: 0,
      bottom: 0,
      left: margins,
      right: margins,
    });
    $("#top").css({
      top: 0
    });
    $("#bottom").css({
      bottom: 0
    });
  } else if (IMAGE.width/IMAGE.height > windowWidth/windowHeight) {
    var margins = (windowHeight - windowWidth * (IMAGE.height/IMAGE.width))/2
    $(".background-container").css({
      top: margins,
      bottom: margins,
      left: 0,
      right: 0,
    });
    $("#top").css({
      top: margins
    });
    $("#bottom").css({
      bottom: margins
    });
  } else {
    $(".background-container").css({
      top: 0,
      bottom: 0,
      left: 0,
      right: 0,
    });
    $("#top").css({
      top: 0
    });
    $("#bottom").css({
      bottom: 0
    });
  }
  $("#body").fadeIn();
}

function validateEmail(email) { 
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
} 

$(document).ready(function(){
  var NUMBER_OF_BACKGROUNDS = 4;


  changeBackgroundSize();
  $(window).resize(changeBackgroundSize);


  var backgroundNumber = Math.floor(Math.random() * (NUMBER_OF_BACKGROUNDS + 1));
  if (backgroundNumber > 0) {
    $(".random").addClass("random-" + backgroundNumber);
  }

  // Binding event listeners for the form on document ready

  $('#email').defaultText('Email');

  // 'working' prevents multiple submissions
  var working = false;
  
  $('#page form').submit(function(){
    
    if(working){
      return false;
    }
    if (!validateEmail($('#email').val())) {
      $('#message').fadeOut(function() {
        $(this).text("Direccion invalida!").removeClass('success')
            .addClass('error').fadeIn();
      });
      return false;
    }

    working = true;
    var jqxhr = $.post("index.php",{email:$('#email').val()},function(data,
      textStatus, jqXHR){
      if (!data.error) {
        $('#message').fadeOut(function() {
          $(this).text("Gracias! Tu alma nos pertenece.").removeClass('error')
              .addClass('success').fadeIn(function() {
                 $(this);
              });
        });
        $("input").blur();
      }
      else {
        $('#message').fadeOut(function() {
          $(this).text(data.error).removeClass('success')
              .addClass('error').fadeIn();
        });
       }
      working = false;
    },'json').error(function() {
        $('#message').fadeOut(function() {
          $(this).text("No pudimos guardar tu email, intenta mas tarde.")
              .removeClass('success').addClass('error').fadeIn();
        });
    });
    console.log(jqxhr.error);
    return false;
  });
});

// A custom jQuery method for placeholder text:

$.fn.defaultText = function(value){
  
  this.val('');
  var element = this.eq(0);
  element.data('defaultText',value);
  
  element.focus(function(){
    if(element.val() == value){
      element.val('').removeClass('defaultText');
    }
  }).blur(function(){
    if(element.val() == '' || element.val() == value){
      element.addClass('defaultText').val(value);
    }
  });
  
  return element.blur();
}

