﻿function DivMove(_n){
	if(_n == null || typeof(_n) != "string"){
		alert("变量名无效!");
		return false;
	}

	this._mar = null;
	this._speed = 30;
	this._direction = "up";
	this._framediv = null;
	this._indiv1 = null;
	this._indiv2 = null;
	this._name = _n;
	this._content = "";
	this._width = 88;
	this._height = 200;	
	this.init = function(){
		var show_html = '';
		switch(this._direction){
			case "left":
			case "right":
				show_html = '<div id="'+this._name+ '_frame" style="overflow:hidden;width:'+this._width+'px;height:'+this._height+'px;" onMouseOver="clearInterval('+this._name+'._mar);" onMouseOut="'+this._name+'._mar = setInterval(\''+this._name+'.scrollback()\','+this._name+'._speed);">';
				show_html += '<table  border="0" cellspacing="0" cellpadding="0">';
				show_html += '  <tr>';
				show_html += '    <td id="'+ this._name+ '_in1">'+this._content+'</td>';
				show_html += '    <td id="'+ this._name+ '_in2"></td>';
				show_html += '  </tr>';
				show_html += '</table>';
				show_html += '</div>';
				break;
			case "down":
			default:
				show_html = '<div id="'+this._name + '_frame" style="overflow:hidden;width:'+this._width+ 'px;height:'+this._height+ 'px;" onMouseOver="clearInterval('+this._name+'._mar);" onMouseOut="'+this._name+'._mar = setInterval(\''+this._name+'.scrollback()\','+this._name+'._speed);">';
				show_html += '    <div id="'+ this._name+ '_in1">'+this._content+ '</div>';
				show_html += '    <div id="'+ this._name+ '_in2"></div>';
				show_html += '</div>';
				break;
		}
		document.write(show_html);
		this._framediv = eval(this._name+"_frame");
		this._indiv1 = eval(this._name+"_in1");
		this._indiv2 = eval(this._name+"_in2");
		if(this._direction == "right"){
			this._framediv.scrollLeft=this._framediv.scrollWidth;
		}
		if(this._direction == "down"){
			this._framediv.scrollTop=this._framediv.scrollHeight;
		}
		this._indiv2.innerHTML = this._indiv1.innerHTML; 
		this._mar = setInterval(this._name+".scrollback()",this._speed);
	}

	this.setContent = function(_str){
		if(_str == null || typeof(_str) != "string"){
			alert("内容必须是脚本文本!");
			return false;
		}
		this._content = _str;
	}


	this.setDirection = function(_dir){
		if(_dir != null && typeof(_dir) == "string"){
			this._direction = _dir.toLowerCase();
		}
	}

	this.setSpeed = function(_num){
		if(_num != null && typeof(_num) == "number"){
			_num = Math.abs(_num);
			if(_num != 0){
				this._speed = _num;
			}
		}
	}

	this.setWidth = function(_num){
		if(_num != null && typeof(_num) == "number"){
			_num = Math.abs(_num);
			if(_num != 0){
				this._width = _num;
			}
		}
	}

	this.setHeight = function(_num){
		if(_num != null && typeof(_num) == "number"){
			_num = Math.abs(_num);
			if(_num != 0){
				this._height = _num;
			}
		}
	}

	this.scrollback = function(){
		switch(this._direction){
			case "left":
				if(this._indiv2.offsetWidth <= this._framediv.scrollLeft){
					this._framediv.scrollLeft -= this._indiv1.offsetWidth;
				}else{
					this._framediv.scrollLeft++;
				}
				break;
			case "right":
				if(this._indiv1.offsetWidth >= this._framediv.scrollLeft){
					this._framediv.scrollLeft += this._indiv2.offsetWidth;
				}else{
					this._framediv.scrollLeft--;
				}
				break;
			case "down":
				if(this._indiv1.offsetTop >= this._framediv.scrollTop){
					this._framediv.scrollTop += this._indiv2.offsetHeight;
				}else{
					this._framediv.scrollTop--;
				}
				break;
			default:
				if(this._indiv2.offsetTop <= this._framediv.scrollTop){
					this._framediv.scrollTop -= this._indiv1.offsetHeight;
				}else{
					this._framediv.scrollTop++;
				}
				break;
		}
	}
}