//=============================================================================
//Program:			CM Callout Rotator
//Version:			1.0
//Date:				12/18/2006
//Last Modified:	12/18/2006
//=============================================================================
var rotator = null;
var rotationInterval = 0;
var isRandom = 1;

//Initial rotation.
function DisplayCallouts(cookieName, defaultId, currentId, isRandom1, refreshRate)
{
	//set default based on cookie
	isRandom = isRandom1;
	var anchorContainer = document.getElementById("calloutLinks");
	var contentDestination = document.getElementById(currentId);
	rotator = new CalloutRotator(refreshRate, anchorContainer, contentDestination, "currentAnchor");
	if(isRandom == 0)
	{
		rotator.isRandomSet = true;
	}
	StartRotator(rotator, defaultId, cookieName);
	
	//set the cookie for next time
	if(defaultId != 0)
	{
		Set_Cookie(cookieName, 'True', 365, '', '','');
	}
}

function LoadCallout(CalloutName) {
	try {
		rotator.OnHover(CalloutName);
	} 
	catch(er) {
		// throw er;
	} 
}
function MouseOutResumeRotator() {
	try { 
		rotator.Resume();
	} 
	catch(er) {
		// throw er;
	}
} 
			
function StartRotator(calloutRotator, defaultId, cookieName){
	rotator = calloutRotator;
	if(Get_Cookie(cookieName) == 'True' || defaultId == '0')
	{
		//go ahead normally
		RotateCallouts();
	}
	else
	{	
		//set to default
		var index = 0;
		for(var i = 0; i < 	rotator.callouts.length; i++)
		{			
			if(rotator.callouts[i] == defaultId)
			{
				index = i;
			}
		}	
		rotator.SetCurrentCallout(rotator.callouts[index]);	
	}
	
	ResumeRotator();
}

//Starts the callout rotation.
function ResumeRotator(){	
	rotationInterval = setInterval("RotateCallouts()", rotator.rotationDelay);
}

//Rotates the callouts.
function RotateCallouts(){	
	
	var upperBound = rotator.calloutsCount -1;	
	if(!rotator.isRandomSet) {
		if(isRandom == 1)
		{
			rotator.currentCalloutIndex = GetRandomNumber(upperBound);
			rotator.isRandomSet = true;
		}
		else
		{
			rotator.currentCalloutIndex = GetNextNumber(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] ); 	
}

//Public constructor.
function CalloutRotator(rotationDelay, anchorContainer, contentDestination, cssHoverClass){
	this.calloutAnchors = this.LoadCalloutAnchors( anchorContainer.getElementsByTagName('a') );
	this.callouts = this.LoadCalloutsNameMap();
	this.calloutsCount = this.GetAnchorsCount();
	this.contentDestination = contentDestination;
	this.cssHoverClass = cssHoverClass;
	this.currentCalloutIndex = -1;
	this.hasBeenMousedOver = false;
	this.isRandomSet = false;
	this.rotationDelay = rotationDelay*1000;
}

//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.
CalloutRotator.prototype.LoadCalloutsNameMap = 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.
CalloutRotator.prototype.LoadCalloutAnchors = function(anchorElements){	
	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;
			//Dear Jesus, Why is IE so dumb!
			if(mouseOverEventString != null){	
				calloutName = mouseOverEventString.substring(mouseOverEventString.indexOf("'") + 1, mouseOverEventString.lastIndexOf("'"));	
				if(document.getElementById(calloutName) != null) {
					validAnchors[calloutName] = currentAnchor;
				}
			}
		}
	}	
	return validAnchors;
}

//Pauses callout rotation and sets the current callout.
CalloutRotator.prototype.OnHover = function(calloutName){	
	clearInterval(rotationInterval);
	this.SetCurrentCallout(calloutName);
}

//Pauses callout rotation and sets the current callout.
CalloutRotator.prototype.Resume = function(){	
	this.hasBeenMousedOver = true;
	ResumeRotator();
}

//Inserts the the content of the callout into the contentDestination
CalloutRotator.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.
CalloutRotator.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.
CalloutRotator.prototype.ClearCalloutHighlighting = function(){	
	for(var name in this.calloutAnchors){
		this.calloutAnchors[name].className = "";
	}
}

//Returns the number of valid callout anchors;
CalloutRotator.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("GetRandomNumber error: " + oError);
}
}

function Set_Cookie( name, value, expires, path, domain, secure ) 
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
	expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function Get_Cookie( name ) {
	
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) &&
	( name != document.cookie.substring( 0, name.length ) ) )
	{
	return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

/* ========== Search Function ========== */

	function runSearch() {
	var search = "";					
	var temp = document.getElementById('search');
	if(temp != null){search = temp.value;}
	window.location = "/search-results.aspx?search=" + search;}
						
/* ========== END SEARCH FUNCTION ========== */

/* ============ CLEAR DEFAULT INPUT VALUE ============ */
function clickclear(thisfield, defaulttext) {
if (thisfield.value == defaulttext) {
thisfield.value = "";
}
}
function clickrecall(thisfield, defaulttext) {
if (thisfield.value == "") {
thisfield.value = defaulttext;
}
}

/* ========== END CLEAR DEFAULT INPUT VALUE ========== */


/* ========== FAQ Function ========== */
function toggleAnswer(nodeID)
{
	var answer = document.getElementById('sp' + nodeID);
	if(answer)
	{
		if(answer.className == "answer")
		{
			answer.className = "answerOpen";
		}
		else
		{
			answer.className = "answer";
		}
	}
	var li = document.getElementById('li' + nodeID)
	if(li)
	{
		if(li.className == "current")
		{
			li.className = "";
		}
		else
		{
			li.className = "current";
		}
	}

}
/* ========== END FAQ FUNCTION ========== */