/*!
 * jQuery.Buttonizer
 *
 * @projectDescription Simple utility which makes ...
 * @author Jiri Prokop
 * @version 0.1
 *
 * @id jQuery.buttonizer
 * @id jQuery.fn.buttonizer
 * @param {Object} settings Hash of settings, nothing is required.
 * @return {jQuery} Returns the same jQuery object, for chaining.
 */

(function($) {
  var Buttonizer = function(element, options) {
    // private fields and shortcuts
    var $elem = $(element);
    var me = this;
    var settings = $.extend({
      enabled: true      
    }, options || {});
    var enabled = null;
    
    // public methods
    this.enable = function() {
      if (enabled === null || !enabled) {
        $elem.css('cursor', 'pointer');
        $elem.css('background-position', '0% 0%');
        $elem.hover(function() {
          $elem.css('background-position', '0% 50%');
        }, function() {
          $elem.css('background-position', '0% 0%');
        });
        enabled = true;
      }
    };
    
    this.disable = function() {
      if (enabled === null || enabled) {
        $elem.css('cursor', 'auto');
        $elem.unbind('mouseenter mouseleave');
        $elem.css('background-position', '0% 100%');
        enabled = false;
      }
    };
    
    this.isEnabled = function() {
      return enabled;
    };
    
    // private methods
    var initialize = function() {
      if (settings.enabled) {
        me.enable();
      } else {
        me.disable();
      }
    };
    
    // initialize
    initialize();
  };

  $.fn.buttonizer = function(options) {
    return this.each(function() {
      var $elem = $(this);
      var key = 'buttonizer';
      var data = $elem.data(key);
      var instanceExists = false;
      
      if (typeof data !== 'undefined' && data instanceof Buttonizer) {
        instanceExists = true;
      } else if (typeof data === 'object') {
        // options from html data-element are merged together with options of plugin when initializing
        options = $.extend(data, options || {});
      }
      
      if (!instanceExists) {
        $elem.data(key, new Buttonizer(this, options));
      }
    });
  };
})(jQuery);
