/* 
 * Wedgwood Homepage Rotator
 *
 * @package		Wedgwood
 * @author		Oskar Krawczyk (o.krawczyk@keepthinking.it)
 * @version		1.3
 * @dependecies	MooTools 1.2+
 * @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 Rotator = new Class({
	
	Implements: Options,
		
	options: {
		rotators: 	'.rotator',
		counter: 	0,
		init: 		false,
		timer: 	{
			delay: 		9000,
			scolling: 	9500,
			fading: 	2000,
			fps: 		200 
		},
		opacity: {
			min: 0,
			max: 1
		},
		backgroundPosition: -130
	},
	
	/* Initialize needed methods
	 ======================================================================= */
	initialize: function(options) {
		this.setOptions(options);
		
		this.container 		= $('intro-banner');
		this.rotators 		= $$(this.options.rotators);
		this.captionFlag 	= false;
		this.captions 		= '.sbDesc';
		
		this.hideRotators();
		this.initFirst();
		this.initRotators.periodical(this.options.timer.delay, this);
	},
	
	/* check for Firefox on Mac
	 ======================================================================= */
	isFx2Mac: function() {
		if(Browser.Platform.mac && Browser.Engine.gecko && (Browser.Engine.version == 18)) return true;
		else return false;
	},
	
	hideRotators: function() {
		
		// hide all elements initially so we can nicely fade them in when the document finishes loading 
		this.rotators.set('styles', {opacity: this.options.opacity.min, zIndex: 500});
	},
	
	initRotators: function() {
		// hide the animated loader
		this.container.set('styles', {background: 'none'});
				
		this.rotators.each(function(rotator) {
			rotator.set('morph', {duration: this.options.timer.fading}); 
			rotator.morph({opacity: this.options.opacity.min});
		}, this);

		// reset the counter when rotator reaches the last element
		if(this.options.counter.toInt() == this.rotators.length.toInt()) this.options.counter = 0;
		
		// show the current slide
		this.showCurrent();	
		
		// iterate count
		this.options.counter++;
	},
	
	applyCaption: function(el) {
		var desc = el.getElements(this.captions);
		desc.set('morph', {transition: 'linear'}); 
		desc.morph({
			opacity: (this.captionFlag ? 0 : 1), 
			left: (this.captionFlag ? [20, 40] : [0, 20])
		});
		this.captionFlag = (this.captionFlag ? false : true);
	},
	
	scrollBanner: function(el) {
		el.set('morph', {fps: this.options.timer.fps, duration: this.options.timer.scolling, transition: 'linear'}); 
		
		// always start re-positioning from 0
		el.morph({backgroundPosition: [0, this.options.backgroundPosition]});
		
		// show the caption
		this.applyCaption.bind(this, el).delay(this.options.timer.fading);
		
		// hide the caption
		this.applyCaption.bind(this, el).delay(this.options.timer.delay-1000);
	},

	showCurrent: function() {
		
		// get the current rotator
		var current = this.rotators[this.options.counter];
		
		current.getElements(this.captions).morph({opacity: 0});
		
		// morph the text opacity
		current.set('styles', {zIndex: 600});
		current.set('morph', {duration: this.options.timer.fading}); 
		current.morph({opacity: this.options.opacity.max});
		
		// scroll the background
		var cChild = current.getElement('a');
		this.scrollBanner(cChild);
	},

	// fires the image slide shortly after the page gets loaded
	initFirst: function() {
		
		// get the last rotator
		var last = this.rotators.getLast();
		last.set('styles', {zIndex: 600});
		$$(this.captions).set('styles', {opacity: 0});

		// start scrolling the background
		var lChild = last.getElement('a');
		this.scrollBanner(lChild);
	}
});