// JQuery 
$(function(){

  $(".love_it_orb[title]").tooltip();
  
  // Hide all error messages
  $(".form_error").hide();  
  
  //add focus and blur events to hide/show default text
  var loveEmailInput = $("#love_it_email");

  loveEmailInput
    .attr('DefaultValue', loveEmailInput.attr("value")) // Assign default value
    .focus(function(e) { // Add onFocus event handler
      if($(this).attr("value") == $(this).attr('DefaultValue')) {
        $(this).attr("value", "");
      }
    })
    .blur(function(e) { // Add onBlur event handler
      if($(this).attr("value") == "") {
        $(this).attr("value", $(this).attr('DefaultValue'));  
      }
    });
  
  // Attach autoSubmit event to love it tab #3
  $("#divproductlink3.love_it a").click(autoSubmitLoveItRequest);
  
  // Add submit button click handler
  $("#btn_submit_love_it").click(function(){
    
    // Prevent subsequent clicks
    if(this._loved == true) 
    {
      return false;
    }
    
    // Hide all error messages
    $(".form_error").hide(); 
    
    // validate and process    
    var loveEmailInput = $("#love_it_email");
    var email = loveEmailInput.val();  
    if (email == "" || email == loveEmailInput.attr("DefaultValue")) {  
      $("span#love_it_email_error").html("This field is required").fadeIn();  
      loveEmailInput.focus();  
      return false;  
    }  
    // Validate email address
    if(!(/^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/.test(email))) {
      $("span#love_it_email_error").html("Email address is not valid").fadeIn();  
      loveEmailInput.focus();  
      return false;  
    }
    
    // Show loading message
    $("#love_it_loading").show();
    
    // Save email in cookie
    createCookieThatNeverExpires('bohemia_love_it', email);
    
    // AJAX form post     
    submitLoveItRequest()
    
    // Prevent subsequent clicks
    this._loved = true;
    
    return false;
  })
});

function autoSubmitLoveItRequest()
{
  var email = readCookie('bohemia_love_it');
  if(email) {
    $("#love_it_email").val(email); // set value in form to that of cookie
    $("#love_it_newsletter").val(0).removeAttr('checked'); // uncheck the subscribe option
    
    // AJAX form post     
    submitLoveItRequest();
        
    $('#divproductcontent3').html('<span id="love_it_loading">loading</span>');
  }
}

function submitLoveItRequest()
{
  // AJAX form post     
  $.post("/index.php", 'ajax=1&page=shop.product_love&task=add&' + $("#love_it_form").serialize(), function(json) {
    
    // Replace contents of this div with the response data      
    $('#divproductcontent3').html(json.responseText);
    
    if(json.updated) {
      //updateLoveItOrb();
    }
    
    // remove autoSubmit event handler and reattch tab click handler
    $("#divproductlink3.love_it a").unbind('click', autoSubmitLoveItRequest);
    
  }, 'json');
}

function updateLoveItOrb()
{
  // TODO: add orb if it does not exist
  // TODO: update title attr for tooltip
  var count;
  var orb = $(".love_it_orb p");
  
  if(orb)
  {
    count = parseInt(orb.text());
    orb.text(++count);
  }
}
