/*****************************************************************************
 * getPlugin - jQuery Plugin for on-demand loading of scripts and styles
 *
 * Documentation : http://nicolas.rudas.info/jQuery/getPlugin/
 * Issues		 : http://plugins.jquery.com/project/getPlugin/
 *
 * Version: 080405 - 05 April 2008
 *
 ******************************/

// Define your plugins here

var plugin = {
  calendar : {
    _function: 'YAHOO.widget.Calendar',
    selectors: ["input.date"],
    files: [
      {_type : "script", src:'http://yui.yahooapis.com/2.5.2/build/yahoo-dom-event/yahoo-dom-event.js' },
      {_type : "script", src:'http://yui.yahooapis.com/2.5.2/build/calendar/calendar-min.js' },
      {_type : "css", href:'http://yui.yahooapis.com/2.5.2/build/calendar/assets/calendar.css'}
    ]
  }
};

/*
	tabs : { // Plugin's name
		_function: '$().tabs', //the name of a function contained in your external script. _function is used to determined whether an external script has loaded or not.
		selectors : ["[class*='nav-tabs']"], //The HTML attributes that signify that an external script or style is required. In jQuery selectors syntax.
		files : [
			{_type : "script", src:'../scripts/jq.tabs.pack.js' },
			{_type : "css", href:'../styles/style.css' }
		]
	}
};
*/

(function($){
// Set debug to true to show messages in Firebug's console or Safari's javascript console
// Note: showing these messages may make the script slower, so keep this to false when used live
	var debug  = false;
	var plugin_defaults = {
		files : {
// kaolin - css default isn't working???
			css : {
				media : "screen",
				type : "text/css",
				rel : "stylesheet"
			},
			script : {
				type : "text/javascript"
			}
		}
	};
	var time = 0;
// Update array of plugins with defaults	
	jQuery.each(plugin,function(){
		var p = this;
		jQuery.each(p.files,function(){
			var f = this;f._loaded=false;
			jQuery.extend(f,plugin_defaults.files[f._type]);
		});
	});
	
	
	isNeeded = function(selectors){
		if(window.console && debug) {
			var t = new Date().valueOf();
			console.info('Start isNeeded ('+selectors.join(', ')+')');
		}
		var _return = false;
		for (i=0;i<selectors.length;i++){
			if($(selectors[i]).length>0) { _return = true;}
		}
		if(window.console && debug) {
			var t2 = new Date().valueOf();
			time += (t2-t);
			console.info('End isNeeded in '+(t2-t)+' ms ('+selectors.join(', ')+') '+ _return);
		}
		return _return;		
	};
	
	whichNeeded = function(){
		var _plugins = [];
		if(window.console && debug) {
			var t = new Date().valueOf();
			console.info('Start whichNeeded');
		}
		jQuery.each(plugin,function(){
			var plugin = this, selectors = plugin.selectors;
			if (isNeeded(selectors)) { _plugins.push(plugin);  $.getPlugin(this,false); }
		});
		if(window.console && debug) {
			var t2 = new Date().valueOf();
			time += (t2-t);
			console.info('End whichNeeded in '+(t2-t)+' ms ('+_plugins.join(', ')+')');
		}
		return _plugins;
	};
	
	check = function(_options){
		if(window.console && debug) {
			var t = new Date().valueOf();
			console.info('Start check ('+_options._function+')');
		}
		if(eval("typeof "+_options._function) != "undefined") {
			
			for(f=0;f<_options._callbacks.length;f++){
				if(typeof _options._callbacks[f] == "function"){
				 	_options._callbacks[f].apply(_options._data[f]);
				}
				_options._callbacks[f]= "" ;
				_options._data[f] = "";
			}
			
			clearInterval(_options.timer);
			clearTimeout(_options.timer_not_found);
			_options.timer = false;
		}
		if(window.console && debug) {
			var t2 = new Date().valueOf();
			time += (t2-t);
			console.info('End check in '+(t2-t)+' ms ('+_options._function+')');
		}
	};
	
	call = function(_options){
		if(!_options.timer) {
			_options.timer = true;
			
			var interval = 3+Math.random().toString().substring(2,4);
			
			_options.timer = setInterval(function(){
				check(_options);
			},interval);
			
			_options.timer_not_found = setTimeout(function(){
				clearInterval(_options.timer);
				clearTimeout(_options.timer_not_found);
			},4000);
		} else {
			
		}
	};
	
	$.extend($, {
		getPlugin : function(){
			this.getNeeded = function(){return whichNeeded();};
			if(arguments.length == 0){return this;}
			
			var _options = arguments[0];
			if(window.console && debug) {
				var t = new Date().valueOf();
				console.info('Start $.getPlugin ('+_options._function+')');
			}
		// Defaults
			var _check_attributes = true, _data = false, _callback = false;
			
			
			for(var i =1; i<arguments.length;i++){
				if(typeof arguments[i] == "boolean") {_check_attributes = arguments[i];}
				else if(typeof arguments[i] == "function"){_callback = arguments[i];}
				else {_data = arguments[i];}
			}
			
								
			(typeof _options._callbacks == "object") ? _options._callbacks.push(_callback) : _options._callbacks = [_callback];
			(typeof _options._data == "object") ? _options._data.push(_data) : _options._data = [_data];
						
			if ( (_check_attributes && isNeeded(_options.selectors) ) || !_check_attributes){
				jQuery.each(_options.files,function(){
					var file = this, file_type = file._type; file_element = (file_type=="css") ? "link" : file_type;
					var file_location = (file.src || file.href), file_location_attr = (file.src) ? "src" : "href";
																																						
					if(!_check_attributes || !file._loaded && (_check_attributes && $(file_element+"["+file_location_attr+"='"+file_location+"']").length == 0))  {
						
						if(window.console && debug) {
							var p = new Date().valueOf();
							console.info('Importing ('+file_location+')');
						}
						
						var _load = (document.getElementsByTagName("head")[0] || document.getElementsByTagName("body")[0]);
					//	var _load = document.getElementsByTagName("body")[0];
						_load = _load.appendChild(document.createElement(file_element));
						
						jQuery.each(file, function(a,v){// k = attribute name, v = value
							if(a.toString().substring(0,1) != "_"){
								_load[a] = v.replace(/\//g,"\/");
							}
						});
						
						file._loaded = true;
						if(window.console && debug) {
							var p2 = new Date().valueOf();
							time += (p2-p);
							console.info('End Importing in '+(p2-p)+' ms ('+file_location+')');
						}
					} else {
					//	console.error('FILE ALREADY FOUND:')
					//	console.error(file_location)
					}
	
				}); // end importing files
				if(_callback){ call(_options) ;}
			} // end check whether to import or not
			if(window.console && debug) {
				var t2 = new Date().valueOf();
				time += (t2-t);
				console.info('End $.getPlugin in '+(t2-t)+' ms ('+_options._function+')');
			}
		} // end $.getPlugin
	}); // end jQuery extend
	whichNeeded();
	$(document).bind('ready',function(){
		setTimeout(function(){
			if(window.console && debug){console.warn('$.getPlugin Total Time: '+time+' ms'); }
			time = 0;
		},5000);
	});
})(jQuery);
