/* developped by Arnaud Binard www.wikio.com */
dom = {toString : new function(){return "Dom Builder By Arnaud B.";}}
dom.agt = navigator.userAgent.toLowerCase();
dom.ie	   = ((dom.agt.indexOf("msie") != -1) && (dom.agt.indexOf("opera") == -1));
dom.opera  = (dom.agt.indexOf("opera") != -1);
dom.mac	   = (dom.agt.indexOf("mac") != -1);
dom.mac_ie = (dom.ie && dom.mac);
dom.win_ie = (dom.ie && !dom.mac);
dom.gecko  = (navigator.product == "Gecko");
//Les tableaux.
dom.table =  function(id,cssClass,style,width,height){
	var table = document.createElement("TABLE");
	setTable(table);
	if(id) table.id = id;
	if(cssClass) table.className = cssClass;
	if(style) table.setAttribute("style",style);
	if(width) table.style.width = width;
	if(height) table.style.height = height;
	return table; 
	}
	
dom.tbody =  function(id,cssClass,style){
	var tbody = document.createElement("TBODY");
	if(id) tbody.id = id;
	if(cssClass) tbody.className = cssClass;
	if(style) tbody.setAttribute("style",style);
	return tbody; 
	}


dom.tr =  function(id,name,cssClass,style){
	var tr = document.createElement("TR");
	if(id) tr.setAttribute("id",id);
	if(cssClass) tr.className = cssClass;
	if(style) tr.setAttribute("style",style);
	return tr; 
	}

dom.td =  function(id,name,cssClass,align,valign,noWrap,colSpan,rowSpan,fct,param){
	var td = document.createElement("TD");
	if(id) td.id = id;
	if(name) td.name= name;
	if(cssClass) td.className = cssClass;
	if(align) td.style.textAlign = align;
	if(valign) td.style.verticalAlign = valign;
	if(noWrap) td.setAttribute("noWrap","yes");
	if(colSpan) td.setAttribute("colSpan",colSpan);
	if(rowSpan) td.setAttribute("rowSpan",rowSpan);
	if(fct) td.fct = fct;
	if(param) td.param = param;
	return td; 
	}

dom.text =  function(txt){
	var text = document.createTextNode(txt);
	return text; 
	}

dom.a =  function(name,href,target,value){
	var anchor = document.createElement("A");
	if (name) anchor.setAttribute("name",name);
	if (href) anchor.setAttribute("href",href);
	if (target) anchor.setAttribute("target",target);
	if (value) anchor.appendChild(dom.text(value));
	return anchor; 
	}	
	

dom.div =  function(id,name,cssClass,visibility,width,height,position,top,left,zIndex,overflow,fct,param){
	var div = document.createElement("DIV");
	if(id) div.id = id;
	if(id) div.name = name;
	if(cssClass) div.className = cssClass;
	if(visibility) div.style.visibility = visibility;
	if(width) div.style.width = width;
	if(height) div.style.height = height;
	if(position) div.style.position = position;
	if(top) div.style.top = top;
	if(left) div.style.left = left;
	if(zIndex) div.style.zIndex = zIndex;
	if(overflow) div.style.overflow = overflow;
	if(fct) div.fct = fct;
	if(param) div.param = param;
	return div; 
	}

dom.img =  function(src,id,name,title,cssClass,width,height,border,fct,param){
	var img = document.createElement("IMG");
	if(src) img.src = src;
	if(id) img.id = id;
	if(name) img.name = name;
	if(title) img.title = title;
	if(cssClass) img.className = cssClass;
	if(width) img.style.width = width;
	if(height) img.style.height = height;
	img.border = (border)? border:0;
	if(fct) img.fct = fct;
	if(param) img.param = param;
	if(src) return img; 
	}
	
dom.iframe =  function(id,name,source,visibility,display,height,width){
	var iframe = document.createElement("IFRAME");
	if(id) iframe.id = id;
	if(name) iframe.name = name;	
	iframe.style.visibility = (visibility)? visibility : "hidden";
	iframe.style.display = (display)? display : "";
	iframe.style.height = (height)? height : "0px";
	iframe.style.width = (width)? width : "0px";
	iframe.src = (source)? source : "";
	return iframe; 
}

dom.xml =  function(id,src){
	var xmlIsland = document.createElement("XML");
	if(id) xmlIsland.id = id;
	if(src) xmlIsland.src = src;
	return xmlIsland; 
}

dom.dataIsland =  function(id,src){
	if (dom.ie) return dom.xml(id,src);
	else return dom.iframe(id,id,src);
}

dom.getflash =  function(FileName,width,height){
	width = (width)? width : 320;
	height = (height)? height : 240;
	
	if(dom.ie){
		var div = document.createElement("DIV");
		div.id = "media";
		document.body.appendChild(div);
		var flash = document.getElementById("media");
		var tmpElt = document.createElement("em");
		var param = document.createElement("param");
		param.setAttribute("name","movie");
		param.setAttribute("value",FileName);
		tmpElt.appendChild(param);
		var param = document.createElement("param");
		param.setAttribute("name","autostart");
		param.setAttribute("value","true");
		tmpElt.appendChild(param); 
		var param = document.createElement("param");
		param.setAttribute("name","wmode");
		param.setAttribute("value","transparent");
		tmpElt.appendChild(param); 
		tmpElt.setAttribute("data","sample.swf");
		tmpElt.setAttribute("type","application/x-shockwave-flash");
		tmpElt.setAttribute("width","200");
		tmpElt.setAttribute("height","200");
		flash.appendChild(tmpElt);
		
		flash.innerHTML = flash.innerHTML.replace(/(<\/?)em/ig,'$1object');
		}
	else{
	width = (width)? width : 320;
	height = (height)? height : 240;
	var flash = document.createElement("embed","wmode");
	flash.setAttribute("src",FileName);
	flash.setAttribute("width",width);
	flash.setAttribute("height",height);
	flash.setAttribute("wmode", "transparent");
	return flash; 
	}
}

//conteneurs types
dom.makeTable =  function(id,cssClass,style,width,height){
	table = dom.table(id,cssClass,style,width,height);
	tbody = dom.tbody();
	tr = dom.tr();	
	table.appendChild(tbody);
	tbody.appendChild(tr);	
	return table;
}


// fonctions utiles

function setTable(e,w){
	e.setAttribute("cellPadding","0");
	e.setAttribute("cellSpacing","0");
	e.setAttribute("border","0");
	if (w) e.style.width = "100%";
}

function addEvent(obj, evType, fn){
 if (obj.addEventListener){
   obj.addEventListener(evType, fn, true);
   return true;
 } else if (obj.attachEvent){
   var r = obj.attachEvent("on"+evType, fn);
   return r;
 } else {
   return false;
 }
}

function findInChildren(event,fromNode,searchedNode,id){
	subTgt = null;
	return scanChildNodes(event,fromNode,searchedNode,id);
}

function scanChildNodes(event,fromNode,searchedNode,id){
	var k = id+'_';
	for(k = 0 ; k < fromNode.childNodes.length && subTgt == null; k++){
		if(fromNode.childNodes[k].name && fromNode.childNodes[k].name == searchedNode){		
			subTgt = fromNode.childNodes[k];
			}
		else if(fromNode.childNodes[k].id && fromNode.childNodes[k].id == searchedNode){		
			subTgt = fromNode.childNodes[k];
			}
		else if(fromNode.childNodes[k].childNodes.length > 0)
			findInChildren(event,fromNode.childNodes[k],searchedNode,k);
		if (subTgt != null) break;
	}
	if(subTgt != null) return subTgt;
}