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