//*** 	AC-ContentRotator
//*		Author: 		J. Corcoran 
//*		Last modified: 	9/23/2009 by M. Dempster
//* 	Dependency:  	Mootools 1.2.x core and More
//*		Parameters:  	rotatorCt: 			Class of the element that contains all slides
//* 					slide:				The individual slide elements
//*						speed:				Sets the time in milliseconds that a slide is shown before moving to the next slide 
//* 					jsonPath: 			The URL of the JSON file

var ACRotator = new Class({
	Implements: [Options,Chain],
	options: {
		rotatorCt: '.AC-Rotator',
		slide: '.AC-Slide',
		speed: false,
		jsonPath: 'sliderJSON.js'
	}, 
	initialize: function(options){
		this.setOptions(options);
		this.loadSlideJSON();
		this.currentSlide = 0;
		this.slideCount = 0;		
		if (this.options.speed) { this.timer = this.slideNext.periodical(this.options.speed, this); }; 
	},
	attachButtons: function(){
		buttonCt = new Element('div',{'class':'sliderButtonCt clearfix'}).inject($$(this.options.rotatorCt)[0]);
		leftButton = new Element('div',{'id':'slidePrev','class':'sliderCtrl','html':'&lt;'}).inject(buttonCt);
		rightButton = new Element('div',{'id':'slideNext','class':'sliderCtrl','html':'&gt;'}).inject(buttonCt);
		leftButton.addEvent('click',function(){$clear(this.timer); this.slidePrev();}.bind(this));	
		rightButton.addEvent('click',function(){$clear(this.timer); this.slideNext();}.bind(this));	
	},
	slidePrev: function(){
		nextSlide = (this.currentSlide == 0)? this.slideCount - 1 : this.currentSlide - 1 ;		
		this.fadeOutSlide(nextSlide);	
	},
	slideNext: function(){
		nextSlide = (this.currentSlide == (this.slideCount - 1))? 0 : this.currentSlide + 1;		
		this.fadeOutSlide(nextSlide);	
	},
	fadeOutSlide: function(nextSlide){		
		$$(this.options.rotatorCt)[0].getChildren(this.options.slide)[this.currentSlide]
			.get('tween')
			.start('opacity',1,0)
			.chain(function(){
				$$(this.options.rotatorCt)[0].getChildren(this.options.slide)[this.currentSlide].setStyles({'display':'none'});
				this.fadeInSlide(nextSlide)
			}.bind(this));	
	},
	fadeInSlide: function(slideNum){
		this.currentSlide = slideNum;		
		$$(this.options.rotatorCt)[0].getChildren(this.options.slide)[this.currentSlide]
			.setStyles({'opacity':'0','display':'block'})
			.get('tween')
			.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);
					this.slideCount = allSlides.slides.length;
					allSlides.slides.each(function(item,index){
						newSlide = $$(this.options.rotatorCt + ' ' + this.options.slide)[0].clone().setStyles({'display':'none'});
						newSlide.set('class',this.options.slide.substring(1,this.options.slide.length));
						if ($chk(newSlide.getElements('.AC-BlockImage')[0])) {
							newSlide.getElements('.AC-BlockImage')[0].empty();						
							slideImg = new Element('img',{'alt':item.title,'src':item.image}).inject(newSlide.getElements('.AC-BlockImage')[0]); 
						}
						if ($chk(newSlide.getElements('.AC-BlockCt .AC-BlockHeader H4')[0])) { newSlide.getElements('.AC-BlockCt .AC-BlockHeader H4')[0].set('html',item.type); }
						if ($chk(newSlide.getElements('.AC-BlockCt .AC-BlockText')[0])) { newSlide.getElements('.AC-BlockCt .AC-BlockText')[0].set('html',item.teaser); } 
						if ($chk(newSlide.getElements('.AC-BlockCt .AC-BlockTitle')[0])) { newSlide.getElements('.AC-BlockCt .AC-BlockTitle')[0].set('html',item.title); }
						if ($chk(newSlide.getElements('.AC-BlockCt .AC-BlockCtrl')[0])) { newSlide.getElements('.AC-BlockCt .AC-BlockCtrl')[0].set('html',item.link); }
						newSlide.inject($$(this.options.rotatorCt)[0]);						
					}.bind(this));
					if ($$(this.options.rotatorCt + ' ' + this.options.slide).length > 1) { this.attachButtons(); }			
				}
				catch(error) {
					alert(error);
				}				
			}.bind(this),
			onFailure:function(error){
				alert(error);
			}.bind(this)
		}).get();	
	}
});

window.addEvent('domready',function(){var rotator = new ACRotator({'speed':5000, 'jsonPath':'Content Rotator - JSON.js'});});