
function initManageModule()
{
	managerNode = document.createElement('div');
	managerNode.setAttribute("class", "mduManagerId");
	managerNode.setAttribute("className", "mduManagerId");
	managerNode.setAttribute("id","mduManagerId");
	managerNode.style.position = 'absolute';
	managerNode.style.top = '0px';
	managerNode.style.left = '0px';
	managerNode.style.textAlign = 'center';
	
	// DEBUG ONLY
	//managerNode.style.top = '300px';
	//managerNode.style.left = '500px';
	//managerNode.style.textAlign = 'left';
	
	document.body.appendChild(managerNode);
	
	setEditionMode(0)
}

function setEditionMode(mode)
{
	if(mode == 0)
	{
		document.getElementById('mduManagerId').innerHTML = ' \
			<div> \
				Mode : Preview<br>\
				<span class="mduManagerButton" onmousedown="setEditionMode(1);">Edition</span>  - \
				<span class="mduManagerButton" onmousedown="setEditionMode(2);">Info</span> \
			</div>\
		';
		
		return 0;
	}

	if(mode == 1)
	{
		document.getElementById('mduManagerId').innerHTML = ' \
			<div> \
				Mode : Edition<br> \
				<img src="../admin/img/mdumanager.gif" align="absmiddle">&nbsp; \
				<input type="submit" value="REC." style="font-size:11px;font-weight:bold"  onclick="globalReport();"> \
				<div id="mduManagerResultAreaId"></div> \
			</div> \
		';
	}
	if(mode == 2)
	{
		document.getElementById('mduManagerId').innerHTML = ' \
			<div> \
				Mode : Info<br> \
				<img src="../admin/img/mdumanager.gif" align="absmiddle">&nbsp; \
				<input type="submit" value="REC." style="font-size:11px;font-weight:bold"  onclick="globalReport();"> \
				<div id="mduManagerResultAreaId"></div> \
			</div>\
		';
	}
	
	var patternContainer 		= new RegExp("module.Id")
	var patternModule 			= new RegExp("mdu.....containerId")
	var divList 						= document.getElementsByTagName("div"); 
	
	var divResultList 			= Array();
	var countContainer 			= 0;
	
	var trash 							= Array();
	var trashWidth 					= Array();
	var trashHeight 				= Array();
	var countTrash 					= 0;
	
	// GLOBAL
	divIdContainerList 		= Array();
	divIdModuleList				= Array();
	
	//
	// LOOP 1 : Building array of elements Id
	// 
	// divIdContainerList 	: id des containers
	// divIdModuleList			: id des modules
	
	for (var i = 0; i < divList.length; i++)
	{
		divContainerId 							= divList[i].getAttribute("id");
		
		if(patternContainer.test(divContainerId))
		{
			
	  	divIdContainerList[countContainer] = divContainerId;
	  	
	  	countContainer++;
  	}
	}
	
	//alert(divIdContainerList);
	
	divIdContainerList.sort();
	
	
	
	
	for (var i = 0; i < divIdContainerList.length; i++)
	{
		focusContainer = document.getElementById(divIdContainerList[i]);
		//alert(divList[i].getAttribute("id"));
		
		//alert(divList[i].getAttribute("id"))
  	divContainerList 	= focusContainer.getElementsByTagName("div");
  	
  	var divContainerModuleList 		= Array();
		var countModule 			= 0;
		
  	for (var j = 0; j < divContainerList.length; j++)
  	{
  		divModuleId = divContainerList[j].getAttribute("id");
  		
  		if(patternModule.test(divModuleId))
  		{
  			divContainerModuleList[countModule] = divModuleId;
  			countModule++;
  		}
  	}
  	
  	divIdModuleList[i] = new Array(countModule);
  	
		for (var k = 0; k < countModule; k++)
		{
			divIdModuleList[i][k] = divContainerModuleList[k];
		}
		
	}
	
	
	//alert('divIdContainerList : ' + divIdContainerList);
	//alert('divIdModuleList : ' + divIdModuleList[0]);
	//alert('divIdModuleList : ' + divIdModuleList[1]);
	//alert('divIdModuleList : ' + divIdModuleList[2]);
	//alert('divIdModuleList : ' + divIdModuleList[3]);
	
	
	//divIdModuleList.sort();
	//alert('divIdContainerList : ' + divIdContainerList);
	//alert('divIdModuleList[0] : ' + divIdModuleList[0]);

	//return 0;
	
	
	//alert('avant ' + divIdContainerList);
	//divIdContainerList.sort();
	
	//	alert('apres ' + divIdModuleList[0]);
	//	return 0;	
	
	//
	// LOOP 2 : Remover
	// 
	
	for( var x = 0; x < divIdContainerList.length; x++)
	{
		trash[x] = new Array(divIdModuleList[x].length);
		trashWidth[x] = new Array(divIdModuleList[x].length);
		trashHeight[x] = new Array(divIdModuleList[x].length);
		
		for( var y = 0; y < divIdModuleList[x].length; y++)
		{
			//alert('treating ' + divIdContainerList[x] + ' '  + divIdModuleList[x][y]);
			parentNodeToRemove		= document.getElementById(divIdContainerList[x]);
			nodeToRemove 					= document.getElementById(divIdModuleList[x][y]);
			trashWidth[x][y] 			= nodeToRemove.offsetWidth
			trashHeight[x][y] 		= nodeToRemove.offsetHeight
			trash[x][y]				 		= parentNodeToRemove.removeChild(nodeToRemove);
			//alert('done ' + divIdModuleList[x][y]);
		}
	}
	
	//alert('remove done');
	
	//return 0;
	
	//
	// LOOP 3 : Builder
	// 
	
	for( var x = 0; x < divIdContainerList.length; x++)
	{
		ulNode = document.createElement('UL');
		ulNode.setAttribute("class", "sortable boxmodule");
		ulNode.setAttribute("className", "sortable boxmodule");
		ulNode.setAttribute("id","module"+ x + "ulId");
		ulNode.style.border = "1px dotted red";
		ulNode.style.margin = "0";
		ulNode.style.padding = "0";
		
		for( var y = 0; y < divIdModuleList[x].length; y++)
		{
			liNode = document.createElement('LI');
			liNode.setAttribute("target", "module");
			liNode.setAttribute("mduuseid", parseInt(divIdModuleList[x][y].substr(3,5),10));
			liNode.setAttribute("id",y);
			liNode.style.margin = "0";
			liNode.style.padding = "0";
			
			liNode.appendChild(trash[x][y]);
			
			cacheNode = document.createElement('DIV');
			cacheNode.style.position = "absolute";
			cacheNode.style.border = "0px solid green";
			cacheNode.style.top = "0px";
			cacheNode.style.left = "0px";
			cacheNode.style.width = (trashWidth[x][y] - 2) + "px";
			cacheNode.style.height = (trashHeight[x][y] - 6) + "px";

			if(mode == 1)
			{
				cacheNode.style.backgroundImage = 'url(../admin/img/cacheedition.png)';
				cacheNode.style.color = "white";
				cacheNode.innerHTML = "&nbsp;" + trashWidth[x][y] + "px" + trashHeight[x][y] + "px";
			}

			if(mode == 2)
			{
				cacheNode.style.backgroundImage = 'url(../admin/img/cacheinfo.png)';
				cacheNode.style.color = "white";
				cacheNode.innerHTML = "&nbsp;" + trashWidth[x][y] + "px" + trashHeight[x][y] + "px";
			}
			
			liNode.appendChild(cacheNode);
			
			ulNode.appendChild(liNode);
		}
		
		el = document.getElementById(divIdContainerList[x]);
		el.appendChild(ulNode);
	}
	
	var listModule = Array();
	
	for( var x = 0; x < divIdContainerList.length; x++)
	{
		listModule[x] = document.getElementById("module"+ x + "ulId");
		DragDrop.makeListContainer( listModule[x] );
		listModule[x].onDragOver = function() { memBorder = this.style["border"]; this.style["border"] = "1px dotted red"; };
		listModule[x].onDragOut = function() { this.style["border"] = memBorder; };
	}
}

function globalReport()
{
	var reg = new RegExp("\\?");
	var sep = "?";
	urlResponse = "";
		
	for( var x = 0; x < divIdContainerList.length; x++)
	{
		urlResponse = urlResponse + 'data[module][' + x + ']=' + makeReport('module' + x + 'ulId') + '&';
	}
	
	if(confirm('Diffuser ce positionnement sur toutes les pages\nutilisant ce modèle de mise en page ?\n\nOK : Toutes les pages\nAnnuler : Niveau actuel uniquement'))
	{
		loadXMLDoc('mduManagerResultAreaId','../include/fnc_reloadModule.php?' + urlResponse);
	}
	else
	{
		loadXMLDoc('mduManagerResultAreaId','../include/fnc_reloadModule.php?lvlonly=1&' + urlResponse);
	}
	
	return;
}

function makeReport(listId)
{
	result = "";
	
	if(document.getElementById(listId))
	{
		list = document.getElementById(listId);
		var items = list.getElementsByTagName( "li" );
		var result = "";
		for (var i = 0; i < items.length; i++)
		{
			if(items[i].mduuseid)
				result = result + items[i].id + "," + items[i].mduuseid + "|";
			else
				result = result + items[i].id + ",0|";
		}
	}
	return result;
}

function makeTrashed()
{
	list = document.getElementById('availableId');
	var items = list.getElementsByTagName( "li" );
	var result = "";
	var itemused = 0;
	for (var i = 0; i < items.length; i++)
	{
		if(items[i].mduuseid)
		{
			result = result + "data[trash][" + itemused + "]=" + items[i].mduuseid + "&";
			itemused++;
		}
	}
	
	return result;
}
