// ------------------------------------------------------------
//      FloatTicker.js
//      Provides prototype for a floating ticker. 
// ------------------------------------------------------------
//      Author: Hans Hawe
//      Date:   05.12.2008
// ------------------------------------------------------------
//      Properties:
//      delay:          the time between two steps in ms
//      steplength:     the distance in px for one step
//      stopOnMouseOver:indicates wether to stop ticker on mouseover
// ------------------------------------------------------------
//      Methods:
//      start:          starts floating the ticker
//      stop:           stops floating the ticker
// ------------------------------------------------------------
//      Required script files:      prototype.js
//
// ------------------------------------------------------------
//  copyright by Team ModulAcht. All rights reserved
// ------------------------------------------------------------

var FloatTicker = Class.create();
FloatTicker.prototype = 
{
    // members
    delay : 25,    
    stepLength : 1,
    stopOnMouseOver : false,
    elementToMove : null,
    parent : null,    
    startPos: 0,
    move : false,
    stopMoving : false, 

    // ------------------------------------------------------------
    //  constructor
    // ------------------------------------------------------------    
    initialize : function(id)
    {
        // member init        
        this.parent = $(id);        
        this.elementToMove = $(id + "txt");

        // ensure styles
        this.parent.style.overflowX = "hidden";  
            
        // ensure positioning        
        this.elementToMove.style.whiteSpace = "nowrap";        
        this.elementToMove.style.top = (this.parent.offsetHeight / 2) - (this.elementToMove.offsetHeight / 2) + "px"; 
        this.elementToMove.style.position = "absolute";   
        this.startPos = this.parent.clientWidth;
        this.elementToMove.style.left = this.startPos + "px";
       
        // obeserve events
        Event.observe(id, "mouseover", this._setStopFlag.bind(this, true));
        Event.observe(id, "mouseout", this._setStopFlag.bind(this, false));
    },
    
    // ------------------------------------------------------------
    //  starts movements of the ticker
    // ------------------------------------------------------------
    start : function()
    {    
        // check for neccesary elements
        if(this.elementToMove != null && this.parent != null)
        {   
            // set flag and start moving
            this.move = true;
            this._move();            
        }
    },
    
    // ------------------------------------------------------------
    //  stops movement of the ticker
    // ------------------------------------------------------------
    stop : function()
    {       
        // set move flag
        this.move = false; 
    },
    
    // ------------------------------------------------------------
    //  sets flag wether to stop tickermovement. works with "stopOnMouseOver"
    // ------------------------------------------------------------
    _setStopFlag : function(stop)
    {        
        // set flag
        this.stopMoving = stop;        
    },
    
    // ------------------------------------------------------------
    //  moves the ticker element by the specified step and delay (recursive)
    // ------------------------------------------------------------
    _move : function()
    {
        // check for stepLength and moving flag
        if(this.stepLength > 0 && this.move)
        {                       
            if((this.stopOnMouseOver && !this.stopMoving) || this.stopOnMouseOver == false)            
            {            
                // get left margin
                var left= parseInt(this.elementToMove.style.left);

                // remember new pos
                var newPos = (left - this.stepLength);

                // set new position
                this.elementToMove.style.left = newPos + "px";
                
                // check wether to reset tickerpos
                if((newPos + this.elementToMove.getWidth()) <= 0)
                {

                    this.elementToMove.style.left = this.startPos + "px";
                }
            }
        }
        // move again, but after specified delay
        setTimeout(this._move.bind(this), this.delay);
    }
};