var pop_rolling = function(popContainer) {
	// 시간단위는 ms로 1000이 1초
	if (popContainer.nodeType == 1) {
		this.popContainer = popContainer;
	} else {
		this.popContainer = document.getElementById(popContainer);
	}
	this.is_rolling = false;
	this.counter = 0;
	this.pop_children = null;
	this.time_dealy = 5000; //움직이는 타임딜레이
	this.time_timer = null;
	this.time_timer_pause = null;
	this.mouseover = false;
	this.init();
}

pop_rolling.prototype.init = function() {
	var pop_children = this.popContainer.childNodes;
	for (var i=(pop_children.length-1); 0<=i; i--) {
		if (pop_children[i].nodeType != 1) {
			this.popContainer.removeChild(pop_children[i]);
		}
	}

	this.pop_children = this.popContainer.childNodes;
	var oRoll = this;

	for (var i=0; i<this.pop_children.length; i++) {
		for (var k=0; k<this.pop_children[i].childNodes.length; k++) {
			if (this.pop_children[i].childNodes[k].nodeType == 1) {
				this.pop_children[i].childNodes[k].onclick = function() {
					oRoll.moveAt(this.firstChild);
					return false;
				}
				this.pop_children[i].childNodes[k].onfocus = function() {
					oRoll.moveAt(this.firstChild);
					return false;
				}
				break;	// 첫번째 링크(A)에만 이벤트 할당
			}
		}
	}
}

pop_rolling.prototype.moveAt = function(oBtn) {
	var i = oBtn.id.substring(12);
	this.mouseover = true;
	if (!this.time_timer_pause) {
		this.counter = (i-1);
		this.move_right();
		this.pause();
	}
}

pop_rolling.prototype.move_right = function() {
	var oRoll = this;
	var nTemp = 0;
	for (var i=0, m=oRoll.pop_children.length; i<m; i++) {
		nTemp = 0;
		for (var k=0; k<this.pop_children[i].childNodes.length; k++) {
			if (this.pop_children[i].childNodes[k].nodeType == 1) {
				nTemp++;
				if (nTemp == 1) {
					var child_1 = oRoll.pop_children[i].childNodes[k].childNodes[0];	//버튼이미지
					child_1.src = child_1.src.replace(/_on.gif/gi,".gif");

					if (i == oRoll.counter) {
						child_1.src = child_1.src.replace(/.gif/gi,"_on.gif");
					}
				} else {
					var child_2 = oRoll.pop_children[i].childNodes[k].childNodes[0];	//배너이미지
					child_2.style.display = "none";
					if (i == oRoll.counter) {
						child_2.style.display = "block";
					}
				}

			}
		}
	}

	oRoll.counter++;
	if (oRoll.counter >= oRoll.pop_children.length) {
		oRoll.counter = 0;
	}
}

pop_rolling.prototype.start = function() { //롤링 시작
	var oRoll = this;
	this.stop();
	this.is_rolling = true;

	var act = function() {
		if(oRoll.is_rolling){
			oRoll.move_right();
		}
	}
	if (!this.time_timer) {
		act();	// 처음 로딩시 첫번째 버튼이 즉시 선택되도록
	}
	this.time_timer = setInterval(act,this.time_dealy);
}

pop_rolling.prototype.pause = function() { //일시 멈춤
	this.is_rolling = false;
}

pop_rolling.prototype.resume = function() { //일시 멈춤 해제
	this.is_rolling = true;
}

pop_rolling.prototype.stop = function() { //롤링을 끝냄
	this.is_rolling = false;
	if (!this.time_timer) {
		clearInterval(this.time_timer);
	}
	this.time_timer = null;
}


function rollingMainBanner (id)
{
	var delay		= 2000;
	var root		= document.getElementById(id);
	var direction	= 'next';
	var pause		= false;

	if (typeof(root)!='object') { return false; }
	
	root.style.overflow	= 'hidden';
	root.style.position	= 'relative';

	try
	{
		for (var i in root.childNodes)
		{
			if (root.childNodes[i].nodeType!=1)
			{
				root.removeChild(root.childNodes[i]);
			}
		}
	}
	catch (e) {}

	var prev	= document.createElement('a');
		prev.setAttribute('href', '#' + id);
		prev.setAttribute('title', '이전');
		prev.style.position	= 'absolute';
		prev.style.top		= '20px';
		prev.style.left		= '-3px';
		prev.style.display	= 'block';
		prev.style.width	= '14px';
		prev.style.height	= '22px';
		prev.style.background	= 'url("/_UTOUR/img/common/btn_prev.gif") no-repeat';
		root.parentNode.insertBefore(prev, root);
		prev.onclick	= function ()
		{
			direction	= 'prev';
			root.insertBefore(root.lastChild, root.firstChild);
			return false;
		}
	
	var next	= document.createElement('a');
		next.setAttribute('href', '#' + id);
		next.setAttribute('title', '다음');
		next.style.position	= 'absolute';
		next.style.top		= '20px';
		next.style.right	= '3px';
		next.style.display	= 'block';
		next.style.width	= '14px';
		next.style.height	= '22px';
		next.style.background	= 'url("/_UTOUR/img/common/btn_next.gif") no-repeat';
		root.parentNode.appendChild(next);
		next.onclick	= function ()
		{
			direction	= 'next';
			root.appendChild(root.firstChild);
			return false;
		}
		
		prev.onfocus = prev.onblur = next.onfocus = next.onblur = function (event)
		{
			var event	= event ? event : window.event;
			switch (event.type)
			{
				case 'focus' : pause	= true;
								break;
				case 'blur'  : pause	= false;
								break;
			}
			
		};

	var a = root.getElementsByTagName("a");
	
	for (var i in a)
	{
		a[i].onmouseover = a[i].onmouseout = a[i].onfocus = a[i].onblur = function (event)
		{
			var event	= event ? event : window.event;

			switch (event.type)
			{
				case 'mouseover'	:
				case 'focus'		:
										pause	= true;
										break;
				case 'mouseout'		:
				case 'blur'			:
										pause	= false;
										break;
			}
		}
	}

	setInterval(function()
	{
		if (!root.style.cssText) { return false; }
		if (pause==true) { return false;}

		if (direction=='prev')
		{
			root.insertBefore(root.lastChild, root.firstChild);
		}
		else
		{
			root.appendChild(root.firstChild);
		}
	}
	);
	
}
