//*** 	AC-PhotoRotator
//*		Author: 		J. Corcoran 
//*		Last modified: 	7/17/2009
//* 	Dependency:  	Mootools 1.2.x core and More
//*		Parameters:  	elements: 			Class of the element that holds the image that will rotate
//*						rotateSpeed:		The speed at which the slideshow will move from slide to slide

var ACPhotoRotator = new Class({
	Implements: [Options,Chain],
	options: {
		element: 'rotatorCt',
		rotateSpeed: 6000
	}, 
	initialize: function(options){
		this.setOptions(options);
		this.arrImages = '';
		this.showStart = '';
		if($chk($(this.options.element))){
			this.arrImages = $(this.options.element).getChildren('img')[0].get('alt').split('|');
			this.arrImages = new Asset.images(this.arrImages,{
				onComplete: function(){
					this.arrImages[0].replaces($(this.options.element).getChildren('img')[0]);
					showStart = this.slideShow.periodical(this.options.rotateSpeed,this);
				}.bind(this)
			});
			rightArrow = new Element('a',{'class':'rightArrow','html':'&gt;'}).addEvent('click',function(e){this.moveNext(e);}.bind(this)).inject($(this.options.element),'top');
			leftArrow = new Element('a',{'class':'leftArrow','html':'&lt;'}).addEvent('click',function(e){this.movePrevious(e)}.bind(this)).inject($(this.options.element),'top');				
			$(this.options.element).addEvent('mouseenter',function(e){this.showArrows(e);}.bind(this));
			$(this.options.element).addEvent('mouseleave',function(e){this.hideArrows(e);}.bind(this));
		}
	},
	moveNext: function(e){
		$clear(showStart);
		el = e.target;
		currentImage = this.arrImages.indexOf($(this.options.element).getChildren('img')[0]);
		nextImage = ((currentImage+1) >= this.arrImages.length)? 0 : currentImage + 1;
		var fader = new Fx.Tween(el.getNext('img'));
		el.removeEvents('click');
		el.getPrevious('.leftArrow').removeEvents('click');
		fader.start('opacity',1,0).chain(function(){this.arrImages[nextImage].setStyle('opacity',0).replaces($(this.options.element).getChildren('img')[0]);this.switchSrc(el,nextImage);}.bind(this));				
	},
	movePrevious: function(e){
		$clear(showStart);
		el = e.target;
		currentImage = this.arrImages.indexOf($(this.options.element).getChildren('img')[0]);
		prevImage = ((currentImage-1) <= 0)? this.arrImages.length-1 : currentImage-1;
		var fader = new Fx.Tween(el.getNext('img'));
		el.removeEvents('click');
		el.getNext('.rightArrow').removeEvents('click');
		fader.start('opacity',1,0).chain(function(){this.arrImages[prevImage].setStyle('opacity',0).replaces($(this.options.element).getChildren('img')[0]);this.switchSrc(el,nextImage);}.bind(this));				
	},
	switchSrc: function(el,index){
		var fader = new Fx.Tween(el.getNext('img'));
		fader.start('opacity',0,1).chain(function(){
			if(el.hasClass('leftArrow')){
				el.addEvent('click',function(e){this.movePrevious(e)}.bind(this));
				el.getNext('.rightArrow').addEvent('click',function(e){this.moveNext(e)}.bind(this));			
			}else{
				el.addEvent('click',function(e){this.moveNext(e)}.bind(this));
				el.getPrevious('.leftArrow').addEvent('click',function(e){this.movePrevious(e)}.bind(this));			
			}
		}.bind(this));
		$(this.options.element).addEvent('mouseenter',function(e){this.showArrows(e);}.bind(this));
		$(this.options.element).addEvent('mouseleave',function(e){this.hideArrows(e);}.bind(this));
	},
	slideShow: function(){
		currentImage = this.arrImages.indexOf($(this.options.element).getChildren('img')[0]);
		nextImage = ((currentImage+1) >= this.arrImages.length)? 0 : currentImage + 1;
		var fader = new Fx.Tween($(this.options.element).getChildren('img')[0]);
		$$('#'+this.options.element + ' .rightArrow')[0].removeEvents('click');
		$$('#'+this.options.element + ' .leftArrow')[0].removeEvents('click');
		fader.start('opacity',1,0).chain(function(){this.nextSlide(nextImage)}.bind(this));						
	},
	nextSlide:function(index){
		this.arrImages[index].setStyle('opacity',0).replaces($(this.options.element).getChildren('img')[0]);
		var fader = new Fx.Tween($(this.options.element).getChildren('img')[0]);
		fader.start('opacity',0,1).chain(function(){
			$(this.options.element).getChildren('.leftArrow')[0].addEvent('click',function(e){this.movePrevious(e)}.bind(this));
			$(this.options.element).getChildren('.rightArrow')[0].addEvent('click',function(e){this.moveNext(e)}.bind(this));			
		}.bind(this));
		$(this.options.element).addEvent('mouseenter',function(e){this.showArrows(e);}.bind(this));
		$(this.options.element).addEvent('mouseleave',function(e){this.hideArrows(e);}.bind(this));
		
	},
	showArrows: function(e){
		e.target.getAllPrevious('a').setStyle('display','block');
		//console.log('Show!');
	},
	hideArrows: function(e){
		e.target.getAllPrevious('a').setStyle('display','none');	
		//console.log('Hide!');
	}
});

window.addEvent('domready',function(){var photoRotator = new ACPhotoRotator();});