$(document).ready(function(){

  //the the homepage of the user
  homepage.establishHome();
  
  //set the affiliate of the user
  homepage.establishGV();

  //set up tracking
  homepage.establishTracking();

  //on some homepages (mms365 pages), we have a form in the AboutCopy area
  //this next bit tries to capture information from that form
  //for the tracking table.
  if ($('#AboutCopy [method=post]:first').length) {

    // this gives an id to the form
    // this gets around the problem that we face having an embedded form with no id
    // because the form comes from another place
    $('#AboutCopy [method=post]:first').attr('id','capture-tracking-form');
    
    //define the button that is to be clicked
    homepage.captureTrackingButton = $("#capture-tracking-form :submit");
    
    //handle the user's click of the button
    homepage.captureTrackingButton.click(function() {
      return homepage.trackFormData();
    });    
  }
});


/**
 * Encapsilates homepage js into a single object
 */
var homepage = homepage || {};

//a variable to know which pass we have made on the trackFromData function
homepage.captureTrackingFormReady = false;

//a place to store the button
homepage.captureTrackingButton = {};

//this creates a place for ajax calls
homepage.url = window.location.protocol + '//' + whoIs.websiteName;

/**
 * Establishes the homepage cookie for whois page
 *
 * If it already exists, then do nothing, otherwise sets "whoIsHome"
 */
homepage.establishHome = function () {
  
  //this sets the individual WID home page
  if(getCookie('whoIsHome')){
    return;
  }

  //parse the url
  var url = window.location.pathname;
  url = url.split('/',2);
  
  //get the affiliate directory
  var whoIsName=url[1];
  setCookie('whoIsHome', whoIsName);
}

/**
 * Establish the affiliate as cookie "whoIsGv"
 *
 * The variable whoIsGv is set as inline script the index.html page
 * 
 */
homepage.establishGV = function () {
  
  //this tracks the userId, the grapevine of the referring person for new sign ups
  if(!getCookie('whoIsGv')){
    setCookie('whoIsGv', whoIsGv);
  }
  return true;  
}

/**
 *
 * Creates a tracking entry in the tracking table on first visit
 *  
 * Records IP address and whoIsGv; it then sets a cookie "whoIsTracking" to speed up subsequent page loads
 */
homepage.establishTracking = function () {  
  if (getCookie('whoIsTracking') || !whoIsGv) {
    return false;
  }

  //no cross domain ajax allowed
  if (!homepage.isCrossDomain()) {  
    $.post(homepage.url + '/app/ajax/whois.php', {a : 'track', gv : whoIsGv}, function (data){
      if (data == 'success'){
        setCookie('whoIsTracking', 'success');
      }
    });
    return true;
  }
  return false;
}

/**
 * This will take the submit button and submit to ajax first then trigger itself to send onward
 *
 * @todo Cross site ajax support?
 */
homepage.trackFormData = function (){

  //on the second pass don't save data, but post the form
  if(homepage.captureTrackingFormReady){
    return true;
  }
  
  var form = $('#capture-tracking-form');
  var name = $("#capture-tracking-form :input[name=name]").val();
  
  //the form changed on aweber so we have to support legacy too
  var email = $("#capture-tracking-form :input[name=email]").val();
  if (!email) {
    email = $("#capture-tracking-form :input[name=from]").val();
  }
  
  //this will skip our tracking and promote to aweber's form error handling
  //we skip tracking cause there's nothing new to track
  if (!name && !email){
    return true;
  }
  
  if (!homepage.isCrossDomain()) {
    $.post(homepage.url + '/app/ajax/whois.php', {a : "track", name : name, email : email, gv : whoIsGv}, function(){
      homepage.captureTrackingFormReady = true;
      homepage.captureTrackingButton.trigger('click');
    });
    return false;
  }
  else {
    
    //if we are cross site, then don't do the ajax call instead return true on the form submit so it heads to destination
    //we may look into cross site support for ajax later
    return true;
  }
}

/**
 * Tells if our ajax call would be cross domain
 */
homepage.isCrossDomain = function () {
  return whoIs.websiteName != window.location.hostname;
}

