//*** 	AC-Slide
//*		Author: 		J. Corcoran 
//*		Last modified: 	9/9/2009
//* 	Dependency:  	Mootools 1.2.x core and More
//*		Parameters:  	sliderCt: 			Class of the element that contains all slides
//* 					slide:				The individual slide elements
//* 					jsonPath: 			The URL of the JSON file

var ACSlider = new Class({
	Implements: [Options,Chain],
	options: {
		sliderCt: '.AC-Slider',
		slide: '.AC-Slide',
		jsonPath: 'sliderJSON.js'
	}, 
	initialize: function(options){
		this.setOptions(options);
		this.loadSlideJSON();
		this.currentSlide = 0;
	},
	attachButtons: function(){
		buttonCt = new Element('div',{'class':'sliderButtonCt clearfix'}).inject($$(this.options.sliderCt)[0]);
		leftButton = new Element('div',{'class':'sliderButtonLeft','html':'&lt;'}).inject(buttonCt);
		rightButton = new Element('div',{'class':'sliderButtonRight','html':'&gt;'}).inject(buttonCt);
		leftButton.ACaddEvent('click',function(){this.slideLeft();}.bind(this));	
		rightButton.ACaddEvent('click',function(){this.slideRight();}.bind(this));	
	},
	slideLeft: function(){
		nextSlide = (this.currentSlide == 0)? $$(this.options.sliderCt + ' ' + this.options.slide).length - 1 : this.currentSlide - 1 ;		
		var fader = new Fx.Tween($$(this.options.sliderCt + ' ' + this.options.slide)[this.currentSlide]);
		fader.start('opacity',1,0).chain(function(){$$(this.options.sliderCt + ' ' + this.options.slide)[this.currentSlide].setStyles({'display':'none'});this.fadeInSlide(nextSlide)}.bind(this));		
	},
	slideRight: function(){
		nextSlide = (this.currentSlide == ($$(this.options.sliderCt + ' ' + this.options.slide).length - 1))? 0 : this.currentSlide + 1 ;		
		var fader = new Fx.Tween($$(this.options.sliderCt + ' ' + this.options.slide)[this.currentSlide]);
		fader.start('opacity',1,0).chain(function(){$$(this.options.sliderCt + ' ' + this.options.slide)[this.currentSlide].setStyles({'display':'none'});this.fadeInSlide(nextSlide)}.bind(this));		
	},
	fadeInSlide: function(slideNum){
		this.currentSlide = slideNum;		
		$$(this.options.sliderCt + ' ' + this.options.slide)[slideNum].setStyles({'opacity':'0','display':'block'});
		sIFR.activate(dinengschrift, frutigerLight);
		var fader = new Fx.Tween($$(this.options.sliderCt + ' ' + this.options.slide)[slideNum]);
		fader.start('opacity',0,1);	
	},
	loadSlideJSON: function(){		
		this.slideJSON = new Request.HTML({
			url: this.options.jsonPath,
			onSuccess:function(responseTree, responseElements, responseHTML, responseJavascript){			
				try {					
					var allSlides = JSON.decode(responseHTML);
					allSlides.slides.each(function(item,index){
						newSlide = $$(this.options.sliderCt + ' ' + this.options.slide)[0].clone().setStyles({'display':'none'});
						newSlide.getElements('.AC-BlockImage')[0].empty();						
						slideImg = new Element('img',{'alt':'Photo of ' + item.title,'src':item.image}).inject(newSlide.getElements('.AC-BlockImage')[0]);
						newSlide.getElements('.AC-BlockCt .AC-BlockHeader H4')[0].set('html',item.type);
						newSlide.getElements('.AC-BlockCt .AC-BlockText')[0].set('html',item.teaser);
						newSlide.getElements('.AC-BlockCt .AC-BlockTitle')[0].set('html',item.title);
						newSlide.getElements('.AC-BlockCt .AC-BlockCtrl')[0].set('html',item.link);
						newSlide.inject($$(this.options.sliderCt)[0]);						
					}.bind(this));
					this.attachButtons();			
				}
				catch(error) {
					alert(error);
				}				
			}.bind(this),
			onFailure:function(error){
				alert(error);
			}.bind(this)
		}).get();	
	}
});

window.ACaddEvent('domready',function(){var slider = new ACSlider({'jsonPath':'spotlightJSON.js'});});
