/**************************************************
 * CScroller, version 0.14
 * Copyright 2007 Ivengi BV. Benelux
 **************************************************/
/*
CONSTRUCTOR
new CScroller(ContainerDivId, ScrollerDivId, DelayInMs, ScrollerSpeed, PauseOnMouseOver, HaltTime)
ContainerDivId 		-> ID of the DIV that contains the scrolling div
ScrollerDivId	  	-> ID of the DIV that needs to be scrolled
DelayInMs					-> Time in Ms to wait after the page loads before scroller starts to scroll
ScrollerSpeed			-> Scroll speed (default: 25)
PauseOnMouseOver 	-> boolean (true/false) Scoller pauses on MouseOver
HaltTime					-> number -> time halt after scrolling a page, uninterrupted scrolling: 0

FUNCTIONS
StartScroller()   -> Initiates the script
MouseOver()				-> Manually call the pause scroll function (to use in for example a button) be sure to include MouseOut() as well
MouseOut()				-> Manually resume the scripts scrolling function (used with MouseOver())

###########
# EXAMPLE #
###########

<style type="text/css">
#Scrollercontainer {
  width: 300px;   
  height: 200px;  
  overflow: hidden;
}
</style>

<script type="text/javascript">
<script language="JavaScript" type="text/javascript" src="CScroller.js"></script>
var Scroll = new CScroller("Scrollercontainer", "vScroller", 1000, 1, 1, 1000);
Scroll.StartScroller();
</script>


<div id="Scrollercontainer">
  <div id="vScroller" style="position: absolute; width: 300px; border: 1px solid blue;"><br class="clear">
		<div>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Mauris hendrerit,</div> 
		<div>ipsum et lacinia aliquet, lacus leo pharetra ligula, vel vehicula enim quam id</div> 
		<div>tellus. Nam dolor lacus, accumsan eu, consectetuer et, adipiscing eget,  nibh.</div> 
		<div>Sed vel lorem sit  amet massa tempor vehicula. (...)</div>
  </div>
</div>

*/
/***********************************************
* Code based on Cross browser Marquee II 
* © Dynamic Drive (www.dynamicdrive.com)
***********************************************/

function CScroller(ContainerDivId, ScrollerDivId, DelayInMs, ScrollerSpeed, PauseOnMouseOver, HaltTime)
{
	this.ScrollerSpeed    = 1;     //Specify marquee scroll speed(needs to be 1)
  this.CopySpeed        = 1;		// variables used to define speed

	this.IntervalSpeed 		= ScrollerSpeed;
	
  this.ScrollerDivId    = ScrollerDivId;
  this.ContainerDivId   = ContainerDivId;
  
  this.DelayInMs        = DelayInMs;        // Specify initial delay before marquee starts to scroll on page in Ms
  this.PauseOnMouseOver = PauseOnMouseOver; //Pause Scroller onMousever (0=no. 1=yes)?
  

  this.PauseSpeed       = (this.PauseOnMouseOver == 0) ? this.CopySpeed : 0;
  
  this.ScrollerHeight   = '';
  this.ContainerHeight  = '';
  this.ScrollerDiv      = '';
  
  this.bSpeedAltered    = false;
  this.ScollPassed			= 0;
  this.HaltTime					= HaltTime;
  this.TimeOut          = null;
  this.startDelay 			= null;
  this.startDelayRunning = false;
}

CScroller.prototype.ScrollScroller = function()
{
	var _this = this;
	if (this.ScollPassed < this.ContainerHeight) {
		if(this.CopySpeed > 0) {
		  if (parseInt(this.ScrollerDiv.style.top) > ((this.ScrollerHeight)  * (-1))) {
		  	tempSpeed = this.CopySpeed;
		  	if((this.ScollPassed + tempSpeed) > this.ContainerHeight) {
		  		tempSpeed = this.ContainerHeight - (this.ScollPassed + tempSpeed);
		  	}
		    this.ScrollerDiv.style.top = parseInt(this.ScrollerDiv.style.top) - tempSpeed + "px";
		    this.ScollPassed += tempSpeed;
		  } else {
		    this.ScrollerDiv.style.top = 0; //parseInt(this.ContainerHeight);
		  }
		} else {
			if (parseInt(this.ScrollerDiv.style.top) < 0 ) {
		  	tempSpeed = this.CopySpeed;
		  	if((this.ScollPassed + tempSpeed) > this.ContainerHeight) {
		  		tempSpeed = this.ContainerHeight - (this.ScollPassed + tempSpeed);
		  	}
		    this.ScrollerDiv.style.top = parseInt(this.ScrollerDiv.style.top) - tempSpeed + "px";
		    this.ScollPassed += tempSpeed;
		  } else {
		    this.ScrollerDiv.style.top = ((this.ScrollerHeight)  * (-1)); //parseInt(this.ContainerHeight);
		  }
		}
	} else {
		if(!this.bSpeedAltered) {
			this.CopySpeed = this.PauseSpeed;
			this.TimeOut = setTimeout(function() {_this.Resume(); }, this.HaltTime );
		}
		this.ScollPassed = 0;
	}
}

CScroller.prototype.AlterDefaultSpeed = function(newDefaultSpeed)
{
	this.ScrollerSpeed = newDefaultSpeed;
	this.CopySpeed = newDefaultSpeed;
}

CScroller.prototype.DelayScrollScroller = function()
{
	this.startDelayRunning = false;
  var _this = this;
  var execfunction = function() {_this.ScrollScroller(); };
  
  this.timer = new CTimer(execfunction,this.IntervalSpeed);
  this.timer.init();
}

CScroller.prototype.Resume = function()
{
	this.bSpeedAltered = false;
  this.CopySpeed = this.ScrollerSpeed;
}

CScroller.prototype.MouseOver = function()
{
  this.CopySpeed = this.PauseSpeed;
  clearTimeout(this.TimeOut);
}

CScroller.prototype.MouseOut = function()
{
  this.CopySpeed = this.ScrollerSpeed;
}

CScroller.prototype.AlterSpeed = function(newSpeed)
{
  clearTimeout(this.TimeOut);
  if (this.startDelayRunning)
  {
  	clearTimeout(this.startDelay);
 		this.DelayScrollScroller();
	}
	this.bSpeedAltered = true;
  this.CopySpeed = newSpeed;
}


CScroller.prototype.InitializeScroller = function()
{
  var _this = this;
  
  this.ScrollerDiv = document.getElementById(this.ScrollerDivId);
  if (!this.ScrollerDiv) {
    alert("ScrollerDivId(\"" + this.ScrollerDivId + "\") not found ");
  }
  
  var ContainerDiv = document.getElementById(this.ContainerDivId);
  if (!ContainerDiv) {
    alert("ContainerDiv(\"" + this.ContainerDiv + "\") not found ");
  }
  
  ContainerDiv.onmouseover = function() { _this.MouseOver(); };
	ContainerDiv.onmouseout  = function() { _this.MouseOut(); };
	
  this.ScrollerDiv.style.top = 0;
  this.ContainerHeight = ContainerDiv.offsetHeight;
  this.ScrollerHeight  = this.ScrollerDiv.offsetHeight;
  
  var bScroll = true;
  if (this.ScrollerHeight <= this.ContainerHeight) {
  	bScroll = false;
  }
  
  if ((navigator.userAgent.indexOf("Netscape/7") != -1)) {
 //if (window.opera || (navigator.userAgent.indexOf("Netscape/7") != -1)) { 
    //if Opera or Netscape 7x, add scrollbars to scroll and exit... change v0.13: Opera works?
    this.ScrollerDiv.style.height = this.ScrollerHeight + "px";
    this.ScrollerDiv.style.overflow = "scroll";
  } else {
  	if (!bScroll) {
  		this.ScrollerDiv.style.height = this.ScrollerHeight + "px";
    	this.ScrollerDiv.style.overflow = "hidden";
  	} else {
    	this.ScrollerDiv.innerHTML += 	this.ScrollerDiv.innerHTML;
    	this.startDelayRunning = true;
    	this.startDelay = setTimeout(function() { _this.DelayScrollScroller(); }, this.DelayInMs);
    }
  }
}

CScroller.prototype.StartScroller = function()
{
  var _this = this;
  var Fn = function() {_this.InitializeScroller(); };
  
  if (typeof window.onload == 'function') {
  	var FnOnload = window.onload;
  	window.onload = function() {
  	  Fn();
  	  FnOnload();
  	}
  } else {
	  window.onload = Fn;
	}  
}

function CTimer(evalfunction,intervalTime)
{
	this.evalfunction = evalfunction;
	this.counter = 0;
	this.stopped = true;
	this.start = 0;
	this.intervalTime = intervalTime;
	this.interval1 = null;
	
	this.previousTime = 0;
}

CTimer.prototype.init = function()
{
	var d = new Date();
	this.start = d.getTime();
	var _this = this;
	this.interval1 = setInterval(function() {_this.intervaltick(); }, 15);
	this.stopped = false;
}
	
CTimer.prototype.intervaltick = function()
{
	if(!this.stopped) {
		var d = new Date();
		current = d.getTime();
		var diff = this.previousTime - current;
		this.previousTime = current
		if (current < (this.start + (this.intervalTime*this.counter))) {
		} else {
		  var executions = ((current - this.start) / this.intervalTime) - this.counter;
		  for(var j = 0;j < executions;j++) {
				this.evalfunction();
				this.counter++;
			}
		}
	}
}

CTimer.prototype.stop = function()
{
	this.stopped = true;
	clearInterval(this.interval1);
	return this.counter;
}