/**
 * COMMON DHTML FUNCTIONS
 * These are handy functions I use all the time.
 *
 * By Seth Banks (webmaster at subimage dot com)
 * http://www.subimage.com/
 *
 * Up to date code can be found at http://www.subimage.com/dhtml/
 *
 * This code is free for you to use anywhere, just keep this comment block.
 */

/**
 * X-browser event handler attachment and detachment
 * TH: Switched first true to false per http://www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html
 *
 * @argument obj - the object to attach event to
 * @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
 * @argument fn - function to call
 */
function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}
function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}

/**
 * Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/
 *
 * Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
 *
 * Gets the full width/height because it's different for most browsers.
 */
function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 

	return window.undefined; 
}
function getViewportWidth() {
	var offset = 17;
	var width = null;
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
}

/**
 * Gets the real scroll top
 */
function getScrollTop() {
	if (self.pageYOffset) // all except Explorer
	{
		return self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollTop;
	}
}
function getScrollLeft() {
	if (self.pageXOffset) // all except Explorer
	{
		return self.pageXOffset;
	}
	else if (document.documentElement && document.documentElement.scrollLeft)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollLeft;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollLeft;
	}
}
var Dragger = Class.create();
Dragger.prototype = {
	initialize : function() {
		this.dragArray = new Array();
	},
	makeDraggable : function(parameters) {
		var item = new DraggerItem(parameters);
		this.dragArray.push(item);
	}
};
var DraggerItem = Class.create();
DraggerItem.prototype = {
	initialize : function(parameters) {
		this.targetObj = parameters.targetObj;
		this.objContainer = parameters.objContainer
				? parameters.objContainer
				: document.body;
		this.ponMouseDown = parameters.onMouseDown;
		this.ponMouseMove = parameters.onMouseMove;
		this.ponMouseUp = parameters.onMouseUp;
		this.targetObj.onmousedown = this.ponMouseDown ? this.ponMouseDown
				.bind(this) : this.onMouseDown.bind(this);
		this.targetObj.onmousemove = this.ponMouseMove ? this.ponMouseMove
				.bind(this) : this.onMouseMove.bind(this);
		this.targetObj.onmouseup = this.ponMouseUp
				? this.ponMouseUp.bind(this)
				: this.onMouseUp.bind(this);
		this.caculateBorderCoor();
	},
	caculateBorderCoor : function() {
		// 默认容器，即document
		if (this.objContainer == document.body) {
			// 左边框坐标
			this.x0 = this.objContainer.clientLeft;
			// 右边框坐标
			this.x1 = this.objContainer.clientWidth;
			// 上边框坐标
			this.y0 = this.objContainer.clientTop;
			// 下边框坐标
			this.y1 = this.objContainer.clientHeight;
		} else// 是Div
		{
			var body = document.body;
			// 左边框坐标
			this.x0 = this.objContainer.offsetLeft;
			// 右边框坐标
			this.x1 = this.x0 + this.objContainer.offsetWidth - body.clientLeft;
			// 上边框坐标
			this.y0 = body.clientTop + this.objContainer.offsetTop;
			// 下边框坐标
			this.y1 = this.y0 + this.objContainer.clientHeight - body.clientTop;
		}
	},
	onMouseDown : function() {
		
		if (event.button == 1)// 按下左键
		{

			if (this.ponMouseDown) {
				Try.these(this.ponMouseDown);
			} else {
				this.targetObj.moveable = true;
				this.targetObj.setCapture();
				this.startX = parseInt(this.targetObj.style.left);
				this.startY = parseInt(this.targetObj.style.top);
				this.mouseX = event.clientX;
				this.mouseY = event.clientY;
			}
		}
	},
	onMouseMove : function() {
		if (event.button == 1)// 按下左键
		{
			if (this.ponMouseMove) {
				Try.these(this.ponMouseMove);
			} else if (this.targetObj.moveable) {

				var left = this.startX + event.clientX - this.mouseX;
				var top = this.startY + event.clientY - this.mouseY;
				// 超过容器左边框或上边框时
				left = left < this.x0 ? this.x0 : left;
				top = top < this.y0 ? this.y0 : top;
				// 超过容器左边框或下边框时
				this.targetObj.style.left = left > (this.x1 - this.targetObj.clientWidth)
						? (this.x1 - this.targetObj.clientWidth)
						: left;

				this.targetObj.style.top = top > (this.y1 - this.targetObj.clientHeight)
						? (this.y1 - this.targetObj.clientHeight)
						: top;
				window.status = "" + left + "," + top;
			}
		}
	},
	onMouseUp : function() {
		if (event.button == 1)// 按下左键
		{
			if (this.ponMouseUp) {
				Try.these(this.ponMouseUp);
			} else if (this.targetObj.moveable) {
				this.targetObj.moveable = false;
				this.targetObj.releaseCapture();
			}
		}
	}
}

