/*
----------------------------------------------------------------------------------------------
Scorlling dock [v2.0]
Author:   Tribal DDB HK (Garfield Chan)
Created:  6 November 2007

History:
[2007/10/31]	- Basic functionalities completed
[2007/11/2]	- Take out BThumbClass, v1.0 completed
[2007/11/23]	- v2.0 completed

---------------------------------------------------------------------------------------------- */

function DockClass() {
	// private members
	var that = this;
	var dockDir = "l";
	var dockSpeed = 100;

	/* class definition
	---------------------------------------- */
	this.BuildDock = BuildDock;
	this.CreateDocks = CreateDocks;
	this.UpdateDocks = UpdateDocks;
	this.RemoveDock = RemoveDock;
	this.oDockInners = [];

	// privileged methods
	this.DockDir = DockDir;
	this.DockSpeed = DockSpeed;

	/* instance handler
	---------------------------------------- */
	if (arguments.length) {
		this.oName = arguments[0];
		this.element = $(this.oName);
		this.BuildDock();
	}
	return this;

	/* class methods definition
	---------------------------------------- */
	// build the dockinner - HTML
	function BuildDock() {
		var oDockInner = document.createElement("div");
		oDockInner.setAttribute(_isIE?"className":"class","homeDockInner floatFix");
		if (!arguments.length) {this.CreateDocks(oDockInner);}
		else {this.CreateDocks(oDockInner,arguments[0]);}
	}

	// insert or append the dockinner to the parent - HTML
	function CreateDocks() {
		var oDockInner = arguments[0];
		if ((arguments.length > 1) && (arguments[1] == "first")) {
			this.element.insertBefore(oDockInner,this.element.firstChild);
		} else {
			this.element.appendChild(oDockInner);
		}
		this.UpdateDocks(oDockInner);
	}

	// create the instance and update the array - JS
	function UpdateDocks() {
		var oDockInner = new DockInnerClass(arguments[0]);
		if (this.DockDir() == "l") {this.oDockInners.push(oDockInner);}
		else if (this.DockDir() == "r") {this.oDockInners.unshift(oDockInner);}
	}

	// remove existing dockinner and update the array - HTML & JS
	function RemoveDock() {
		if (arguments.length) {
			var oDockInner = arguments[0];
			while (oDockInner.hasChildNodes()) {
				oDockInner.removeChild(oDockInner.firstChild);
			}
			oDockInner.parentNode.removeChild(oDockInner);

			if (this.DockDir() == "l") {this.oDockInners.shift();}
			else if (this.DockDir() == "r") {this.oDockInners.pop();}
		}
	}

	/* privileged methods
	---------------------------------------- */
	// setter and getter of scroll direction of dockinner - JS
	function DockDir() {
		if (arguments.length) {dockDir = arguments[0];}
		return dockDir;
	}

	// setter and getter of scrolling speed of dockinner - JS
	function DockSpeed() {
		if (arguments.length) {dockSpeed = arguments[0];}
		return dockSpeed;
	}
}

function DockInnerClass() {
	// private members
	var that = this;
	var posLeft = 0;

	/* class definition
	---------------------------------------- */
	// sthumb related
	this.GrabSThumb = GrabSThumb;
	this.BuildSThumb = BuildSThumb;
	this.CreateSThumb = CreateSThumb;
	this.oSThumbs = [];

	// movement related
	this.SetPos = SetPos;
	this.StartMove = StartMove;
	this.StopMove = StopMove;
	this.MoveLeft = MoveLeft;
	this.MoveRight = MoveRight;
	this.PassPosLeft = PassPosLeft;
	this.t;

	// privileged methods
	this.PosLeft = PosLeft;

	/* instance handler
	---------------------------------------- */
	if (arguments.length) {
		this.element = arguments[0];
		this.SetPos();
		this.GrabSThumb();
	}
	return this;

	/* class methods definition
	---------------------------------------- */
	// grab 6 sthumb nodes - Ajax
	function GrabSThumb() {
		xmlHttp = getXmlHttpObject();
		var url = "asplib/grabthumb_v2.asp";
		if (this.element.previousSibling) {
			var dir = "last";
			var oSThumbs = $$(this.element.previousSibling,"div");
			var oID = oSThumbs[oSThumbs.length-1].id;
		}
		if (this.element.nextSibling) {
			var dir = "first";
			var oSThumbs = $$(this.element.nextSibling,"div");
			var oID = oSThumbs[0].id;
		}
		url = (this.element.previousSibling || this.element.nextSibling) ? url+"?q="+oID+"&d="+dir : url;

		xmlHttp.onreadystatechange = this.BuildSThumb;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}

	// insert the sthumb into the dockinner - HTML
	function BuildSThumb() {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			that.element.innerHTML = xmlHttp.responseText;
			that.CreateSThumb();
		}
	}

	// create the instance of sthumb object - JS
	function CreateSThumb() {
		var a = $$(this.element,"div");
		for (var i=0; i<a.length; i++) {this.oSThumbs[i] = new SThumbClass(a[i]);}
		this.StartMove();
	}

	/* movement related
	---------------------------------------- */
	// set the initial position of the sthumb - HTML
	function SetPos() {
		if (this.element.previousSibling) {
			var prePos = this.element.previousSibling.style.left;
			prePos = prePos.toNum();
			this.element.style.left = prePos + 1104 + "px";
		} else if (this.element.nextSibling) {
			var nexPos = this.element.nextSibling.style.left;
			nexPos = nexPos.toNum();
			this.element.style.left = nexPos - 1104 + "px";
		}
	}

	// start the move of the individual dockinner - JS
	function StartMove() {
		if (oDock.DockDir() == "l") {this.MoveLeft();}
		else if (oDock.DockDir() == "r") {this.MoveRight();}
	}

	// stop the move of the individual dockinner - JS
	function StopMove() {
		window.clearTimeout(this.t);
	}

	// move the dockinner to left - HTML, JS
	function MoveLeft() {
		if (that.element) {
			var s = oDock.DockSpeed();
			var oriPos = that.element.style.left;
			oriPos = ((oriPos != "") && (typeof oriPos == "string")) ? parseInt(oriPos) : oriPos;
			if (that.PosLeft() == -138) {oDock.BuildDock("last");}
			if (that.PosLeft()+that.element.clientWidth >= -138) {
				if ((that.element.previousSibling) && (that.element.offsetLeft - that.element.previousSibling.offsetLeft != 1104)) {
					that.SetPos();
					that.PosLeft(that.element.offsetLeft);
					that.PassPosLeft();
				} else {
					//var newPos = oriPos - 0;
					var newPos = oriPos - 1;
					that.PosLeft(that.element.offsetLeft);
					that.element.style.left = newPos + "px";
					that.PassPosLeft();
				}
				that.t = window.setTimeout(that.MoveLeft,s);
			} else {
				that.StopMove();
				oDock.RemoveDock(that.element);
			}
		}
	}

	// add the sthumb to right - HTML, JS
	function MoveRight() {
		if (that.element) {
			var s = oDock.DockSpeed();
			var oriPos = that.element.style.left;
			oriPos = ((oriPos != "") && (typeof oriPos == "string")) ? parseInt(oriPos) : oriPos;
			if (that.PosLeft()+that.element.clientWidth == 966) {oDock.BuildDock("first");}
			if (that.PosLeft() <= 966) {
				if ((that.element.previousSibling) && (that.element.offsetLeft - that.element.previousSibling.offsetLeft != 1104)) {
					that.SetPos();
					that.PosLeft(that.element.offsetLeft);
					that.PassPosLeft();
				} else {
					//var newPos = oriPos + 0;
					var newPos = oriPos + 1;
					that.PosLeft(that.element.offsetLeft);
					that.element.style.left = newPos + "px";
					that.PassPosLeft();
				}
				that.t = window.setTimeout(that.MoveRight,s);
			} else {
				that.StopMove();
				oDock.RemoveDock(that.element);
			}
		}
	}

	// pass the offsetleft of dockinner to individual sthumb - JS
	function PassPosLeft() {
		for (var i=0; i<this.oSThumbs.length; i++) {this.oSThumbs[i].UpPosLeft(this.PosLeft());}
	}

	/* privileged methods
	---------------------------------------- */
	// setter and getter of offsetleft of individual sthumb - JS
	function PosLeft() {
		if (arguments.length) {posLeft = arguments[0];}
		return posLeft;
	}
}

function SThumbClass() {
	// private members
	var that = this;
	var posLeft = 0;
	var upPosLeft = 0;

	/* class definition
	---------------------------------------- */
	this.MouseOver = MouseOver;
	this.MouseOut = MouseOut;

	// privileged methods
	this.PosLeft = PosLeft;
	this.UpPosLeft = UpPosLeft;

	/* instance handler
	---------------------------------------- */
	if (arguments.length) {
		this.element = arguments[0];
		this.element.onmouseover = this.MouseOver;
		this.element.onmouseout = this.MouseOut;
	}
	return this;

	/* class methods definition
	---------------------------------------- */
	// apply changes for onmouseover - JS
	function MouseOver() {
		for (var i=0; i<oDock.oDockInners.length; i++) {oDock.oDockInners[i].StopMove();}
		that.PosLeft(that.element.offsetLeft + that.UpPosLeft());
		var oImg = $$(that.element,"img");
		oImg[0].src = oImg[0].src.replace("home_sthumb_frm.gif","home_sthumb_frmon.gif");
		oDesc.StartDesc(that);
	}

	// apply changes for onmouseout - JS
	function MouseOut() {
		for (var i=0; i<oDock.oDockInners.length; i++) {oDock.oDockInners[i].StartMove();}
		var oImg = $$(that.element,"img");
		oImg[0].src = oImg[0].src.replace("home_sthumb_frmon.gif","home_sthumb_frm.gif");
		oDesc.ClearDesc();
	}

	/* privileged methods
	---------------------------------------- */
	// setter and getter of offsetleft of individual sthumb - JS
	function PosLeft() {
		if (arguments.length) {posLeft = arguments[0];}
		return posLeft;
	}

	// setter and getter of upper offsetleft (dockinner) - JS
	function UpPosLeft() {
		if (arguments.length) {upPosLeft = arguments[0];}
		return upPosLeft;
	}
}

function DescClass() {
	// private members
	var that = this;

	/* class definition
	---------------------------------------- */
	this.StartDesc = StartDesc;
	this.ClearDesc = ClearDesc;
	this.SetPos = SetPos;
	this.GrabDesc = GrabDesc;
	this.UpdateDesc = UpdateDesc;

	/* instance handler
	---------------------------------------- */
	if (arguments.length) {
		this.oName = arguments[0];
		this.element = $(this.oName);
	}
	return this;

	/* class methods definition
	---------------------------------------- */
	// show the description layer with conditions - HTML
	function StartDesc() {
		var oSThumb = arguments[0];
		var oSThumbMpt = oSThumb.PosLeft() + oSThumb.element.clientWidth/2;
		if ((oSThumbMpt > 0) && (oSThumbMpt < oDock.element.clientWidth)) {
			this.SetPos(oSThumbMpt);
			this.GrabDesc(oSThumb);
		}
	}

	// hide the description layer - HTML
	function ClearDesc() {
		this.element.style.visibility = "hidden";
		$("homeDescInner").innerHTML = "";
	}

	// adjust the position of the description layer - HTML
	function SetPos() {
		var oSThumbMpt = arguments[0];
		var oDockTop = (_isIE) ? $("homeDock").offsetTop : $("homeDockArea").offsetTop + 37;
		var oDockLeft = (_isIE) ? $("homeDock").offsetLeft : $("homeDockArea").offsetLeft + 35 + 32;
		this.element.style.top = oDockTop - (this.element.clientHeight - 13) + "px";
		this.element.style.left = oDockLeft + oSThumbMpt - this.element.clientWidth/2 + "px";
		this.element.style.visibility = "visible";
	}

	// grab the description of the related sthumb - Ajax
	function GrabDesc() {
		var oID = arguments[0].element.id;
		xmlHttp2 = getXmlHttpObject();
		var url = "asplib/grabdesc_v2.asp";
		url = url + "?q=" + oID;

		xmlHttp2.onreadystatechange = this.UpdateDesc;
		xmlHttp2.open("GET",url,true);
		xmlHttp2.send(null);
	}

	// update the text inside the description layer - HTML
	function UpdateDesc() {
		if (xmlHttp2.readyState == 4 && xmlHttp2.status == 200) {
			var resText
			resText = xmlHttp2.responseText.replace(/&lt;br&gt;/, "<br>");
			$("homeDescInner").innerHTML = resText;
		}
	}
}

function DockControlClass() {
	// private members
	var that = this;

	/* class definition
	---------------------------------------- */
	this.MouseDown = MouseDown;
	this.MouseUp = MouseUp;

	/* instance handler
	---------------------------------------- */
	if (arguments.length) {
		this.oName = arguments[0];
		this.element = $(this.oName);
		this.element.onmousedown = this.MouseDown;
		this.element.onmouseup = this.MouseUp;
	}
	return this;

	/* class methods definition
	---------------------------------------- */
	// change the direction and speed of the sthumb - JS
	function MouseDown() {
		dir = (that.element.id.indexOf("Left") > -1) ? "l" : "r";
		for (var i=0; i<oDock.oDockInners.length; i++) {
			oDock.oDockInners[i].StopMove();
			oDock.DockDir(dir);
			oDock.DockSpeed(1);
			oDock.oDockInners[i].StartMove();
		}
		return false;
	}

	// change the direction and speed of the sthumb - JS
	function MouseUp() {
		for (var i=0; i<oDock.oDockInners.length; i++) {
			oDock.oDockInners[i].StopMove();
			oDock.DockSpeed(100);
			oDock.oDockInners[i].StartMove();
		}
		return false;
	}
}

function init() {
	oDock = new DockClass("homeDock");
	oDesc = new DescClass("homeDesc");
	oDockArrLeft = new DockControlClass("homeDockArrLeft");
	oDockArrRight = new DockControlClass("homeDockArrRight");
}
addLoadEvent(init);