// self contained scroller class

var scroller = function(containerId, textObj, classText){
	this.timerVal = '';
  this.posX = 0;
  this.textObj = textObj;
  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');
  
  tempNode.innerHTML = this.textObj[0].text;
  this.textNode = tempNode;
  this.textNode.style.display = 'block';
  this.textNode.style.position = 'absolute';
  this.textNode.style.top = this.textObj[0].topOffset;

  this.testNode = testNode;
  this.testNode.style.visibility = 'hidden';
  this.testNode.innerHTML = this.textObj[0].text;

//  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;
  
//	Set Current Text
	this.currentTextIndex = 0;	
	
//	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 = 0;
  	  }
  	  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 / 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 = 0;
  	  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 allll off.
	this.init = function(){
//  Make sure things re-adjust if the window is resized  
    window.onresize = this.resizeClosure(this);

	  this.setSpeed();
	}	
}