/**
* 
* Copyright 2005, addObject.com. All Rights Reserved
* Author Jack Hermanto, www.addobject.com
*/
var nlsScroller = new Object();

function NlsScroller(s) {
  //private
  this.scrlId = s;
  this.intRef = null;
  nlsScroller[s] = this;
  this.lsTpc = null;
  
  //properties
  this.contents = [];
  this.setContents = setContents;
  this.scrollerWidth = 150;
  this.scrollerHeight = 80;
  this.scrollerSpeed = 50;
  this.scrollerStep = 1;
  this.scrollerWait = 0;
  this.scrollerDir = "up";
  this.stylePref = "";
  this.stopOnMouseOver = true;
  
  //methods
  this.render = render;
  this.start = start;
  this.stop= stop;
  this.resume = resume;
  this.run = run;
}

function setContents(cnt) {
  cnt = cnt.replace(/<span>nlsscroller-break<\/span>/gi, "<span>nlsscroller-break</span>");
  this.contents = cnt.split("<span>nlsscroller-break</span>");
}

//render the scroller
function render(plc) {
  var pos="relative", dsp="none";
  if (window.navigator.appName=="Microsoft Internet Explorer" && window.navigator.appVersion.indexOf("MSIE 4") != -1) {
    pos="absolute"; dsp="block";
  }  
  var str = "";
  for (var i=0; i<this.contents.length; i++) {
    str += ("<table id='"+this.scrlId+"line"+i+"' width='100%' style='display:"+dsp+";position:absolute;top:0px' border='0' cellpadding='0' cellspacing='0'><tr><td class='"+this.stylePref+"content'>" + this.contents[i] + "</td></tr></table>");
  }
  str = ("<div class='"+this.stylePref+"scroller' style='position:"+pos+";overflow:hidden;clip:rect(0px "+this.scrollerWidth+"px "+this.scrollerHeight+"px 0px);width:"+this.scrollerWidth+"px;height:"+this.scrollerHeight+"px;' onmouseover='if (nlsScroller."+this.scrlId+".stopOnMouseOver) nlsScroller."+this.scrlId+".stop()' onmouseout='if (nlsScroller."+this.scrlId+".stopOnMouseOver) nlsScroller."+this.scrlId+".resume()'>" + str + "</div>" );
  if (plc) {
    var tPlc = NlsGetElementById(plc);
    tPlc.innerHTML = str; return str;
  } else {
    document.write(str); return "";
  }
}

//check whether the content is visible
function inViewPort(scr, pos, bound) {
  switch (scr.scrollerDir) {
    case "up":; case "left": return (pos<=bound);
    case "down":; case "right":return (pos>=bound);
  }
}

//scroll the scroller one step
function run() {
  switch (this.scrollerDir) {
    case "up":runScroll(this, -this.scrollerStep, true);break;
    case "down":runScroll(this, this.scrollerStep, true);break;
    case "left":runScroll(this, -this.scrollerStep, false);break;
    case "right":runScroll(this, this.scrollerStep, false);break;    
  }
}

//run the scroller
function runScroll(scr, step, vert) {
  var line, pos, bound;
  for (var i=0; i<scr.contents.length; i++) {
    line = NlsGetElementById(scr.scrlId+"line"+i);
    pos = parseInt(vert?line.style.top:line.style.left) + step;  
    switch (scr.scrollerDir) {
      case "up": bound=-line.offsetHeight;break;
      case "down": bound=scr.scrollerHeight;break;
      case "left": bound=-line.offsetWidth;break;
      case "right": bound=scr.scrollerWidth;break;
    }
    if (inViewPort(scr, pos, bound)) {
      switch (scr.scrollerDir) {
        case "up":
          var t=parseInt(scr.lsTpc.style.top) + parseInt(scr.lsTpc.offsetHeight);
          if (t > scr.scrollerHeight) { line.style.top = (t)+"px"; } else { line.style.top = (scr.scrollerHeight) + "px"; }
          break;
        case "down": 
          var t=parseInt(scr.lsTpc.style.top);
          if (t<0) { line.style.top = (t-line.offsetHeight) + "px"; } else { line.style.top = -line.offsetHeight + "px"; }
          break;
        case "left":
          var t=parseInt(scr.lsTpc.style.left) + parseInt(scr.lsTpc.offsetWidth);//scr.scrollerWidth;
          if (t > scr.scrollerWidth) {line.style.left=(t)+"px";} else {line.style.left=(scr.scrollerWidth)+"px";}
          break;
        case "right":
          var t=parseInt(scr.lsTpc.style.left);
          if (t<0) {line.style.left = (t-line.offsetWidth) + "px";} else {line.style.left = -line.offsetWidth + "px";}
          break;
      }
      scr.lsTpc=line;
      if (scr.scrollerWait>0) {
        scr.stop();
        window.setTimeout("eval(nlsScroller."+scr.scrlId+".resume())", scr.scrollerWait);
      }
    } else {
      if (vert) line.style.top = pos+"px"; else line.style.left = pos+"px"; 
    }
  }
}

//initialize an run the scroller
function start() {
  var lineNext;
  var tmpPos=0;
  for (var i=0; i<this.contents.length; i++) {
    lineNext= NlsGetElementById(this.scrlId+"line"+i);
    lineNext.style.display = "";
    switch (this.scrollerDir) {
      case "up": 
        lineNext.style.left = 0;
        lineNext.style.top = tmpPos+"px";
        tmpPos += lineNext.offsetHeight;
        break;
      case "down":
        lineNext.style.left = 0;
        tmpPos += lineNext.offsetHeight;
        lineNext.style.top = (this.scrollerHeight-tmpPos) + "px";      
        break;
      case "left":
        lineNext.style.top = 0;
        lineNext.style.left = tmpPos+"px";
        tmpPos += lineNext.offsetWidth;      
        break;
      case "right":
        lineNext.style.top = 0;
        tmpPos += lineNext.offsetWidth;
        lineNext.style.left = (this.scrollerWidth-tmpPos) + "px";
        break;       
    }
  }
  this.lsTpc=lineNext;
  this.stop();
  this.intRef = window.setInterval("eval(nlsScroller."+this.scrlId+".run())", this.scrollerSpeed);
}

//resume the scroller
function resume() {
  if (this.intRef==null) {
    this.intRef = window.setInterval("eval(nlsScroller."+this.scrlId+".run())", this.scrollerSpeed);
  }
}

//stop the scroller
function stop() {
  if (this.intRef) {
    window.clearInterval(this.intRef);
    this.intRef = null;
  }
}

function NlsGetElementById(id) {
    if (document.all) {
        return document.all(id);
    } else
    if (document.getElementById) {
        return document.getElementById(id);
    }
}