/*
 * 	ImageRotator.js
 * 		2008 - Michael P. Schmidt for Department of Energy
 * 
 * v2 2009 - Michael P. Schmidt different design elements
 * 
 * 		ImageRotator.items = [
		{
			href: 'http://www.doe.gov',
			img: 'images/banner1.jpg', 
			alt: 'AdvancedMed is part of CSC Federal...',
			title: 'Welcome'		
		},
		{
			href: 'http://www.google.com',
			img: 'images/banner2.png', 
			alt: 'AdvancedMed 2 is part of CSC Federal...',
			title: 'Flu Shots'		
		},
		{
			
			href: 'http://www.microsoft.com',
			img: 'images/banner3.png', 
			alt: 'AdvancedMed 3   is part of CSC Federal...',
			title: 'Health News & Tips'		
		},
		{
			href: 'http://www.hanford.gov',
			img: 'images/banner4.png', 
			alt: 'AdvancedMed 4 is part of CSC Federal...',
			title: 'Active Aging'		
		}
		
	]; 
	
	ImageRotator.divImageRotator = "overlayLoc";
	ImageRotator.useTabs == boolean
	
	if not using tab interface the following items need to appear:
		ImageRotator.divImageRotatorCounter = "imageLocCounter";
		ImageRotator.divImageRotatorTitle = "imageLocTitle";
		
			
	ImageRotator.useTransition = boolean; 
	 If true then uses Scriptaculous Event.Appear 
				
	ImageRotator.intervalWaitMS = 4000;
	ImageRotator.imageWidth = 640;
	ImageRotator.imageHeight = 270;
 * 
 */	
ImageRotator = Class.create();
ImageRotator.padding = 7;
ImageRotator.divImageRotator = undefined;
ImageRotator.image= undefined;
ImageRotator.imageWidth = 667;
ImageRotator.imageHeight = 238;
ImageRotator.buttonList = new Array();
ImageRotator.activeButton = undefined;
ImageRotator.rotatingInterval = undefined;
ImageRotator.currentIndex = undefined;
ImageRotator.intervalWaitMS = 5000;
ImageRotator.items = undefined;
ImageRotator.useTabs = true;
ImageRotator.swapActive = function(newActive)
{
	
	if ( this.activeButton)
		this.activeButton.setActive(false);
	
	this.activeButton = newActive;
	this.activeButton.setActive(true);	
}
ImageRotator.loadFromDiv = function(){
	var tmp = $(this.divImageRotator)
	
		this.items = new Array();
	var tmpStr = new Object();
	var children = tmp.getElementsByTagName("a");
	for (i = 0; i < children.length; i++) {
		tmpStr = new Object();
		tmpStr.href = children[i].getAttribute("href");
		tmpStr.img = children[i].getAttribute("img");
		tmpStr.alt = children[i].getAttribute("alt");
		tmpStr.title = children[i].innerHTML;
		tmpStr.image = new Image();
		tmpStr.image.src = tmpStr.img;
		this.items.push(tmpStr);
	}
	//tmp.childElements.
	
}


ImageRotator.initialize = function()
{
	
	if ( ! this.divImageRotator )
		return;
	if (! this.items)
	{
		//this.items = this.demo();
		this.loadFromDiv();
		
	}
	
	this.setImageRotator(this.items);
}

ImageRotator.setImageRotator = function(itms)
{
		var lastLeft = 0;
	var elemWidth = 162;
	this.buttonList.length = 0;
	$(this.divImageRotator).innerHTML = "";
	
	if ( ! itms.length)
		itms = this.demo();

	var totPadding = this.padding * (itms.length -1 );
	 
	 elemWidth = parseInt(( this.imageWidth - totPadding ) / itms.length);
	
	for (var i = 0; i < itms.length; i++) {
		
		
		itms[i].elemWidth = elemWidth;
		itms[i].elemPos = lastLeft;
		
		this.buttonList.push(new this(itms[i]));
		
		lastLeft = elemWidth +lastLeft + this.padding;
		
	}
	
	this.swapActive(this.buttonList[0]);
	this.startRotating();
}
ImageRotator.resetImage = function()
{
 	this.image.style.height = "";

}
ImageRotator.setImage = function(elem)
{
	if ( this.image == undefined)
	{
		var img = new Element("img");
		
		this.image = img;
		img.width = this.imageWidth;
		img.height = this.imageHeight;
		this.image = $(this.divImageRotator).appendChild(img);
		this.image.addClassName("ImageRotator_Image");
		Event.observe(this.image, "click", this.clickImage.bindAsEventListener(this));

	} 
	try {
	if (Effect && this.useTransition )
	{
		Effect.Appear(this.image, {afterFinish: this.resetImage.bindAsEventListener(this)});
		this.image.style.display = "none";
	}
	} catch (e) {
	}
	this.image.src = elem.image.src;
	this.image.title = elem.alt;
	this.image.alt = elem.alt;

	if ( ! this.useTabs )
	{
		$(ImageRotator.divImageRotatorTitle).update(elem.alt);
	}
}

ImageRotator.clickImage = function()
{
	if (this.activeButton.href )
	{
		window.location.href = this.activeButton.href;
	}
}

ImageRotator.startRotating = function()
{
	if ( this.rotatingInterval )
		clearInterval(this.rotatingInterval);
	
	this.currentIndex = 0;
	this.rotatingInterval = setInterval(this.nextImage.bindAsEventListener(this), this.intervalWaitMS);
}

ImageRotator.stopRotating = function()
{
	if ( this.rotatingInterval)
		clearInterval(this.rotatingInterval);
	this.rotatingInterval = undefined;
	this.currentIndex = undefined;
}

ImageRotator.nextImage = function()
{
	this.currentIndex++;
	if ( this.currentIndex > (this.buttonList.length - 1) )
		this.currentIndex = 0;
	this.swapActive(this.buttonList[this.currentIndex]);
}

ImageRotator.demo = function()
{
var itms = [
		{
			href: 'http://www.doe.gov',
			img: 'images/banner1.jpg', 
			alt: 'AdvancedMed is part of CSC Federal...',
			title: 'Welcome'		
		}
		
	]
	return itms;
}
ImageRotator.counter = 1;
ImageRotator.prototype = {
	initialize : function(strItem ) {
		if (strItem) {
			for (var idx in strItem) {
				this[idx] = strItem[idx];
				
			}
		}
		var	elem = new Element("div");
		this.elem = elem;
		if ( ImageRotator.useTabs )
			elem.style.width = this.elemWidth;
			
		elem.style.top = 0;
		elem.style.left = this.elemPos;
		if ( !ImageRotator.useTabs )
		{
			elem.update(ImageRotator.counter);
			ImageRotator.counter++;
		} else {
			elem.update(strItem.title);
		}
		elem.addClassName("ImageRotator_Button");
		elem.addClassName("ImageRotator_Transparent");
		
		if ( ImageRotator.useTabs )
			$(ImageRotator.divImageRotator).appendChild(elem);
		else
			$(ImageRotator.divImageRotatorCounter).appendChild(elem);
		
		Event.observe(elem, "click", this.activatePress.bindAsEventListener(this));
		return this;
	},
	
	activatePress : function()
	{
		ImageRotator.stopRotating();
		ImageRotator.swapActive(this);
	
	},
	
	setActive : function(bActive)
	{
		if (bActive) {
			this.elem.removeClassName("ImageRotator_Button");
		
			this.elem.addClassName("ImageRotator_SelectedButton");
		} else {
			this.elem.addClassName("ImageRotator_Button");
		
			this.elem.removeClassName("ImageRotator_SelectedButton");
		
		}	
		ImageRotator.setImage(this);
		
	}
};