﻿// 스크롤 배너
function HBScroll() {
	this.name           = "HBScroll";
	this.item           = new Array();
	this.itemcount      = 0;
	this.currentspeed   = 0;
	this.scrollspeed    = 50;
	this.pausedelay     = 1000;
	this.pausemouseover = false;
	this.stop           = false;
	this.type           = 1;
	this.height         = 100;
	this.width          = 100;
	this.stopHeight     = 0;
	this.i              = 0;
	
    this.changeevent    = function () {};       // 배너가 변경이 되었을 때 호출이 되는 함수
    this.scrollSize     = 1;                    // 한번에 배너 롤링 되는 속도
    
	this.add = function () {
		var text = arguments[0];
		this.item[this.itemcount] = text;
		this.itemcount = this.itemcount + 1;
	};

	this.add2 = function () {
		var url = arguments[0];
		var title = arguments[1];
		this.item[this.itemcount] = "<a href=" + url + " target=_blank>" + title + "</a>";
		this.itemcount = this.itemcount + 1;
	};
	
	this.add3 = function () {
		var url = arguments[0];
		var title = arguments[1];
		this.item[this.itemcount] = "<a href=" + url + " >" + title + "</a>";
		this.itemcount = this.itemcount + 1;
	};
	
	this.add4 = function () {
		var url = arguments[0];
		var title = arguments[1];
		var var_width= arguments[2];
		var var_height= arguments[3];
		this.item[this.itemcount] = "<a href=\"javascript:MM_openBrWindow('"+ url +"','POPUP','status=yes,resizable=no,width="+var_width+", height="+var_height+",scrollbars=auto');\">" + title + "</a>";
		this.itemcount = this.itemcount + 1;
	};
	
	this.add5 = function () {
		var url = arguments[0];
		var title = arguments[1];
		this.item[this.itemcount] = "<a href=\"javascript:"+ url  +"\">" + title + "</a>";
		this.itemcount = this.itemcount + 1;
	};
	
	this.add6 = function () {
		var url = arguments[0];
		var title = arguments[1];
		var mainText = arguments[2];
		this.item[this.itemcount] = "<a href=" + url + " target=_blank>" + title + "</a><br /><table width=\"100%\"  border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr height=\"25\"><td><img src='/images/main/bul_gray.gif' width='2' height='2' hspace='3' align='absmiddle'><a href=" + url + " target=_blank><strong>" + mainText + "</strong></a></td></tr></table>";
		this.itemcount = this.itemcount + 1;
	};
	
	this.addOfficialTopBanner = function () {
		this.item[this.itemcount] = "<a href=\""+ arguments[0]  +"\" target=_blank><img height=23 border=0 src=" + arguments[1] + "></a>";
		this.itemcount = this.itemcount + 1;
	};

	this.start = function () {
		if ( this.itemcount == 1 ) {
			this.add(this.item[0]);
		}
		this.display();
		this.currentspeed = this.scrollspeed;
		if ( this.type == 1 || this.type == 2 || this.type == 3 || this.type == 4 || this.type == 5 ) {
			this.stop = true;
			setTimeout(this.name+'.scroll()',this.currentspeed);
			window.setTimeout(this.name+".stop = false", this.pausedelay);
		}
		else {
			this.stop = true;
			setTimeout(this.name+'.scroll()',this.currentspeed);
			window.setTimeout(this.name+".stop = true", this.pausedelay);
		}
	};

	this.display = function () {
		document.write('<div id="'+this.name+'" style="height:'+this.height+'px; width:'+this.width+'px; position:relative; overflow:hidden; " OnMouseOver="'+this.name+'.onmouseover(); " OnMouseOut="'+this.name+'.onmouseout(); ">');
		
		for(var i = 0; i < this.itemcount; i++) {
			if ( this.type == 1 )   // 아래에서 위로
			{
				document.write('<div id="'+this.name+'item'+i+'"style="left:0px; width:'+this.width+'px; position:absolute; overflow:hidden; top:'+(this.height*i)+'px; ">');
				document.write(this.item[i]);
				document.write('</div>');
			}
			else if ( this.type == 2 )   // 위에서 아래로
			{
				document.write('<div id="'+this.name+'item'+i+'"style="left:0px; width:'+this.width+'px; position:absolute; overflow:hidden; top:'+(this.height*(-i))+'px; ">');
				document.write(this.item[i]);
				document.write('</div>');
			}
			else if ( this.type == 3 )  // 왼쪽에서 오른쪽
			{
				document.write('<div id="'+this.name+'item'+i+'"style="left:'+(this.width*(-i))+'px; width:'+this.width+'px; position:absolute; overflow:hidden; top:0px; ">');
				document.write(this.item[i]);
				document.write('</div>');
			}
			else if ( this.type == 4 )  // 오른쪽에서 왼쪽
			{
				document.write('<div id="'+this.name+'item'+i+'"style="left:'+(this.width*i)+'px; width:'+this.width+'px; position:absolute; overflow:hidden; top:0px; ">');
				document.write(this.item[i]);
				document.write('</div>');
			}
			else if ( this.type == 5 )  // 순차적으로
			{
				document.write('<div id="'+this.name+'item'+i+'"style="left:'+(this.width*i)+'px; width:'+this.width+'px; position:absolute; overflow:hidden; top:0px; ">');
				document.write(this.item[i]);
				document.write('</div>');
			}
			else if ( this.type == 6 )  // 랜덤으로
			{
				document.write('<div id="'+this.name+'item'+i+'"style="left:0px; width:'+this.width+'px; position:absolute; overflow:hidden; top:0px; visibility:hidden;">');
				document.write(this.item[i]);
				document.write('</div>');
			}
		}
		document.write('</div>');
	};

	this.scroll = function () {

		if ( this.pause == true ) {

			window.setTimeout(this.name+".scroll()",this.pausedelay);
			this.pause = false;

		} else {

			this.currentspeed = this.scrollspeed;
			
			if ( !this.stop ) {
				for (i = 0; i < this.itemcount; i++) {
					obj = document.getElementById(this.name+'item'+i).style;
					
					if ( this.type == 1 )       // 아래에서 위로
					{
						obj.top = (parseInt(obj.top) - this.scrollSize)+"px";
						
						if ( parseInt(obj.top) <= this.height * (-1) ) obj.top = (this.height * (this.itemcount-1))+"px";
						if ( parseInt(obj.top) == 0 ) { this.currentspeed = this.pausedelay; this.i = i; }
					}
					
					else if ( this.type == 2 )  // 위에서 아래로
					{
						obj.top = (parseInt(obj.top) + this.scrollSize)+"px";
						
						if (parseInt(obj.top) * (-1) <= this.height * (-1) ) obj.top = (-this.height * (this.itemcount-1))+"px";
						if ( parseInt(obj.top) == 0 ) { this.currentspeed = this.pausedelay; this.i = i; }
					}
					
					else if ( this.type == 3 )  // 왼쪽에서 오른쪽
					{
						obj.left = (parseInt(obj.left) + this.scrollSize)+"px";
						
						if ( parseInt(obj.left) * (-1) <= this.width * (-1) ) obj.left = (-this.width * (this.itemcount-1))+"px";
						if ( parseInt(obj.left) == 0 ) { this.currentspeed = this.pausedelay; this.i = i; }
					}
					
					else if ( this.type == 4 )  // 오른쪽에서 왼쪽
					{
						obj.left = (parseInt(obj.left) - this.scrollSize)+"px";
						
						if ( parseInt(obj.left) <= this.width * (-1) ) obj.left = (this.width * (this.itemcount-1))+"px";
						if ( parseInt(obj.left) == 0 ) { this.currentspeed = this.pausedelay; this.i = i; }
					}
					
					else if ( this.type == 5 )  // 순차적으로
					{
						obj.left = (parseInt(obj.left) - this.width)+"px";
						
						if ( parseInt(obj.left) <= this.width * (-1) ) obj.left = (this.width * (this.itemcount-1))+"px";
						if ( parseInt(obj.left) == 0 ){ this.currentspeed = this.pausedelay; this.i = i; }
					}
				}
			}
		    else if ( this.type == 6 )  // 랜덤으로
		    {
		        var item = Math.floor(Math.random() * (this.itemcount));
				document.getElementById(this.name+'item'+item).style.visibility = "visible";
		        return;
		    }
		    this.changeevent();
			window.setTimeout(this.name+".scroll()",this.currentspeed);
		}
	};
    
    this.setBanner = function() {
//        for (i = 0; i < this.itemcount; i++) {
//		    obj = document.getElementById(this.name+'item'+i).style;
//		    obj.top = (i*this.height);
//		}
    
	    for (i = 0; i < this.itemcount; i++) {
		    obj = document.getElementById(this.name+'item'+i).style;
            
            if ( this.type == 1 )
            {
		        if ( this.i != i)
		        {
		            if (i-this.i < 0)
		            {
		                obj.top = (this.height * (this.i-i))+"px";
		            }
		            else
		            {
		                obj.top = (this.height * (i-this.i))+"px";
		            }
		        }
		        else
		        {
		            obj.top = 0+"px";
		            this.pause = true;
		        }
		    }
		    else if ( this.type == 2 )  // 위에서 아래로
		    {
		        if ( this.i != i)
		        {
		            if (i-this.i < 0)
		            {
		                obj.top = -(this.height * ((i-this.i)+this.itemcount))+"px";
		            }
		            else
		            {
		                obj.top = -(this.height * (i-this.i))+"px";
		            }
		        }
		        else
		        {
		            obj.top = 0+"px";
		            this.pause = true;
		        }
		    }
		    else if ( this.type == 3 )  // 왼쪽에서 오른쪽
		    {
		        if ( this.i != i)
		        {
		            if (i-this.i < 0)
		            {
		                obj.left = -(this.width * ((i-this.i)+this.itemcount))+"px";
		            }
		            else
		            {
		                obj.left = -(this.width * (i-this.i))+"px";
		            }
		        }
		        else
		        {
		            obj.left = 0+"px";
		            this.pause = true;
		        }
		    }
		    else if ( this.type == 4 )  // 오른쪽에서 왼쪽
		    {
		        if ( this.i != i)
		        {
		            if (i-this.i < 0)
		            {
		                obj.left = (this.width * ((i-this.i)+this.itemcount))+"px";
		            }
		            else
		            {
		                obj.left = (this.width * (i-this.i))+"px";
		            }
		        }
		        else
		        {
		            obj.left = 0+"px";
		            
		            this.pause = true;
		        }
		    }
	    }
        this.changeevent()		    
    };
    
	this.rolling = function () {
		if ( this.stop == false  ) {
			this.next();
		}
		window.setTimeout(this.name+".rolling()",this.scrollspeed);
	};

	this.onmouseover = function () {
		if ( this.pausemouseover ) {
			this.stop = true;
		}
	};

	this.onmouseout = function () {
		if ( this.pausemouseover ) {
			this.stop = false;
		}
	};

	this.next = function() {

		for (i = 0; i < this.itemcount; i++) {
			obj = document.getElementById(this.name+'item'+i).style;
			if ( parseInt(obj.left) < 1 ) { 
				width = (this.width + parseInt(obj.left))+"px";
				break;
			}
		}
		for (i = 0; i < this.itemcount; i++) {
			obj = document.getElementById(this.name+'item'+i).style;
			if ( parseInt(obj.left) < 1 ) { 
				obj.left = (this.width * (this.itemcount-1))+"px";
			} else {
				obj.left = (parseInt(obj.left) - width)+"px";
			}
		}

	};

	this.prev = function() {

		for (i = 0; i < this.itemcount; i++) {
			obj = document.getElementById(this.name+'item'+i).style;
			if ( parseInt(obj.left) < 1 ) { 
				width = (parseInt(obj.left) * (-1))+"px";
				break;
			}
		}
		if ( width == 0 ) {
			total_width = this.width * (this.itemcount-1);
			for (i = 0; i < this.itemcount; i++) {
				obj = document.getElementById(this.name+'item'+i).style;
				if ( parseInt(obj.left) + 1 > total_width ) { 
					obj.left = 0+"px";
				} else {
					obj.left = (parseInt(obj.left) + this.width)+"px";
				}
			}
		} else {
			for (i = 0; i < this.itemcount; i++) {
				obj = document.getElementById(this.name+'item'+i).style;
				if ( parseInt(obj.left) < 1 ) { 
					obj.left = 0+"px";
				} else {
					obj.left = (parseInt(obj.left) + width)+"px";
				}
			}
		}
	};

	this.unext = function () {
		this.onmouseover();
		this.next();
		window.setTimeout(this.name+".onmouseout()",this.pausedelay);
	};

	this.uprev = function () {
		this.onmouseover();
		this.prev();
		window.setTimeout(this.name+".onmouseout()",this.pausedelay);
	};

}