
/**
 * Unobstrusive xiti click tags
 * 
 * @author Julien Lirochon <julien@lirochon.net>
 */


/**
 * helper function internally used by the plugin
 * 
 * @author Julien Lirochon <julien@lirochon.net>
 */
jQuery.marker = {
  debug: false,
  handleXitiClick: function(element, options) {
    // default type
    if (options.xitiType == null)
    {
      options.xitiType = 'N'; // navigation
    }
    
    // default level2
    if (options.xitiLevel2 == null)
    {
      options.xitiLevel2 = 2;
    }
    
    if(options.popup == true)
    {
      element.target = '_blank';
    }
    
    if (!jQuery.marker.debug)
    {
      return xt_click(element, 'C', options.xitiLevel2, options.xitiName, options.xitiType);
    }
    
    try
    {
      console.log('xt_click(element, \'C\', %s, %s, %s)', options.xitiLevel2, options.xitiName, options.xitiType);
    }
    catch(e)
    {}
    
    return true;
  },
  handleClick: function(event) {
    var options = jQuery(this).data('markerOptions');
    var hasTimeout = true;
    if (options.xitiName != null)
    {
      hasTimeout = !jQuery.marker.handleXitiClick(this, options);
    }
    
    if (jQuery.marker.debug || hasTimeout)
    {
      event.preventDefault();
    }
  }
};

/**
 * Adds 'addClickMarker' method on jQuery objects
 * usage : jQuery('#my-button-id').clickMarker({xitiType: 'N', xitiLevel2: 1, xitiName: 'my-button-was-clicked'});
 * 
 * @author Julien Lirochon <julien@lirochon.net>
 */
jQuery.fn.addClickMarker = function(options)
{
  // nothing selected
  if (!this.length)
  {
    return this;
  }
  
  // wrong argument
  if (options == null || typeof options != 'object') {
    return this;
  }
  
  // record options
  this.data('markerOptions', options);
  
  // bind click event
  this.click(jQuery.marker.handleClick);
}
