var myLinks = document.getElementsByTagName('a');
for(var i = 0; i < myLinks.length; i++) {
  myLinks[i].addEventListener('touchstart', function(){this.className = "hover";}, false);
  myLinks[i].addEventListener('touchend', function(){this.className = "";}, false);
}


function checkEmail(email) {  
  var pattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  var emailVal = $(email).val();
  return pattern.test(emailVal);
}


$(function() {
  
  var email_loading,
  email_loadingTimer,
  email_loadingFrame = 1,
  support_loading,
  support_loadingTimer,
  support_loadingFrame = 1,
  
  recipy_animate_email_loading = function() {
    if (!email_loading.is(':visible')){
      clearInterval(email_loadingTimer);
      return;
    }
    $('div', email_loading).css('top', (email_loadingFrame * -40) + 'px');
    email_loadingFrame = (email_loadingFrame + 1) % 12;
  },
  recipy_animate_support_loading = function() {
    if (!support_loading.is(':visible')){
      clearInterval(support_loadingTimer);
      return;
    }
    $('div', support_loading).css('top', (support_loadingFrame * -40) + 'px');
    support_loadingFrame = (support_loadingFrame + 1) % 12;
  };
  
  $.recipy = function() {
  };
  
  $.recipy.showActivity = function() {
    clearInterval(email_loadingTimer);
    email_loading.show();
    email_loadingTimer = setInterval(recipy_animate_email_loading, 66);
  };

  $.recipy.hideActivity = function() {
    email_loading.hide();
  };
  
  $.recipysupport = function() {
  };
  
  $.recipysupport.showActivity = function() {
    clearInterval(support_loadingTimer);
    support_loading.show();
    support_loadingTimer = setInterval(recipy_animate_support_loading, 66);
  };

  $.recipysupport.hideActivity = function() {
    support_loading.hide();
  };
  
  
  $("#support-form input:submit").click(function() {
    // Validate email address with regex
    var error = 0;
    $("label#support-email-error").hide();
    $("label#support-message-error").hide();
    if (!checkEmail("input#support-email")) {
      $("label#support-email-error").show();
      $("input#support-email").focus();
      $error = 1;
    }
    var email = $("input#support-email").val();
    if (email == "") {
      $("label#support-email-error").show();
      $("input#support-email").focus();
      $error = 1;
    }
    var message = $("textarea#support-message").val();
    if (message == "") {
      $("label#support-message-error").show();
      $("input#support-message").focus();
      $error = 1;
    }
    if ($error != 0) return false;

    var dataString = 'email=' + email + '&message=' + message;
    //alert (dataString);return false;

    $.recipysupport.showActivity();
    $.ajax({
      type: "POST",
      url: "support/process.php",
      data: dataString,
      success: function() {
        $.recipysupport.hideActivity();
        $("#recipy-support").hide();
        $("#support-confirmation").slideDown("slow");
        $("#support-confirmation").tabIndex = -1;
        $("#support-confirmation").focus();
      }
    });
    return false;
  });
  
  $("#email-form input:submit").click(function() {  

    // First, disable the form from submitting
    $('form#email-form').submit(function() { return false; });

    // Grab form action
    var formAction = $("form#email-form").attr("action");

    // Hacking together id for email field
    var id = "curlr";

    // Validate email address with regex
    if (!checkEmail("#email-email")) {
      $("#email-email-error").show();
      $("#email-email").focus();
      return false;
    }

    // Serialize form values to be submitted with POST
    var str = $("form#email-form").serialize();

    // Add form action to end of serialized data
    // CDATA is used to avoid validation errors
    //<![CDATA[
    var serialized = str + "&action=" + formAction;
    // ]]>

    // Submit the form via ajax
    $.recipy.showActivity();
    $.ajax({
      url: "email/cm-proxy.php",
      type: "POST",
      data: serialized,
      success: function(data){
        $.recipy.hideActivity();
        if (data.search(/invalid/i) != -1) {
          alert(data);
          alert('The email address you supplied is invalid and needs to be fixed before you can subscribe to this list.');
        } else {
          $(".recipy-email").hide();
          $(".email-confirmation").slideDown("slow");
          $(".email-confirmation").tabIndex = -1;
          $(".email-confirmation").focus();
        }
      }
    });
  });
  
  $('#email-name').focus(function() {
    if (this.value == 'Name') this.value = '';
  }).blur(function() {
    if (this.value == '') this.value = 'Name';
  });

  $('#email-email').focus(function() {
    if (this.value == 'E-mail') this.value = '';
  }).blur(function() {
    if (this.value == '') this.value = 'E-mail';
  });

  $('.screen').fancybox({
  		ajax : {
  		    type	: "GET"
  		}
  	});

  $(document).ready(function() {
    email_loading = $('.email-loading');
    support_loading = $('#support-loading');
  });
});


