
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//	SYSTEME QUI PERMET D'INCLURE DES FICHIERS HTML CONTENANT DU CODE JS
//	AU MOMENT DE L'APPEL D'UNE FONCTION QU'IL CONTIENT
//
// Améliorations à apporter : essayer d'intégrer le système avec Ajax
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

var jsinc_calls = new Array(); //Défini les fichiers js inclus
var jsinc_interval;
var jsinc_prm = new Array();

function jsInc_Hash(sFile) {
 if (!sFile || sFile=="") return 1;
 var h=0,g=0;
 for (var i=sFile.length-1;i>=0;i--) {
  var c=parseInt(sFile.charCodeAt(i));
  h=((h << 6) & 0xfffffff) + c + (c << 14);
  if ((g=h & 0xfe00000)!=0) h=(h ^ (g >> 21));
 }
 return h.toString();
}

function jsInc_isFunction(a) {
		if (a == undefined)return false;
    return typeof a == 'function';
}

function is_called(sFile)
{
	var sHash = jsInc_Hash(sFile);
	if (jsinc_calls[sHash]!=undefined)return true
	else return false;
}


function jsInc(sFile,sFonction)
{
			var sHash = jsInc_Hash(sFile);
	
			//Le fichier est-il déjà chargé
			if (!is_called(sFile))
			{
				CSLib.jsLoader.load(sFile, 
				{
					beginLoading: function(e){},
					endLoading: function(e){jsinc_calls[e]="1";}
				});
			}		

			if (is_called(sFile))
			{
				if (sFonction != undefined)
				{
					//Les parametres de la fonction

						// Initialisation ////////////////////////////////////////////////////////////////////////////////////////////
					
						if (!jsinc_prm[sHash])jsinc_prm[sHash] = new Array();
					
						// Gestion des arguments à passer à la fonction à appeler ////////////////////////////////////////////////////
						if (jsinc_prm[sHash][sFonction])delete jsinc_prm[sHash][sFonction];

						jsinc_prm[sHash][sFonction] = new Array();
						if (jsInc.arguments.length > 2)
						{
							for (var i=2; i<jsInc.arguments.length; i++)
							{
								jsinc_prm[sHash][sFonction][i-2]=arguments[i];
							}
						}
						
						var args = "";
						for (var i=0;i<jsinc_prm[sHash][sFonction].length;i++)
						{
							if (args != "") args+=",";
							args += "jsinc_prm["+sHash+"][\""+sFonction+"\"][\""+i+"\"]";
						}						
					
						// Preparation de l'appel de la fonction

						var call_str = sFonction+"("+args+")";

						window.setTimeout(call_str,5);
				}
			}

}


var CSLib = {};

CSLib.jsLoader =
    {
      
        load : function( fileName, options)
        {
            if (typeof(fileName) == 'object')
            {
               for (s in fileName)
                    if (typeof(fileName[s]) == 'string')
                        CSLib.jsLoader.load(fileName[s],options);
                return;
            }
            if (this.loaded[fileName] || this.loading[fileName])
                return;
            
            var head = document.getElementsByTagName('head')[0]; 
            
            var script = document.createElement('script'); 
            script.src = fileName;
            script.type = 'text/javascript';
            
            var filehash = jsInc_Hash(fileName);
            
	          head.appendChild(script);
            
            with (CSLib.jsLoader)
            {
                options = options || {};
                var emptyFunction = function(){};
                _beginLoading[filehash] = (options.beginLoading || emptyFunction);
                _endLoading[filehash] = (options.endLoading || emptyFunction );
                
                CSLib.jsLoader.beginLoading[filehash] = function(e)
                    {
                        loading[filehash] = true;
                        _beginLoading[filehash](e);
                    };
                CSLib.jsLoader.endLoading[filehash] = function(e)
                    {
                        loading[filehash] = false;
                        loaded[filehash] = true;
                        _endLoading[filehash](e);
                    };
                    
                beginLoading[filehash](filehash);
                endLoading[filehash](filehash);
            }
        },
        loading : {},
        loaded : {}, 
        _beginLoading: function(){},
        _endLoading: function(){},
        beginLoading: function(){},
        endLoading: function(){}
    }