function replace(string,text,by) {
  var strLength = string.length, txtLength = text.length;
  if ((strLength == 0) || (txtLength == 0)) return string;
  var i = string.indexOf(text);
  if ((!i) && (text != string.substring(0,txtLength))) return string;
  if (i == -1) return string;
  var newstr = string.substring(0,i) + by;
  if (i+txtLength < strLength) newstr += replace(string.substring(i+txtLength,strLength),text,by);
  return newstr;
}
function trim(str){return replace(str,'\t','');}

function Scrollbox (ref, container, canvas, dx, dy, interval) {
  this.reference = ref;
  this.container = container;
  this.canvas = canvas;
  this.canvasWidth = 0;
  this.canvasHeight = 0;
  this.containerWidth = 0;
  this.containerHeight = 0;
  this.x = 0;
  this.y = 0;
  this.dx = dx;
  this.dy = dy;
  this.interval = interval;
}

Scrollbox.prototype.getContainer = function(){
  var parentEl = document.getElementById(this.canvas).parentNode;
  while(parentEl != document.body){
    if(parentEl.id === this.container) {
      return parentEl;
    }
    parentEl = parentEl.parentNode;
  }
  return null;
}

Scrollbox.prototype.init = function () {
  var container = this.getContainer();
  if(!container) {throw "Missing container element with ID = '" +this.container +"'";}
  
  var canvas = document.getElementById(this.canvas);
  var canvas_hidden = document.getElementById(this.canvas + "_hidden");
  if(canvas&&canvas_hidden){
    if (document.defaultView && document.defaultView.getComputedStyle) {  // is mozilla
      this.canvasWidth = parseInt(document.defaultView.getComputedStyle(canvas_hidden, null).getPropertyValue("width"));
      this.canvasHeight = parseInt(document.defaultView.getComputedStyle(canvas, null).getPropertyValue("height"));
      this.containerWidth = parseInt(document.defaultView.getComputedStyle(container, null).getPropertyValue("width"));
      this.containerHeight = parseInt(document.defaultView.getComputedStyle(container, null).getPropertyValue("height"));
    } else if (canvas.currentStyle) { // is IE
      canvas.style.canvasWidth = container.style.width;
      canvas.style.height=(container.style.height == "" )?'23px':(parseInt(container.style.height.substring(0, container.style.height.indexOf("px"))) / 2) + "px";
      this.canvasWidth = canvas.offsetWidth;
      this.canvasHeight = canvas.offsetHeight;
      this.containerWidth = parseInt(container.currentStyle["width"]);
      this.containerHeight = parseInt(container.currentStyle["height"]);
    };
    this.x = this.containerWidth;
  }
  return this;
}

Scrollbox.prototype.scroll = function () {
  var canvas = document.getElementById(this.canvas);
  if(canvas){
    if (this.x + this.canvasWidth < 0) {
      this.x = this.containerWidth;
    } else {
      this.x -= this.dx;
    }
    canvas.style.marginLeft = this.x+"px";
  }
}
Scrollbox.prototype.start=function(){this.timer=setInterval(this.reference+".scroll()", this.interval);}
Scrollbox.prototype.stop=function(){this.timer=clearInterval(this.timer);}

//FK Soft tinc-Parser (Börsenticker Finanzen.net)
var Ticker={
  stocks:{},
  indices:{},
  code:false,
  inject:function(p){if(this.code)this.code.toDOM().each(function(i){i.inject(p);});},
  start:function(){this.stocks.start();this.indices.start();},
  stop:function(){
    if(Ticker.stocks.timer) Ticker.stocks.stop();
    if(Ticker.indices.timer) Ticker.indices.stop();
  },
  get:function(php,val,id,p,autostart){
    //var php='tinc.php',val='key=hPmO8ELL',url=php+'?'+val;
    return new Request.HTML({
      url:php,method:'get',asynch:true,data:val,evalResponse:false,evalScripts:false
    }).addEvent('success',function(rTree,rEl,rHTML){
      //Parse the file (special tinc?key=hPmO8ELL)
      var tc=rHTML.split('{};');
      tc.shift();tc.shift();
      tc=tc[0].split(');');
      
      var code=tc.shift()+');';
      delete tc;
      //Convert from html to plain jscript code
      code=replace(replace(trim(code),"document.writeln('",''),"');",'');
      code=replace(replace(replace(replace(replace(code,"&amp;",'&'),"&nbsp;",' '),"&nbsp;&nbsp;",''),"&lt;",'<'),"&gt;",'>');
      
      //Ready to execute
      //convert html to DOM and inject all parsed elements (stock and indices content)
      Ticker.code=code;
      Ticker.inject(p);
      Ticker.stocks=new Scrollbox('Ticker.stocks', 'tinc_content', 'canvas2', 1, 5, 25).init();
      Ticker.indices=new Scrollbox('Ticker.indices', 'tinc_content', 'canvas1', 1, 10, 50).init();
      if(autostart) Ticker.start();
    }).send();
  }  
};

