
//main class for ticker
function ticker(divID, delay, divCount)
{
	//number of elements
	this.m_divCount = divCount;
	
	//ID of the div to write the ticker to
	this.m_divID = divID;
	
	 //boolean to indicate whether mouse is currently over ticker
	this.m_bMouseOver = false;

	//index of message array for hidden div
	this.m_iElement = 1;
	
	//delay
	this.m_delay = delay;

	
	//document.write('<div id="'+this.m_divID+'" class="'+divClass+'" style="position: relative; overflow: hidden"><div class="innerDiv" style="position: absolute; width: 100%" id="'+this.m_divID+'1">'+content[0]+'</div><div class="innerDiv" style="position: absolute; width: 100%; visibility: hidden" id="'+this.m_divID+'2">'+content[1]+'</div></div>')
	var instance = this;
	
	//run onload in DOM2 browsers
	if (window.addEventListener)  {
		window.addEventListener("load", function() { instance.initialize(); }, false)	
	}
	
	//run onload in IE5.5+
	else if (window.attachEvent) {
		window.attachEvent("onload", function() { instance.initialize(); } )	
	}	

	//if legacy DOM browsers, just start scroller after 0.5 sec
	else if(document.getElementById) {
		setTimeout(function() { instance.initialize(); }, 500)
	}
}



// initialize() method - gets divs, sets positions, schedules animation
//---------------------------------------------------------------------
ticker.prototype.initialize = function() 
{
	var instance = this;
	
	//get div object
	this.m_divObject = ebid(this.m_divID)
	
	//get visible div - mandatory !!!
	this.m_visibleDivObject = ebid(this.m_divID + "1");
	
	//hidden div ?
	this.m_hiddenDivObject = ebid(this.m_divID + "2");
	
	//get top padding of the ticker div
	this.m_topPadding = parseInt(this.getTopPadding(this.m_divObject));


	//set width of inner DIVs to outer DIV's width minus padding (padding assumed to be top padding x 2)
	this.setWidths();
	//this.m_visibleDivObject.style.width = this.m_hiddenDivObject.style.width = this.m_divObject.offsetWidth - (this.m_topPadding * 2) + "px"


	this.getInline(this.m_visibleDivObject, this.m_hiddenDivObject);
	this.m_hiddenDivObject.style.visibility="visible";
	ebid(this.m_divID).onmouseover = function() { instance.m_bMouseOver = true; }
	ebid(this.m_divID).onmouseout = function() { instance.m_bMouseOver = false; }

	//Clean up loose references in IE
	if (window.attachEvent) {
		window.attachEvent("onunload", function() { instance.m_divObject.onmouseover = instance.m_divObject.onmouseout = null; } )
	}

	
	setTimeout( function() { instance.animateUp(); }, this.m_delay ) ;
	
}


ticker.prototype.setWidths = function()
{	
    var j = 1;
    var oDiv = null;
    
	for(j = 1; j <= this.m_divCount; j++)
	{
		oDiv = 	ebid(this.m_divID + j);
		oDiv.style.width = this.m_divObject.offsetWidth - (this.m_topPadding * 2) + "px";
	}
}



// animateUp() method - animates
//------------------------------
ticker.prototype.animateUp = function()
{
	var instance = this;

	if (parseInt(this.m_hiddenDivObject.style.top) > (this.m_topPadding + 5)) {
		this.m_visibleDivObject.style.top = parseInt(this.m_visibleDivObject.style.top) - 5 + "px";
		this.m_hiddenDivObject.style.top = parseInt(this.m_hiddenDivObject.style.top) - 5 + "px";
		setTimeout( function() { instance.animateUp() }, 50);
	}
	else {
		this.getInline(this.m_hiddenDivObject, this.m_visibleDivObject);
		this.swapDivs();
		this.getInline(this.m_visibleDivObject,this.m_hiddenDivObject);
		setTimeout( function() { instance.restart(); }, this.m_delay);
	}
}



// swapDivs() method - replaces between the hidden to the visible
//--------------------------------------------------------------------
ticker.prototype.swapDivs = function() 
{
	var i = this.m_iElement;
	var ceiling = this.m_divCount;
	this.m_iElement = (i+1 > ceiling) ? 1 : i+1;
	this.m_visibleDivObject = ebid(this.m_divID + this.m_iElement);	
	var iNext = (this.m_iElement + 1 > ceiling) ? 1 : this.m_iElement + 1;
	this.m_hiddenDivObject = ebid(this.m_divID + iNext);
}



// getInline() method 
//-------------------
ticker.prototype.getInline = function(divObject1, divObject2)
{
	divObject1.style.top = this.m_topPadding + "px";
	divObject2.style.top = Math.max(divObject1.parentNode.offsetHeight, divObject1.offsetHeight) + "px";
}



// restart() method - checks if mouse is over or not and if not restarts the animation
//----------------------------------------------------------------------------------------
ticker.prototype.restart = function() 
{
	var instance = this;
	
	//if mouse is currently over scoller, do nothing (pause)
	if (this.m_bMouseOver == true) {
		setTimeout( function() { instance.restart(); },100)
	}
	else
	{
		this.m_hiddenDivObject.style.visibility="visible";	
		this.animateUp();
	}
}


// getTopPadding() method - Populate the hidden div with the next message before it's visible
//----------------------------------------------------------------------------------------

ticker.prototype.getTopPadding = function(divObject)
{ 
	//get CSS padding value, if any
	if (divObject.currentStyle) {
		return divObject.currentStyle["paddingTop"];
	}
	//if DOM2
	else if (window.getComputedStyle) {
		return window.getComputedStyle(divObject, "").getPropertyValue("padding-top");
	}
	else {
		return 0;
	}	
}

