/**
 * Javascript
 * 
 * @author     banhthidiem <banhthidiem@gmail.com>
 * @copyright  2007 Bach Khoa Computer Inc.
 * @version    $Id: ScrollBodyLeft$BTD.js, v1.0 2007/12/5
 */
 
function ScrollBodyLeft$BTD(id, myName)
{
	this.isOpen = true;
	this.isStatic = false;
	this.oTimer = null;
	this.myName = myName;
	this.oParent = utilObj.getElById(id);
	this.canDo = false;
	var self = this;
	utilObj.addEvent(utilObj.d, "mousemove", function(e) { self.mouseMove(e); });
	this.oChild = this.oParent.firstChild;
	while (this.oChild != null && this.oChild.tagName == null)
	{
		this.oChild = this.oChild.nextSibling;
	}
	this.oChild.style.left = "0px";
	this.oChild.style.backgroundColor = "#FFF";
	this.firstLeftSize = utilObj.getElementSize(this.oParent); // Left
	var oRight = this.oParent.parentNode.nextSibling;
	while (oRight != null && oRight.tagName == null)
	{
		oRight = oRight.nextSibling;
	}
	this.firstRightSize = utilObj.getElementSize(oRight); // Right
	this.firstWidthMin = 0;
	this.oRight = oRight;
	this.firstPosition = utilObj.getElementPosition(this.oParent);
	this.oParent.style.width = this.firstLeftSize.width + "px";
	this.oParent.parentNode.style.height = this.firstLeftSize.height + "px";
	var self = this;
	utilObj.addEvent(utilObj.wd, "resize", function(e)
	{
		self.whenResizeWindow(e);
	});
	utilObj.addEvent(null, "scroll", function(e)
	{
		self.whenScrollWindow(e);
	});
};

ScrollBodyLeft$BTD.prototype.whenResizeWindow = function(e)
{
	var o = this.createBar();
	if (o.style.display == "none")
	{
		return;
	}
	var de = utilObj.getDocument();
	this.firstPosition = utilObj.getElementPosition(this.oParent);
	o.style.height = (de.clientHeight > screen.height ? screen.height : de.clientHeight) - 200 + "px";
	o.style.top = this.firstPosition.Y + "px";
	o.style.left = this.firstPosition.X + "px";
};

ScrollBodyLeft$BTD.prototype.whenScrollWindow = function(e)
{
	var o = this.createBar();
	if (o.style.display == "none")
	{
		return;
	}
	var de = utilObj.getDocument();
	o.style.top = (this.firstPosition.Y + utilObj.getDocumentScroll().scrollTop) + "px";
};

ScrollBodyLeft$BTD.prototype.run = function(isOpen, isStatic)
{
	/*
	if (utilObj.isIE && utilObj.appVersion == 6)
	{
		return;
	}*/
	this.hideBar();
	this.isOpen = isOpen;
	this.oParent.style.position = "absolute";
	this.oParent.style.zIndex = 2000;
	this.oChild.style.position = "relative";
	this.oChild.style.display = "block";
	this.createListNotMove();
	if (typeof(isStatic) != "undefined") this.isStatic = isStatic;
	if (this.isStatic) this.canDo = false;
	this.scroll();
};

ScrollBodyLeft$BTD.prototype.mouseMove = function(e)
{
	if (!this.canDo) return;
	var oTarget = utilObj.getTargetElement();
	var canDo = true;
	while (oTarget != null)
	{
		if (oTarget == this.oParent)
		{
			this.isOpen = canDo;
			canDo = false;
			break;
		}
		oTarget = oTarget.parentNode;
	}
	if (canDo) this.run(false, false);
};

ScrollBodyLeft$BTD.prototype.mouseOverBar = function()
{
	this.canDo = true;
	this.run(true, false);
};

ScrollBodyLeft$BTD.prototype.createListNotMove = function()
{
	if (BKC.oBlockPage == null) return;
	if (typeof(tblStatistics) != "undefined") BKC.oBlockPage.addItemNotMove(tblStatistics);
	if (typeof(tblCat) != "undefined") BKC.oBlockPage.addItemNotMove(tblCat);
	if (typeof(tblSearch) != "undefined") BKC.oBlockPage.addItemNotMove(tblSearch);
	if (typeof(tblExplore) != "undefined") BKC.oBlockPage.addItemNotMove(tblExplore);
	if (typeof(tblNews) != "undefined") BKC.oBlockPage.addItemNotMove(tblNews);
	if (typeof(tblSupport) != "undefined") BKC.oBlockPage.addItemNotMove(tblSupport);
};

ScrollBodyLeft$BTD.prototype.deleteListNotMove = function()
{
	if (BKC.oBlockPage == null) return;
	BKC.oBlockPage.removeAllItemNotMove();
};

ScrollBodyLeft$BTD.prototype.createBar = function()
{
	var o = utilObj.getElById("scrollBodyBar");
	if (o == null)
	{
		o = utilObj.createEl("DIV");
		o.id = "scrollBodyBar";
		o.style.position = "absolute";
		var de = utilObj.getDocument();
		o.style.height = (de.clientHeight > screen.height ? screen.height : de.clientHeight) - 200 + "px";
		o.style.top = this.firstPosition.Y + "px";
		o.style.left = this.firstPosition.X + "px";
		try { o.style.cursor = "pointer"; } catch (e) { }
		var self = this;
		utilObj.addEvent(o, "mouseover", function() { self.mouseOverBar(); });
		utilObj.addEvent(o, "click", function() { self.mouseOverBar(); });
		utilObj.addChildToBody(o);
	}
	return o;
};

ScrollBodyLeft$BTD.prototype.showBar = function()
{
	var o = this.createBar(); 
	o.style.display = "";
	var de = utilObj.getDocument();
	this.firstPosition = utilObj.getElementPosition(this.oParent);
	o.style.height = (de.clientHeight > screen.height ? screen.height : de.clientHeight) - 200 + "px";
	o.style.top = this.firstPosition.Y + "px";
	o.style.left = this.firstPosition.X + "px";
};

ScrollBodyLeft$BTD.prototype.hideBar = function()
{
	var o = this.createBar();
	if (o) o.style.display = "none";
	o.style.top = "-1000px";
	o.style.left = "-1000px";
};

ScrollBodyLeft$BTD.prototype.scrollEnd = function(w)
{
	if (w == this.firstLeftSize.width || w == this.firstWidthMin)
	{
		utilObj.wd.clearTimeout(this.oTimer);
		if (w == this.firstWidthMin)
		{
			this.oChild.style.display = "none";
			this.oParent.parentNode.style.width = "5px";
			this.oRight.style.width = "98.5%";
			this.showBar();
			this.canDo = false;
		}
		else
		{
			if (this.isStatic)
			{
				this.oParent.parentNode.style.width = this.firstLeftSize.width + "px";
				this.oRight.style.width = this.firstRightSize.width + "px";
				this.deleteListNotMove();
			}
		}
		if (this.isStatic)
		{
			this.oParent.style.position = "static";
			this.oChild.style.position = "static";
		}
	}
};

ScrollBodyLeft$BTD.prototype.scroll = function()
{
	var el = this.oParent;
	var w = parseInt(el.style.width);
	var x = parseInt(this.oChild.style.left);
	
	var num = 20;
	if (x <= this.firstWidthMin && x >= -this.firstLeftSize.width)
	{
		if (!this.isOpen)
		{
			x = (x - num) < -this.firstLeftSize.width ? -this.firstLeftSize.width : (x - num);
		}
		else
		{
			x = (x + num) > this.firstWidthMin ? this.firstWidthMin : (x + num);
		}
		this.oChild.style.left = x + "px";
	}
	if (w <= this.firstLeftSize.width && w >= this.firstWidthMin)
	{
		if (!this.isOpen)
		{
			w = (w - num) < this.firstWidthMin ? this.firstWidthMin : (w - num);
		}
		else
		{
			w = (w + num) > this.firstLeftSize.width ? this.firstLeftSize.width : (w + num);
		}
		el.style.width = w + "px";
		
		this.oTimer = utilObj.wd.setTimeout(this.myName + ".scroll();", 1);
	}
	this.scrollEnd(w);
};