/*****************************************************************************
 * $.plugin - 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: 081002 - 02 Oct 2008
 *
 *****************************************************************************/
(function($){$.plugins=$.plugins||{};$.plugins.cache=window.sessionStorage||{};$.plugins.settings={cache:true,ajax:{cache:true},context:$(document),target:$('head',this.context),init:function(){},preLoad:function(){},postLoad:function(){}};var defaults=$.plugins.settings,cache=$.plugins.cache;Plugin=function(name,settings){var that=this;this.name=name;for(var i in settings){that[i]=settings[i];};this.context=this.context||settings.context;this.target=this.target||settings.target;this.loaded={};this.queue=[];this.init.apply(this);return this;};Plugin.prototype.getFile=function(url){if(!url||typeof url!='string'){throw new Error('$.plugin.getFile(url) - url {String} must be specified');}
var that=this,extension=url.split('.')[url.split('.').length-1],fileId=url.replace(/\W/gi,''),cached=cache[url],caching=(defaults.cache===true||defaults.cache=='true');if(extension!='css'&&extension!='js'){throw new Error('$.plugin.getFile(url) - Invalid extension:'+extension+'\n\t'+url);return this;}
if(caching&&this.loaded[url]){return this;}
this.beforeGet(url);$('[data-file-id="'+fileId+'"]').remove();if(caching&&cached&&cached!='undefined'){if(extension=='css'){this.target.append('<style type="text\/css" rel="stylesheet" data-file-id="'+fileId+'">'+cached+'<\/style>');}
else if(extension=='js'){this.target.append('<script type="text\/javascript" data-file-id="'+fileId+'">'+cached+'<\/script>');}
setTimeout(function(){that.afterGet(url);},0);}else{if(extension=='css'){(function(){var opts=$.extend({url:url},defaults.ajax),onSuccess=opts.success||function(){};opts.success=function(response){onSuccess.apply(this,arguments);that.loaded[url]=true;cache[url]=response;that.target.append('<style type="text\/css" rel="stylesheet" data-file-id="'+fileId+'">'+response+'<\/style>');that.afterGet(url);};$.ajax(opts);})();}
else if(extension=='js'){(function(){var opts=$.extend({dataType:"script",url:url},defaults.ajax),onSuccess=opts.success||function(){};opts.success=function(){onSuccess.apply(this,arguments);var response=(typeof arguments[0]=='string')?arguments[0]:null;cache[url]=response;that.loaded[url]=true;that.afterGet(url);};$.ajax(opts);})();}}
return this;};Plugin.prototype.beforeGet=function(url){this.queue.push(url);defaults.preLoad.call(this,url);return this;};Plugin.prototype.afterGet=function(url){var that=this,callback=this.tmp_callback,index=$.inArray(url,this.queue);if(index==-1){throw new Error('$.plugin.afterGet(url) - Ignoring postLoad for file that should not be in queue:\n '+url);return this;}
this.queue.splice(index,1);if(this.queue.length==0&&callback){setTimeout(function(){callback.apply(that);delete that.tmp_callback;},0);}
defaults.postLoad.call(this,url);return this;};Plugin.prototype.get=function(){var that=this,files=(typeof this.files=='string')?[this.files]:this.files,callback=arguments[0]||this.callback;this.tmp_callback=callback;if(this.isNeeded()!==true){return this;}
var getFile=function(file){that.getFile(file);};for(var i=0;i<files.length;i++){(function(){var file=files[i];if($.browser.opera){setTimeout(function(){getFile(file);},500);}
else{getFile(file);}})();}
return this;};Plugin.prototype.isNeeded=function(){var that=this,selectors=(typeof this.selectors=='string')?[this.selectors]:this.selectors,isNeeded;for(var i=0;i<selectors.length;i++){var selector=selectors[i];if($(selector,that.context).length>0){isNeeded=true;break;}};return isNeeded||this;};$.extend($,{plugin:function(name,param){var self=$.plugin;if(arguments.length==0){for(var i in $.plugins){if(i=='settings'||i=='cache'){continue;}$.plugins[i].get();};return self;}
else if(typeof name!='string'){throw new Error('$.plugin(name,[settings||callback])\n\t\t@param name\t\t{String}\n\t\t@param settings\t{Object}\n\t\t@param callback\t{Function}');return self;}
if(typeof param=='object'){$.plugins[name]=new Plugin(name,$.extend(defaults,param));}
else{var plugin=$.plugins[name];if(typeof plugin!='object'){throw new Error('$.plugin: '+name+' is not specified');return self;}
if(typeof param=='function'){plugin.get(param);}
else if(!param){return plugin;}}
return self;},getPlugin:function(){return $.plugin.apply(this,arguments);}});})(jQuery);