/*
 * Script: MooFlow.js v.0.11
 * Copyright: Copyright (c) 2007 Tobias Wetzel (ToBSn), <http://outcut.de/>
 * License: MIT-style license
 */
 
var MooFlow = new Class({
	getOptions: function(){
		return {
			onStart: Class.empty,
			onClickView: Class.empty,
			container: 'MooFlow',
			imgContainer: 'images',
			slider: false,
			caption: false,
			reflection: 0.5,
			factor: 115,
			startIndex: 0,
			buttons: false,
			interval: 3000,
			useWindowResize: false,
			useMouseWheel: false,
			useKeyInput: false
		}
	},
	initialize: function(options){
		this.setOptions(this.getOptions(), options);
		this.MooFlow = $(this.options.container);
		this.images = $$(this.options.images);
		this.iL = this.images.length-1;
		this.factor = this.options.factor;
		this.ref = this.options.reflection;
		this.foc = 150;
		this.cur = 0;
		this.tar = 0;
		this.index = this.options.startIndex;
		this.isRun = false;
		this.sli = null;
		this.checker = null;
		this.interval = this.options.interval;
		if(this.options.buttons) this.initButtons(this.options.buttons);
		if(this.options.useWindowResize) window.addEvent('resize', this.update.bind(this));
		if(this.options.useMouseWheel) this.MooFlow.addEvent('mousewheel', this.wheelTo.bind(this));
		if(this.options.useKeyInput) document.addEvent('keydown', this.keyTo.bind(this));
		this.init();
	},
	init: function(){
		this.images.each(function(image, i){
			image.setStyle('display','block');
			image.addEvent('click', this.clickTo.bind(this,[i]));
			image.addEvent('dblclick', this.callBack.bind(this, [image, i]));
			image.w = image.width;
			image.h = image.height;
		}, this);
		this.update();
		this.fireEvent('onStart');
	},
	update: function(){
		this.oW = this.MooFlow.getSize().size.x;
		this.MooFlow.setStyles({'height':this.oW*0.57,'visibility':'visible'});		
		$(this.options.imgContainer).setStyle('height', this.oW*0.54);	
		this.sz = this.oW * 0.5;
		if(this.options.slider){
			this.sli = new Slider($(this.options.slider.slider), $(this.options.slider.knob),{steps: this.iL}).set(this.index);
			$(this.options.slider.knob).setStyles({'width':$(this.options.slider.slider).getSize().size.x/this.iL,'opacity':0.5});
			this.sli.addEvent('onChange', this.glideTo.bind(this));
		}		
		this.process(this.index*-this.foc);
		this.glideTo(this.index);
		
	},
	initButtons: function(btns){
		if($chk(btns.prev)){ $(btns.prev).addEvent('click', this.prev.bind(this)); }
		if($chk(btns.stop)){ $(btns.stop).addEvent('click', this.stop.bind(this)); }
		if($chk(btns.play)){ $(btns.play).addEvent('click', this.play.bind(this)); }
		if($chk(btns.next)){ $(btns.next).addEvent('click', this.next.bind(this)); }
	},
	callBack: function(image, index){
		if(this.index != index) return;
		this.fireEvent('onClickView', image);
	},
	prev: function(){
		if(this.index > 0) this.clickTo(this.index-1);
	},
	stop: function(){
		$clear(this.autoPlay);
	},
	play: function(){
		this.autoPlay = this.auto.periodical(this.interval, this);
	},
	auto: function(){
		if(this.index < this.iL)
		this.next();
		else if(this.index == this.iL)
		this.clickTo(0);
	},
	next: function(){
		if(this.index < this.iL) this.clickTo(this.index+1);
	},
	start: function(){
		this.isRun = true;
		this.checker = this.check.periodical(50, this);
	},
	end: function(){
		$clear(this.checker);
	},
	keyTo: function(e){
		e = new Event(e);
		switch (e.code){
			case 37:
				e.stop();
				this.prev();
				break;
			case 39:
				e.stop();
				this.next();
		}
	},
	wheelTo: function(e){
		e = new Event(e).stop();
		var d = e.wheel;
		if(e.preventDefault) e.preventDefault();		
		if(d > 0) this.prev();
		if(d < 0) this.next();
	},
	clickTo: function(i){
		if(this.options.slider)
		this.sli.set(i);
		else this.glideTo(i);
	},
	glideTo: function(i){
		if(!this.isRun){this.start();}
		if(this.options.caption && this.images[i]){$(this.options.caption).setHTML(this.images[i].alt);}
		this.index = i;
		this.tar = i*-this.foc;
	},
	check: function(){
		switch(this.tar < this.cur-1 || this.tar > this.cur+1){
			case true:
				this.process(this.cur + (this.tar-this.cur)/3);
				break;
			default:
				this.isRun = false;
				this.end();
		}
	},
	process: function(x){
		this.cur = x;
		var zI=this.iL, z, iH, iW, f=this.factor, ref=this.ref, oW=this.oW, s=this.sz, fo=this.foc;
		with (Math) {
			this.images.each(function(img){
				if(x<-fo*5 || x>fo*5){
					img.setStyle('display','none');	
				} else {
					iH = img.h, iW = img.w;
					z = Math.sqrt(10000 + x * x) + 100;
					L = x / z * s + s - (f / 2) / z * s;
					H = (iH / iW * f) / z * s;
					T = (oW * 0.4 - H) + ((H / (ref + 1)) * ref);
					W = round(iW * H / iH);
					if(H >= iW * 0.5) W = round(f / z * s);
						
					img.setStyle('left', round(L));
					img.setStyle('height', round(H));
					img.setStyle('width', round(W));
					img.setStyle('top', round(T));
					img.setStyle('zIndex', x < 0 ? zI++ : zI--);
					img.setStyle('display','block');	
				}			
				x += fo;
			});
		}
	}
});

MooFlow.implement(new Options);
MooFlow.implement(new Events);