var rotators = new Array();
var rotatorIntervals = new Array();

function AddRotator(cookieName, defaultId, currentId, anchorsId, random, refreshRate)
{
	//set default based on cookie
	try
	{
		if(refreshRate == null)
		{
			refreshRate = 1000;
		}
		
		var isRandom;
		if(random == 1)
		{
			isRandom = true;
		}
		else
		{
			isRandom = false;
		}
		var anchorContainer = document.getElementById(anchorsId);
		var contentDestination = document.getElementById(currentId);
		var rotator = new Rotator(refreshRate, anchorContainer, contentDestination, "currentAnchor", isRandom, currentId);
		
		rotators[currentId] = rotator;
		Rotate(currentId);
		rotatorIntervals[currentId] = setInterval("Rotate('" + currentId + "')", rotator.rotationDelay);
		
	}
	catch(oError)
	{
		//alert("AddRotator error " + oError);
	}
}

function Load(currentId, CalloutName) {
	try {
		var rotator = rotators[currentId];
		rotator.OnHover(CalloutName);
	} 
	catch(er) {
		// throw er;
		//alert("Load error " + er);
	} 
}
function MouseOutResume(currentId) {
	try {
		var rotator = rotators[currentId];
	 
		rotator.Resume();
	} 
	catch(er) {
		//alert("MouseOutResume error " + er);
	}
} 

function Rotate(currentId){	
	try
	{
		var rotator = rotators[currentId];
		//alert(rotator.calloutsCount);
		var upperBound = rotator.calloutsCount -1;	
		if(!rotator.isRandomSet) {
			rotator.currentCalloutIndex = GetRandomNumber(upperBound);
			rotator.isRandomSet = true;
		}	
		if(rotator.hasBeenMousedOver){
			rotator.hasBeenMousedOver = false;
			--rotator.currentCalloutIndex;
		}	
		if(rotator.currentCalloutIndex == upperBound || rotator.currentCalloutIndex < 0)
			rotator.currentCalloutIndex = -1;
 		rotator.SetCurrentCallout( rotator.callouts[++rotator.currentCalloutIndex] );
 	}
 	catch(oError)
 	{
 		//alert("Rotate error " + oError);
 	}
}

function Rotator(rotationDelay, anchorContainer, contentDestination, cssHoverClass, isRandom, currentId){
	this.calloutAnchors = this.LoadAnchors( anchorContainer.getElementsByTagName('a') );
	this.callouts = this.LoadNameMap();
	this.calloutsCount = this.GetAnchorsCount();
	this.contentDestination = contentDestination;
	this.cssHoverClass = cssHoverClass;
	this.currentCalloutIndex = -1;
	this.hasBeenMousedOver = false;
	this.isRandomSet = false;
	this.rotationDelay = rotationDelay*1000;
	this.isRandom = isRandom;
	this.currentId = currentId;
}

//Creates a index based array that maps to the associative array of anchors.
//This is soley used to rotate the callouts in the order they appear on the page.
Rotator.prototype.LoadNameMap = function(){
	var calloutNames = new Array();
	var index = 0;
	for(var name in this.calloutAnchors){
		calloutNames[index++] = name;
	}
	return calloutNames;
}

//Looks for valid callout anchors. A valid anchor has an onMouseOver event 
//attatched.
Rotator.prototype.LoadAnchors = function(anchorElements){	
	//alert(anchorElements.length);
	var calloutName = "";	
	var currentAnchor = null;
	var mouseOverEventString = null;
	var validAnchors = new Array();	
	for(var index = 0; index < anchorElements.length; index++){			
		currentAnchor = anchorElements[index];
		if(currentAnchor.attributes.getNamedItem('onmouseover')){
			mouseOverEventString = currentAnchor.attributes.getNamedItem('onmouseover').nodeValue;
			//IE
			if(mouseOverEventString != null){	
				calloutName = mouseOverEventString.substring(mouseOverEventString.indexOf(",'") + 2, mouseOverEventString.lastIndexOf("'"));	
				if(document.getElementById(calloutName) != null) {
					validAnchors[calloutName] = currentAnchor;
				}
			}
		}
	}	
	return validAnchors;
}

//Pauses callout rotation and sets the current callout.
Rotator.prototype.OnHover = function(calloutName){	
    clearInterval(rotatorIntervals[this.currentId]);
	this.SetCurrentCallout(calloutName);
}

//Pauses callout rotation and sets the current callout.
Rotator.prototype.Resume = function(){	
	rotatorIntervals[this.currentId] = setInterval("Rotate('" + this.currentId + "')", this.rotationDelay);
}

//Inserts the the content of the callout into the contentDestination
Rotator.prototype.SetCurrentCallout = function(calloutName){
	
	this.HighlightCallout(calloutName);
	this.contentDestination.innerHTML = document.getElementById(calloutName).innerHTML;
	//alert(calloutName);
	//alert(document.getElementById(calloutName).innerHTML);
}

//Sets the cssHoverClass to the current anchor.
Rotator.prototype.HighlightCallout = function(calloutName){
	this.ClearCalloutHighlighting();
	try{
		this.calloutAnchors[calloutName].className = this.cssHoverClass;
	}catch(er){
		//debug = new CalloutRotatorDiagnostics(this);
		//debug.ShowStatus();
	}
}

//Removes the cssHoverClass from all callout anchors.
Rotator.prototype.ClearCalloutHighlighting = function(){	
	for(var name in this.calloutAnchors){
		this.calloutAnchors[name].className = "";
	}
}

//Returns the number of valid callout anchors;
Rotator.prototype.GetAnchorsCount = function(){	
	var anchorsCount = 0;	
	for(var name in this.calloutAnchors){
		anchorsCount++;
	}
	return anchorsCount;
}

//Returns a random number <= to the number of callouts.
function GetRandomNumber(upperBound){	
try
{
	var seed = new Date().getTime();
	seed = (seed*9301+49297) % 233280;
	seed = seed/(233280.0);
	return Math.ceil(seed*upperBound);
}
catch(oError)
{
	//alert("GetRandomNumber error: " + oError);
}
}

function GetNextNumber(upperBound){	
	try
	{
		if(rotator.currentCalloutIndex = upperBound)
		{
			return 0;
		}
		else
		{
			return (rotator.currentCalloutIndex + 1);
		}
	}
	catch(oError)
	{
		//alert("GetNextNumber error: " + oError);
	}
}
