/**
 * fhHover - Freshheads Hover
 *   http://www.freshheads.com
 *
 * Copyright (c) 2009 Freshheads
 * 
 * Built on top of the jQuery library
 *   http://jquery.com
 *
 * Usage:
 * $.fhHover({
 *   hoverClassName: String ('fhHover-active')
 * });
 *
 * The hoverClassName is optional

 * Authors: Jeroen van Beek, Gijs van Zon MA
 * Return: Object (this)
 * Options: {
 *   hoverClassName: String ('fhHover-active')
 * }
 */

(function($) {

  $.fn.fhHover = function(options) {

    var defaults = {
      hoverClassName: 'fhHover-active'
    }
    
    defaults = $.extend({}, defaults, options || {});
    
    return this.each(function(){
      
      /* Get element and set Clickable to true */
      var $element = $(this);

      $element.data('fhhClickable', true);
      
      /* Get sub-links and disable the block click when they are hovered */
      $element.find('a')
        .bind('mouseover', function(e) {
          $element.data('fhhClickable', false);
        })
        .bind('mouseout', function(e) {
          $element.data('fhhClickable', true);
        });
    
      /* Set mouseover class to element */
      $element
        .bind('mouseover', function(e) {
          $(this)
            .addClass(defaults.hoverClassName)
            .css({
              'cursor': 'pointer'
            });
        })
        /* Remove mouseover class */
        .bind('mouseout', function(e) {
          $(this).removeClass(defaults.hoverClassName);
        })
        /* Set click action */
        .bind('click', function(e) {
          /* Check if element is clickable */
          if($element.data('fhhClickable') == true){
           
            /* Check if target is given */
            var link = $element.find('a[rel*=fhhLocation]');
            if(link.length > 0) {
              /* Check whether or not to open te url in a new window */
              if(link.attr('target') == '_blank') {
                window.open(link.attr('href'));
              } else {
                window.location = link.attr('href');
              }
              return false;
            }
          }
        })
    });
  };

})(jQuery);
