/**
Script: Slideshow.KenBurns.js
	Slideshow.KenBurns - KenBurns extension for Slideshow, includes zooming and panning effects.

License:
	MIT-style license.

Copyright:
	Copyright (c) 2008 [Aeron Glemann](http://www.electricprism.com/aeron/).
	
Dependencies:
	Slideshow.
*/

Slideshow.KenBurns = new Class({
	Extends: Slideshow,
	
	options: {
		pan: [100, 100],
		zoom: [50, 50]
	},
	
/**
Constructor: initialize
	Creates an instance of the Slideshow class.

Arguments:
	element - (element) The wrapper element.
	data - (array or object) The images and optional thumbnails, captions and links for the show.
	options - (object) The options below.

Syntax:
	var myShow = new Slideshow.KenBurns(element, data, options);
*/

	initialize: function(el, data, options){
		options.overlap = true;
		options.resize = true;
		Array.each(['pan', 'zoom'],function(p){
				if (this[p]!=null){
					if (typeOf(this[p]) != 'array') this[p] = [this[p], this[p]];
					this[p].map(function(n){return (n.toInt() || 0).limit(0, 100);});					
				}
		}, options);
		this.parent(el, data, options);
	},

/**
Private method: show
	Does the slideshow effect.
*/   _ARGID:function(i){
		return function(){
			return arguments[i];
		};
	},
    
	_show: function(fast){
		
 		if (!this.image.retrieve('morph')){
 			
 			Array.each(['a', 'b'],function(image){
				this[image].set('tween', {'duration': this.options.Duration, 'link': 'cancel', 'onStart': this._start.bind(this), 'onComplete': this._complete.bind(this), 'property': 'opacity'}
				).set('morph',{'duration': (this.options.delay + this.options.Duration * 2), 'link': 'cancel', 'transition':this._ARGID(0)}).get('morph');
			}, this);
		}
		this.image.set('styles', {'bottom': 'auto', 'left': 'auto', 'right': 'auto', 'top': 'auto'}).setProperties({'width':this.image.getCoordinates().width+'px','height':this.image.getCoordinates().height+'px'});
		
		var props = ['top left', 'top right', 'bottom left', 'bottom right'][this.counter % 4].split(' ');
		
		Array.each(props,function(prop){this.image.setStyle(prop, 0);}, this);
		
		dh = this.height / this.preloader.height;
		dw = this.width / this.preloader.width;
		
		delta = (dw > dh) ? dw : dh;
		var values = {};
		
		var zoom = (Number.random.apply(this,this.options.zoom) / 100.0) + 1;
		
		var pan = Math.abs((Number.random.apply(this,this.options.pan) / 100.0) - 1);
		
		Array.each(['height', 'width'],function(prop, i){
			
			var e = Math.ceil(this.preloader[prop] * delta);
			var s = (e * zoom).toInt();		
			values[prop] = [s, e];  
			if (dw > dh || i){
				
				e = (this[prop] - this.image[prop]);
				s = (e * pan).toInt();
				
				values[props[i]] = [s, e];
			}
		}, this);
		
		var paused = (this.firstrun && this.options.paused);
		if (fast || paused){
			this._center(this.image);
			this.image.get('morph').cancel();
			if (paused)
				this.image.set('tween',{'opacity': 0}).get('tween').cancel().start(1);
			else
				this.image.set('tween',{'opacity': 1}).get('tween').cancel();
		} 
		else{
			this.image.get('morph').start(values);
			this.image.get('tween').set(0).start(1);
			
		}
	}
});
