/**
 * 'requires' - 1.0
 * Copyright  - DigitalTomato 2009
 *
 * Script paths can be passed to the function to load the required script once and only once per page.
 * This is useful when developing widgets/portlets.  Note: in order to avoid duplicate script loads,
 * the paths passed for a particular script must be an exact match.
 * 
 * Usage: $().requires(path)
 */
jQuery.fn.requires = function(path, callback) {
  // init if not loaded
  if (jQuery.requiredScripts === undefined) {
    jQuery.requiredScripts = [];
  }

  // check if the script is already loaded
  if (jQuery.inArray(path, jQuery.requiredScripts) > -1) {
    if (window['console'] && window['console']['debug']) {
      console.debug('Script already loaded: ' + path);
    }
    return this;
  }

  if (window['console'] && window['console']['debug']) {
    console.debug('Loading script: ' + path);
  }

  // do it!
  jQuery.getScript(path, callback);
  jQuery.requiredScripts.push(path);

  return this;
}

