(function($,window,undefined) {
	BannerMessage = function(id, dir, article) {
		this.id = id;
		this.dir = dir;
		this.article = article;
		
		this.mouse = true;
		this.status = true;
		this.direction = "";
				
		this.interval;
		this.increment;
		this.pause;
		this.bannerColor;
		this.bannerWidth;
		this.bannerHeight;
		this.bannerLeft;
		this.bannerTop;
	    
	    this.setTimeOut = null;
		this.current = null;
		this.next = null;
	};
	
	BannerMessage.prototype.initValue = function( interval, increment, pause, bannerColor, bannerWidth, bannerHeight, bannerLeft, bannerTop ) {		
		this.interval = interval;
		this.increment = increment;
		this.pause = pause;
		this.bannerColor = bannerColor;
		this.bannerWidth = bannerWidth;
		this.bannerHeight = bannerHeight;
		this.bannerLeft = bannerLeft;
		this.bannerTop = bannerTop;
	};
	
	BannerMessage.prototype.setMouseValue = function( mouse ) {
		this.mouse = mouse;
	};
	
	BannerMessage.prototype.setDirection = function( direction ) {
		this.direction = direction;
	};
	
	BannerMessage.prototype.Run = function () {
		if (this.direction == "" ) { return; }
		if (this.direction == "left") { this.moveLeft(); }
		if (this.direction == "right") { this.moveRight(); }
		if (this.direction == "up") { this.moveUp(); }
		if (this.direction == "down") { this.moveDown(); }
	};
	
	BannerMessage.prototype.moveUp = function() {
		if ( this.mouse && this.status ) {
			var This = this;
			if (this.current == null) { this.current = 0; }
			window.clearTimeout(this.setTimeOut);
			for (var i = 0; i < this.article.length; i++) {
				document.getElementById(this.id+"_message" + i).style.top = (document.getElementById(this.id+"_message" + i).offsetTop - this.increment) + "px";
			}
			this.next = (this.current + 1 >= this.article.length) ? 0 : this.current + 1;			
			if (document.getElementById(this.id+"_message" + this.next).offsetTop <= 0) {
				var nextPosition = 0;
				for (var i = 0; i < this.article.length; i++) { 
					if ( i != this.current ) {
						nextPosition += document.getElementById(this.id+"_message" + i).offsetHeight; 
					}
				}
				document.getElementById(this.id+"_message" + this.current).style.top = nextPosition + "px";
				this.current = (this.current + 1 >= this.article.length) ? 0 : this.current + 1;
				this.setTimeOut = window.setInterval(function () { This.Run(); },this.pause);
			} else {
				this.setTimeOut = window.setInterval(function () { This.Run(); },this.interval);
			}
		}
	};
	
	BannerMessage.prototype.moveDown = function () {
		if ( this.mouse && this.status ) {
			var This = this;
			if (this.current == null) { this.current = this.article.length - 1; }
			window.clearTimeout(this.setTimeOut);
			for (var i = 0; i < this.article.length; i++) {
				document.getElementById(this.id+"_message" + i).style.top = (document.getElementById(this.id+"_message" + i).offsetTop + this.increment) + "px";
			}
			this.next = (this.current - 1 < 0) ? this.article.length - 1 : this.current - 1;
			if (document.getElementById(this.id+"_message" + this.current).offsetTop >= 0) {
				document.getElementById(this.id+"_message" + this.next).style.top = -document.getElementById(this.id+"_message" + this.next).offsetHeight + "px";
				this.current = (this.current - 1 < 0) ? this.article.length - 1 : this.current - 1;
				this.setTimeOut = window.setInterval(function () { This.Run(); },this.pause);
			} else {
				this.setTimeOut = window.setInterval(function () { This.Run(); },this.interval);
			}
		}
	};
	
	BannerMessage.prototype.moveLeft = function () {
		if ( this.mouse && this.status ) {
			var This = this;
			if (this.current == null) { this.current = 0; }
			window.clearTimeout(this.setTimeOut);
			for (var i = 0; i < this.article.length; i++) {
				document.getElementById(this.id+"_message" + i).style.left = (document.getElementById(this.id+"_message" + i).offsetLeft - this.increment) + "px";
			}
			this.next = (this.current + 1 >= this.article.length) ? 0 : this.current + 1;
			if (document.getElementById(this.id+"_message" + this.next).offsetLeft <= 0) {
				var nextPosition = 0;
				for (var i = 1; i < this.article.length; i++) { nextPosition += document.getElementById(this.id+"_message" + i).offsetWidth; }
				document.getElementById(this.id+"_message" + this.current).style.left = nextPosition + "px";
				this.current = (this.current + 1 >= this.article.length) ? 0 : this.current + 1;
				this.setTimeOut = window.setInterval(function () { This.Run(); },this.pause);
			} else {
				this.setTimeOut = window.setInterval(function () { This.Run(); },this.interval);
			}
		}
	};
	
	BannerMessage.prototype.moveRight = function () {
		if ( this.mouse && this.status ) {
			var This = this;
			if (this.current == null) { this.current = this.article.length - 1; }
			window.clearTimeout(this.setTimeOut);
			for (var i = 0; i < this.article.length; i++) {
				document.getElementById(this.id+"_message" + i).style.left = (document.getElementById(this.id+"_message" + i).offsetLeft + this.increment) + "px";
			}
			this.next = (this.current - 1 < 0) ? this.article.length - 1 : this.current - 1;
			if (document.getElementById(this.id+"_message" + this.current).offsetLeft >= 0) {
				document.getElementById(this.id+"_message" + this.next).style.left = -document.getElementById(this.id+"_message" + this.next).offsetWidth + "px";
				this.current = (this.current - 1 < 0) ? this.article.length - 1 : this.current - 1;
				this.setTimeOut = window.setInterval(function () { This.Run(); },this.pause);
			} else {
				this.setTimeOut = window.setInterval(function () { This.Run(); },this.interval);
			}
		}
	};
	
	BannerMessage.prototype.prevBanner = function() {
		this.status = false;
		this.toggleBanner();
		
		if ( this.direction == "left" ) {			
		} else if ( this.direction == "right" ) {
			this.setDirection( "left" );
		} else if ( this.direction == "up" ) {			
		} else if ( this.direction == "down" ) {
			this.setDirection( "up" );
		} 
		
		this.Run();
	};
	
	BannerMessage.prototype.nextBanner = function() {
		this.status = false;
		this.toggleBanner();
		
		if ( this.direction == "left" ) {
			this.setDirection( "right" );
		} else if ( this.direction == "right" ) {			
		} else if ( this.direction == "up" ) {	
			this.setDirection( "down" );		
		} else if ( this.direction == "down" ) {			
		} 
		
		this.Run();
	};
	
	BannerMessage.prototype.toggleBanner = function() {
		if ( this.direction == "left" || this.direction == "right" ) {
			if ( this.status ) {
				this.status = false;				
				$j( "#"+this.id+"_stop_img" ).css("background-image", "url(" + this.dir + "/images/btn_h_play.gif)");
				$j( "#"+this.id+"_stop_img" ).attr("title", "배너 스크롤 재생");
			} else {
				this.status = true;				
				$j( "#"+this.id+"_stop_img" ).css("background-image", "url(" + this.dir + "/images/btn_h_stop.gif)");
				$j( "#"+this.id+"_stop_img" ).attr("title", "배너 스크롤 정지");
			}
		} else if ( this.direction == "up" || this.direction == "down" ) {
			if ( this.status ) {
				this.status = false;				
				$j( "#"+this.id+"_stop_img" ).css("background-image", "url(" + this.dir + "/images/btn_v_play.gif)");
				$j( "#"+this.id+"_stop_img" ).attr("title", "배너 스크롤 재생");
			} else {
				this.status = true;				
				$j( "#"+this.id+"_stop_img" ).css("background-image", "url(" + this.dir + "/images/btn_v_stop.gif)");
				$j( "#"+this.id+"_stop_img" ).attr("title", "배너 스크롤 정지");
			}
		}
	};
	
	BannerMessage.prototype.initBanner = function() {
		var obj = document.getElementById(this.id);
		//var text = '<div id="'+this.id+'_banner" style="position:absolute;">';
		var text = '<div id="'+this.id+'_banner">';
		for (var i = this.article.length - 1; i >= 0; i--) {
			text += '<div id="'+this.id+'_message' + i + '" class="banner-box" style="position:absolute;"></div>';
		}
		text += '</div>';
		
		obj.innerHTML = text;
		
		var obj = document.getElementById(this.id+"_banner");		
		with ( obj.style ) {
			width           = this.bannerWidth + 'px';
			height          = this.bannerHeight + 'px';
			clip            = "rect(0 " + this.bannerWidth + "px " + this.bannerHeight + "px 0)";
			backgroundColor = this.bannerColor;
			left            = this.bannerLeft + 'px';
			top             = this.bannerTop + 'px';
		}
		
		for (var i = 0; i < this.article.length; i++) {
			document.getElementById(this.id+"_message" + i).innerHTML = this.article[i];
			document.getElementById(this.id+"_message" + i).style.backgroundColor = this.bannerColor;
		}
		
		for (var i = 0; i < this.article.length; i++) {			
			
			if (i > 0) {
				if (this.direction == "left" || this.direction == "right") {					
					document.getElementById(this.id+"_message" + i).style.left = (document.getElementById(this.id+"_message" + (i-1)).offsetLeft + document.getElementById(this.id+"_message" + (i-1)).offsetWidth) + "px";
				} else if (this.direction == "up" || this.direction == "down") {					
					document.getElementById(this.id+"_message" + i).style.top = (document.getElementById(this.id+"_message" + (i-1)).offsetTop + document.getElementById(this.id+"_message" + (i-1)).offsetHeight) + "px";
				}
			}
			
			if (i == this.article.length - 1) {
				if (this.direction == "right") {
					document.getElementById(this.id+"_message" + i).style.left = -document.getElementById(this.id+"_message" + i).offsetWidth + "px";
				} else if (this.direction == "down") {
					document.getElementById(this.id+"_message" + i).style.top = -document.getElementById(this.id+"_message" + i).offsetHeight + "px";
				}
			}
		}
	};
	
	BannerMessage.prototype.startBanner = function() {
		this.initBanner();
		clearTimeout(timer);
		var timer = setTimeout(this.id+".Run()",this.pause);
	};
}($, window));
