var bannerContainer;
var pauseContainer;
var backContainer;
var nextContainer;
var informationContainer;
var contentWidth;
var contentHidden;
var imageArrayRotator;
var imageSpecialArray;
var specialNameArray = new Array();
var currentSash = '';
var sashID = '';
var sashLink = '';
var slider;
var activeColor = "#fbb324";
var inactiveColor = "#049bc6";
var borderOff = "#000";
var borderOn = "#d03d1e";
//imageArrayRotator.sort(randOrd);
var rotateSize;
var rotateSpecialSize;
var currentDisplay;
var oldSrc = false;
var scrollingActive = false;
var rotatorDelay = 4000;

var systemPause = false;

//Once the site has loaded start the rotator
//Event.observe(window, 'load', function() {	
function rotatorInit() {
	//The primary container
	bannerContainer = $("banner");
	
	//Pause Container
	pauseContainer = $("pause");
	new Effect.Opacity(pauseContainer, {
		from: 1,
		to: 0,
		duration: 0.0
	});
	pauseContainer.show();
	
	//Control Containers
	backContainer = $("controlBack");
	nextContainer = $("controlNext");
	
	new Effect.Opacity(backContainer, {
		from: 1,
		to: 0,
		duration: 0.0
	});
	backContainer.show();
	new Effect.Opacity(nextContainer, {
		from: 1,
		to: 0,
		duration: 0.0
	});
	nextContainer.show();
	
	backContainer.observe('click', function(event) {
		rotateImages(-1);
	});
	nextContainer.observe('click', function(event) {
		rotateImages(1);
	});
	
	//Information Container
	informationContainer = $("information");
	new Effect.Opacity(informationContainer, {
		from: 1,
		to: 0,
		duration: 0.0
	});
	informationContainer.show();
	
	//All the main images
	imageArrayRotator = $$("img.rotate");
	
	//Set up links
	imageArrayRotator.each(function(s, index) {
		if(s.readAttribute('rel')) {
			s.observe('click', function(event) {
				document.location.href = s.readAttribute('rel');
			});
		}
	});
	
	//Gather the special sashes
	imageSpecialArray = $$("img.rotateSash");
	
	//imageArrayRotator.sort(randOrd);
	rotateSize = imageArrayRotator.size()-1;
	currentDisplay = rotateSize+1;
	
	initRotator();

	//Rotator Pause
	Event.observe(bannerContainer, 'mouseenter', function(e) {
		pauseRotator(true);
	});
	Event.observe(bannerContainer, 'mouseleave', function(e) {
		pauseRotator(false);
	});
}

function randOrd() {
	return (Math.round(Math.random())-0.5);
} 

function rotateImages(manualChoice) {
	//Normal timed rotator
	if(!systemPause && !manualChoice) {
		currentDisplay++;
	//Paused with manual selection
	}else if(systemPause && manualChoice !== false) {
		currentDisplay+=manualChoice;
	//Paused
	}else if(systemPause && !manualChoice) {
		return;
	}
		
	if(currentDisplay > rotateSize) {
		currentDisplay = 0;
	}else if(currentDisplay < 0) {
		currentDisplay = rotateSize;
	}
	
	fadeDelay = 2.0;
	if(manualChoice !== false) {
		fadeDelay = 0.5;
	}

	imageArrayRotator.each(function(q, index) {
		if(index == currentDisplay) {
			//display the special sash if one is called for
			if(sashID = q.readAttribute('rel')) {
				sashLink = $(sashID).readAttribute('rel')
				//Initialize Current Sash
				if(!currentSash) {
					currentSash = sashID;
					$(sashID).appear({ duration: fadeDelay });
				}else if(currentSash != sashID) {
					$(sashID).appear({ duration: fadeDelay });
				}
				
				q.observe('click', function(event) {
					document.location.href = sashLink;
				});
				$(sashID).observe('click', function(event) {
					document.location.href = sashLink;
				});
				
				q.setStyle({
					cursor: 'pointer'
				});

			}
			
			q.appear({ duration: fadeDelay });
			infoText = '';
			if(q.readAttribute('alt')) {
				infoText = q.readAttribute('alt') + ' - ';
			}
			informationContainer.update(infoText + (currentDisplay+1)+' of '+(rotateSize+1));
		}else{
			if(q.visible()) {
				q.fade({ duration: fadeDelay });
				q.setStyle({
					cursor: 'default'
				});
				q.stopObserving('click');
				
				if(sashID) {
					$(sashID).stopObserving('click');
				}
			}
		}
		
		if(currentSash != sashID) {
			$(currentSash).fade({ duration: fadeDelay });
			currentSash = sashID;
		}
	});
}

function initRotator() {
	rotateImages(false);
	rotatorTimer = setInterval("rotateImages(false)", rotatorDelay);
}

function pauseRotator(pause) {
	if(pause) {
		systemPause = true;
		
		new Effect.Opacity(pauseContainer, {
			from: 0,
			to: 0.5,
			duration: 0.5
		});
		
		new Effect.Opacity(backContainer, {
			from: 0,
			to: 0.5,
			duration: 0.5
		});
		new Effect.Opacity(nextContainer, {
			from: 0,
			to: 0.5,
			duration: 0.5
		});
		
		new Effect.Opacity(informationContainer, {
			from: 0,
			to: 0.5,
			duration: 0.5
		});
	}else{
		systemPause = false;
		
		new Effect.Opacity(pauseContainer, {
			from: 0.5,
			to: 0,
			duration: 0.5
		});
		
		new Effect.Opacity(backContainer, {
			from: 0.5,
			to: 0,
			duration: 0.5
		});
		new Effect.Opacity(nextContainer, {
			from: 0.5,
			to: 0,
			duration: 0.5
		});
		
		new Effect.Opacity(informationContainer, {
			from: 0.5,
			to: 0,
			duration: 0.5
		});
	}
}
