/*
 * Created on Sep 25, 2006
 * 
 * Copyright (c), Riada International Pty. Ltd.
 * 
 */

function Scroller(containerId, contentId, speed)
	{
	var scrollerInstance = this;	

	this.intervalTimer = null;
	
	this.containerId = containerId;
	this.contentId = contentId;
	this.speed = speed;
			
	this.initialDelay = 2000		// Initial pause before starting to scroll
	this.interval = 100;				// How many milliseconds before we get called back
	this.speed = speed;				// How many pixels we scroll per interval
	this.pause = false;
	
	this.contentHeight = ''

	// Get the browser to call initialise after the page loads
	if (window.addEventListener)
		window.addEventListener("load", function(){scrollerInstance.initialise()}, false);
	else if (window.attachEvent)
		window.attachEvent("onload", function(){scrollerInstance.initialise()});
	else if (document.getElementById)
		{
		setTimeout(function(){scrollerInstance.initialise()}, 500 + this.initialDelay);	// Legacy browsers - run initialisation after 500ms + initialDelay
		this.initialDelay = 0;															// Don't need this now...
		}
	}
	
Scroller.prototype.initialise = function()
	{
	var scrollerInstance = this;

	this.container = document.getElementById(this.containerId);
	this.content = document.getElementById(this.contentId);
	this.content.style.top = 0;
	
	this.containerHeight = this.container.offsetHeight;
	this.contentHeight = this.content.offsetHeight;

	// Set up the container
	this.container.style.position = "relative";
	this.container.style.overflow = "hidden";
	
	this.container.onmouseover = function(){scrollerInstance.onMouseOver()};
	this.container.onmouseout = function(){scrollerInstance.onMouseOut()};
	
	// Make sure the content area is set correctly
	this.content.style.position = "absolute";
	
	if (window.opera || navigator.userAgent.indexOf("Netscape/7") != -1) // If Opera or Netscape 7x, add scrollbars to scroll and exit
		{
		this.content.style.height = this.containerHeight + "px";
		this.content.style.overflow = "scroll";
		
		return;
		}

	// Set timer to call scroll after initial delay		
	setTimeout(function(){scrollerInstance.scroll()}, this.initialDelay);
	}
	
Scroller.prototype.scroll = function()
	{
	if (this.pause)
		return;
		
	var scrollerInstance = this;
	
	var top = this.content.style.top;
	var contentHeight = this.contentHeight;
	var cond1 = (parseInt(this.content.style.top) > (this.contentHeight * (-1) + 8));
	var val1 = parseInt(this.content.style.top) - this.speed + "px";
	
	if (parseInt(this.content.style.top) + 40 > (this.contentHeight * (-1) + 8))
		this.content.style.top = parseInt(this.content.style.top) - this.speed + "px";
	else
		this.content.style.top = parseInt(this.containerHeight) + 8 + "px";
	
	// Set up timer to call back this function after interval
	if (!this.intervalTimer)
		this.intervalTimer = setInterval(function(){scrollerInstance.scroll()}, this.interval);
	}
	
Scroller.prototype.onMouseOver = function()
	{
	this.pause = true;	
	}
	
Scroller.prototype.onMouseOut = function()
	{
	this.pause = false;
	}
	
var count = 0;

function debugOut(message)
	{
	if (count++ > 100)
		return;
		
	var debugmessage = document.getElementById("debugmessage");

	debugmessage.value += message + "\n";
	}
