/* main object for scrolling text */

function ScrollEngine ( name ) {
    this.name = name;
    this.content = '';
    this.messageWidth = 0;
    this.messageHeight = 0;
    this.paused = 0;
    this.speed = 0;
    this.divMainId = null;
    this.divMainElmt = null;
    this.divTempId = null;
    this.divTempElmt = null;
    this.divContId = null;
    this.divContElmt = null;
    this.messagePosX = 0;
    this.maxSize = 0;
    
    
    this.setSize    = ScrollEngine_setSize;
    this.setSpeed   = ScrollEngine_setSpeed;
    this.setDivsId   = ScrollEngine_setDivsId;
    this.setPaused  = ScrollEngine_setPaused;
    this.insertText = ScrollEngine_insertText;
    this.scrollText = ScrollEngine_scrollText;
    this.startEngine = ScrollEngine_startEngine;
    this.initEngine = ScrollEngine_initEngine;


}

function ScrollEngine_setSize ( w, h ) {
    this.messageWidth = w;
    this.messageHeight = h;
}

function ScrollEngine_setSpeed ( s ) {
    this.speed = Math.max ( s, 1 );
}

function ScrollEngine_setDivsId ( i, t, c ) {
    this.divMainId = i;
    this.divTempId = t;
    this.divContId = c;
}

function ScrollEngine_setPaused ( p ) {
    this.paused = p;
}

function ScrollEngine_insertText ( content, sep ) {
    this.content += content + sep;
}

function ScrollEngine_scrollText ( ) {
    
// check if message is passed
    if ( this.messagePosX-this.speed < -1*( this.maxSize-this.messageWidth*0.75) ) {
        // message is passed : go back to start position
        this.messagePosX = this.messageWidth;
    } else {
        // message is not passed : continue to scroll it
        if ( this.paused == 0 ) {
            this.messagePosX = this.messagePosX - this.speed;
        }
    }
    
    this.divMainElmt.style.left = this.messagePosX;
}

function ScrollEngine_startEngine ( ) {
    this.initEngine();
    setInterval(this.name+'.scrollText()', 50 );
}

function ScrollEngine_initEngine ( ) {
    // get div objects
    this.divMainElmt = document.getElementById( this.divMainId );
    this.divTempElmt = document.getElementById( this.divTempId );
    this.divContElmt = document.getElementById( this.divContId );
    
    // set content & position of main div
    this.messagePosX = this.messageWidth;
    this.divMainElmt.style.left = this.messagePosX;
    this.divMainElmt.innerHTML = this.content;
    

    // set size of main div
    this.divTempElmt.innerHTML = this.content;
    this.maxSize = this.divTempElmt.offsetWidth; 
    this.divMainElmt.style.width = this.maxSize;
    this.divContElmt.style.display= 'none';    
}

function scrollutil_attachEventHandlerToElement(element, event, handler) {
  if ( document.addEventListener )
    element.addEventListener(event, handler, false);
  else if ( document.attachEvent )
    element.attachEvent("on" + event, handler);
}