﻿//----------------------------------------------
// Update 2008-06-23 Menos
//----------------------------------------------
/*               [1]       [2]     [3] [4] [5][6] [7] [8]
 * new Rolling("rollup", "textup", 100, 30, 1, 2, 20, 20);
 * [1] 노출 할 위치 DIV
 * [2] 정보 가져온 Element
 * [3] Width
 * [4] Height
 * [5] Rolling Method : //0: left, 1: right, 2: up, 3: down
 * [6] 몇 pexel씩 Move할 것인가
 * [7] 개체당 다시 동작하는 시간 
 * [8] 다음 개체로 동작하는 시간 
 * 

function Rolling(canvas,cont,width,height,direction,pixelgap,timegap,idlegap)
{
	this.canvas = document.getElementById(canvas);
	this.width = width;
	this.height = height;
	this.direction = direction;	//0: left, 1: right, 2: up, 3: down
	this.pixelgap = pixelgap;
	this.timegap = timegap;
	this.idlegap = idlegap;
	this.box = document.createElement("DIV");
	this.box.style.width = this.width + "px";
	this.box.style.height = this.height + "px";
	this.box.style.overflow = "hidden";
	
	var strHtml = document.getElementById(cont).value.replace(/\n/g,"");
		strHtml = strHtml.replace(/\t/g,"");
	this.box.innerHTML = strHtml;
	
	this.setTimeOut = null;
	this.current = null;
	this.next = null;

}

Rolling.prototype =
{
	Init: function () {
		var This = this;
		this.box.onmouseover = function () { window.clearTimeout(This.setTimeOut); }
		this.box.onmouseout = function () { This.setTimeOut = window.setTimeout(function () { This.Run(); },This.timegap); }
		//this.box.onclick = function () { This.moveChange(); } ***** 해당 주석을 넣으면 클릭시 방향이 체인지 된다.
		this.canvas.appendChild(this.box);
		this.box.style.position = "absolute";
		for (var i = 0; i < this.box.childNodes.length; i++) {
			this.box.childNodes[i].style.position = "absolute";
			if (i > 0) {
				if (this.direction == 0 || this.direction == 1) {
					this.box.childNodes[i].style.left = (this.box.childNodes[i - 1].offsetLeft + this.box.childNodes[i - 1].offsetWidth) + "px";
				} else if (this.direction == 2 || this.direction == 3) {
					this.box.childNodes[i].style.top = (this.box.childNodes[i - 1].offsetTop + this.box.childNodes[i - 1].offsetHeight) + "px";
				}
			}
			if (i == this.box.childNodes.length - 1) {
				if (this.direction == 1) {
					this.box.childNodes[i].style.left = -this.box.childNodes[i].offsetWidth + "px";
				} else if (this.direction == 3) {
					this.box.childNodes[i].style.top = -this.box.childNodes[i].offsetHeight + "px";
				}
			}
		}
	},
	Run: function () {
		if (this.box.hasChildNodes() == false) { return; }
		if (this.direction == 0) { this.moveLeft(); }
		if (this.direction == 1) { this.moveRight(); }
		if (this.direction == 2) { this.moveUp(); }
		if (this.direction == 3) { this.moveDown(); }
	},
	moveLeft: function () {
		var This = this;
		if (this.current == null) { this.current = 0; }
		window.clearTimeout(this.setTimeOut);
		for (var i = 0; i < this.box.childNodes.length; i++) {
			this.box.childNodes[i].style.left = (this.box.childNodes[i].offsetLeft - this.pixelgap) + "px";
		}
		this.next = (this.current + 1 >= this.box.childNodes.length) ? 0 : this.current + 1;
		if (this.box.childNodes[this.next].offsetLeft <= 0) {
			var nextPosition = 0;
			for (var i = 1; i < this.box.childNodes.length; i++) { nextPosition += this.box.childNodes[i].offsetWidth; }
			this.box.childNodes[this.current].style.left = nextPosition + "px";
			this.current = (this.current + 1 >= this.box.childNodes.length) ? 0 : this.current + 1;
			this.setTimeOut = window.setTimeout(function () { This.Run(); },this.idlegap);
		} else {
			this.setTimeOut = window.setTimeout(function () { This.Run(); },this.timegap);
		}
	},
	moveRight: function () {
		var This = this;
		if (this.current == null) { this.current = this.box.childNodes.length - 1; }
		window.clearTimeout(this.setTimeOut);
		for (var i = 0; i < this.box.childNodes.length; i++) {
			this.box.childNodes[i].style.left = (this.box.childNodes[i].offsetLeft + this.pixelgap) + "px";
		}
		this.next = (this.current - 1 < 0) ? this.box.childNodes.length - 1 : this.current - 1;
		if (this.box.childNodes[this.current].offsetLeft >= 0) {
			this.box.childNodes[this.next].style.left = -this.box.childNodes[this.next].offsetWidth + "px";
			this.current = (this.current - 1 < 0) ? this.box.childNodes.length - 1 : this.current - 1;
			this.setTimeOut = window.setTimeout(function () { This.Run(); },this.idlegap);
		} else {
			this.setTimeOut = window.setTimeout(function () { This.Run(); },this.timegap);
		}
	},
	moveUp: function () {
		var This = this;
		if (this.current == null) { this.current = 0; }
		window.clearTimeout(this.setTimeOut);
		for (var i = 0; i < this.box.childNodes.length; i++) {
			this.box.childNodes[i].style.top = (this.box.childNodes[i].offsetTop - this.pixelgap) + "px";
		}
		this.next = (this.current + 1 >= this.box.childNodes.length) ? 0 : this.current + 1;
		if (this.box.childNodes[this.next].offsetTop <= 0) {
			var nextPosition = 0;
			for (var i = 1; i < this.box.childNodes.length; i++) { nextPosition += this.box.childNodes[i].offsetHeight; }
			this.box.childNodes[this.current].style.top = nextPosition + "px";
			this.current = (this.current + 1 >= this.box.childNodes.length) ? 0 : this.current + 1;
			this.setTimeOut = window.setTimeout(function () { This.Run(); },this.idlegap);
		} else {
			this.setTimeOut = window.setTimeout(function () { This.Run(); },this.timegap);
		}
	},
	moveDown: function () {
		var This = this;
		if (this.current == null) { this.current = this.box.childNodes.length - 1; }
		window.clearTimeout(this.setTimeOut);
		for (var i = 0; i < this.box.childNodes.length; i++) {
			this.box.childNodes[i].style.top = (this.box.childNodes[i].offsetTop + this.pixelgap) + "px";
		}
		this.next = (this.current - 1 < 0) ? this.box.childNodes.length - 1 : this.current - 1;
		if (this.box.childNodes[this.current].offsetTop >= 0) {
			this.box.childNodes[this.next].style.top = -this.box.childNodes[this.next].offsetHeight + "px";
			this.current = (this.current - 1 < 0) ? this.box.childNodes.length - 1 : this.current - 1;
			this.setTimeOut = window.setTimeout(function () { This.Run(); },this.idlegap);
		} else {
			this.setTimeOut = window.setTimeout(function () { This.Run(); },this.timegap);
		}
	}, 
	
	moveChange: function () {
		var This = this;
		if(this.direction == "0") {
			this.direction = 1;
		}
		else if(this.direction == "1") {
			this.direction = 0;
		}
		else if(this.direction == "2") {
			this.direction = 3;
		}
		else if(this.direction == "3") {
			this.direction = 2;
		}
		This.Run();
	}
	
}
 */
 
 
 
 
 
/*
	이미지 롤링 클래스
	option Description
		target	: 타겟 DIV
		delay	: 딜레이 시간
		direct	: 롤링 방향 ( S : 정지, L : 왼쪽, R : 오른쪽, U : 위로, D : 아래로 )
*/
function RScrolling(target, delay, direct, num) {
	
	this.targetDiv = document.getElementById(target); // 타겟 DIV
	this.delay = delay; // 지연시간 ( 1000 == 1sec )
	this.direct = direct // 방향 ( 

    this.viewNum = num; // 뷰 갯수
	this.imgObj = new Array(); // 이미지 배열
	this.nowIdx = 0; // 배열위치
	this.timer = null;
	this.targetDiv.isover = false;
	
	this.targetDiv.onmouseover = function() { this.isover=true; }
	this.targetDiv.onmouseout = function() { this.isover=false; }


}
RScrolling.prototype = {
	/*
		이미지 추가 함수
		option Description
			rimg	: 롤링 이미지
			url		: 배너 클릭시 이동 페이지 URL
			vimg	: 클릭시 뷰이미지
	*/
	Add:function(rimg, vimg, url) {
		var self = this;
		var imgInfo = "<span style='float:left;padding-left:5px;display:block'><a href=\""+ url +"\">"+ rimg +"</a></span>"

		this.imgObj.push(imgInfo);

	},
	
	Print:function() {
		var self = this;
		var print = "";

        if(!this.targetDiv.isover) {
		    for(var i=0; i<this.imgObj.length; i++) {
			    if(i==0) {
				    this.imgObj[i] = this.imgObj[i].replace("margin:0px;", "margin:0px;");
				}
			    else
				    this.imgObj[i] = this.imgObj[i].replace("margin:0px;", "margin:0px;");

                if(i<this.viewNum ) {
                    this.imgObj[i] = this.imgObj[i].replace("display:none", "display:block");
                }
                else {
                    this.imgObj[i] = this.imgObj[i].replace("display:block", "display:none");
                }

                
			    print += this.imgObj[i]; 
		    }
		    //alert(print);
		    this.targetDiv.innerHTML = print;
		}
		this.timer = window.setTimeout(function () { self.Exec(); }, this.delay);
	},
	
	Exec:function() {
	    
		var self = this;
		clearTimeout(this.timer);

		if(this.direct == "S") { self.MoveStop(); }
		if(this.direct == "L") { self.MoveLeft(); }
		if(this.direct == "R") { self.MoveRight(); }
		if(this.direct == "U") { self.MoveUp(); }
		if(this.direct == "D") { self.MoveDown(); }
	},
	
	MoveStop:function() {
		var self = this;
	},
	
	MoveLeft:function() {
		var self = this;
		
		if(!this.targetDiv.isover) {
		    //alert(this.isover);
		    if(this.nowIdx >= (this.nowIdx-1))
			    this.nowIdx = 0;
		    else
			    this.nowIdx++;

		    var tmp = "";
		    tmp = this.imgObj[0];

		    for(var i=1; i<this.imgObj.length; i++) 
			    this.imgObj[i-1] = self.imgObj[i];

		    this.imgObj[self.imgObj.length-1] = tmp;
        }
		self.Print();
	},
	
	MoveRight:function() {
		var self = this;
	},

	MoveUp:function() {
		var self = this;
	},
	
	MoveDown:function() {
		var self = this;
	},
	
	ClickPrint:function() {
		var self = this;
		var print = "";

        if(this.targetDiv.isover) {
		    for(var i=0; i<this.imgObj.length; i++) {
			    if(i==0) {
				    this.imgObj[i] = this.imgObj[i].replace("margin:0px;", "margin:0px;");
				}
			    else
				    this.imgObj[i] = this.imgObj[i].replace("margin:0px;", "margin:0px;");

                if(i<this.viewNum ) {
                    this.imgObj[i] = this.imgObj[i].replace("display:none", "display:block");
                }
                else {
                    this.imgObj[i] = this.imgObj[i].replace("display:block", "display:none");
                }

			    print += this.imgObj[i]; 
		    }
		    //alert(print);
		    this.targetDiv.innerHTML = print;
		}
	},
	
	MoveNext:function() {
		var self = this;
		
		
		if(this.targetDiv.isover) {
    		//alert(this.targetDiv.isover);
		    var tmp = "";
		    tmp = this.imgObj[this.imgObj.length-1];

		    for(var i=this.imgObj.length-2; i>=0; i--) 
			    this.imgObj[i+1] = self.imgObj[i];

		    this.imgObj[0] = tmp;
        }
		self.ClickPrint();
	},
	
	MovePrev:function() {
		var self = this;
		
		if(this.targetDiv.isover) {
		    //alert(this.isover);
		    if(this.nowIdx >= (this.nowIdx-1))
			    this.nowIdx = 0;
		    else
			    this.nowIdx++;

		    var tmp = "";
		    tmp = this.imgObj[0];

		    for(var i=1; i<this.imgObj.length; i++) 
			    this.imgObj[i-1] = self.imgObj[i];

		    this.imgObj[self.imgObj.length-1] = tmp;
        }
		self.ClickPrint();
		 
	},
	
	Over:function() {
	    this.targetDiv.isover = true;
	}, 
	Out: function() {
	    this.targetDiv.isover = false;
	}

}



/*
    DIV 롤링 클래스
*/
function DivRScrolling(target, delay, direct) {
	
	this.targetDiv = document.getElementById(target); // 타겟 DIV
	this.delay = delay; // 지연시간 ( 1000 == 1sec )
	this.direct = direct // 방향 ( 

	this.divObj = new Array(); // DIV 배열
	this.nowIdx = 0; // 배열위치
	this.timer = null;
	this.targetDiv.isover = false;
	
	this.targetDiv.onmouseover = function() { this.isover=true; }
	this.targetDiv.onmouseout = function() { this.isover=false; }


}
DivRScrolling.prototype = {
	Add:function(divnm) {
		var self = this;
		
		this.divObj.push(divnm);
	},
	
	Print:function() {
		var self = this;
		var print = "";

        if(!this.targetDiv.isover) {
		    for(var i=0; i<this.divObj.length; i++) {
			    if(i==0 )
				    document.getElementById(this.divObj[i]).style.display = "";
			    else
				    document.getElementById(this.divObj[i]).style.display = "none";
		    }
		}
		this.timer = window.setTimeout(function () { self.Exec(); }, this.delay);
	},
	
	Exec:function() {
    
		var self = this;
		clearTimeout(this.timer);

		if(this.direct == "S") { self.MoveStop(); }
		if(this.direct == "L") { self.MoveLeft(); }
		if(this.direct == "R") { self.MoveRight(); }
		if(this.direct == "U") { self.MoveUp(); }
		if(this.direct == "D") { self.MoveDown(); }
	},
	
	MoveStop:function() {
		var self = this;
	},
	
	MoveLeft:function() {
		var self = this;
		
		if(!this.targetDiv.isover) {
		    //alert(this.isover);
		    if(this.nowIdx >= (this.nowIdx-1))
			    this.nowIdx = 0;
		    else
			    this.nowIdx++;

		    var tmp = "";
		    tmp = this.divObj[0];

		    for(var i=1; i<this.divObj.length; i++) 
			    this.divObj[i-1] = self.divObj[i];

		    this.divObj[self.divObj.length-1] = tmp;
        }
		self.Print();
	},
	
	MoveRight:function() {
		var self = this;
	},

	MoveUp:function() {
		var self = this;
	},
	
	MoveDown:function() {
		var self = this;
	},
	
	ClickPrint:function() {
		var self = this;
		var print = "";

        if(!this.targetDiv.isover) {
		    for(var i=0; i<this.divObj.length; i++) {
			    if(i==0 )
				    document.getElementById(this.divObj[i]).style.display = "";
			    else
				    document.getElementById(this.divObj[i]).style.display = "none";
		    }
		}
	},
	
	MoveNext:function() {

		var self = this;
	    var tmp = "";
	    this.targetDiv.isover = true;
	    tmp = this.divObj[0];

	    for(var i=1; i<this.divObj.length; i++) 
		    this.divObj[i-1] = self.divObj[i];

	    this.divObj[self.divObj.length-1] = tmp;
	    this.targetDiv.isover = false;
		self.ClickPrint();
	},
	
	MovePrev:function() {

		var self = this;
	    var tmp = "";
	    this.targetDiv.isover = true;
	    tmp = this.divObj[self.divObj.length-1];
	    

	    for(var i=this.divObj.length-2; i>=0; i--) 
		    this.divObj[i+1] = self.divObj[i];

	    this.divObj[0] = tmp;
	    this.targetDiv.isover = false;
		self.ClickPrint();
		 
	}, 
	
	Over:function() {
	    this.targetDiv.isover = true;
	}, 
	Out: function() {
	    this.targetDiv.isover = false;
	}

}




/*
    DIV 롤링 옵션추가 클래스

*/

function DivOptRScrolling(target, delay, direct, num, width) {
	
	this.targetDiv = document.getElementById(target); // 타겟 DIV
	this.delay = delay; // 지연시간 ( 1000 == 1sec )
	this.direct = direct // 방향 ( 
	this.viewNum = num; // 뷰 갯수
	this.width = width; // 각 아이템 넓이

	this.divObj = new Array(); // DIV 배열
	this.nowIdx = 0; // 배열위치
	this.timer = null;
	this.targetDiv.isover = false;
	
	this.targetDiv.onmouseover = function() { this.isover=true; }
	this.targetDiv.onmouseout = function() { this.isover=false; }


}
DivOptRScrolling.prototype = {
	Add:function(divnm) {
		var self = this;
		
		this.divObj.push(divnm);
	},
	
	Print:function() {
		var self = this;
		var print = "";

        if(!this.targetDiv.isover) {
		    for(var i=0; i<this.divObj.length; i++) {
			    if(i<this.viewNum ) {
				    document.getElementById(this.divObj[i]).style.display = "";
				    document.getElementById(this.divObj[i]).style.width = this.width+"px";
				}
			    else {
				    document.getElementById(this.divObj[i]).style.display = "none";
				    document.getElementById(this.divObj[i]).style.width = "0px";
			    }
		    }
		}
		this.timer = window.setTimeout(function () { self.Exec(); }, this.delay);
	},
	
	Exec:function() {
    
		var self = this;
		clearTimeout(this.timer);

		if(this.direct == "S") { self.MoveStop(); }
		if(this.direct == "L") { self.MoveLeft(); }
		if(this.direct == "R") { self.MoveRight(); }
		if(this.direct == "U") { self.MoveUp(); }
		if(this.direct == "D") { self.MoveDown(); }
	},
	
	MoveStop:function() {
		var self = this;
	},
	
	MoveLeft:function() {
		var self = this;
		
		if(!this.targetDiv.isover) {
		    //alert(this.isover);
		    if(this.nowIdx >= (this.nowIdx-1))
			    this.nowIdx = 0;
		    else
			    this.nowIdx++;

		    var tmp = "";
		    tmp = this.divObj[0];

		    for(var i=1; i<this.divObj.length; i++) 
			    this.divObj[i-1] = self.divObj[i];

		    this.divObj[self.divObj.length-1] = tmp;
        }
		self.Print();
	},
	
	MoveRight:function() {
		var self = this;
	},

	MoveUp:function() {
		var self = this;
	}, 
	
	MoveDown:function() {
		var self = this;
	},
	
	ClickPrint:function() {
		var self = this;
		var print = "";

        if(!this.targetDiv.isover) {
		    for(var i=0; i<this.divObj.length; i++) {
			    if(i<this.viewNum ) {
				    document.getElementById(this.divObj[i]).style.display = "";
				    document.getElementById(this.divObj[i]).style.width = this.width+"px";
				}
			    else {
				    document.getElementById(this.divObj[i]).style.display = "none";
				    document.getElementById(this.divObj[i]).style.width = "0px";
			    }
		    }
		}
	},
	
	MoveNext:function() {

		var self = this;
	    var tmp = "";
	    this.targetDiv.isover = true;
	    tmp = this.divObj[0];

	    for(var i=1; i<this.divObj.length; i++) 
		    this.divObj[i-1] = self.divObj[i];

	    this.divObj[self.divObj.length-1] = tmp;
	    this.targetDiv.isover = false;
		self.ClickPrint();
	},
	
	MovePrev:function() {

		var self = this;
	    var tmp = "";
	    this.targetDiv.isover = true;
	    tmp = this.divObj[self.divObj.length-1];
	    

	    for(var i=this.divObj.length-2; i>=0; i--) 
		    this.divObj[i+1] = self.divObj[i];

	    this.divObj[0] = tmp;
	    this.targetDiv.isover = false;
		self.ClickPrint();
		 
	}

}