// ±âÃÊ ¼±¾ð
function addEvent(target, en, func)
{
	if (window.addEventListener) 
	{
//		if ((target == window) && (en.toLowerCase() == "load"))
		if ((target == window) && (en.toLowerCase() == "load") && !window.opera && !(!document.all && document.childNodes && !navigator.taintEnabled))
			document.addEventListener("DOMContentLoaded", func, false);
		else	
	        target.addEventListener(en, func, false);
    } 
    else if (target.attachEvent) 
	{
		if ((target == window) && (en.toLowerCase() == "load"))
	    	document.attachEvent("onreadystatechange", function(e) { if (document.readyState == "complete") func(); });
		else
	        target.attachEvent("on"+en, func);
    } 
	else 
	{
        var __func = eval("target.on"+en);

        eval("target.on"+en) = function() 
		{
            func();
            __func();
        };
    }
};

var rolling = function(thisName, objid, max, delay, func)
{
	this.name = thisName;
	this.objBaseId = objid;
	this.max = max;
	this.nowIdx = 0;
	this.delay = delay;
	this.sto = null;
	this.rollPause = false;
	this.func = null;

	if (typeof(func) != "undefined")
		this.func = func;
};

rolling.prototype.start = function()
{
	var objid = null;
	var s = typeof this.objBaseId;
	var max = -1;

	if (s === 'object')
		if (this.objBaseId)
			if (this.objBaseId instanceof Array)
				s = 'array';

	if (s == "array")
	{
		for(var x=0, cnt=this.objBaseId.length;x<cnt;x++)
		{
			objid = this.objBaseId[x];
			for(var i=0;i<this.max;i++)
			{
				if (document.getElementById(objid+i) == null)
				{
					if (max < i) max = i;
					break;
				}
			}
		}
	}
	else
	{
		objid = this.objBaseId;
		for(var i=0;i<this.max;i++)
		{
			if (document.getElementById(objid+i) == null)
			{
				max = i;
				break;
			}
		}
	}
	
	if (max >= 0)
		this.max = max;

	this.sto = setTimeout(this.name + ".showByDir()", this.delay);
};

rolling.prototype.show = function(idx)
{
	var obj = null;
	var objid = null;
	var s = typeof this.objBaseId;
	
	if (s === 'object') 
		if (this.objBaseId) 
			if (this.objBaseId instanceof Array) 
				s = 'array';            

	if (s == "array")
	{
		for(var x=0, cnt=this.objBaseId.length;x<cnt;x++)
		{
			objid = this.objBaseId[x];
			for(var i=0;i<this.max;i++)
			{   
				obj = document.getElementById(objid+i);

				if (obj == null)
					continue;

				obj.style.display = (i != idx) ? "none" : "block";
			}
		}
	}
	else
	{
		objid = this.objBaseId;
		for(var i=0;i<this.max;i++)
		{   
			obj = document.getElementById(objid+i);

			if (obj == null)
				continue;

			obj.style.display = (i != idx) ? "none" : "block";
		}
	}
	
	if (this.func != null)
	{
		eval(this.func + "('" + idx + "', '" + this.max + "');");
	}

	return true;
};

rolling.prototype.pause = function()
{
	this.rollPause = true;

	if (this.sto != null)
		clearTimeout(this.sto);
};

rolling.prototype.play = function()
{
	this.rollPause = false;

	if (this.sto != null)
		clearTimeout(this.sto);

	this.sto = setTimeout(this.name + ".showByDir()", this.delay);
};

rolling.prototype.showByDir = function(dir)
{
	if (!this.rollPause)
	{
		if ((dir == null) || (typeof(dir) == "undefined"))    // ÀÚµ¿ ½ºÅ©·ÑÀÎ °æ¿ì
		{
			dir = 1;
		}

		if (this.sto != null)
			clearTimeout(this.sto);
		
		this.nowIdx = (this.nowIdx + dir) % this.max;

		if (this.nowIdx < 0)
			this.nowIdx = parseInt(this.max) + parseInt(this.nowIdx);

		this.show(this.nowIdx);
	}

	this.sto = setTimeout(this.name + ".showByDir()", this.delay);
};
