google.load("scriptaculous","1.8.2");
if (typeof console == "undefined") { var console = {}; console.log=function(){}; } 

var rotatorSlideshow = 0;
var current_banner_id = 1;
var total_banners = 0;

var hideBanner = function(banner,index) { 
	banner.setStyle({zIndex:80+parseInt(index),left: '900px'}); 
	banner.select('.content').each(function(item,i) {
			item.fade({duration:0});
		});
	};

var showBanner = function(index) {
	index = parseInt(index);
	if (index > total_banners) { index = 1; } 
	
	if (index != current_banner_id) {
	
		//hide all banners
		$$('.banner').each(function(banner,index) { 
				//don't hide the current banner off to the right
				if(banner.id != 'Banner'+current_banner_id) { hideBanner(banner,index); }
				//but do z-index it under the new banner
				else { $('Banner'+current_banner_id).setStyle({zIndex:79}); }
			});
		
		//slide IN bg to make NEW banner selected
		$('Navitem_'+index).addClassName("selected");

		$$('.navitem').each(function(banner,i) { 
			if(banner.id != "Navitem_"+index) {
				banner.removeClassName("selected"); 
			}
		});
		
		//console.log("current: " + $('Banner'+current_banner_id).getStyle("zIndex"));
		//console.log("new: " + $('Banner'+index).getStyle("zIndex"));
		new Effect.Morph('Banner'+index,{
			style:'left: 0px',
			duration: .5,
			afterFinish:function(){
					$('Banner'+index).select('.content').each(function(item,i) { item.appear({duration:0}); });
					current_banner_id = index;
				}
			});
		
	}
};
var setupRotator = function() {
	//console.log('setting up rotator...');

	rotatorSlideshow = setInterval(function(){showBanner(current_banner_id+1)},5000);
	
	$$('.navitem').each(function(item,index) {
		total_banners++;
		
		item.observe('click', function() { 
			var banner_id = $(this).id.substring(8,$(this).id.length);
			showBanner(banner_id); 
			clearInterval(rotatorSlideshow);
		});  //observer
	}); //navitem each
	
	$('Banner1').select('.content').each(function(item,i) { item.appear({duration:.5}) });
	
}; // setup Rotator
Event.observe(window, 'load', function() {setupRotator(); });

		
