
var jPlugins = (	function(){
	
  function _require (libraryName) {
    // inserting via DOM fails in Safari 2.0, so brute force approach
    document.write('<script type="text/javascript" src="'+libraryName+'"><\/script>');
  };
  
  var _REQUIRED_JQUERY = "1.3.2";
  var _PLUGIN_FOLDER = "jplugins";
  var _collections = {
  	"all"    : "jquery.cookies-1.0,jquery.nyroModal-1.5.1-ah,ui-1.7.2/ui.core,ui-1.7.2/ui.draggable,ui-1.7.2/ui.datepicker.js",
  	"fh"     : "jquery.cookies-1.0,jquery.nyroModal-1.5.1-ah,ui-1.7.2/ui.core,ui-1.7.2/ui.draggable",
  	"home"   : "jquery.cookies-1.0,ui-1.7.2/ui.datepicker.js"
  };
  
  function _convertVersionString(versionString){
      var r = versionString.split('.');
      return parseInt(r[0])*100000 + parseInt(r[1])*1000 + parseInt(r[2]);
  };
  
  var _log = false;  
  function _alert(msg){if (_log && window.console)console.log(msg);};
  
  function _unique (notUniqArray) {
    // Check that we were given an array
    // If not, return the object
    if (!jQuery.isArray(notUniqArray)) return notUniqArray
    // Add each array value as a key in a map
    var map = {}
    for (var index = 0; index<notUniqArray.length ;index++) {
      value = notUniqArray[index]
      // Store type_value as a map key,
      // unless 5 and '5' would be the same as a map key
      map[typeof value + '_' + value] = value
    }
    // Build a new array with each map keys
    var uniqValues = []
    for (var key in map) {
      uniqValues.push(map[key])
    }
    return uniqValues
  };

  
  return {
  	load: function() {
    if(typeof jQuery=='undefined'||
    		_convertVersionString(jQuery.prototype.jquery) < 
        _convertVersionString(_REQUIRED_JQUERY)
        ) {
       throw("jPlugins requires the jQuery JavaScript framework >= " + _REQUIRED_JQUERY);
    }
    jQuery.each(jQuery.grep(jQuery("script"), function(s) {
    	return s.src.match(/jplugins\.js(#.*)?$/);
    })
    , function(s) {
    	var path = jQuery(this).attr('src').replace(/jplugins\.js(#.*)?$/,'')+_PLUGIN_FOLDER+'/';
      var load = jQuery(this).attr('src').match(/#.*load=([a-z,]*)/); load  = (load)?load[1]:load;
      var set  = jQuery(this).attr('src').match(/#.*set=([a-z]*)/);   set  = (set)?(set[1]):set;
      
      var inc ="";
      if (load && !set) {_alert("load seul : " + load); inc = load;}
      else if (!load && set) {_alert("set seul : " + set); inc = _collections[set] || _collections.all;}
      else if (load && set) {_alert("set et load"); inc = _collections[set] || _collections.all; inc = inc + "," + load}
      else  /*(!load && !set)*/ {_alert("rien"); inc = _collections.all;}
      inc = _unique(inc.split(',')); 
      _alert(inc);
      jQuery.each(inc,
       function(include) { _require(path+this+'.js') });
    });
  }
  
	}
	
})();

jPlugins.load();