﻿function DivScroll (containerName) {
	this.containerName = containerName;//容器名字
	this.contentContainer = null;//位移容器
	this.listContainer = null;//框体容器
	this.divContent = new Array();//容器内容
	this.divCount = 5;//翻页个数
	this.scrollSpeed = 10;//速度(毫秒)
	this.scrollSpace = 5;//每次移动(px)
	this.scrollComp = 0;//位移标志
	this.scrollFill = 0;//整体移位
	this.scrollLock = false;//锁定
	this.scrollTime = null;//定时器
	this.scrollRange = 0;//翻页宽度
	this.scrollButton = true;//翻页按钮
	this.autoScrollCallback = null;//翻页方向
	
	this.getElementById = function (objName) {
		if (document.getElementById) {
			return eval('document.getElementById("'+objName+'")');
		} else {
			return eval('document.all.'+objName);
		}
	}
	
	this.append = function (text) {
		this.divContent[this.divContent.length] = text;
	}
	
	this.drawLeft = function (className) {
		this.contentContainer = this.getElementById(this.containerName+'_Content');
		if (this.contentContainer == null) {
			var text = this.divContent.join('</div><div class="pic">');
			document.write('<div class="'+className+'" id="' + this.containerName + '" style="display:none">');
				if (this.scrollButton) {
					document.write('<div class="LeftBotton" id="' + this.containerName + '_LeftBotton"></div>');
				}
				document.write('<div class="Content" id="' + this.containerName + '_Content">');
					document.write('<div id="' + this.containerName + '_Page" class="Page">');
						document.write('<div class="List" id="' + this.containerName + '_List">');
							document.write('<div class="pic"');
								document.write(text);
							document.write('</div>');
						document.write('</div>');
						document.write('<div class="List">');
							document.write('<div class="pic">');
								document.write(text);
							document.write('</div>');
						document.write('</div>');
					document.write('</div>');
				document.write('</div>');
				if (this.scrollButton) {
					document.write('<div class="RightButton" id="' + this.containerName + '_RightButton"></div>');
				}
			document.write('</div>');
			this.contentContainer = this.getElementById(this.containerName+'_Content');
		}
		this.listContainer = this.getElementById(this.containerName+'_List');
		
		if (this.autoScrollCallback == null) {
			this.autoScrollCallback = this.right;
		}
	}
	
	this.drawTop = function (className) {
		this.contentContainer = this.getElementById(this.containerName+'_Content');
		if (this.contentContainer == null) {
			var text = this.divContent.join('</div><div class="pic">');
			document.write('<div class="'+className+'" id="' + this.containerName + '" style="display:none">');
				if (this.scrollButton) {
					document.write('<div class="UpBotton" id="' + this.containerName + '_UpBotton"></div>');
				}
				document.write('<div class="Content" id="' + this.containerName + '_Content">');
					document.write('<div id="' + this.containerName + '_Page">');
						document.write('<div class="List" id="' + this.containerName + '_List">');
							document.write('<div class="pic">');
								document.write(text);
							document.write('</div>');
						document.write('</div>');
						document.write('<div class="List">');
							document.write('<div class="pic">');
								document.write(text);
							document.write('</div>');
						document.write('</div>');
					document.write('</div>');
				document.write('</div>');
				if (this.scrollButton) {
					document.write('<div class="DownButton" id="' + this.containerName + '_DownButton"></div>');
				}
			document.write('</div>');
			this.contentContainer = this.getElementById(this.containerName+'_Content');
		}
		
		this.listContainer = this.getElementById(this.containerName+'_List');
		
		if (this.autoScrollCallback == null) {
			this.autoScrollCallback = this.down;
		}
	}
	
	this.execute = function () {
		if (this.divContent.length < 1) {
			return ;
		}
		
		this.getElementById(this.containerName).style.display = '';
		this.contentContainer.style.width = 10000 + "px";
		if (this.autoScrollCallback == this.down || this.autoScrollCallback == this.up) {
			this.contentContainer.scrollTop = this.scrollFill;
			this.scrollRange = this.listContainer.offsetHeight;
			
			this.contentContainer.style.width = this.listContainer.offsetWidth/this.divContent.length + "px";
			this.contentContainer.style.height = this.scrollRange * this.divCount + "px";
	
			this.getElementById(this.containerName+ '_Page').style.height = this.scrollRange * this.divCount * 100 + "px";
			
			if (this.scrollButton) {
				var obj = this;
				var upBotton = this.getElementById(this.containerName+'_UpBotton');
				var downButton = this.getElementById(this.containerName+'_DownButton');
				
				upBotton.onmousedown = function () {obj.down();};
				downButton.onmousedown = function () {obj.up();};
			}
			this.getElementById(this.containerName).style.width = this.contentContainer.style.width;
		} else {
			this.scrollRange = this.listContainer.offsetWidth/this.divContent.length;
			
			this.contentContainer.scrollLeft = this.scrollFill;
			this.contentContainer.style.width = this.scrollRange * this.divCount + "px";
			this.contentContainer.style.heigth = this.listContainer.offsetHeight + "px";
			
			this.getElementById(this.containerName+ '_Page').style.width = this.scrollRange * this.divCount * 100 + "px";
			if (this.scrollButton) {
				var obj = this;
				var leftButton = this.getElementById(this.containerName+'_LeftBotton');
				var rightButton = this.getElementById(this.containerName+'_RightButton');
				
				leftButton.onmousedown = function () {obj.left()};
				rightButton.onmousedown = function () {obj.right()};
				
				this.getElementById(this.containerName).style.width = this.scrollRange*this.divCount + leftButton.offsetWidth + rightButton.offsetWidth + "px";
			} else {
				this.getElementById(this.containerName).style.width = this.scrollRange*this.divCount + "px";
			}
		}
	}
	
	this.scrollLeft = function (space) {
		if (space == 0 || space == null) {
			return ;
		}
		if (this.contentContainer == null) {
			return ;
		}
		if (this.listContainer == null) {
			return ;
		}
		
		if (this.contentContainer.scrollLeft >= this.listContainer.offsetWidth) {
			this.contentContainer.scrollLeft = this.contentContainer.scrollLeft - this.listContainer.offsetWidth;
		}
		if (this.contentContainer.scrollLeft <= 0) {
			this.contentContainer.scrollLeft = this.contentContainer.scrollLeft + this.listContainer.offsetWidth;
		}
		
		var abs = Math.abs(space);
		this.scrollComp += abs;
		if (this.scrollComp > this.scrollRange) {
			if (space > 0) {
				this.contentContainer.scrollLeft += this.scrollRange%abs;
			} else {
				this.contentContainer.scrollLeft -= this.scrollRange%abs;
			}
			this.scrollLock = false;
			this.scrollComp = 0;
		} else {
			this.contentContainer.scrollLeft += space;
			this.scrollLock = true;
			
			var obj = this;
			setTimeout(function () {obj.scrollLeft(space);}, this.scrollSpeed);
		}
	}
	
	this.scrollTop = function (space) {
		if (space == 0 || space == null) {
			return ;
		}
		if (this.contentContainer == null) {
			return ;
		}
		if (this.listContainer == null) {
			return ;
		}
		
		if (this.contentContainer.scrollTop >= this.listContainer.offsetHeight) {
			this.contentContainer.scrollTop = this.contentContainer.scrollTop - this.listContainer.offsetHeight;
		}
		if (this.contentContainer.scrollTop <= 0) {
			this.contentContainer.scrollTop = this.contentContainer.scrollTop + this.listContainer.offsetHeight;
		}
		
		var abs = Math.abs(space);
		this.scrollComp += abs;
		if (this.scrollComp > this.scrollRange) {
			if (space > 0) {
				this.contentContainer.scrollTop += this.scrollRange%abs;
			} else {
				this.contentContainer.scrollTop -= this.scrollRange%abs;
			}
			this.scrollLock = false;
			this.scrollComp = 0;
		} else {
			this.contentContainer.scrollTop += space;
			this.scrollLock = true;
			
			var obj = this;
			setTimeout(function () {obj.scrollTop(space);}, this.scrollSpeed);
		}
	}

	this.left = function () {
		if (this.scrollLock) {
			return ;
		}
		this.scrollLeft(-this.scrollSpace);
	}

	this.right = function () {
		if (this.scrollLock) {
			return ;
		}
		this.scrollLeft(this.scrollSpace);
	}

	this.up = function () {
		if (this.scrollLock) {
			return ;
		}
		this.scrollTop(-this.scrollSpace);
	}

	this.down = function () {
		if (this.scrollLock) {
			return ;
		}
		this.scrollTop(this.scrollSpace);
	}
	
	this.autoScroll = function (speed) {
		if (this.divContent.length < 1) {
			return ;
		}
		var obj = this;
		setTimeout(function () {obj.autoScrollCallback();obj.autoScroll(speed);}, this.scrollSpeed * Math.ceil(this.scrollRange/this.scrollSpace) + speed * 1000);
	}
}
