/*
 * 2005-11-01
 * Edit 2005-11-17
 * Edit 2005-12-13
 * Edit 2006-01-06
 * var roll = new ERoll('roll','<img src="0708_06_03.gif" width="107" height="50">',107, 51, "left");
 * roll.write("t");
 * version 1.1 2006-03-22 Ôö¼Ómove(direction)·½·¨
 */

var ERoll = function (rollId, content, width, height, direction, speed, mouseover)
{
  this.rollId = rollId;
	this.content = content;
	this.width = width ? width: 0;
	this.height = height ? height : 0;
	this.direction = direction ? direction : "up";
	this.speed = speed ? speed : 30;
	this.step = 1;
	this.firstSpacer = 0;
	this.spacer = 0;
	this.spacerSpeed = 2500;
	this.tempSpacer = 0;
	this.spacerTimer = false;
	this.isFirstSpacer = true;
	this.isFirstPause = this.spacer ? true : false;
	this.mouseover = (mouseover != null) ? mouseover : true;
	
	var demoId = this.rollId + "demo";
	var demoId1 = this.rollId + "demo1";
	var demoId2 = this.rollId + "demo2";
	
	var demo = new Object();
	var demo1 = new Object();
	var demo2 = new Object();
	
	this.write = function(obj)
	{
		if (obj)
		{
		  document.getElementById(obj).innerHTML = this.rollHTML();
		}
		else
		{
		  document.write(this.rollHTML());
		}
	  var hasObject = false;
		
		if (this.direction == 'up' || this.direction == 'down' || this.direction == 'left' || this.direction == 'right')
		{
			demo = document.getElementById(demoId);
			demo1 = document.getElementById(demoId1);
			demo2 = document.getElementById(demoId2);
			demo2.innerHTML = demo1.innerHTML;
			hasObject = true;
		}
		
		hasObject ? this.move() : "";
	}
	
	this.rollHTML = function()
	{
	  var rst = '';
		rst = '<div id="' + demoId + '" style="overflow:hidden; width:' + this.width + 'px; height:' + this.height + 'px;" ';
		if (this.mouseover)
		{
			rst += 'onmouseover="' + this.rollId + '.stop()" onmouseout="' + this.rollId + '.move()"';
		}
		rst += '>';
		
		if (this.direction == 'up' || this.direction == 'down')
		{
			rst += '<div id="' + demoId1 + '">' + this.content + '</div><div id="' + demoId2 + '"></div>';
		}
		else if (this.direction == 'left' || this.direction == 'right')
		{
		  rst += '<table cellpadding="0" cellspace="0" border="0"><tr><td id="' + demoId1 + '"><table width="' + this.width + '" border="0" align="center" cellpadding="0" cellspacing="3"><tr><td nowrap="nowrap">' + this.content + '</td></tr></table></td><td id="' + demoId2 + '"></td></tr></table>';
		}
		
		rst += '</div>';
		return rst;
	}
	
	this.rollTime;
	this.move = function()
	{
		if (arguments[0])
		{
			this.stop();
			this.direction = arguments[0];
		}
		this.rollTime = setInterval(this.rollId + '.' + this.direction + '()', this.speed);
	}
	
	this.stop = function()
	{
		clearInterval(this.rollTime);
	}
	
	this.moveSpacer = function()
	{
		if (this.isFirstPause || (this.spacer > 0 && this.tempSpacer >= (this.isFirstSpacer ? (this.spacer + this.firstSpacer) : this.spacer)))
		{
			this.tempSpacer = 0;
			this.stop();
			this.rollTime = setInterval(this.rollId + '.' + this.direction + '()', this.spacerSpeed);
			this.spacerTimer = true;
		  this.isFirstSpacer = this.isFirstPause ? true : false;
			this.isFirstPause = false;
			return;
		}
		if (this.spacerTimer)
		{
			this.stop();
			this.rollTime = setInterval(this.rollId + '.' + this.direction + '()', this.speed);
			this.spacerTimer = false;
		}		
	}
	
	this.moveOver = function()
	{
		this.tempSpacer = 0;
		this.isFirstSpacer = true;
	}
	
	this.moving = function()
	{
		this.tempSpacer += this.step;
	}
	
	this.up = function()
	{
		this.moveSpacer();
		if (demo2.offsetHeight <= demo.scrollTop)
		{
		  demo.scrollTop = demo.scrollTop - demo1.offsetHeight;
			this.moveOver();
		}
		else
		{
		  demo.scrollTop = demo.scrollTop + this.step;
			this.moving();
		}
	}
	
	this.down = function()
	{
		this.moveSpacer();
		if (demo.scrollTop == 0)
		{
		  demo.scrollTop = demo.scrollTop + demo2.offsetHeight;
			this.moveOver();
		}
		else
		{
		  demo.scrollTop = demo.scrollTop - this.step;
			this.moving();
		}
	}
	
	this.left = function()
	{
		this.moveSpacer();
		if (demo2.offsetWidth - demo.scrollLeft <= 0)
		{
		  demo.scrollLeft -= demo1.offsetWidth;
			this.moveOver();
		}
		else
		{
		   demo.scrollLeft = demo.scrollLeft + this.step;
			 this.moving();
		}
  }
	
	this.right = function()
	{
	  this.moveSpacer();
		if (demo.scrollLeft <= 0)
		{
		  demo.scrollLeft += demo2.offsetWidth;
			this.moveOver();
		}
		else
		{
		  demo.scrollLeft = demo.scrollLeft - this.step;
			this.moving();
		}
  }

}
