var BannerManager = new Class({
	Binds: ['showImage','startSlideShow','stopSlideShow','nextImage','previousImage','thumbnailClick'],
    Implements: [Events, Options],
    options:{
        mainImgId: 'primary-banner-image',
		slideShowInterval: 6000,
		next : 'arrow-right',
		previous : 'arrow-left',
		captionClass : false,
		stopSlideShowOnClick : false
    },
	
	/*********************************
	* Mootools dependancies : Core, More: Reveal
	*
	*
	*
	*********************************/
	
	initialize:function(mainImgId,thumbnailSelector, options){
        
		this.setOptions(options);
		this.busy = false;
		this.mainImg = $(mainImgId);
		this.currentImageIndex = 0;
		this.thumbnailSelector = thumbnailSelector;
				
		//add events to thumbnails
		this.thumbnails = $$(thumbnailSelector);
		this.numThumbs = this.thumbnails.length;
		
		this.thumbnails.each(function(el){
			this.boundThumbnailClickHandler = this.thumbnailClick.bind(this, el);
			this.options.stopSlideShowOnClick ? el.addEvent('click',this.stopSlideShow) : 0;
			el.addEvent('click', this.boundThumbnailClickHandler);			
		},this);
		
		//add 'current' class to first thumbnail
		this.thumbnails[0] ? this.thumbnails[0].addClass('current') : 0;
		
		//add events to next / previous buttons
		$(this.options.next).addEvent('click',this.nextImage);
		$(this.options.previous).addEvent('click',this.previousImage);
		
		//hide all captions except first one
		if(this.options.captionClass){
			
			this.captions = $$('.'+this.options.captionClass);			
			this.captions.setStyle('visibility','hidden');
			this.captions[0].setStyle('visibility','visible');
		} else {
			this.captions = false;
		}
				
	},
	
	
	/**************************
	* IMAGE SWAPPING / LOADING
	**************************/
	
	thumbnailClick : function(el){
		if(this.busy)
			return false;
			
		this.busy = true;
		
		this.previousImageIndex = this.currentImageIndex;
		this.currentImageIndex = this.thumbnails.indexOf(el);
		this.showImage(this.currentImageIndex);
		return false;
	},
	
	showImage: function(index){
		//console.log('showImage(' + index + ')');
		//var newSrc = this.thumbnails[index].get('href') !== '' && this.thumbnails[index].get('href') !== null && this.thumbnails[index].get('href') !== null ? this.thumbnails[index].get('href') : this.thumbnails[index].get('data-href');
		var newSrc = this.thumbnails[index].get('data-bannersrc');
		if(newSrc === '' || newSrc === 'undefined' || newSrc === null)
			return false;
			
		this.thumbnails.removeClass('current');
		this.thumbnails[index].addClass('current');
		
		var myFx = new Fx.Tween(this.mainImg,{
			property:'opacity',
			link:'chain',
			duration:'normal',
			onComplete:this.fadeImageSwap.bind(this,newSrc)
		});
		
		myFx.start(0);
		this.changeText();
		myFx.start(1);
		return false;
	},
	
	fadeImageSwap:function(newSrc){
		//console.log('fadeImageSwap(' + newSrc + ')');
		if(this.mainImg.getStyle('opacity').toInt() == 0){
			this.mainImg.setStyle('background-image','url(' + newSrc + ')');			
		} else{			
			this.busy = false;
		}
		
		return false;
	},
	
	/*****************
	* SLIDE SHOW
	*****************/
	
	startSlideShow:function(){		
		this.slideShowId = this.nextImage.periodical(this.options.slideShowInterval,this);
		this.slideShowStopBtn ? this.slideShowStopBtn.fade('in') : 0;
		this.slideShowStartBtn ? this.slideShowStartBtn.fade('out') : 0;
	},
	
	stopSlideShow:function(){
		clearInterval(this.slideShowId);
		this.slideShowStopBtn ? this.slideShowStopBtn.fade('out') : 0;
		this.slideShowStartBtn ? this.slideShowStartBtn.fade('in') : 0;
		return false;
	},
	
	nextImage:function(){
			
		if(this.busy)
			return false;
			
		this.busy = true;
		
		this.previousImageIndex = this.currentImageIndex;
		
		//determine index of next image
		if(this.currentImageIndex < this.numThumbs - 1){			
			this.currentImageIndex++;
		} else {
			this.currentImageIndex = 0;
		}
		
		this.showImage(this.currentImageIndex);

	},
	
	previousImage:function(){
		if(this.busy)
			return false;		

		this.busy = true;
		
		this.previousImageIndex = this.currentImageIndex;		
		//determine index of next image
		if(this.currentImageIndex > 0 ){
			this.currentImageIndex--;
		} else {
			this.currentImageIndex = this.numThumbs - 1;
		}
		
		
		this.showImage(this.currentImageIndex);

	},
	
	
	/*******************
	* TEXT FUNCTIONS
	*******************/
	
	
	changeText:function(){
		
		var currentCaption = this.captions[this.previousImageIndex]
		if(!currentCaption)
			return false;
			
		var nextCaption = this.captions[this.currentImageIndex];
		if(!nextCaption)
			return false;
			
		if($('banner-link')){
				
			var link = this.thumbnails[this.currentImageIndex].href;
			var projectName = this.thumbnails[this.currentImageIndex].get('text');
			$('banner-link').set({
				'href':link,
				'text':projectName
				});
			
		}
		
		var theFx = new Fx.Morph(currentCaption,{});
		var theNextFx = new Fx.Morph(nextCaption,{});
		
		theFx.start({
			'left':[0,-100],
			'opacity':[1,0]
		}).chain(function(){
			this.start({
				'left':[0]
			});
		});
		
		theNextFx.start({			
			'opacity':[0,1]
		});
		
		return true;
	}
	
	
	
	
	
});

