﻿/*******************public methods ********************/

//internal master control object
var masterControl;

//initialize the master control
function initMasterControl(imageControlName, alphaIncrement, timeBetweenFadesMS, hiliteClass, lowliteClass, delayForImageLoad)
{
	//set up the internal master control object
	masterControl = new masterControlObject(imageControlName, alphaIncrement, timeBetweenFadesMS, hiliteClass, lowliteClass, delayForImageLoad);
	//send it back for use in the page
	return masterControl;
}

//run the master control
function startMasterControl(timer)
{
	//ensure the timer has a value
	if (!timer) {timer = 0; }
	//start the show at the timed interval
	masterControl.runFaderTimerId = setTimeout("masterControl.runFader();", timer);
}

function hilite(faderName){
	//find the fader in question
	var fader = masterControl.faders.getFaderByKey(faderName);
	//get the current fader
	var curFader = masterControl.faders.getCurrentFader();

	//stop the fading effects
	masterControl.stopFader();
	if (curFader)
	{
		//lowlite the "current" fader
		curFader.lowlite();
	}
	//show the hilite on this fader
	masterControl.showFader(fader);
}

function lowlite(faderName){
	//find the fader in question
	var fader = masterControl.faders.getFaderByKey(faderName);
	//get the current fader
	var curFader = masterControl.faders.getCurrentFader();
	//check them
	if (fader != curFader)
	{
		//show the lowlite on this object
		fader.lowlite();
	}
	//continue running master control
	startMasterControl();
}

function linkTo(faderName)
{
	//find the fader in question
	var fader = masterControl.faders.getFaderByKey(faderName);
	//stop the master control, if it's running
	masterControl.stopFader();
	//link to the new url
	window.location.href=fader.url;	
}

/****************Object Model Code****************/
		
//master control, to control the fading and highlighting
function masterControlObject(imageControlName, alphaIncrement, timeBetweenFadesMS, hiliteClass, lowliteClass, delayForImageLoad)
{

	//the main image control that we fade around
	this.imageControlName = imageControlName;

	//the faders we'll be using
	this.faders = new FaderObjectCollection(hiliteClass, lowliteClass);
	
	//the alpha channel increment
	this.alphaIncrement = alphaIncrement;
	
	//the current alpha value
	this.currentAlpha = 1;
	
	//the ammount of time between fades
	this.timeBetweenFades = timeBetweenFadesMS;
	
	//the CSS class for hiliting the titles
	this.hiliteClass = hiliteClass;
	
	//the css class for lowliting the titles
	this.lowliteClass = lowliteClass;
	
	//the timeout value for delay between loading images
	this.delayForImageLoad = delayForImageLoad;
	
	//are we currently running
	this.inProgress = false;
	
	//store the timer id
	this.fadeInTimerId;
	this.fadeOutTimerId;
	this.runFaderTimerId;
	
	//start / stop var
	this.start = false;
	
	//get the image control
	this.getImageControl = getImageControl;
	function getImageControl()
	{
		return document.getElementById(this.imageControlName);
	}
	
	//run the fader process
	this.runFader = runFader;
	function runFader()
	{
		//set the start var
		this.start = true;
		//do the fade
		this.fade();
		//set up the timer to re-run
		this.runFaderTimerId = setTimeout("startMasterControl();", this.timeBetweenFades);
	}
	
	//stop the fade process
	this.stopFader = stopFader;
	function stopFader()
	{
		//set the stop vars
		this.start = false;
		this.inProgress = false;
		//stop the timers
		if (this.fadeInTimerId)
			clearTimeout(this.fadeInTimerId);
		if (this.fadeOutTimerId)
			clearTimeout(this.fadeOutTimerId);
		if (this.runFaderTimerId)
			clearTimeout(this.runFaderTimerId);
	}
	
	//start the actual fade...
	this.fade = fade;
	function fade()
	{
		//make sure we're not already running
		if (!this.inProgress && this.start){
			//we're running
			this.inProgress = true;
			//start fading
			this.fadeOut();
		}
	}
	
	this.fadeOut = fadeOut;
	function fadeOut()
	{
		//decrement the current alpha
		this.currentAlpha -= this.alphaIncrement;

		//check the start var
		if (!this.start)
		{
			//reset all alpha
			this.currentAlpha = 1;
			this.setAlpha();
			//exit the function
			return;
		}

		//ceck the current alpha value
		if (this.currentAlpha >= 0)
		{
			//set the alpha on the image
			this.setAlpha();
			//recursively call this method.
			if (this.start)
				this.fadeOutTimerId = setTimeout("masterControl.fadeOut();", 0);
		}
		else
		{
			//get the next fader
			this.faders.moveNextFader();
			
			//reset the alpha to zero, to completely hide.
			this.currentAlpha = 0;
			this.setAlpha();
			
			//get the previous fader, if there was one, and lowlite it
			var prevFader = this.faders.getPreviousFader();
			if (prevFader)
				prevFader.setControlClass(this.lowliteClass);
			
			//get the current fader
			var curFader = this.faders.getCurrentFader();

			//set the new image from the current fader
			this.showFaderImage(curFader);

			//set the css class for the link hilite
			curFader.setControlClass(this.hiliteClass);

			//should we start?
			if (this.start)
			{
				//start the fade in
				this.fadeInTimerId = setTimeout("masterControl.fadeIn();", this.delayForImageLoad);
			}
		}
	}
	
	this.fadeIn = fadeIn;
	function fadeIn()
	{
		//get the current fader
		var curFader = this.faders.getCurrentFader();

		//increment the alpha
		this.currentAlpha += this.alphaIncrement;

		//check the current alpha
		if (this.currentAlpha <= 1){
			//set the alpha
			this.setAlpha();
			//recursively call the fade in
			if (this.start)
				this.fadeInTimerId = setTimeout("masterControl.fadeIn();", 0);
		}
		else
		{
			//all done.
			this.inProgress = false;
			//reset the alpha to 1 to ensure full visibility
			this.currentAlpha = 1;
			this.setAlpha();
		}
	}
	
	//sets the alpha on the image control
	this.setAlpha = setAlpha;
	function setAlpha()
	{
		//find the image control to use
		var img = this.getImageControl();
		//set the alpha for firefox
		img.style.opacity = this.currentAlpha;
		//set the alpha for IE
		img.style.filter = "alpha(opacity:" + (this.currentAlpha * 100) + ")";
	}
	
	//set the image display
	this.showFaderImage = showFaderImage;
	function showFaderImage(fader)
	{
		//get the image control
		var img = this.getImageControl();
		//set the image source
		img.src = fader.image.src;
	}

	//show a specific fader when moused over
	this.showFader = showFader;
	function showFader(fader)
	{
		//hilite it
		fader.hilite();
		//show it's picture
		this.showFaderImage(fader);
		//set the alpha
		this.currentAlpha = 1;
		this.setAlpha();
	}

}

//the fader objects that we will fade between
function FaderObjectCollection(hiliteClass, lowliteClass)
{

	//the CSS class for hiliting the titles
	this.hiliteClass = hiliteClass;
	
	//the css class for lowliting the titles
	this.lowliteClass = lowliteClass;

	//the current fader we're working with
	this.currentFader = -1;
	
	//the previous fader index
	this.previousFader = -1;

	//the fader objects that we will fade between
	this.fadersKey = new FaderObject();
	this.fadersIndex = new Array();

	//add a fader
	this.addFader = addFader;
	function addFader(name, controlName, imageSrc, url)
	{
		var fader = new FaderObject(name, controlName, imageSrc, url, this.hiliteClass, this.lowliteClass);
		this.fadersKey[fader.name] = fader;
		this.fadersIndex[this.fadersIndex.length] = fader;
	}
	

	//get a fader by key
	this.getFaderByKey = getFaderByKey;
	function getFaderByKey(key)
	{
		return this.fadersKey[key];
	}
	
	//get a fader by index
	this.getFaderByIndex = getFaderByIndex;
	function getFaderByIndex(index)
	{
		return this.fadersIndex[index];
	}
	
	//get the current fader;
	this.getCurrentFader = getCurrentFader;
	function getCurrentFader()
	{
		return this.fadersIndex[this.currentFader];
	}
	
	//the previous fader, if any
	this.getPreviousFader = getPreviousFader;
	function getPreviousFader()
	{
		if (this.previousFader >= 0){
			return this.fadersIndex[this.previousFader];
		}
	}
	
	//move to the next fader
	this.moveNextFader = moveNextFader;
	function moveNextFader()
	{
		//set the previous
		this.previousFader = this.currentFader;
		
		//increment the current
		this.currentFader++;
		if (this.currentFader == this.fadersIndex.length)
		{
			this.currentFader = 0;
		}
	}
	
}

//the fader object that contains the image and control to highlight
function FaderObject(name, controlName, imageSrc, url, hiliteClass, lowliteClass)
{
	//create global cached object
	var image = new Image();
	image.src = imageSrc;
	//hold a reference locally
	this.image = image;
	
	//url for links
	this.url = url;
	
	//the CSS class for hiliting the titles
	this.hiliteClass = hiliteClass;
	
	//the css class for lowliting the titles
	this.lowliteClass = lowliteClass;
	
	//control to highlight
	this.controlName = controlName;
	this.getControl = getControl;
	function getControl() 
	{
		return document.getElementById(this.controlName);
	}
	
	//name of this object
	this.name = name;
	
	//hiliter functions for the control
	this.setControlClass = setControlClass;
	function setControlClass(className)
	{
		var ctl = this.getControl();
		ctl.className = className;
	}
	
	//do the hilite
	this.hilite = hilite;
	function hilite(){
		this.setControlClass(this.hiliteClass);
	}
	
	//do the lowlite
	this.lowlite = lowlite;
	function lowlite(){
		this.setControlClass(this.lowliteClass);
	}
}


