var Drag = {
	obj : null,
	positions : null,
	positionsIndex :  [[213], [213], [213], [213], [213], [213], [213], [213],
	                   [300], [300], [300], [300], [300], [300], [300], [300],
	                   [387], [387], [387],               [474], [474], [474],
	                   [387], [387], [387],               [474], [474], [474],
	                   [561], [561], [561], [561], [561], [561], [561], [561],
	                   [648], [648], [648], [648], [648], [648], [648], [648]],
	                   
	positionCat :     [[213], [213], [213], [213], [213], [213], [213], [213],
	                   [],           [300], [300], [300], [300],        [],
	                                 [387], [387], [387], [387],
	                   [474], [474], [474], [],    [],    [561], [561], [561],
	                   [474], [474], [474],               [561], [561], [561],
	                   [648], [648], [648], [648], [648], [648], [648], [648]],
	                   
	positionRub :     [[213], [213], [213], [213], [213], [213], [213], [213],
	                   [300], [300], [300], [300], [300], [300], [300], [300],
	                   [387], [387], [387], [387], [387], [387], [387], [387],
	                   [474], [474], [474], [474], [474], [474], [474], [474],
	                   [561], [561], [561], [561], [561], [561], [561], [561],
	                   [648], [648], [648], [648], [648], [648], [648], [648]],
	cellWidth : 117,
	cellHeight : 87,
	dragedOver : null,
	dragged : null,
	
	init : function(pageType) // index = 1, grosse cellule = 2, full petite cellule = 3
	{
		var o;
		if(pageType == 1)
		{
			Drag.positions = Drag.positionsIndex;
			for(i = 0; i < 44; i++)
			{
				o = document.getElementById('cell' + i);
				if(o == null)
					continue;
				
				container = o.getElementsByTagName("div");
				if(container.length == 1)
				{
					Drag.cellWidth = container.item(0).offsetWidth;
					Drag.cellHeight = container.item(0).offsetHeight;
				}
			}
			
			for(i = 0; i < 44; i++)
			{
				o = document.getElementById('cell' + i);
				if(o == null)
					continue;
				
				container = o.getElementsByTagName("div");
				if(container.length == 1)
				{
					if(container.item(0).getAttribute('type') == 'full' || container.item(0).getAttribute('type') == 'fulloff')
						o.onmousedown = Drag.start;
					
					if(i < 8)
						Drag.positions[i][1] = (o.offsetLeft + (Drag.cellWidth * i));
					else if(i < 16)
						Drag.positions[i][1] = (o.offsetLeft + (Drag.cellWidth * (i - 8)));
					else if(i < 19)
						Drag.positions[i][1] = (o.offsetLeft + (Drag.cellWidth * (i - 16)));
					else if(i < 22)
						Drag.positions[i][1] = (o.offsetLeft + (Drag.cellWidth * (i - 19)));
					else if(i < 25)
						Drag.positions[i][1] = (o.offsetLeft + (Drag.cellWidth * (i - 22)));
					else if(i < 28)
						Drag.positions[i][1] = (o.offsetLeft + (Drag.cellWidth * (i - 25)));
					else if(i < 36)
						Drag.positions[i][1] = (o.offsetLeft + (Drag.cellWidth * (i - 28)));
					else
						Drag.positions[i][1] = (o.offsetLeft + (Drag.cellWidth * (i - 36)));
				}
			}
		}
		else if(pageType == 2)
		{
			Drag.positions = Drag.positionCat;
			for(i = 0; i < 40; i++)
			{
				o = document.getElementById('cell' + i);
				if(o == null)
					continue;
				
				container = o.getElementsByTagName("div");
				if(container.length == 1)
				{
					Drag.cellWidth = container.item(0).offsetWidth;
					Drag.cellHeight = container.item(0).offsetHeight;
				}
			}
			
			for(i = 0; i < 40; i++)
			{
				o = document.getElementById('cell' + i);
				if(o == null)
					continue;
				
				container = o.getElementsByTagName("div");
				if(container.length == 1)
				{
					if(container.item(0).getAttribute('type') == 'full' || container.item(0).getAttribute('type') == 'fulloff')
						o.onmousedown = Drag.start;
					
					if(i < 8)
						Drag.positions[i][1] = (o.offsetLeft + (Drag.cellWidth * i));
					else if(i < 13)
						Drag.positions[i][1] = (o.offsetLeft + (Drag.cellWidth * (i - 9)));
					else if(i < 18)
						Drag.positions[i][1] = (o.offsetLeft + (Drag.cellWidth * (i - 14)));
					else if(i < 21)
						Drag.positions[i][1] = (o.offsetLeft + (Drag.cellWidth * (i - 18)));
					else if(i < 26)
						Drag.positions[i][1] = (o.offsetLeft + (Drag.cellWidth * (i - 23)));
					else if(i < 29)
						Drag.positions[i][1] = (o.offsetLeft + (Drag.cellWidth * (i - 26)));
					else if(i < 32)
						Drag.positions[i][1] = (o.offsetLeft + (Drag.cellWidth * (i - 29)));
					else
						Drag.positions[i][1] = (o.offsetLeft + (Drag.cellWidth * (i - 32)));
				}
			}
		}
		else if(pageType == 3)
		{
			Drag.positions = Drag.positionRub;
			for(i = 0; i < 48; i++)
			{
				o = document.getElementById('cell' + i);
				if(o == null)
					continue;
				
				container = o.getElementsByTagName("div");
				if(container.length == 1)
				{
					Drag.cellWidth = container.item(0).offsetWidth;
					Drag.cellHeight = container.item(0).offsetHeight;
				}
			}
			
			for(i = 0; i < 48; i++)
			{
				o = document.getElementById('cell' + i);
				if(o == null)
					continue;
				
				container = o.getElementsByTagName("div");
				if(container.length == 1)
				{
					if(container.item(0).getAttribute('type') == 'full' || container.item(0).getAttribute('type') == 'fulloff')
						o.onmousedown = Drag.start;
					
					if(i < 8)
						Drag.positions[i][1] = (o.offsetLeft + (Drag.cellWidth * i));
					else if(i < 16)
						Drag.positions[i][1] = (o.offsetLeft + (Drag.cellWidth * (i - 8)));
					else if(i < 24)
						Drag.positions[i][1] = (o.offsetLeft + (Drag.cellWidth * (i - 16)));
					else if(i < 32)
						Drag.positions[i][1] = (o.offsetLeft + (Drag.cellWidth * (i - 24)));
					else if(i < 40)
						Drag.positions[i][1] = (o.offsetLeft + (Drag.cellWidth * (i - 32)));
					else
						Drag.positions[i][1] = (o.offsetLeft + (Drag.cellWidth * (i - 40)));
				}
			}
		}
	},

	start : function(e)
	{
		var o = Drag.obj = document.getElementById('drag');
		var number = parseInt(this.id.substring(4));
		Drag.dragged = number;
		o.style.left = Drag.positions[number][1] + "px";
		o.style.top = Drag.positions[number][0] + "px";
		o.style["visibility"] = "hidden";
		o.style["display"] = "none";
		o.innerHTML = this.innerHTML;
		
		e = Drag.fixE(e);
		o.origineX = o.lastMouseX	= e.clientX;
		o.origineY = o.lastMouseY	= e.clientY;
		
		document.onselectstart = new Function ("return false");
		
		document.onmousemove	= Drag.dragNotVisible;
		document.onmouseup		= Drag.end;
		
		return false;
	},

	dragNotVisible : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var nx, ny;

		nx = Math.abs(o.origineX - o.lastMouseX);
		ny = Math.abs(o.origineY - o.lastMouseY);

		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		if(nx > 5 || ny > 5)
		{
			o.style["visibility"] = "visible";
			o.style["display"] = "block";
			o.style["filter"] = "alpha(opacity=50)";
			o.style["-moz-opacity"] = "0.5";
			o.style["KhtmlOpacity"] = ".5";
			o.style["opacity"] = ".5";
			closepopup();
			document.onmousemove	= Drag.drag;
		}
		
		return false;
	},
	
	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;

		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.style.top);
		var x = parseInt(o.style.left);
		var nx, ny;

		nx = x + ex - o.lastMouseX;
		ny = y + ey - o.lastMouseY;

		Drag.obj.style["left"] = nx + "px";
		Drag.obj.style["top"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;
		
		ex += document.documentElement.scrollLeft;
		ey += document.documentElement.scrollTop;
		
		if(Drag.dragedOver != null)
		{
			if(ex < Drag.positions[Drag.dragedOver][1] || ex > (Drag.positions[Drag.dragedOver][1] + Drag.cellWidth) ||
			   ey < Drag.positions[Drag.dragedOver][0] || ey > (Drag.positions[Drag.dragedOver][0] + Drag.cellHeight))
			{
				cell = document.getElementById('cell' + Drag.dragedOver).getElementsByTagName("div");
				if(cell.length > 0)
				{
					cell = cell.item(0);
					if(cell.getAttribute('type') == "full" || cell.getAttribute('type') == "fulloff")
						cell.className = 'favorisy_cellule_1';
					else if(cell.getAttribute('type') == 'empty' || cell.getAttribute('type') == 'emptyoff')
						cell.className = "favorisy_cellule_1_1";
				}
				Drag.dragedOver = null;
			}
			else return false;
		}
			
		for(i = 0; i < Drag.positions.length; i++)
		{
			if(Drag.positions[i].length == 0)
				continue;
			
			if(ex >= (Drag.positions[i][1] + 5) && ex <= (Drag.positions[i][1] + Drag.cellWidth - 5) &&
			   ey >= (Drag.positions[i][0] + 5) && ey <= (Drag.positions[i][0] + Drag.cellHeight - 5))
			{
				Drag.dragedOver = i;
				cell = document.getElementById('cell' + i).getElementsByTagName("div");
				if(cell.length > 0)
				{
					cell = cell.item(0);
					if(cell.getAttribute('type') == "full" || cell.getAttribute('type') == "fulloff")
						cell.className = 'favorisy_cellule_1_on';
					else if(cell.getAttribute('type') == 'empty' || cell.getAttribute('type') == 'emptyoff')
						cell.className = "favorisy_cellule_1_1_on";
				}
			}
		}

		return false;
	},

	end : function()
	{
		document.onmousemove=positiontip; // thumbs
		document.onmouseup   = null;
		
		if(Drag.dragedOver != null)
		{
			cell = document.getElementById('cell' + Drag.dragedOver).getElementsByTagName("div");
			if(cell.length > 0)
			{
				cell = cell.item(0);
				if(cell.getAttribute('type') == "full" || cell.getAttribute('type') == "fulloff")
					cell.className = 'favorisy_cellule_1';
				else if(cell.getAttribute('type') == 'empty')
				{
					cell.className = "favorisy_cellule_1_1";
					cell.setAttribute('onClick', 'afficherMenuPreview();loadAjout("' + Drag.dragged + '")');
					document.getElementById('cell' + Drag.dragged).onmousedown = null;
					document.getElementById('cell' + Drag.dragedOver).onmousedown = Drag.start;
				}
				else if(cell.getAttribute('type') == 'emptyoff')
				{
					cell.className = "favorisy_cellule_1_1";
					document.getElementById('cell' + Drag.dragged).onmousedown = null;
					document.getElementById('cell' + Drag.dragedOver).onmousedown = Drag.start;
				}
				
				tmp = document.getElementById('cell' + Drag.dragedOver).innerHTML;
				document.getElementById('cell' + Drag.dragedOver).innerHTML = document.getElementById('cell' + Drag.dragged).innerHTML;
				document.getElementById('cell' + Drag.dragged).innerHTML = tmp;
				if(Drag.dragged != Drag.dragedOver && (cell.getAttribute('type') == "full") || (cell.getAttribute('type') == "empty")) dragAndDropCell(Drag.dragged, Drag.dragedOver);
			}
		}
		
		Drag.dragged = Drag.dragedOver = null;
		document.getElementById('drag').innerHTML = '';
		document.getElementById('drag').style["top"] = '0px';
		document.getElementById('drag').style["left"] = '0px';
		document.getElementById('drag').style["visibility"] = 'hidden';
		document.getElementById('drag').style["display"] = 'none';
		Drag.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};