/*
Script: mootools-rpflo.js

	License:
		MIT-style license.

	Author:
		Ryan Florence <rpflorence@gmail.com>
		Documentation <http://moodocs.ryanflorence.com>

*/

var Loop = new Class({

	loopCount: 0,
	isStopped: true,
	isLooping: false,
	loopMethod: $empty,

	setLoop: function(fn,delay){
		this.loopMethod = fn;
		this.loopDelay = delay || 3000;
		return this;
	},

	stopLoop: function() {
		this.isStopped = true;
		this.isLooping = false;
		$clear(this.periodical);
		return this;
	},

	startLoop: function(delay) {
		if(this.isStopped){
			var delay = (delay) ? delay : this.loopDelay;
			this.isStopped = false;
			this.isLooping = true;
			this.periodical = this.looper.periodical(delay,this);	
		}
		return this;
	},

	resetLoop: function(){
		this.loopCount = 0;
		return this;
	},

	looper: function(){
		this.loopCount++;
		this.loopMethod(this.loopCount);
		return this;
	}

});

var SimpleSlides = new Class({

	Implements: [Options, Events, Loop], 

		options: {
			/*
			onShow: $empty
			onLabelEvent: $empty,
			*/
			labelStorageAttribute: 'alt',
			slideSelector: 'img',
			attributePrefix: 'simple_slides_',
			labelEvent: 'click',
			delay: 4000
		},

	initialize: function(container,navigation,options){
		this.setOptions(options);
		this.setLoop(this.loop, this.options.delay);
		this.container = document.id(container);
		this.navigation = document.id(navigation);
		this.labels = [];
		this.slides = this.container.getElements(this.options.slideSelector);
		this.currentIndex = 0;
		this.build();
	},

	build: function(){

		if(this.container.getStyle('position') != 'absolute') this.container.setStyle('position','relative');

		this.slides.each(function(slide,index){
	
			slide.setStyles({
				'position': 'absolute',
				'top':0,
				'left':0
			});
	
			this.labels[index] = new Element('div',{
				'id': this.options.attributePrefix + 'label_'+index,
				'class': this.options.attributePrefix + 'label',
				'html': slide.get(this.options.labelStorageAttribute)
			})
			.addEvent(this.options.labelEvent,this.labelEvent.bind(this, index))
			.inject(this.navigation);
	
			if(index != 0) slide.fade('hide');
			else this.labels[index].addClass(this.options.attributePrefix + 'label_current');
	
		}.bind(this));

		return false;
	},

	show: function(index){

		this.fireEvent('show', [this.currentIndex, index]);
		this.slides[this.currentIndex].fade('out');
		this.labels[this.currentIndex].removeClass(this.options.attributePrefix + 'label_current');

		this.currentIndex = index;
		this.slides[this.currentIndex].fade('in');
		this.labels[this.currentIndex].addClass(this.options.attributePrefix + 'label_current');

		return this;
	},

	loop: function(){
		var nextIndex = (this.currentIndex == this.slides.length-1) ? 0 : this.currentIndex + 1;
		this.show(nextIndex);
		return this;
	},

	labelEvent: function(index){
		this.fireEvent('labelEvent', index);
		if(this.isLooping) this.stopLoop();
		this.show(index);
		return this;
	}


});

var InfiniteTicker = new Class({
	
	Extends: Fx.Scroll,
	Implements: Loop,
	
		options: {
			delay: 4000,
			direction: 'down'
		},
	
	initialize: function(element, options){
		this.parent(element,options);
		this.setLoop(this.toNext, this.options.delay);
		
		this.elements = this.element.getChildren();
		
		this.elementsHeight = this.elements[0].getSize().y;
		this.scrollHeight = this.element.getScrollSize().y;
		this.elementHeight = this.element.getStyle('height').toInt();
		this.scrollOffset = ((this.elementHeight / this.elementsHeight) + 1) * this.elementsHeight;
		
		this.moveElement('first');
				
		this.addEvent('onComplete',function(){
			this.moveElement();
		}.bind(this));
		
	},
	
	toNext: function(){
		this.checkLooping();
		switch(this.options.direction){
			case 'down':
				this.start(0,this.element.getScroll().y - this.elementsHeight);
			break;
			case 'up':
				this.start(0,this.element.getScroll().y + this.elementsHeight);
			break;
		}
		
	},
	
	checkLooping: function(){
		if(this.isLooping) this.stopLoop().startLoop();
	},
	
	moveElement: function(target){
		switch(this.options.direction){
			case 'up':
				this.element.getFirst().dispose().inject(this.element, 'bottom');
			break;
			case 'down':
				this.element.getLast().dispose().inject(this.element, 'top');
			break;
		}
		this.set(0,this.scrollHeight - this.scrollOffset)
	}
	
});
var TestimonialTicker = new Class({
	
	Extends: InfiniteTicker,
	
	options: {
		delay: 9000,
		direction: 'up'
	},
	
	initialize: function(element,options){
		this.parent(element,options);
		this.playPause = document.id('ticker_play_pause');
		this.nextButton = document.id('ticker_next');
		this.attach();
	},
	
	attach: function(){
		this.playPause.addEvent('click',function(event){
			event.stop();
			if(this.playPause.hasClass('pause')){
				this.playPause.removeClass('pause').addClass('play');
				this.stopLoop();
				this.paused = true;
			} else {
				this.playPause.removeClass('play').addClass('pause');
				this.startLoop().toNext();
				this.paused = false;
			}
		}.bind(this));
		
		this.nextButton.addEvent('click',function(event){
			event.stop();
			this.toNext();
		}.bind(this));
		
		this.element.addEvents({
			'mouseenter': function(){
				this.stopLoop();
			}.bind(this),
			'mouseleave': function(){
				if(!this.paused) this.startLoop();
			}.bind(this)
		});
		
	}
	
});

Element.implement({
	
	loadSwiff: function(path, options){
		var swiff = new Swiff(path, options);
		this.store('swiff',swiff);
		swiff.inject(this);
		return this;
	}
	
});