var DropDownMenu = Class.create();

DropDownMenu.prototype = {
	initialize:function(menuElement) {
		menuElement.childElements().each(function(node){
			node.onmouseover = function() {
				node.addClassName('hover');
			}
			node.onmouseout = function() {
				node.removeClassName('hover');
			}
		});
	}
};

var rotator = Class.create({
	initialize:function(containerID, instanceName, showNumbers, slideLength, fadeLength, topZ, bottomZ){
		container = containerID;
		slideList = [];
		currentSlide = 0;
		nextSlide = 1;
		slideTime = 7;
		slideFade = 2;
		updater = null;
		navMenu = false;
		topZIndex = 1000;
		bottomZIndex = 999;
		
		if (!empty(slideLength)) {slideTime = slideLength}
		if (!empty(fadeLength)) {slideFade = fadeLength}
		if (!empty(topZ)){topZIndex = topZ}
		if(!empty(bottomZ)){bottomZIndex = bottomZ}
		
		$(container).childElements().each(function(el){
			slideList.push(el);
			if(slideList.length > 1){ el.hide();}
		});
		
		if (!empty(showNumbers) && showNumbers){
			$(container).insert(new Element('div',{'class':'slide_nav', id: containerID+'_nav'}));
			
			slideList.each(function(slide){
				$(containerID+'_nav').insert(new Element('a',{href:'javascript:' + instanceName + '.goToSlide('+slideList.indexOf(slide)+')',id:containerID+'_link_'+slideList.indexOf(slide)}).update(slideList.indexOf(slide)+1));

			});
			
			$(containerID + '_link_0').addClassName('current');
			navMenu = true;
		}
		
		updater = new PeriodicalExecuter(this.changeSlide, slideTime);
	},
	
	changeSlide:function(){
		slideList[currentSlide].style.zIndex = topZIndex;
		slideList[nextSlide].style.zIndex = bottomZIndex;
		new Effect.Fade(slideList[currentSlide], {duration:slideFade});
		new Effect.Appear(slideList[nextSlide],{duration:slideFade});
		
		if (navMenu){
			$(container+'_link_'+currentSlide).removeClassName('current');
			$(container+'_link_'+nextSLide).addClassName('current');
		}
		
		currentSlide = nextSlide;
		
		if(nextSlide < slideList.length - 1){
			nextSlide++;
		} else {
			nextSlide = 0;
		}
	},
	
	goToSlide:function(slideNumber){
		updater.stop();
		
		if(slideNumber != currentSlide){
			nextSlide = slideNumber;
			this.changeSlide();
		}
		
		updater = new PeriodicalExecuter(this.changeSlide, slideTime);
	}
});


