﻿/* BannerImageRotator.ascx */
var bannerRotators = new Array();

function BannerRotator(prefix,count)
{
	this.count = count;
	this.prefix = prefix;
	
	//add to collection
	bannerRotators[prefix] = this;
	
	//initiate transitions
	if (this.count > 1)
		this.setTransitionTimeout();
}
BannerRotator.prototype.count = 0;
BannerRotator.prototype.currentIndex = 0;
BannerRotator.prototype.prefix = null;
BannerRotator.prototype.transitionTimeout = null;

BannerRotator.prototype.select =
	function(index)
	{
		//clear transition timeout
		window.clearTimeout(this.transitionTimeout);
	
		//set active item
		for (var i=0; i<this.count; i++)
		{
			var el = document.getElementById(this.prefix + "_Banner" + i);
			el.className = (i == index) ? "ActiveBannerImage" : "BannerImage";
			this.setLinkClass(i,(i == index) ? "Active" : "");
			
			if (i == index)
				bannerRotator_setOpacity(el, 1);
		}
		
		this.currentIndex = index;
	}

BannerRotator.prototype.setLinkClass =
	function(index,className)
	{
		var link = document.getElementById(this.prefix + "_Nav" + index);
		link.className = className;
	}
	

BannerRotator.prototype.setTransitionTimeout =
	function()
	{
		this.transitionTimeout = window.setTimeout("bannerRotator_transition('" + this.prefix + "');", 8000);
	}
	
BannerRotator.prototype.transition =
	function()
	{
		var index = this.currentIndex;
		var nextIndex = index + 1;
		var id, el;
		
		//determine next banner to show
		if (nextIndex >= this.count) nextIndex = 0;
		id = this.prefix + "_Banner" + nextIndex;
		el = document.getElementById(id);
		
		//update index
		this.currentIndex = nextIndex;
		
		//fade out existing 
		bannerRotator_fade(this.prefix + "_Banner" + index, "out", 1000, 20, 0);
		bannerRotator_fade(id, "in", 1000, 20, 0);
		
		//show banner image and set link
		el.className = "ActiveBannerImage";
		this.setLinkClass(index,"");
		this.setLinkClass(nextIndex,"Active");
		
		//set timeout for next transition
		this.setTransitionTimeout();
	}	
	
//helper methods
function bannerRotator_select(prefix,index)
{
	var rotator = bannerRotators[prefix];
	if (rotator) rotator.select(index);
}

function bannerRotator_transition(prefix)
{
	var rotator = bannerRotators[prefix];
	if (rotator) rotator.transition();
}

//fade methods
function bannerRotator_fade(id,direction,duration,steps,delay)
{
	var el = document.getElementById(id)
	if (el)
	{
		var state = el.getAttribute("fadestate");
		var perform = false;
		var level;
		
		//correct state on first pass
		if (!state)
		{
			if (direction == "in")
				state = "0";
			else
				state = "2";
		}
		
		if (direction == "in" && state == "0") //fade in if object is transparent
		{
			//set initial opacity to 0
			bannerRotator_setOpacity(el, 0);
			level = 0;
			perform = true;
		}
		else if (direction == "out" && state == "2") //fade out if object is opaque
		{
			//set initial opacity to 1
			bannerRotator_setOpacity(el, 1);
			level = 1;
			perform = true;
		}
		
		if (perform)
			window.setTimeout("bannerRotator_setOpacityRecursive('" + id + "'," + level + ",'" + direction + "'," + duration/steps + "," + 1/steps + ",true)",delay);
	}
}

function bannerRotator_setOpacity(el,level)
{
	el.style.opacity = level;
	el.style.MozOpacity = level;
	el.style.KhtmlOpacity = level;
	el.style.filter = "alpha(opacity=" + (level * 100) + ");";
}

function bannerRotator_setOpacityRecursive(id,level,direction,interval,increment,recursive)
{
	var el = document.getElementById(id);
	bannerRotator_setOpacity(el,level);	
	
	var newLevel, proceed;
	if (direction == "in")
	{
		newLevel = level + increment;
		if (newLevel > 1) {newLevel = 1;}
		proceed = (level < 1 && newLevel <= 1);
		if (!proceed) {el.setAttribute("fadestate","2");}
	}
	else if (direction == "out")
	{
		newLevel = level - increment;
		if (newLevel < 0) {newLevel = 0;}
		proceed = (level > 0 && newLevel >= 0);
		if (!proceed)
		{
			el.setAttribute("fadestate","0");
			el.className = "BannerImage";
		}
	}
	
	if (proceed && recursive)
		window.setTimeout("bannerRotator_setOpacityRecursive('" + id + "'," + newLevel + ",'" + direction + "'," + interval + "," + increment + ",true)", interval);
}
