/**
 * This script catches clicks on the homepage and processes them for the ProtectedPropertyDetails module
 * @author Christopher Millward
 */
var is_superbox_loaded = false;

jQuery(document).ready(function() { 
  // Catch the click on the homepage
  jQuery('#featureimg a').click(function(event) {
    check_for_existing_user(event);  // Figure out the appropriate page action to take
  });
  
  jQuery('div.column-fp a').click(function(event) {
    check_for_existing_user(event);  // Figure out the appropriate page action to take
  });
  
  // Catch the click on the listings pages
  jQuery('div.details-button a').click(function(event) {
    check_for_existing_user(event);// Figure out the appropriate page action to take
  });
  
  jQuery('.short-result-sub-p > a').click(function(event) {
    check_for_existing_user(event);// Figure out the appropriate page action to take
  });
  
  // Catch the click on the feature-sheet pages
  jQuery('.sub-p4 a').click(function(event) {
    check_for_existing_user(event);// Figure out the appropriate page action to take 
  });
  
  // Load the superbox functionality
  jQuery(function(){
    init_superbox();
  });
});

/**
 * Checks to see if the user has visited the site before, and allows the user to register if not
 */
function check_for_existing_user(event) { 
  var sidCookieValue = Get_Cookie('sid');
  if (null == sidCookieValue && true == can_show_registration_form() && true == is_superbox_loaded) {  // Cookie doesn't exist
    
    // Save the destination and load the form 
    var $target = jQuery(event.target);
    var destination = '';
    if ($target.is("img")) {
      var $parent = $target.parent();
      destination = $parent[0];
      
      // Prevent multiple calls to this function from 'stacking' the registration URL again and again
      var propertyURLPosition = destination.href.indexOf('destination='); 
      if (-1 != propertyURLPosition) {
        destination.href = destination.href.substr(propertyURLPosition + 'destination='.length);
      }
      
      var page = window.location;
      var form_url = page.protocol + "//" + page.host + "/protectedpropertydetails/register" + "?destination=" + destination;
      $parent[0].href = form_url;
    }
    else if ($target.is("a")) { 
      destination = $target[0].href;
      
      // Prevent multiple calls to this function from 'stacking' the registration URL again and again
      var propertyURLPosition = destination.indexOf('destination='); 
      if (-1 != propertyURLPosition) {
        destination = destination.substr(propertyURLPosition + 'destination='.length);
      }
      
      var page = window.location;
      var form_url = page.protocol + "//" + page.host + "/protectedpropertydetails/register" + "?destination=" + destination;
      $target[0].href = form_url;
    } 
  }
}

/**
 * Checks to see if the registration form should be shown
 * @return TRUE if the registration form should be shown, else FALSE
 */
function can_show_registration_form() {
  
  // If no max is defined, show the form for every view
  var max_views = parseInt(Get_Cookie('max_views'));
  var free_views = parseInt(Get_Cookie('free_views'));
  
  if (null == max_views) {
    return (true);
  }
  
  if (null == free_views) {
    free_views = 0;
  }
  
  if (free_views >= max_views) {
    return (true);
  }
  else {
    return (false);
  }
}

/**
 * Gets the value of a cookie
 * @param check_name
 * @return The value of the named cookie, or null
 * 
 * Code for this function was taken from http://techpatterns.com/downloads/javascript_cookies.php
 */
function Get_Cookie( check_name ) {
  // first we'll split this cookie up into name/value pairs
  // note: document.cookie only returns name=value, not the other components
  var a_all_cookies = document.cookie.split( ';' );
  var a_temp_cookie = '';
  var cookie_name = '';
  var cookie_value = '';
  var b_cookie_found = false; // set boolean t/f default f

  for ( i = 0; i < a_all_cookies.length; i++ )
  {
    // now we'll split apart each name=value pair
    a_temp_cookie = a_all_cookies[i].split( '=' );


    // and trim left/right whitespace while we're at it
    cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

    // if the extracted name matches passed check_name
    if ( cookie_name == check_name )
    {
      b_cookie_found = true;
      // we need to handle case where cookie has no value but exists (no = sign, that is):
      if ( a_temp_cookie.length > 1 )
      {
        cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
      }
      // note that in cases where cookie is initialized but no value, null is returned
      return cookie_value;
      break;
    }
    a_temp_cookie = null;
    cookie_name = '';
  }
  if ( !b_cookie_found )
  {
    return null;
  }
}

/**
 * Initialize superbox
 */
function init_superbox() {
  var sidCookieValue = Get_Cookie('sid');
  if (false == is_superbox_loaded && null == sidCookieValue && true == can_show_registration_form()) { // Cookie doesn't exist and free views have been used up, show registration form
    // Add the Superbox tabs
    jQuery('#featureimg a').attr('rel', 'superbox[iframe][500][400]');
    jQuery('div.column-fp a').attr('rel', 'superbox[iframe][500][400]');
    jQuery('div.details-button a').attr('rel', 'superbox[iframe][500][400]');
    jQuery('.sub-p4 a').attr('rel', 'superbox[iframe][500][400]');
    jQuery('.short-result-sub-p a').attr('rel', 'superbox[iframe][500][400]'); 
    
    jQuery.superbox();
    is_superbox_loaded = true;  
  }
}
