/*
 * 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.0,
			factor: 150,
			startIndex: 0,
			buttons: false,
			interval: 1000,
			useWindowResize: false,
			useMouseWheel: false,
			useKeyInput: false,
			active_image_id: null
		}
	},
	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;
		this.active_image_id = this.options.active_image_id;
		this.initButtons();
		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(){
		var p = 0;
		this.images.each(function(image, i){
			image.setStyle('display','block');
			image.addEvent('click', this.clickTo.bind(this,[i, image]));
			image.addEvent('dblclick', this.callBack.bind(this, [image, i]));
			image.w = image.width;
			image.h = image.height;
			if(!this.active_image_id){
				this.active_image_id = image.id;
			}
			if(image.id != this.active_image_id){
				image.setOpacity(0.2);
				var fx = new Fx.Style(image, 'opacity', {duration:300, wait:false});
				image.addEvent('mouseover', function(){
					fx.start(1);
				}).addEvent('mouseout', function(){
					fx.start(0.2);
				});
			}
			else{
				image.setOpacity(1);
				p = i;
			}
		}, this);
		this.update();
		this.fireEvent('onStart');
		this.glideTo(p);
		this.ajaxCall('splash');
	},
	initButtons: function(){
		$('overview').addEvent('click', this.ajaxCall.bind(this, ('splash')));
		$('tech_spec').addEvent('click', this.ajaxCall.bind(this, ('tech_spec')));
		$('more_info').addEvent('click', this.redirect.bind(this));
	},
	update: function(){
		this.oW = this.MooFlow.getSize().size.x;
		this.MooFlow.setStyles({'height':175, 'width':725, 'visibility':'visible'});
		//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':14,'opacity':1});
			//$(this.options.slider.knob).setStyles({'width':$(this.options.slider.slider).getSize().size.x/this.iL-26.7,'opacity':1});
			this.sli.addEvent('onChange', this.glideTo.bind(this));
		}
		this.process(this.index*-this.foc);
		this.glideTo(this.index);

	},
	ajaxCall: function(action){
		if(!this.active_image_id){
			this.active_image_id = this.images[0].id;
			this.highlightImage(this.active_image_id);
		}
		var options = {};
		var id = {};
		id['id'] = this.active_image_id;
		$('selected_machine').value = id['id'];
		options['data'] = id;
		options['update'] = 'content';
		options['evalScripts'] = true;
		try{
			switch(action){
				case 'splash': $('viewProduct').action = 'overview.php'; $('viewProduct').send(options); break;
				case 'tech_spec': $('viewProduct').action = 'tech_spec.php'; $('viewProduct').send(options); break;
			}
		}
		catch(e){
			//alert('If you see this message, it means you are either using security software blocking javascript, or phishing filter in IE7 is causing a grief. In order for the website to behave the way it was intended, please add Sprocket to the list of trusted websites. To do it in IE just go to Tools/Internet options/Security tab/Trusted sites; tick off option \'Require server verification\' and simply add normal http url to the list. Although you can try refreshing the page. Thank you.')
			window.location.reload( false );
		}
	},
	redirect: function(){
		if(!this.active_image_id){
			this.active_image_id = this.images[0].id;
		}
		window.location='index.php?action=page&id=29&pid='+this.active_image_id;
	},
	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, 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, image){
		if(i[0] || i[0] == '0'){
			image = i[1];
			i = i[0];
		}
		if(this.options.slider){
			this.sli.set(i);
		}
		else {
			this.glideTo(i);
		}
		if(image){
			this.highlightImage(image.id);
			this.ajaxCall('splash');
		}
	},
	highlightImage: function(id){
		this.images.each(function(image, i){
			if(image.id == id){
				image.setOpacity(1);
				image.removeEvents('mouseover');
				image.removeEvents('mouseout');
				this.active_image_id = image.id;
			}
			else{
				image.removeEvents('mouseover');
				image.removeEvents('mouseout');
				image.setOpacity(0.2);
				var fx = new Fx.Style(image, 'opacity', {duration:300, wait:false});
				image.addEvent('mouseover', function(){
					fx.start(1);
				}).addEvent('mouseout', function(){
					fx.start(0.2);
				});
			}
		}, this);
	},
	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 = 600; //Math.sqrt(10000 + x * x) + 272;
					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);

					/*iH = img.h, iW = img.w;
					z = Math.sqrt(10000 + x * x) + 272;
					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)); //how far to the left line
					img.setStyle('height', round(156));
					img.setStyle('width', round(89));
					img.setStyle('top', round(3));
					img.setStyle('zIndex', x < 0 ? zI++ : zI--);
					img.setStyle('display','block');
				}
				x += fo;
			});
		}
	}
});

MooFlow.implement(new Options);
MooFlow.implement(new Events);
