// Taken from and inspired by Yehuda Katz' "Evented Programming with jQuery"
//   http://yehudakatz.com/2009/04/20/evented-programming-with-jquery/
jQuery.fn.setupPlugin = jQuery.fn.setupPlugin || function(setup, options) {
  for (extra in setup) {
    var self = this;
    if (setup[extra] instanceof Array) {
      for (var i=0; i < setup[extra].length; i++) {
        setup[extra][i].call(self, options);
      }
    } else {
      setup[extra].call(self, options);
    }
  }
};

/* $d("....")
 *
 *  Easy access to jQuery's per-element data cache.
 *
 *  Examples
 *
 *    $d("#foo").age = 12
 *    $d("#foo").age //=> 12
 *    $d("#foo").age = $d("#foo").age + 1
 *
 *  Slightly adapted from http://yehudakatz.com/2009/04/20/evented-programming-with-jquery/ by Yehuda Katz.
 */
$d = function(param) {
  var node = jQuery(param)[0];
  var id   = jQuery.data(node);

  jQuery.cache[id] || (jQuery.cache[id] = {});
  jQuery.cache[id].node = node;

  return jQuery.cache[id];
}

/* $("...").getTarget(options = {})
 *
 *  Retrieves a target element, referenced by default via the "rel" attribute.
 *
 *  options:
 *    attr: indicates which attribute (on this element) holds the selector. Default "rel".
 *    prefix: specifies a prefix to build the target element selector. Default: "" unless attr == "for", in
 *      which case it is "#".
 *
 *  Examples:
 *
 *  * Basic usage
 *      <input id="foo" type="checkbox" rel="#bar" /><div id="bar">baz</div>
 *      $("#foo).getTarget().html(); //=> "baz"
 *
 *  * With Options
 *      <label id="foo" for="bar" >Label</label><input type="text" id="bar" value="baz" />
 *      $("#foo).getTarget({ attr: "for", prefix: "#" }).html(); //=> "baz"
 *      $("#foo).getTarget({ attr: "for" }).html(); //=> "baz", because attr == "for", then prefix == "#" by 
 *        default
 */
jQuery.fn.getTarget = function(options) {
  var self = jQuery(this);
  options                   || (options           = {});
  options["attr"]           || (options["attr"]   = "rel");
  options["prefix"]         || 
    options["prefix"] == "" || (options["prefix"] = options["attr"] == "for" ? "#" : "");

  var selector = self.attr(options["attr"]);
  return selector && jQuery(options["prefix"] + selector);
};

/* taken from http://happygiraffe.net/blog/2007/09/26/jquery-logging/ */
jQuery.fn.log = function (msg) {
      console.log("%s: %o", msg, this);
      return this;
  };


