/*
mooVRotatingMenu - menu totalement inutile donc indispensable
massacred by christopher wait aka virtualgadjo aka grizzly aka l'ot fou

s'appuie sur mootools1.2 (c'te blague...)

05/2008 - un tonneau de single malt plus loin, j'y suis presque !

########## HOW TO ##########
oo -- les Options -- oo
- maxWidth: la largeur maxi que prennent les elements
- maxHeight: devinez...
- topZ: le z-index de l'element du dessus (utile a pas mal de titre, entre autres si vous avez des fenetre modales histoire)
- hPad et vPad: attention, delicat pseudo padding horizontal (hPad) et vertical (vPad).
Plus vos elements sont larges et haut par rapport a la taille du conteneur d'origine, plus le chiffre doit etre grand
pour ne pas qu'ils depassent des bords du conteneur. Un element de placement important
- inertie: plus le nombre est grand moins forte est l'acceleration quand on s'eloigne du centre
- opacity: bon, ben ca, inutile de vous faire un dessin... concerne les elements oeuf corse
############################

Have swing
*/

var mooVRotatingMenu = new Class ({
	
	Implements: [Options, Events],
	
	options: {
		maxWidth	: 200,
		maxHeight	: 100,
		topZ		: 500,
		mode		: 'horizontal',
		hPad		: 1.6,
		vPad		: 2,
		inertie		: 30,
		opacity		: 1
	},
	
	initialize: function(container, elems, options){

		this.setOptions(options);
		this.container	= container;
		this.ziLeft		= this.container.getPosition().x;
		this.ziTop		= this.container.getPosition().y;
		this.ziWidth	= this.container.getCoordinates().width;
		this.ziHeight	= this.container.getCoordinates().height;
		this.ch			= this.ziWidth / 2; //centre horizontal
		this.cv			= this.ziHeight / 2; //centre vertical
		this.elems		= elems;
		this.num		= this.elems.length;
		this.move		= 0;
		this.tbMove		= 0;
		this.speed		= - 1 / this.options.inertie;
		this.deg		= [];
		this.altDeg 	= [];
		this.diff		= 0;
		this.tbDiff		= 0;

		this.elems.each(function(el, i){
			this.deg.push(360 * i / this.num);
			this.altDeg.push(360 * i /this.num);
			el.setStyles({
				'display'	: 'block',
				'float'		: 'none',
				'position'	: 'absolute',
				'opacity'	: this.options.opacity
			});
			this.place(el, i);
		}.bind(this));

		this.container.addEvents({
			'mouseenter'	: function(){
				this.moveAll();
			}.bind(this),
			'mousemove'		: function(e){
				this.mouseX	= e.client.x;
				this.mouseY	= e.client.y;
				this.diff	= ((this.mouseX - (this.ziLeft - window.getScroll().x)) - (this.ziWidth / 2));
				this.tbDiff	= (this.mouseY -(this.ziTop - window.getScroll().y)) - (this.ziHeight / 2);
				this.move	= this.diff * this.speed;
				this.tbMove	= this.tbDiff * this.speed;
			}.bind(this),
			'mouseleave'	: function(){
				this.move	= 0;
				this.tbMove	= 0;
				$clear(this.enRoute);
			}.bind(this)
		});

	},

	place: function(el, i){
		var coef;
		this.options.mode == "horizontal" ? this.deg[i] = this.deg[i] + this.move : this.deg[i] = this.deg[i] + this.tbMove;

		if (this.deg[i] > 360) { this.deg[i] = this.deg[i] % 360; }
		if (this.deg[i] < 0 ) { this.deg[i] = 360 + this.deg[i]; }
		var rad = (this.deg[i] * Math.PI / 180);
		this.deg[i] < 180 ? coef = ((360 - this.deg[i]) / 360) : coef = (this.deg[i]/360);

		if (this.options.mode == "horizontal"){
			this.tbDiff ? this.yMove = this.tbDiff * (Math.cos(rad)) : this.yMove = 0;

			var ziW		= (this.options.maxWidth * coef).toInt();
			var cx		= (this.ch + (this.ch * (Math.sin(rad) / this.options.hPad))).toInt();
			var _x		= cx - (ziW / 2).toInt();
			var ziH		= (this.options.maxHeight * coef).toInt();
			var _y		= ((this.ziHeight - ziH) / 2).toInt() - this.yMove / this.options.vPad;
		}

		else if (this.options.mode == "vertical"){
			this.diff ? this.xMove = this.diff * (Math.cos(rad)) : this.xMove = 0;

			var ziW		= (this.options.maxWidth * coef).toInt();
			var _x		= ((this.ziWidth - ziW) / 2).toInt() - this.xMove / this.options.hPad;
			var ziH		= (this.options.maxHeight * coef).toInt();
			var cy		= (this.cv + (this.cv * (Math.sin(rad) / this.options.vPad))).toInt();
			var _y		= cy - (ziH / 2).toInt();
		}

		el.setStyles({
			'top'		: _y,
			'left'		: _x,
			'width'		: ziW,
			'height'	: ziH,
			'z-index'	: this.options.topZ * coef
		});
	},

	moveAll: function(){
		this.elems.each(function(el, i){
			this.place(el, i);
		}.bind(this));
		this.enRoute = this.moveAll.delay(15, this);
	}
});
