/*
Viewer pour Goupe Dubreuil
Copyright(c) 2008, Skalpel.

Author : Michael
michael@skalpel.fr

Note : 
	-
ToDo :
	- 
*/

/* 
Class : SKjs.Viewer
	Viewer / Slideshow
	
Arguments : 
	container {String} - Id du conteneur
	slide {String} - Id ou Selecteur des diapositives
	options {object} - Objet Options
	
Options :
	mode {string} - Mode de transition des diapositives (top | right | bottom | left | alpha | random)
	play {Boolean} - Activation du la lecture automatique
	
	periodicalTime {Integer} - Durée des intavalles du slideshow
	transition {Boolean} - Effet de transition (Fx.Transition de Mootools)
	duration {Integer} - Durée de la transition (ms)
*/
SKjs.Viewer = new Class ({
	Implements: [Events, Options],
	options: {
		mode: 'alpha', 
		play: true,
		
		periodicalTime: 10000,
		transition: 'sine:in:out',
		duration: 250,
		
		lastCls: 'last',
		
		// Events
		onStart: $empty,
		onShow: $empty
	},
	
	/*
	Property :
		Initialisation de la Class
	*/
	initialize: function(container, slide, options) {
		this.setOptions(options);
		
		// DOM
		this.container = $(container);
		this.slide = ($type(slide) == 'array') ? slide : this.container.getElements(slide);
		
		// Constantes
		this.locked = false;
		this.index = 0;
		this.intervalId = null;
		this.attribs = {
			top: ['top', -this.container.getSize().y, 0],
			right: ['left', this.container.getSize().x, 0],
			bottom: ['top', this.container.getSize().y, 0],
			left: ['left', -this.container.getSize().x, 0],
			alpha: ['opacity', 0, 1]
		};
		this.modes = new Hash(this.attribs).getKeys();
		
		// Reset
		this.slide.removeClass(SKjs.activeCls);
		this.slide[0].addClass(SKjs.activeCls);
			
		// Launch
		if(this.options.play) this.play();
		this.fireEvent('start');
	},
	
	/*
	Property :
	*/
	slideshow: function(index) {
		if(!this.locked) {
			this.locked = true;
			if(this.options.mode == 'random') var attrib = this.attribs[this.modes.getRandom()];
			else var attrib = this.attribs[this.options.mode];
			attrib = attrib.associate(['property', 'from', 'to']);
			
			this.slide[index].setStyles(JSON.decode('{'+ attrib.property +' : '+ attrib.from +'}'));
			this.slide[index].set('tween', {
				transition: this.options.transition, 
				duration: this.options.duration,
				onComplete: function() {
					this.locked = false;
					this.index = index;
					this.fireEvent('show');
				}.bind(this)
			}).tween(attrib.property, attrib.from, attrib.to);
		}
	},
	
	/*
	Property :
	*/
	play: function() {
		$clear(this.intervalId);
		this.intervalId = this.next.periodical(this.options.periodicalTime, this);
	},
	/*
	Property :
	*/
	next: function() {
		var nextIndex = (this.index+1 > this.slide.length-1) ? 0 : this.index+1;
		
		this.slide.removeClass(SKjs.activeCls).removeClass(this.options.lastCls);
		this.slide[this.index].addClass(this.options.lastCls);
		this.slide[nextIndex].addClass(SKjs.activeCls);
		
		this.slideshow(nextIndex);
		
		if(this.options.play) this.play();
	},
	/*
	Property :
	*/
	previous: function() {
		var previousIndex = (this.index-1 < 0) ? this.slide.length-1 : this.index-1;
		
		this.slide.removeClass(SKjs.activeCls).removeClass(this.options.lastCls);
		this.slide[this.index].addClass(this.options.lastCls);
		this.slide[previousIndex].addClass(SKjs.activeCls);
		
		this.slideshow(previousIndex);
		
		if(this.options.play) this.play();
	},
	/*
	Property :
	*/
	stop: function() {
		$clear(this.intervalId);
	}
});
