﻿/////w : 가로, h:세로, max : 화면에 보여줄갯수, loc = 방향, text = HTML
var mqtxt = new Object();
function mqtag(w, h, max, loc, text){
mqtxt.rtn = new Tslide('mqtxt.rtn', w, h);
mqtxt.rtn.width       = w;
mqtxt.rtn.height      = h * max;
mqtxt.rtn.speed       = 6;
mqtxt.rtn.pixel       = 1;
mqtxt.rtn.interval    = 2000;
mqtxt.rtn.size        = h;
mqtxt.rtn.direction   = loc;
mqtxt.txt = text.split("|");
	for(var i=0; i<mqtxt.txt.length; i++){
		mqtxt.rtn.item[i] = "<div style='width:"+w+";height:"+h+";overflow:hidden'><table border='0' cellspacing='0' cellpadding='0'>"
					 + "<tr><td width="+w+" height="+h+">"+mqtxt.txt[i]+"</td></tr></table></div>";
	}
mqtxt.rtn.init();
}

function mqtag_var(vobj, w, h, max, loc){
    if(!eval(vobj).hasOwnProperty("rtn")){
        eval(vobj).rtn = new Tslide(vobj + '.rtn', w, h);
        eval(vobj).rtn.width       = w;
        eval(vobj).rtn.height      = h * max;
        eval(vobj).rtn.speed       = 6;
        eval(vobj).rtn.pixel       = 1;
        eval(vobj).rtn.interval    = 2000;
        eval(vobj).rtn.size        = h;
        eval(vobj).rtn.direction   = loc;
        eval(vobj).rtn.item = new Array();
    }
}

Tslide = function(className, w, h){
        document.write("<div id='tsp_"+ className +"' style='width:"+ w + "px; height:" + h + "px; position:relative;overflow: hidden;'><div id='tsl_"+ className +"'></div></div>");
        this.item       = [];
        this.width      = this.height = this.speed = this.pixel = this.interval = this.size = this.moveCount = this.X = this.Y = 0;
        this.direction  = "";
        this.pLayer     = getObject("tsp_"+ className);
        this.layer      = getObject("tsl_"+ className);
        this.align      = "left";
        this.intervalId = null;
        this.className	= className;
        this.isPause    = false;
}
Tslide.prototype.init = function(){
        with (this.pLayer.style)
        {
            width       = this.width+"px";
            height      = this.height+"px";
            overflow    = "hidden";
        }
       with (this.layer.style)
        {
            width       = this.direction=='up' || this.direction=='down' ? this.width+"px" : this.size*(this.item.length+1)+"px";
            height      = this.direction=='up' || this.direction=='down' ? this.size*(this.item.length+1)+"px" : this.height+"px";
            top         = 0;
            left        = 0;
            position    = "relative";
        }
        for (var i=0; i<parseInt(this.height / this.size, 10)+1; i++)
            this.item[this.item.length] = this.item[i];
        switch (this.direction)
        {
            case "up":      this.X = this.Y = 0; break;
            case "down":    this.X = 0; this.layer.style.top = this.Y = -this.size*(this.item.length-2); alert(this.item.length);break;
            case "left":    this.X = this.Y = 0; break;
            case "right":   this.Y = 0; this.layer.style.left = this.X = -this.width*(this.item.length-1); break;
        }
        var __html = "<div onmouseover='"+this.className+".pause()' onmouseout='"+this.className+".unpause()'>";
        if (this.direction=='up' || this.direction=='down'){
            __html += "<table width='"+ this.width +"' cellspacing='0' cellpadding='0' border='0'>";
            for (var i in this.item)
                __html += "<tr><td height='"+this.size+"' style='overflow:hidden' align='"+this.align+"' valign='middle'>"+this.item[i]+"</td></tr>";
            __html += "</table>";}
        else{
            __html += "<table cellspacing='0' cellpadding='0' border='0'><tr>";
            for (var i in this.item)
				__html += "<td width='"+this.width+"' height='"+ this.layer.style.height +"' align='"+this.align+"'"
                    + "valign='middle' style='overflow:hidden;'>"+this.item[i]+"</td>";
            __html += "</tr></table>";}
        __html += "</div>";
        this.layer.innerHTML = __html;
        setTimeout(this.className+".start()", this.interval);
}
    Tslide.prototype.start = function(){
        this.intervalId = setInterval(this.className+".move()", this.speed);}
    Tslide.prototype.move = function(){
        if (this.isPause) return;
        switch (this.direction){
            case "up":      this.Y -= this.pixel; break;
            case "down":    this.Y += this.pixel; break;
            case "left":    this.X -= this.pixel; break;
            case "right":   this.X += this.pixel; break;}
        if (this.direction=='up' || this.direction=='down'){
            if (Math.abs(this.Y)%this.size==0) this.stop();
            this.layer.style.top = this.Y + 'px';}
        else{
            if (Math.abs(this.X)%parseInt(this.width)==0) this.stop();
            this.layer.style.left = this.X + 'px';}
    }
    Tslide.prototype.stop = function(){
        clearInterval(this.intervalId);
        switch (this.direction){
            case "up":
                if (Math.abs(this.Y) >= parseInt(this.layer.style.height,10)-this.size)this.Y = this.layer.style.top = 0;
				break;
            case "down":
				if (Math.abs(this.Y) <= 0) this.Y = this.layer.style.top = -this.size*(this.item.length-2)
                break;
            case "left":
                if (Math.abs(this.X) >= parseInt(this.width*(this.item.length-2))) this.X = this.layer.style.left = 0;
                break;
            case "right":
                if (Math.abs(this.X) <= 0) this.X = this.layer.style.left = -this.width*(this.item.length-2);
                break;}
        setTimeout(this.className+".start()", this.interval);
	}
Tslide.prototype.pause = function() {this.isPause = true;}
Tslide.prototype.unpause = function() {this.isPause = false;}
