/*
 * Banner Scroller
 * Copyright (C) 1998-2007 Geonjisoft Corporation. All Rights Reserved.
 *
 * Usage)
 * var myBannerScroller = new XBannerScroller(document.getElementById("layers"));
 * myBannerScroller.start();
 */

var XBannerScroller = function(ta, height)
{
	this.ta = ta;
	this.ta_id = 'roll_' + (this.ta.id || this.ta.name);
	this.gap = 1;            // 움직이는 픽셀단위
	this.gap_count = 0;      // 카운팅용:건들지 마세요
	this.gap_time = '1';  // 움직이는 단위시간
	this.gap_sleep = '3000'; // 화면이 멈춰있을 단위시간
	this.over_stop = true;   // 마우스를 올렸을 때 멈출 것인가?
	this.timer = null;
	this.divHeight = height;

	eval(this.ta_id + '=this');
	var temp = eval(this.ta_id);
	this.init_div();
}

XBannerScroller.prototype.start = function()
{
	this.ta.readonly = false;
	this.stop = false;	
	if (!this.timer)
	{
	  this.rolling();
	}
}

XBannerScroller.prototype.stop = function()
{
	this.stop = true;
}

XBannerScroller.prototype.init_div = function()
{
	//this.ta.style.position = "static";
	this.ta.style.overflow = "hidden";
	this.ta.onmouseover = function() { eval("this.readOnly = true;"); }
	this.ta.onmouseout = function() { eval("this.readOnly = false;"); }

	var child = this.ta.childNodes;
	var ch = this.ta.firstChild;
	var ch2 = null;

	while(ch)
	{
		ch2 = ch.nextSibling;
		if (ch.nodeName.toLowerCase() != 'div')
		{
			this.ta.removeChild(ch);
		}
		else
		{
  		ch.style.position = "relative";
  		ch.style.borderStyle = 'none';
  		ch.style.height = this.divHeight;
		}
		ch=ch2;
	}
}

XBannerScroller.prototype.strtonum = function(str)
{
	var num = parseInt(str);
	if(isNaN(num)) num = '0';
	return num;
}

XBannerScroller.prototype.strtopx = function(str)
{
	var num = this.strtonum(str);
	return num + 'px';
}

XBannerScroller.prototype.rolling = function()
{
	if (this.gap_count == 0)
	{
		this.sleep();
		this.gap_count += 1;
		return;
	}
	if (!this.ta.readOnly && !this.stop)
	{
		this.rolling_top();
	}

	this.timer = null;
	var re = this.ta_id + '.rolling()';
	this.timer = setTimeout(re, this.gap_time);
}

XBannerScroller.prototype.rolling_top = function()
{
	this.gap_count += parseInt(this.gap);
	var ch1 = this.ta.firstChild;
	var child = this.ta.childNodes;
	var ta_ch = null;
	
	if (child.length == 0)
		return;
	
	var	top_ori = this.strtonum(child[0].style.top);
	var top = this.strtopx(top_ori - parseInt(this.gap));		

	for (var i = 0, m = child.length; i < m; i++)
	{
		child[i].style.top = top;
	}
	if (this.gap_count >= this.strtonum(ch1.style.height))
	{

		var temp = ch1.cloneNode(true);	
		this.ta.removeChild(ch1);
		this.ta.appendChild(temp);

		for (var i = 0, m = child.length; i < m; i++)
		{
			child[i].style.top = '0px';
		}
		
	  this.gap_count = 0		
	  
	}
	
}

XBannerScroller.prototype.sleep = function()
{
	this.timer = null;
	var re = this.ta_id + '.rolling()';
	this.timer = setTimeout(re, this.gap_sleep);
}
