// self contained scroller class

var scroller = function(containerId, textObj, classText,cnt){
       var i = 0;
	    this.timerVal = '0';
      this.posX = 0;
      this.textObj = textObj;
      this.cnt = cnt;
      this.nodeClass = classText;
    // Setup Container Node Styles  
      this.containerNode = document.getElementById(containerId);
      this.containerNode.style.position = 'relative';
      this.containerNode.style.overflow = 'hidden';
      this.containerNode.style.display = 'block'

    //  Create Scrolling Text Node and assign styles
      var tempNode = document.createElement('span');
      
      var testNode = document.createElement('span');
      

      this.textNode = tempNode;
      this.textNode.style.display = 'block';
      this.textNode.style.position = 'absolute';
 // for (var i=0;i< this.cnt;i++) {
      this.textNode.style.top = this.textObj[i].topOffset;

      this.testNode = testNode;
      this.testNode.style.visibility = 'hidden';
      

    //  Make sure we apply the class (optional) to the hidden area.
      if(this.nodeClass){
        this.textNode.className = this.nodeClass;
        this.testNode.className = this.nodeClass;
      }

      this.containerNode.appendChild(tempNode);  
      this.containerNode.appendChild(testNode);   
      
      this.textNode.style.width = this.textNode.offsetWidth + 'px';
      this.textWidth = this.textNode.offsetWidth;
      
      this.containerWidth = this.containerNode.offsetWidth;
      this.posX = this.containerWidth + this.textWidth;
      this.testNode.innerHTML = this.textObj[i].text;

    //	Set Current Text
	    this.currentTextIndex = i;	
    	
    //	Closure Magic
	    this.timerClosure = function(obj){
		    function timerClosureSet() {
			    var targetObj = obj; 
			    obj.moveText(targetObj);			
		    }
		    return timerClosureSet;
	    }	

    //	Resize Closure Magic
      this.resizeClosure = function(obj){
        function resizeClosureSet(){
          var targetObj = obj;
          obj.readjust(targetObj);
        }
        return resizeClosureSet;
      }
      
      this.readjust = function(obj){
        obj.containerWidth = obj.containerNode.offsetWidth;
      }
          
    //	Set the Timeout
	    this.setSpeed = function(preSpeed){
	      var speed = this.textObj[this.currentTextIndex].speed;
	      if(preSpeed){
	        speed = preSpeed;
	      }	    
	      this.timerVal = setTimeout(this.timerClosure(this), speed);
	    }
    	
    //	Lets move something!
	    this.moveText = function(obj){	
	      var currentTextObj = obj.textObj[obj.currentTextIndex];
  	    obj.posX = obj.posX + currentTextObj.increment; 
      	
  	    if(obj.posX >= obj.containerWidth){  	  
  	      obj.currentTextIndex++;
  	      if(obj.currentTextIndex > obj.textObj.length){
  	        obj.currentTextIndex = i;
  	      }
  	      obj.refreshText();
  	    }
      	
	      if(currentTextObj.direction == 'left'){  	  
	        obj.textNode.style.right = this.posX + 'px';
	      } else {
	        obj.textNode.style.left = this.posX + 'px';
	      }
	      clearTimeout(obj.timerVal);
	      if(currentTextObj.middle_pause == true && ( (obj.posX + obj.textWidth) == obj.containerWidth )) {
    // (obj.posX + obj.textWidth / 2) > obj.containerWidth / 2) && ((obj.posX + obj.textWidth / 2) < (obj.containerWidth / 2) + currentTextObj.increment + .5)) {
	        obj.setSpeed(currentTextObj.middle_pause_time);
	      } else {
  	      obj.setSpeed();
  	    }
	    }
    	
    //	Refresh text parameters and styles for next text session
	    this.refreshText = function(){
	      try {
  	      this.textNode.innerHTML = this.textObj[this.currentTextIndex].text;
  	    } 
  	    catch(e){
  	      this.currentTextIndex = i;
  	      this.refreshText();
  	    }
        this.testNode.innerHTML = this.textObj[this.currentTextIndex].text;
        this.textWidth = this.testNode.offsetWidth;
 	      this.textNode.style.width = this.textWidth + 'px';
     	  
 	      this.posX = 0 - this.textWidth;

  	    clearTimeout(this.timerVal);
	      this.setSpeed();
	    }
    	
    //	Start it all off.
	    this.init = function(){
    //  Make sure things re-adjust if the window is resized  
        window.onresize = this.resizeClosure(this);

	      this.setSpeed();
	    }	
  // }
}


window.onload = function(){
  var textObj = {
    '0' : {
      'text' : '<a href="press/press_4.asp">Pegasus Logistics just nominated for Best Place to Work in DFW!</a>',
	  'topOffset' : 2,
      'direction' : 'left',
      'speed' : 15,
      'increment' : 1,
      'middle_pause' : true,
      'middle_pause_time' : 2000
    },
    '1' : {
      'text' : '<a href="press/press_5.asp">Teradata Presents Pegasus Logistics with 2009 Supplier Excellence Award</a>',
	  'topOffset' : 2,
      'direction' : 'left',
      'speed' : 15,
      'increment' : 1,
      'middle_pause' : true,
      'middle_pause_time' : 2000
    },
    '2' : {
      'text' : '<a href="http://www.inc.com/inc5000/2007/company-profile.html?id=200731670" target="_blank">Pegasus named one of the fastest growing companies in America!</a>',
	  'topOffset' : 2,
      'direction' : 'left',
      'speed' : 15,
      'increment' : 1,
      'middle_pause' : true,
      'middle_pause_time' : 2000
    }
   
  }      
  var scrollObj = new scroller('scroller', textObj, 'scroll_text',textObj.length);
  scrollObj.init();
 
}