/**
 * Drag and Drop Window
 * 
 * @author     banhthidiem <banhthidiem@gmail.com>
 * @copyright  2007 Bach Khoa Computer Inc.
 * @version    $Id: DragAndDropWindow.js, v2.0 2007/10/18
 */

/*
 * Constructor
 */
function DragAndDropWindow$BTD(a, l_5, b, a1, b1)
{
	this.a = a + "_";
	this.l_5 = l_5;
	this.l_6 = new Array();
	this.t_1 = null;
	this.c_1 = null;
	this.d_2 = 0;
	this.b = b;
	this.a1 = a1;
	this.c_3 = false;
	this.c_2 = null;
	this.f_1 = null;
	this.u_1 = 0;
	this.b1 = b1;
	this.d = b1.d;
};

DragAndDropWindow$BTD.prototype = 
{
	run : function()
	{
		var self = this;
		this.b1.addEvent(this.d, "mouseover", function(e) { self._F_1(e); });
		this.b1.addEvent(this.d, "mousedown", function(e) { self.f_2(e); });
		this.b1.addEvent(this.d, "mouseup", function(e) { self.r_1(e); });
		this.b1.addEvent(this.d, "mousemove", function(e) { self.f_5(e); });
		this.b1.addEvent(this.b1.wd, "resize", function(e) { self.r_56(e); });
		this.b1.addEvent(this.d, "scroll", function(e) { self.m_1(e); });
	},
	
	addItemNotMove : function(v)
	{
		this.l_6.push(v);
	},
	
	removeItemNotMove : function(v)
	{
		for (var i = 0; i < this.l_6.length; i++)
		{
			if (this.l_6[i] == v) break;
		}
		this.l_6.splice(i, 1);
	},
	
	removeAllItemNotMove : function()
	{
		this.l_6.splice(0, this.l_6.length);
	},
	
	objInListNotMove : function(o)
	{
		for (var i = 0; i < this.l_6.length; i++)
		{
			if (this.l_6[i].oHeader == o) return true;
		}
		return false;
	},
	
	_F_1 : function(e)
	{
		var o = this.b1.getTargetElement();
		
		if (this.objInListNotMove(o)) return;
		
		if (this.l_5 != null)
		{
			for (var name in this.l_5)
			{
				if (this.l_5[name] && this.l_5[name].oHeader == o)
				{
					o.style.cursor = "move";
					this.b1.disableContextMenu(o);
				}
			}
		}
		else
		{
			if (this.findWordCanDrop(o.className))
			{
				o.style.cursor = "move";
				this.b1.disableContextMenu(o);
			}
		}
	},
	
	s_44 : function(e)
	{
		if (!this.o_12 || !this.o_12.oMenu) return;
		this.o_12.oMenu.createRoot(e);
	},
	
	r_56 : function(e)
	{
		if (this.o_12 && this.o_12.oMenu)
		{
			this.o_12.oMenu.hideAll(e);
		}
	},
	
	f_2 : function(e)
	{
		if (this.o_12 && this.o_12.oMenu) this.o_12.oMenu.hideAll(e);
		e = this.b1.getWindowEvent();
		this.t_1 = this.getWindowObject();
		if (this.t_1 == null) return;
		if ( (this.b1.isFF && e.which > 1) 
			|| (this.b1.isIE && e.button > 1) 
			|| (this.b1.isMaxthon && e.button == 0) )
		{
			this.s_44(e);
			return false;
		}
		this.t_1.style.width = this.t_1.offsetWidth + "px";
		this.c_1 = this.t_1.parentNode;
		this.d_2 = 1;
		this.firstTime = true;
		var mousePos = this.b1.mouseCoords(e);
		var posEl = this.b1.getElementPosition(this.t_1);
		this.c_2 = { X: mousePos.X - posEl.X, Y: mousePos.Y - posEl.Y };
		this.b1.stopEvent();
	},
	
	insertFrameDrop : function()
	{
		var h = (this.isIE ? this.t_1.offsetHeight + 15 : this.t_1.offsetHeight);
		var oFrame = this.g_1(this.t_1.offsetWidth - 3, h);
		
		this.f_1 = oFrame;
		this.c_1.insertBefore(oFrame, this.t_1);
	},

	f_5 : function(e)
	{
		var o = this.t_1;
		if (this.d_2 == 1)
		{
			this.b1.disableSelection(this.d.body);
			e = this.b1.getWindowEvent();
			if (this.firstTime)
			{
				this.firstTime = false;
				this.insertFrameDrop();
			}
			var mousePos = this.b1.mouseCoords(e);
			this.offsetY = e.clientY - this.c_2.Y;
			o.style.position = "absolute";
			o.style.zIndex = 2100;
			if (this.b1.isIE || this.b1.isOpera)
			{
				// o.style.filter = "alpha(opacity = 80)";
			}
			else
				o.style.MozOpacity = .80;
			o.style.left = mousePos.X - this.c_2.X + "px";
			var y = mousePos.Y - this.c_2.Y;			
			o.style.top = y + "px";
			if (y != this.u_1)
			{
				var movingDown = y > this.u_1;
				this.u_1 = y;
				var currentWindow = this.findDropTargetWindow(y);
				if (currentWindow) 
				{
					if (movingDown)
					{
						this.c_1.insertBefore(this.f_1, currentWindow.nextSibling);
					}
					else
					{
						this.c_1.insertBefore(this.f_1, currentWindow);
					}
				}
			}
		}
	},
	
	findDropTargetWindow : function(y)
	{
		var oChilds = this.c_1.childNodes;
		for (var i = 0; i < oChilds.length; i++) 
		{
			var oChild = oChilds[i];
			if (!oChild.tagName || oChild.tagName.toLowerCase() != "table") continue;
			var oChildY = this.b1.getElementPosition(oChild).Y;
			var oChildHeight = parseInt(oChild.offsetHeight) / 2;
			if (oChild.offsetHeight == 0) 
			{
				oChildY = this.b1.getElementPosition(oChild.firstChild).Y;
				oChildHeight = parseInt(oChild.firstChild.offsetHeight) / 2;
			}
			if ((y > oChildY) && (y < (oChildY + oChildHeight))) 
			{
				return oChild;
			}
		}
		return null;
    },
	
	r_1 : function(e)
	{
		try
		{
			this.b1.enableSelection(this.d.body);
			if (this.d_2 == 0 || this.c_1 == null || this.f_1 == null)
			{
				this.d_2 = 0;
				return;
			}
			this.d_2 = 0;
			if (this.f_1.style.display == "none") return;
			this.c_1.insertBefore(this.t_1, this.f_1.nextSibling);
			this.b1.setCookie(this.c_1.id, this.getPosObj());
			if (this.b1.isIE || this.b1.isOpera)
			{
				// this.t_1.style.filter = "alpha(opacity = 100)";
			}
			else
				this.t_1.style.MozOpacity = 1.00;
			this.t_1.style.zIndex = 10;
			this.t_1.style.position = "static";
			this.f_1.style.visibility = "hidden";
			this.f_1.style.display = "none";
		}
		catch (e)
		{
			alert("Can not move!");
		}
	},
	
	getPosObj : function()
	{
		var oChilds = this.c_1.childNodes;
		var sOut = "";
		for (var i = 0; i < oChilds.length; i++) 
		{
			var oChild = oChilds[i];
			if (this.l_5[oChild.id])
			{
				if (sOut == "") sOut += oChild.id;
				else sOut += "," + oChild.id;
			}
        }
        return sOut;
	},
	
	m_1 : function(e)
	{
		if (this.d_2 == 1)
		{
			this.t_1.style.top = (this.b1.getDocument().scrollTop + this.offsetY) + "px";
		}
	},
	
	findWordCanDrop : function(value)
	{
		for (var i = 0; i < this.b.length; i++)
		{
			if (value.indexOf(this.b[i]) != -1)
			{
				return true;
			}
		}
		return false;
	},
	
	/*
	 * Get window
	 */
	getWindowObject : function()
	{
		var o = this.b1.getTargetElement();
		
		if (this.objInListNotMove(o)) return;
		
		if (this.l_5 != null)
		{
			for (var name in this.l_5)
			{
				if (this.l_5[name] && this.l_5[name].oHeader == o)
				{
					this.o_12 = this.l_5[name];
					return this.l_5[name].oOut;
				}
			}
		}
		else
		{
			if (this.findWordCanDrop(o.className))
			{
				while (o.id.indexOf(this.a1) == -1 && o != null)
				{
					o = o.parentNode;
				}
				return o;
			}
		}
		return null;
	},
	
	/*
	 * Get frame can drop
	 */
	g_1 : function(w, h)
	{
		var id = this.a + "_frame_can_drop";
		var o = this.b1.getElById(id);
		if (o == null)
		{
			o = this.b1.createEl("DIV");
			o.id = id;
			o.style.border = "1px #008800 dashed";
			this.d.body.appendChild(o);
		}
		o.style.width = w + "px";
		o.style.height = h + "px";
		o.style.visibility = "visible";
		o.style.display = "block";
		return o;
	}
};
