/* 
 * Schiavo Mazzonis
 *
 * @package		Schiavo Mazzonis
 * @author		Oskar Krawczyk (o.krawczyk@keepthinking.it)
 * @version		1.0
 * @dependecies	MooTools 1.2.1
 * @copyright	Copyright (c) 2008-2010, Oskar Krawczyk (Keepthinking Ltd.)		
 * @link		http://keepthinking.it
 * 
 ======================================================================= */

// e  - event
// el - element

// COMPRESS BEFORE DEPLOYING
// $ java -jar yuicompressor-2.3.4.jar --nomunge --type js -o output.js input.js 

var Slider = new Class({
	
	Implements: Options,
	options: {
		sliderWidth: 800
	},
	
	/* Initialize needed methods
	 ======================================================================= */
	initialize: function(options) {
		this.setOptions(options);
		
		this.gallery 	= $('works-gallery');
		
		// images container
		this.cont 		= $('wg-cont');
		
		// previous and next controls
		this.controls 	= $$('.wgcPrev a', '.wgcNext a');
		this.controlsParent	= this.controls.getParent();
		
		// initial position of the slider
		this.position 	= 0;
		
		// images combined witdh
		this.fullwidth 	= 0;
		
		// image collection
		this.images 	= this.cont.getElements('img');
	},
	
	/* 
	 ======================================================================= */
	controls: function() {
		
		// create the controls
		this.gallery;
	},

	/* 
	 ======================================================================= */
	start: function() {

		// calculate the images combined width
		this.images.each(function(image) {
			this.fullwidth += image.getSize().x.toInt();
		}, this);
				
		// fade in the images
		this.cont.fade('in');
		
		// hide the previous arrow
		this.controlsParent[0].setStyle('opacity', 0);
		
		// hide the next arrow when there's less than one slide of images
		if(this.fullwidth < this.options.sliderWidth) this.controlsParent[1].setStyle('opacity', 0);
		
		// initialize the slides
		if($chk(this.controls)) this.slide();
	},

	/* 
	 ======================================================================= */
	slide: function() {
		
		// set the animations
		this.cont.get('morph', {
			transition: 'cubic:out', 
			duration: 2000,
			onStart: function() {
				
				// show or hide the controls
				this.controlsParent[1].fade((this.position+this.fullwidth < this.options.sliderWidth-1 ? 'out' : 'in'));
				this.controlsParent[0].fade((this.position >= 0 ? 'out' : 'in'));
			}.bind(this)
		});

		// slide
		this.controls.addEvent('click', function(e) {
			if(e.target.get('text').test(/next/i)) 
				this.position -= this.options.sliderWidth;
			else 
				this.position += this.options.sliderWidth;
			
			this.cont.morph({left: this.position});
			e.stop();
		}.bind(this));
	}
});