﻿window.js_launcher = {
constructor:function()
{
	var l = window.js_launcher;
	
	l.div = document.createElement("div");
	l.div.id="loading";
	l.div.innerText = "Loading... Please wait.";
	document.body.appendChild(l.div);
	domtools.className.add(document.body,"load");
		
	window.onerror=l.handleerror;
	l.start = l.gettimeflag();
	l.dir = l.getjspath();
	l.nodescript = document.createElement("script");
	l.nodescript.type = "text/javascript";
	l.head = document.getElementsByTagName("head")[0];
	l.nodes = document.body.getElementsByTagName("*");
	l.timer=null;	
	l.delay=50;
	l.tries = 1;
	l.commands= new Array();
	l.jstoload=new Array();
	l.jstoload[0] = new Array("js_launcher","loaded", 1,
	"version : 0.1.3 / author : Frédéric Pineau (artwaï) <br/> contributors : Denis Lienard (artwaï)");
	l.ck = domtools.className.is(document.body,"ck");
	if(l.ck)
	{
		l.launchjsfile("js_cookiemng");
	}
	//for js_debug include in the page
	if(document.getElementById("loadedjs_debug"))
	{
		l.jstoload[l.jstoload.length] = new Array("js_debug","loaded", 1,
		"version : 0.1 / author : Frédéric Pineau (artwaï) <br/>");
	}
	
	
	
},
definitioncommand:function()
{
	var l = window.js_launcher;
	if(!l.ck)
	{
		for(var i=0; i <l.nodes.length; i++)
		{
			var t = l.nodes[i]; 
			
			if(t.id=="navigation")
			{
				l.addcommandtoload(
					function(o){window.navigation.constructor(o)},l.nodes[i],"js_navigation" 
					);
			}

            /*
             * Test si le formulaire contient le comportement de contrôle
             * de modification du formulaire en cas de changement.
             */
            if(t.tagName=="FORM" && 
                domtools.className.is(t,"js_formcontrol") )
            {
                l.addcommandtoload(function(o){window.formcontrol.init(o)}
                    ,l.nodes[i],"js_formcontrol");
            };

			if(t.tagName=="A" && domtools.className.is(t,"jsappendix") )
			{
				l.addcommandtoload(
					function(o){window.appendix.popupit(o)},l.nodes[i],"js_appendix"
					);
			}
		}
		l.load();
	}
	else
	{
		l.ck=(l.jstoload[1][1].indexOf("loaded") != -1)?false : l.ck;
		l.timer = window.setTimeout("window.js_launcher.definitioncommand()",l.delay);
	}
},
launchjsfile:function(src,handleloaded)
{
	if(!document.getElementById("loaded"+src))
	{
		var l = window.js_launcher;
		var t = new Array(src, "pending");
		l.jstoload.push(t);
		var s = l.nodescript.cloneNode(true);
		s.src = l.dir+src+".js";
		s.id="loaded"+src;
		if( "undefined" != typeof handleloaded )
		{
			//s.attachEvent("onload", handleloaded);
			domtools.event.addListener(s, "load", handleloaded);
		
		}
		l.head.appendChild(s);
	}
},
loaded:function(src,thtml)
{
	var l = window.js_launcher;
	var i = l.findload(src);
	var f = parseInt(l.gettimeflag()-l.start);
	if(l.jstoload[i])
	{
		l.jstoload[i] = new Array(l.jstoload[i][0],"loaded", f, thtml)
	}
},
findload:function(src)
{
	var l = window.js_launcher;
	var t = -1;
	for(var j=0; j<l.jstoload.length ; j++)
	{
		t =(l.jstoload[j][0].indexOf(src) != -1) ? j : t;
	}
	return t;
},
addcommandtoload:function(f,args,j)
{
	var l = window.js_launcher;
	var d = l.launchjsfile(j);
	var t = new Array(f,args,j);
	l.commands.push(t);
},
loading:function()
{
	var l = window.js_launcher;
	window.clearTimeout(l.timer);
	l.timer=null;
	if(l.tries <= 100)
	{
		for (var j=0; j<l.commands.length;j++)
		{
			var t = l.commands[j];
			var d= l.findload(t[2]);
			if(l.jstoload[d][1]=="loaded")
			{
				t[0](t[1]);
				l.commands.splice(j,1);
				j--;
			}
		}
		if(l.commands.length<=0)
		{
			document.onkeydown=l.keydowndebug;
			domtools.className.remove(document.body,"load");
			domtools.className.add(l.div,"screen_hide");
		}
		else
		{
			l.tries++;
			l.load();
		}
	}
	else
	{
		var div = document.createElement("div");
		for(var i=0; i<l.jstoload.length;i++)
		{
			var o = l.jstoload[i];
			if(o[1]=="pending")
			{
				var p = document.createElement("p");
				p.innerHTML+=o[0] +" not found.";
				div.appendChild(p);
			}
		}
		l.popupmessage("Loading error detected", div, "error");
		document.onkeydown=l.keydowndebug;
		domtools.className.remove(document.body,"load");
		domtools.className.add(l.div,"screen_hide");
	}
},
load:function()
{
	var l = window.js_launcher;
	l.timer = window.setTimeout("window.js_launcher.loading()",l.delay);	
},
gettimeflag: function()
{
	var t = new Date();
	t = t.getTime() ;
	return t;
},
getjspath: function()
{
	var dir, src = "js_launcher.js"; 
	var scripts = document.getElementsByTagName("script");
	for (var i=0; i<scripts.length; i++)
	{
		if (scripts[i].src.match(src))
		{ 
			dir = scripts[i].src.replace(src, "");break;
		}
	}
	return dir;
}, 
testdebug :function(handleloaded)
{
	var l = window.js_launcher;
	if(!document.getElementById("loadedjs_debug"))
	{
		l.launchjsfile("js_debug", handleloaded);
	}
	var d= l.findload("js_debug")
	return (l.jstoload[d][1]=="loaded")
},
keydowndebug:function(e)
{
	var evt=(e)?e:(window.event)?window.event:null;
	var l = window.js_launcher; 
	if(evt)
	{ 
		var key=(evt.charCode)?evt.charCode: ((evt.keyCode)?evt.keyCode:((evt.which)?evt.which:0)); 
		if (evt.altKey && evt.ctrlKey && evt.shiftKey && key == 74)
		{
			if(window.js_launcher.testdebug( function(e){window.debug.debuglauncher()} ))
			{
				window.debug.debuglauncher();
			}
		}
	} 
},
popupmessage:function(title,nodes,clss)
{
	if(window.js_launcher.testdebug( function(e){window.debug.popupmessage(title,nodes,clss)} ))
	{
		window.debug.popupmessage(title,nodes,clss)
	}
},
handleerror : function(msg,url,l)
{
	var dl = window.js_launcher; 
	if(!dl.testdebug())
	{
		window.setTimeout("window.js_launcher.handleerror('"+msg+"','"+url+"','"+l+"')",dl.delay);
	}
	else
	{
		window.debug.handleerror(msg,url,l);
	}
}
}
if(document.getElementById)
{
		domtools.event.addListener(window, "load", function()
												{
													window.js_launcher.constructor();
													window.js_launcher.definitioncommand();
												});
}
 