// Initial version of script - http://mihalytch.org.ua/programming/js/begushhaya-stroka-na-javascript-svoimi-rukami.html
// 
// @co-author Pavel Adrianov <p.adrianov@ameria.de>
// @co-author Michael Leibenson <m.leibenson@ameria.de>
// @package TYPO3
// @copyright (c) 2009 by ameria GmbH
// @since 16.09.2009
// 
// @todo 
// moveCrawLine and moveCrawLineQueue looks the same. 
// We use two different functions because of recursion. 
// If you will call same funciton with different timeouts you will get wrong results.
// Because of recusion and same variable values
// We need to think about how to fix it

// Whole container width
var container_width;

// initial offset for the first and second parts of text
var craw_line_offset;
var craw_line_offset_queue;

// step in pixels for shifting
var step = 1;

// delay between "step" increment. in real world it's kinda speed
var delay = 25;

// offset between two text containers
var offset_between = 0;

// make a stop of motion
var GlobalStop = 0;

// Init values and start shifting
function enableCrawLine() {
  container_width = document.getElementById('marquee_container').offsetWidth;
  
  craw_line_offset = 0;
  craw_line_offset_queue = 0;
  
  // hide second text container
  document.getElementById('marquee_text_queue').style.left = parseInt(container_width - craw_line_offset_queue) + 'px';
  
  // width of text container - common for both
  var craw_line_width = document.getElementById('marquee_text').offsetWidth;

  // start shifting
  moveCrawLine(craw_line_width);
}

// shifting step by step for the first text container
function moveCrawLine(w) {
  if (GlobalStop == 0) {
    // text block width
    w = parseInt(w);
    
    // check if text block is visible or not
    if (craw_line_offset < (w + container_width)) {
      // run second text container
      if (craw_line_offset == (w + offset_between)) {
        moveCrawLineQueue(w);
      }
      
      // shift text container left
      craw_line_offset = craw_line_offset + step;
      
      // change possition of text container
      document.getElementById('marquee_text').style.left = parseInt(container_width - craw_line_offset) + 'px';        
      
      // recusive call
      // 1 second = 1000 milliseconds 
      setTimeout('moveCrawLine(' + w + ');', delay);
    } else {
      // initial value
      craw_line_offset = 0;
    }
  } else {
    setTimeout('moveCrawLine(' + w + ');', delay);
  }
}

// shifting step by step for the first text container
function moveCrawLineQueue(w) {
  if (GlobalStop == 0) {
    // text block width
    w = parseInt(w);
    
    // check if text block is visible or not
    if (craw_line_offset_queue < (w + container_width)) {
      if (craw_line_offset_queue == (w + offset_between)) {
        moveCrawLine(w);
      }
      
      // shift text container left
      craw_line_offset_queue = craw_line_offset_queue + step;
      
      // change possition of text container
      document.getElementById('marquee_text_queue').style.left = parseInt(container_width - craw_line_offset_queue) + 'px';      
      
      // recusive call
      // 1 second = 1000 milliseconds 
      setTimeout('moveCrawLineQueue(' + w + ');', delay);        
    } else {
      // initial value
      craw_line_offset_queue = 0;
    }
  } else {
    setTimeout('moveCrawLineQueue(' + w + ');', delay);
  }
}

// init events for both text containers
function initEvents() {
  document.getElementById('marquee_text').onmouseover = function() {
    GlobalStop = 1;
  }
  document.getElementById('marquee_text').onclick = function() {
    GlobalStop = 0;
  }
  document.getElementById('marquee_text').onmouseout = function() {
    GlobalStop = 0;
  }  
  
  document.getElementById('marquee_text_queue').onmouseover = function() {
    GlobalStop = 1;
  }
  document.getElementById('marquee_text_queue').onclick = function() {
    GlobalStop = 0;
  }
  document.getElementById('marquee_text_queue').onmouseout = function() {
    GlobalStop = 0;
  }  
}