var APDrag = {
    "scrollPosX": null,
    "scrollPosY": null,
    "obj": null,
    "init": function(a, aRoot, ee) {
        if (ee == null) {
            a.onmousedown = APDrag.start;
        }
        a.root = aRoot;
        if (isNaN(parseInt(a.root.style.left))) a.root.style.left = "0px";
        if (isNaN(parseInt(a.root.style.top))) a.root.style.top = "0px";
        a.root.onDragStart = new Function();
        a.root.onDragEnd = new Function();
        a.root.onDrag = new Function();
        if (ee != null) {
            var b = APDrag.obj = a;
            ee = APDrag.fixE(ee);
            var c = parseInt(b.root.style.top);
            var d = parseInt(b.root.style.left);
            b.root.onDragStart(d, c, ee.clientX, ee.clientY);
            b.lastMouseX = ee.clientX;
            b.lastMouseY = ee.clientY;
            document.onmousemove = APDrag.drag;
            document.onmouseup = APDrag.end;
            document.onselectstart = function(){"return false"};
        }
    },
    "detach": function(a) {
        a.onmousedown = null;
        a.root = null;
    },
    "start": function(a) {
        var b = APDrag.obj = this;
        a = APDrag.fixE(a);
    		if (Element.hasClassName(a.srcElement, 'webpartHeader') && a.srcElement != this) return false;
        var c = parseInt(b.root.style.top);
        var d = parseInt(b.root.style.left);
        b.root.onDragStart(d, c, a.clientX, a.clientY);
        b.lastMouseX = a.clientX;
        b.lastMouseY = a.clientY;
        document.onmousemove = APDrag.drag;
        document.onmouseup = APDrag.end;
        document.onselectstart = function(){"return false"};
        APDrag.scrollPosX = App.scrollPosX();
        APDrag.scrollPosY = App.scrollPosY();
        return false;
    },
    "drag": function(a) {
        a = APDrag.fixE(a);
        var b = APDrag.obj;
        var c = a.clientY;
        var d = a.clientX;
        var e = parseInt(b.root.style.top);
        var f = parseInt(b.root.style.left);
        var h,
        g;
        h = f + d - b.lastMouseX;
        g = e + c - b.lastMouseY;
        var px = App.scrollPosX();
        var py = App.scrollPosY();
        h -= APDrag.scrollPosX - px;
        g -= APDrag.scrollPosY - py;
        APDrag.scrollPosX = px;
        APDrag.scrollPosY = py;
        b.root.style.left = h + "px";
        b.root.style.top = g + "px";
        b.lastMouseX = d;
        b.lastMouseY = c;
        b.root.onDrag(h, g, a.clientX, a.clientY);
        return false;
    },
    "end": function() {
        document.onmousemove = null;
        document.onmouseup = null;
        document.onselectstart = null;
        APDrag.obj.root.onDragEnd(parseInt(APDrag.obj.root.style.left), parseInt(APDrag.obj.root.style.top));
        APDrag.obj = null;
    },
    "fixE": function(a) {
        if (typeof a == "undefined") a = window.event;
        if (typeof a.layerX == "undefined") a.layerX = a.offsetX;
        if (typeof a.layerY == "undefined") a.layerY = a.offsetY;
        return a;
    }
};


App.DragMe = function(divHandle, div, onDragStartHelper, onDragHelper, onDragEndHelper, e) {
		divHandle.style.cursor = "move";
    if (typeof onDragStartHelper == "undefined") onDragStartHelper = function() {};
    if (typeof onDragHelper == "undefined") onDragHelper = function() {};
    if (typeof onDragEndHelper == "undefined") onDragEndHelper = function() {};
    if (typeof e == "undefined") e = null;
    APDrag.init(divHandle, div, e);
    moduleGhost.style.display = "none";

		div.dragElement = function() {
			if (this.dragClone) return this.dragClone;
			else return this;
		}
		
    div.onDragStart = function(x, y, mousex, mousey) {
        if (onDragStartHelper(x, y, mousex, mousey) === false) return;
        var putGhost;
        this.isDragging = true;
    		this.limitedZone = null;
    		if (Element.hasClassName(this,'document') || Element.hasClassName(this,'tab'))
					this.limitedZone = Element.getParentByIdentifier(this,'className', 'webpart');

        App.draggingItem = false;
        //moduleGhost.style.height = this.offsetHeight + "px";
        moduleGhost.style.height = '40px';
        moduleGhost.style.width = '100%';
        if (Element.hasClassName(this,'webpart'))
       	{
	        this.offsetX = this.getLeft() - mousex - App.scrollPosX();
	        this.offsetY = this.getTop() - mousey - App.scrollPosY();
	        this.initZone = this.parentNode;
       	}
       	else
	        if (Element.hasClassName(this,'tab'))
	        {
		        this.offsetX = this.getLeft() - mousex - App.scrollPosX();
		        this.offsetY = this.getTop() - mousey - App.scrollPosY();	        	
	        }
	        else
		      	if (Element.hasClassName(this,'document'))
		        {
			        this.offsetX = this.getLeft() - mousex - App.scrollPosX();
			        this.offsetY = this.getTop() - mousey - App.scrollPosY();
		        }
		        else
		       	{
			        this.offsetX = this.getLeft() - mousex - ($('divSearchResultPane')?$('divSearchResultPane').getScrollLeft():0) - App.scrollPosX();
			        this.offsetY = this.getTop() - mousey - ($('divSearchResultPane')?$('divSearchResultPane').getScrollTop():0) - App.scrollPosY();
		       	}
    };
    div.onDrag = function(x, y, mousex, mousey) {
        if (onDragHelper(x, y, mousex, mousey) === false) return;
        App.stopScroll();

        if (mousex < 20) App.scrollDirection(document.body, 'scrollbarLeft');
        if (mousey < 20) App.scrollDirection(document.body, 'scrollbarUp');
        if (mousex > (document.body.clientWidth - 20)) App.scrollDirection(document.body, 'scrollbarRight');
        if (mousey  > (document.body.clientHeight - 20)) App.scrollDirection(document.body, 'scrollbarDown');
	    	if (this.isDragging === true && App.draggingItem === false)
	    	{
	    		switch(this.className)
	    		{
		       		case 'tab':		
		       		case 'tab selected':		
															this.dragElement().setStyles(this.dragElement().getCoordinates());
		       										break;
		       		case 'document':		
		       		case 'webpart':
															this.dragElement().setStyles(this.dragElement().getCoordinates());
															App.SetIFrameDisplayNone(this.dragElement());
		       										break;	
		       										
		       		default:
		 													//var oImg = new Element("img",{"src":"Pictures/Catalog/HTMLDocument.jpg"});
															this.dragClone = div.clone().setStyles(this.getCoordinates());
															//this.dragClone = oImg;
															this.dragElement().inject(document.body);
															this.dragElement().setStyles(this.dragElement().getCoordinates());
		       										break;
		      }
					Element.addClassName(this.dragElement(), 'beingDragged');
		    	//this.dragElement().inject(document.body);
	    		App.draggingItem = true;
	    	}

/*
    		var zIndex = this.dragElement().style.zIndex;
    		this.dragElement().style.zIndex = -1;
	    	var DocOver = document.elementFromPoint(mousex,mousey);
    		var oIframe = null;
    		var sVisibility;
    		testdrag.innerText = DocOver.outerHTML;
	    	if (DocOver.tagName.toUpperCase() == 'IFRAME')
	    	{
    			oIframe = DocOver;
    			sVisibility = oIframe.style.visibility;
		    	DocOver = document.elementFromPoint(mousex,mousey);
	    	}
	    	this.dragElement().style.zIndex = zIndex;	
*/
    		this.dragElement().setStyles({'left':mousex + App.scrollPosX() + this.offsetX,'top':mousey + App.scrollPosY() + this.offsetY});

				App.displayModuleGhost(this, this.dragElement(), mousex, mousey, 25, this.limitedZone);
				//if (oIframe) oIframe.style.visibility = sVisibility;
    };
    div.onDragEnd = function(x, y, el) {
    		var itemId = null;
    		var parentId = null;
    		if (!App.draggingItem) return;
        Element.removeClassName(this.dragElement(), 'beingDragged');

				if (Element.hasClassName(this,'webpart'))
       	{
		    	this.setStyle('height','auto');
		    	if (moduleGhost.isToggle)
		    	{
		    		var oWebpart = App.webpartList[this.id];
						App.layoutManager.removeToggleTabGhost();
			    	this.setStyle('width','auto');
	       		itemId = oWebpart.data.urlSuffix;
	       		parentId = 'layouttoggleview';
	       		switch(moduleGhost.position)
	       		{
	       			case -1 : order = App.webpartList[moduleGhost.zone.id].data.order; break;
	       			case 1 : order = App.webpartList[moduleGhost.zone.id].data.order + 1; break;
	       			default : 
			       				if ( App.layoutManager.toggle.elem_tabItem.childNodes.length > 0 )
			       					order = App.webpartList[App.layoutManager.toggle.elem_tabItem.childNodes[App.layoutManager.toggle.elem_tabItem.childNodes.length - 1].name].data.order + 1;
			       				else
			       					order = 1;
	       		}
	       		
		    		App.layoutManager.add({qualifier: this.id, urlSuffix: oWebpart.data.urlSuffix, itemCaption: oWebpart.data.itemCaption, order: order, element: oWebpart.elem_webpart}, 'layouttoggleview');
		    	}
		      else 
		      {
			    	this.setStyle('width','100%');
		        if (moduleGhost.zone) 
		        {
		       		itemId = App.webpartList[this.id].data.urlSuffix;
		       		parentId = Element.getParentByIdentifier(moduleGhost.zone, 'className', 'zoneColumn').id;
		       		switch(moduleGhost.position)
		       		{
		       			case -1 : order = App.webpartList[moduleGhost.zone.id].data.order; break;
		       			case 1 : order = App.webpartList[moduleGhost.zone.id].data.order + 1; break;
		       			default : order = $(parentId).childNodes.length;
		       		}
				    	this.injectBefore(moduleGhost);
		        	//Element.remove(moduleGhost);
		        }
		        else
		        {
				        this.initZone.appendChild(this);
		        }
		      }
		      App.RestoreIFrameDisplay(this.dragElement());
	      }

    		if (moduleGhost.webpart) {moduleGhost.webpart.removeTabGhost(); moduleGhost.webpart = null;}

        document.appendChild(moduleGhost);
      	if (this.dragClone)
      	{
		    	if (moduleGhost.isToggle)
		    	{
						App.layoutManager.removeToggleTabGhost();
	       		itemId = this.id;
	       		parentId = 'layouttoggleview';
	       		switch(moduleGhost.position)
	       		{
	       			case -1 : order = App.webpartList[moduleGhost.zone.id].data.order; break;
	       			case 1 : order = App.webpartList[moduleGhost.zone.id].data.order + 1; break;
	       			default : 
			       				if ( App.layoutManager.toggle.elem_tabItem.childNodes.length > 0 )
			       					order = App.webpartList[App.layoutManager.toggle.elem_tabItem.childNodes[App.layoutManager.toggle.elem_tabItem.childNodes.length - 1].name].data.order + 1;
			       				else
			       					order = 1;
	       		}
		    	}
		      else 
		      {
		        if (moduleGhost.zone) 
		        {
		       		itemId = this.id;
		       		var oWebpart = Element.getParentByIdentifier(moduleGhost.zone, 'className', 'webpart');
		       		if (oWebpart == null)
		       		{
			       		parentId = Element.getParentByIdentifier(moduleGhost.zone, 'className', 'zoneColumn').id;
			       		switch(moduleGhost.position)
			       		{
			       			case -1 : order = App.webpartList[moduleGhost.zone.id].data.order; break;
			       			case 1 : order = App.webpartList[moduleGhost.zone.id].data.order + 1; break;
			       			default : order = $(parentId).childNodes.length;
			       		}
		       		}
		       		else
		       		{
			       		var columnId = Element.getParentByIdentifier(moduleGhost.zone, 'className', 'zoneColumn').id;
			       		var oColumn = $(columnId);
			       		parentId = App.webpartList[oWebpart.id].data.urlSuffix;
		       			if (Element.hasClassName(moduleGhost.zone,'webpart'))
		       			{
				       		switch(moduleGhost.position)
				       		{
				       			case -1 : order = App.webpartList[moduleGhost.zone.id].data.order; parentId = columnId; break;
				       			case 1 : order = App.webpartList[moduleGhost.zone.id].data.order + 1; parentId = columnId; break;
				       			default : 
				       						order = (oColumn.childNodes.length > 0 )?App.webpartList[oColumn.childNodes[oColumn.childNodes.length-1].id].data.order + 1:0;
				       		}
				       	}
		       			else
		       			{
				       		switch(moduleGhost.position)
				       		{
				       			case -1 : order = App.webpartList[oWebpart.id].documentList[moduleGhost.zone.name].data.itemOrder; break;
				       			case 1 : order = App.webpartList[oWebpart.id].documentList[moduleGhost.zone.name].data.itemOrder + 1; break;
				       		}
		       			}
		       		}	       		
		        }
		      }
					App.RestoreIFrameDisplay(this.dragElement());
      		this.dragClone.removeNode(true);
      		this.dragClone = null;
      	}
      	if (this.limitedZone)
      	{
	        if (moduleGhost.zone) 
	        {
	       		itemId = this.name;
	       		var oWebpart = this.limitedZone;
	       		var columnId = Element.getParentByIdentifier(oWebpart, 'className', 'zoneColumn').id;
	       		var oColumn = $(columnId);
	       		var parentWP = App.webpartList[oWebpart.id];
	       		parentId = parentWP.data.urlSuffix;
       			if (Element.hasClassName(moduleGhost.zone,'webpart'))
       			{
       				if (parentWP.tabs)
       				{
       					if (parentWP.tabs.elem_tabItem.childNodes.length > 0)
       					{
   								order = parentWP.documentList[parentWP.tabs.elem_tabItem.childNodes[parentWP.tabs.elem_tabItem.childNodes.length - 1].name].data.itemOrder + 1;
   							}
   							else order = 1;
   						}
   						else
   						{
       					if (parentWP.elem_webpartContent.childNodes.length > 0)
   								order = parentWP.documentList[parentWP.elem_webpartContent.childNodes[parentWP.elem_webpartContent.childNodes.length - 1].name].data.itemOrder + 1;
   							else order = 1;
   								
   						}
		       	}
       			else
       			{
		       		switch(moduleGhost.position)
		       		{
		       			case -1 : order = parentWP.documentList[moduleGhost.zone.name].data.itemOrder; break;
		       			case 1 : order = parentWP.documentList[moduleGhost.zone.name].data.itemOrder + 1; break;
		       		}
       			}
	        }
      	}

				if (itemId) onDragEndHelper(itemId,parentId,order);

				App.RestoreIFrameDisplay(this.dragElement());
        moduleGhost.style.display = "none";
        this.isDragging = false;
        App.draggingItem = false;
    };
};


App.SetIFrameDisplayNone = function(oElem) {

	var i;
	var oIFrames = document.getElementsByTagName('IFRAME');
	var oTabDisplay = new Array();
	if (oIFrames.length > 0)
	{
		for ( i=0 ;  i < oIFrames.length ; i++)
		{
			oTabDisplay[i] = new Object();
			oTabDisplay[i].object = oIFrames[i];
			oTabDisplay[i].display = oIFrames[i].style.visibility;
			//oIFrames[i].style.visibility = 'hidden';
		}
	}
	oElem.DisplayList = oTabDisplay;

	var GlobalDiv = new Element("div",{"class":"DragMaskIFrame"});
	GlobalDiv.style.pixelWidth = document.body.scrollWidth;
	GlobalDiv.style.pixelHeight = document.body.scrollHeight;
	document.body.appendChild(GlobalDiv);
	oElem.DivMask = GlobalDiv;
}

App.RestoreIFrameDisplay = function(oElem) {

	var i;
	if (oElem.DisplayList)
	{
		for ( i=0 ;  i < oElem.DisplayList.length ; i++)
		{
			oElem.DisplayList[i].object.style.visibility = 'hidden';
			oElem.DisplayList[i].object.style.visibility = oElem.DisplayList[i].display;
		}
		oElem.DisplayList = null;
	}

	if (oElem.DivMask)
	{
		oElem.DivMask.removeNode(true);
		oElem.DivMask = null;
	}
}

App.RemoveDragMe = function(divHandle) {
		divHandle.style.cursor = "auto";
    APDrag.detach(divHandle);
    moduleGhost.style.display = "none";
};


var moduleGhost = new Element("div",{"class":"Ghost"});
var moduleGhostContent = new Element("div",{"class":"GhostContent"});
moduleGhostContent.inject(moduleGhost);
moduleGhost.id = "moduleGhost";

//var testdrag = new Element("div",{"style":"background-color:white;position:absolute;width:500;height:200;top:600;left:400;overflow:hidden"});	
//testdrag.id = 'testDrag';
//testdrag.style.display='';

App.displayModuleGhost = function(dragElem, itemDrag, mousex, mousey, wparea, zoneRestriction) {
				var i;
				if (typeof wparea == 'undefined') wparea = 40;
				if (typeof zoneRestriction == 'undefined') zoneRestriction == null;
    		var zIndex = itemDrag.style.zIndex;
    		var zIndex2
    		if (itemDrag.DivMask) zIndex2 = itemDrag.DivMask.style.zIndex;
    		itemDrag.style.zIndex = -1;
    		if (itemDrag.DivMask) itemDrag.DivMask.style.zIndex = -1;
	    	var DocOver = document.elementFromPoint(mousex,mousey);
	    	
	    	itemDrag.style.zIndex = zIndex;	
    		if (itemDrag.DivMask) itemDrag.DivMask.style.zIndex = zIndex2;

				var testDoc = Element.getParentByIdentifier(DocOver,'className', itemDrag.className);
				if (testDoc == itemDrag) 
					DocOver = null;
							
				var dropGhost = Element.getParentByIdentifier(DocOver,'className', 'Ghost');
				if (dropGhost) 
					return;
	    	
	    	/*****************************************************
	    	******************************************************
	    	DEBUT RECHERCHE DES ZONE DE DROP
	    	******************************************************
	    	*****************************************************/
	    	
	    	var dropZone = null;
	    	
	    	var dropToggle = null;
	    	
				var dropWebpart = null;
				var dropDocument = null;

				dropToggle = Element.getParentByIdentifier(DocOver,'id', 'layouttoggletab');
				if (dropToggle != null)
				{
					if (moduleGhost.isToggle)  return;
					App.InjectGhost(dropToggle, 0, false, null, true);
					return;
				}

				if (moduleGhost.isToggle) 
						App.layoutManager.removeToggleTabGhost();
				
	    	if (zoneRestriction != null)   	
	    	{
			    		if (moduleGhost.webpart) {moduleGhost.webpart.removeTabGhost(); moduleGhost.webpart = null;}
	    				dropWebpart = zoneRestriction;
	    	}
	    	else
	    	{
	  			  	dropZone = Element.getParentByIdentifier(DocOver,'className', 'zoneColumn');
	  			   	if (dropZone == null)
				    	{
				    		var zonesRow = App.layoutManager.getRows();
				    		var mouseRow = null;
				    		for ( i = zonesRow.length - 1 ; i > -1 && mouseRow == null ; i-- )
				    		{
				    			if (App.checkMousePosition(mousey, zonesRow[i], 'vertical'))
				    			{
				    				mouseRow = zonesRow[i];
				    				var j;
				    				var zonesColumn = App.layoutManager.getColumnsByZone(App.layoutManager.getZoneId(mouseRow.id));
						    		for ( j = zonesColumn.length - 1 ; j > -1 && dropZone == null ; j-- )
						    		{
						    			if (App.checkMousePosition(mousex, zonesColumn[j], 'horizontal'))
						    			{
						    				dropZone = zonesColumn[j];
						    			}
						    		}
				    			}
				    		}

					    	if (dropZone == null) {moduleGhost.zone = null;moduleGhost.style.display = "none";return;}
				    		
				    	}
				    	else if (dropZone.id == 'layoutmiddle.1.2') dropZone = App.getParentByIdentifier(DocOver,'className', 'zoneColumn');
				    	
				    	if (dropZone)
				    	{
	
				    		if (moduleGhost.webpart) {moduleGhost.webpart.removeTabGhost(); moduleGhost.webpart = null;}
								    	
					    	dropWebpart = Element.getParentByIdentifier(DocOver,'className', 'webpart');
					    	if (dropWebpart == null)
					    	{
					  			for (i = 0 ; i < dropZone.childNodes.length && dropWebpart == null ; i++)
					  			{
					  				var wpHeight = dropZone.childNodes(i).getCoordinates().height;
					  				if (dragElem.id != dropZone.childNodes(i).id && dropZone.childNodes(i).getTop() - App.scrollPosY() + (wpHeight/2) > mousey && !(dropZone.childNodes(i).id == 'moduleGhost'))
					  				{
					  						dropWebpart = dropZone.childNodes(i);
					  				}
					  				if (dropZone.childNodes(i).getTop() - App.scrollPosY() + (wpHeight/2) > mousey && dropZone.childNodes(i).id == 'moduleGhost') return;
					  			}
					  		}
					  	}
				}

	    	/*****************************************************
	    	******************************************************
	    	FIN RECHERCHE DES ZONE DE DROP
	    	******************************************************
	    	*****************************************************/

				
	    	if (dropWebpart == null)
	    	{
		    	/*****************************************************
		    	******************************************************
		    	DROP DANS UNE COLONNE
		    	******************************************************
		    	*****************************************************/
		    	if (moduleGhost.zone == dropZone && moduleGhost.position == 0) return;
					App.InjectGhost(dropZone, 0, false);
	    	}
	    	else
    		{
		    	/*****************************************************
		    	******************************************************
		    	DROP PAR RAPPORT A UN WEBPART
		    	******************************************************
		    	*****************************************************/
					var oDropWebpart = App.webpartList[dropWebpart.id];
					var dropTab = null;
		    	/*****************************************************
		    	******************************************************
		    	RECUPREATION DU DUCMENT DE DROP EVENTUEL
		    	******************************************************
		    	*****************************************************/
		    	if (!Element.hasClassName(dragElem,'webpart') && dragElem.ContentClass != 'www.ActivPortal.mc:WebPart')
		    	{
			    	dropDocument = Element.getParentByIdentifier(DocOver,'className', 'document');
	
			  		if (oDropWebpart.hasTab())
			  		{
				    	if (dropDocument == null)
				    	{
				    		dropTab = Element.getParentByIdentifier(DocOver,'className', 'tab');
				    		if (dropTab != null) dropDocument = oDropWebpart.documentList[dropTab.name].elem_content;
				    	}
				    	else dropTab = oDropWebpart.tabs.getTab(dropDocument.name);
			  		}
			  	}
		    	
		    	if (dropDocument && dropDocument.name.indexOf(oDropWebpart.getIdentifier() + '_') == -1) 
		    			dropDocument = null;
		    	
    			var wpHeight = dropWebpart.getCoordinates().height;
    			if (dropDocument == null)
    			{
			    	/*****************************************************
			    	******************************************************
			    	SORT D'UN DOCUMENT
			    	******************************************************
			    	*****************************************************/
			    	if (zoneRestriction != null)
			    	{
			    		if (moduleGhost.zone == dropWebpart && moduleGhost.position == 0) return;
			    		App.InjectGhost(dropWebpart, 0, true, oDropWebpart);
			    	}
			    	/*****************************************************
			    	******************************************************
			    	DROP D'UN ELEMENT
			    	******************************************************
			    	*****************************************************/
			    	else
			    	{
							    	/*****************************************************
							    	******************************************************
							    	DROP D'UN WEBPART
							    	******************************************************
							    	*****************************************************/
				    				if (Element.hasClassName(dragElem,'webpart') || dragElem.ContentClass == 'www.ActivPortal.mc:WebPart')
				    				{
				    					if (dropWebpart.getTop() - App.scrollPosY() + (wpHeight/2) > mousey)
					    				{
					    					if (moduleGhost.zone == dropWebpart && moduleGhost.position == -1) return;
												App.InjectGhost(dropWebpart, -1, false);
					    				}
					    				else
					    				{
					    					if (moduleGhost.zone == dropWebpart && moduleGhost.position == 1) return;
												App.InjectGhost(dropWebpart, 1, false);
					    				}
				    				}
				    				else 
									    	/*****************************************************
									    	******************************************************
									    	DROP D'UN DOCUMENT PAR RAPPORT A UNE WEBPART
									    	******************************************************
									    	*****************************************************/
									    	
									    	var dropWebpartContent = Element.getParentByIdentifier(DocOver,'className', 'webpartContent');
									    	
									    	//testdrag.setText('mouse : ' + mousey + ' wpTop : ' + dropWebpart.getTop() + ' wpHeight : ' + wpHeight + ' wparea : ' + wparea);
									    	if (dropWebpartContent)
									    	{
									    		if (moduleGhost.zone == dropWebpart && moduleGhost.position == 0) return;
													App.InjectGhost(dropWebpart, 0, true, oDropWebpart);
									    	}
									    	else
					    						if (mousey - (dropWebpart.getTop() - App.scrollPosY()) < (wpHeight / 2))
							    				{
							    					if (moduleGhost.zone == dropWebpart && moduleGhost.position == -1) return;
														App.InjectGhost(dropWebpart, -1, false);
							    				}
							    				else  
							    				{
							    					if (moduleGhost.zone == dropWebpart && moduleGhost.position == 1) return;
														App.InjectGhost(dropWebpart, 1, false);
							    				}
						}
    			}
    			else
    			{
			    	/*****************************************************
			    	******************************************************
			    	DROP SITUE SUR UN DOCUMENT
			    	******************************************************
			    	*****************************************************/
			  		if (oDropWebpart.hasTab())
			  		{
			  			if (moduleGhost.zone == dropDocument && moduleGhost.position == -1) return;
			  			App.InjectGhost(dropDocument, -1, true, oDropWebpart);
			  		}
			  		else
			  		{
							var docHeight = dropDocument.getCoordinates().height;
							//if (dropDocument.previousSibling == null || (dropDocument.previousSibling.id == 'moduleGhost' && dropDocument.previousSibling.previousSibling == null))
							{
								if (dropDocument.getTop() - App.scrollPosY() + (docHeight/2) > mousey)
								{
									if (moduleGhost.zone == dropDocument && moduleGhost.position == -1) return;
									App.InjectGhost(dropDocument, -1, true);
								}
								else
								{
									if (moduleGhost.zone == dropDocument && moduleGhost.position == 1) return;
									App.InjectGhost(dropDocument, 1, true);
								}
							}
							//else
							//{
							//	if (moduleGhost.zone == dropDocument && moduleGhost.position == 1) return;
							//	App.InjectGhost(dropDocument, 1, true);
							//}
						}
    			}
    		}
    		//testDrag.setText('WP : ' + (dropWebpart?dropWebpart.id:'----') + '    Doc : ' + (dropDocument?dropDocument.name:'----'))
}

App.InjectGhost = function(dropElem, position, isDoc, webpart, isToggle) {
	if (typeof isToggle == 'undefined') isToggle = false;
	var dropContainer = dropElem;
	
	if (Element.hasClassName(moduleGhost,'GhostContentWP')) Element.removeClassName(moduleGhost,'GhostContentWP');
	
	if (isDoc && Element.hasClassName(dropElem,'webpart') && position == 0) dropContainer = App.webpartList[dropElem.id].elem_webpartContent;
	
	moduleGhost.zone = dropElem;
	moduleGhost.position = position;
	moduleGhost.isToggle = isToggle;
	
	if (isToggle)
	{
		moduleGhost.zone = dropElem;
		switch(position)
		{
			case 0 : tabPosition = -1; break;
			case -1 : tabPosition = -1; break;
			case 1 : tabPosition = -1; break;
		}
		moduleGhost.style.display = "none";
		App.layoutManager.addToggleTabGhost(tabPosition);
		return ;
	}
	
	if (webpart && webpart.hasTab()) 
	{
		var tabPosition;
		switch(position)
		{
			case 0 : tabPosition = webpart.tabs.elem_tabItem.length; break;
			case -1 : tabPosition = webpart.getTabPositionByIdentifier(dropElem.name); break;
			case 1 : tabPosition = webpart.getTabPositionByIdentifier(dropElem.name) + 1; break;
		}
		moduleGhost.style.display = "none";
		moduleGhost.webpart = webpart;
		webpart.addTabGhost(tabPosition);
		return ;
	}

	if (isDoc && !Element.hasClassName(moduleGhost,'GhostContentWP')) Element.addClassName(moduleGhost,'GhostContentWP');
	moduleGhost.style.display = "";
	switch(position)
	{
		case -1: moduleGhost.injectBefore(dropContainer); break;
		case 1: moduleGhost.injectAfter(dropContainer); break;
		case 0: moduleGhost.inject(dropContainer); break;
	}
}

App.scrollPosY = function() {
    //return window.getScrollTop();
    return Browser.getScrollTop();
};

App.scrollPosX = function() {
    //return window.getScrollLeft();
    return Browser.getScrollLeft();
};

App.getParentByIdentifier = function(elem, attr, attrValue) {
    	var parentElem = elem;
    	while(parentElem != null && ((attr == 'className' && !Element.hasClassName(parentElem,attrValue)) || (attr != 'className' && (eval('parentElem.' + attr)).toLowerCase() != attrValue.toLowerCase()))) {
    			parentElem = parentElem.parentNode;
    	}
    	return parentElem;
    }


App.checkMousePosition = function (mousePos, zone, direction) {
		var zonePos;

		if (typeof direction == 'undefined') direction = 'horizontal';
		if (direction == 'vertical') zonePos = zone.getTop() - App.scrollPosY(); // + zone.getCoordinates().height;
		else zonePos = zone.getLeft() - App.scrollPosX(); // + zone.getCoordinates().width;
		
		if (mousePos >  zonePos) return true;
		return false;
}


var scrollTimeOut = null;

App.stopScroll = function()
{
	if ( scrollTimeOut != null ) window.clearTimeout(scrollTimeOut);
	scrollTimeOut = null;
}

App.scrollDirection = function(oElem, direction) 
{
	if ( scrollTimeOut != null ) window.clearTimeout(scrollTimeOut);
	scrollTimeOut = null;
	switch(direction)
	{
		case 'scrollbarDown' : window.scrollTo(Browser.getScrollLeft(),Browser.getScrollTop() + 5);break;
		case 'scrollbarUp' : window.scrollTo(Browser.getScrollLeft(),Browser.getScrollTop() - 5);break;
		default : oElem.doScroll(direction);
	}
	scrollTimeOut = window.setTimeout(function(){App.scrollDirection(oElem, direction)},15);
}


