/*
                     __                      ___                           __                    
         __         /\ \__                  /\_ \                         /\ \     __            
 __  __ /\_\   _ __ \ \ ,_\  __  __     __  \//\ \       __       __      \_\ \   /\_\     ___   
/\ \/\ \\/\ \ /\`'__\\ \ \/ /\ \/\ \  /'__`\  \ \ \    /'_ `\   /'__`\    /'_` \  \/\ \   / __`\ 
\ \ \_/ |\ \ \\ \ \/  \ \ \_\ \ \_\ \/\ \L\.\_ \_\ \_ /\ \L\ \ /\ \L\.\_ /\ \L\ \  \ \ \ /\ \L\ \
 \ \___/  \ \_\\ \_\   \ \__\\ \____/\ \__/.\_\/\____\\ \____ \\ \__/.\_\\ \___,_\ _\ \ \\ \____/
  \/__/    \/_/ \/_/    \/__/ \/___/  \/__/\/_/\/____/ \/___L\ \\/__/\/_/ \/__,_ //\ \_\ \\/___/ 
                                                         /\____/                  \ \____/       
                                                         \_/__/                    \/___/        

massacred by christopher wait aka virtualgadjo aka grizzly aka l'ôt fou

s'appuie sur mootools1.2 (c'te blague...)

05/2008 - un tonneau de single malt plus loin, j'y suis presque !
08/2008 - ajoût de l'option reset pour faire plus propre... :) + petites améliorations de perf à droite à gauche
04/2010 - l'initialisation des coordonnées passe dans une méthode pour pouvoir la relancer en cas de rezise de la page

########## HOW TO ##########
oo -- les Options -- oo
- maxWidth   : la largeur maxi que prennent les éléments
- maxHeight  : devinez...
- topZ       : le z-index de l'élément du dessus (utile à pas mal de titre, entre autres si vous avez des fenêtre modales histoire)
- hPad, vPad : attention, délicat, pseudo padding horizontal (hPad) et vertical (vPad).
               Plus vos éléments sont larges et haut par rapport à la taille du conteneur d'origine, plus le chiffre doit être grand
               pour ne pas qu'ils dépassent des bords du conteneur. Un élément de placement important
- inertie    : plus le nombre est grand moins forte est l'accelération quand on s'éloigne du centre
- opacity    : bon, ben ça, inutile de vous faire un dessin... concerne les éléments oeuf corse
- reset      : si l'option est sur true, le menu revient à sa position initiale quand la souris sort de la zone active
############################

Have swing
*/

var mooVRotatingMenu = new Class ({
	
	Implements: [Options, Events],
	
	options: {
		maxWidth    : 116,
		maxHeight   : 116,
		topZ        : 500,
		mode        : 'horizontal',
		hPad        : 1.6,
		vPad        : 2,
		inertie     : 90,
		opacity     : 1,
		reset       : true
	},
	
	initialize: function(container, elems, options){

		this.setOptions(options);
		this.container  = container;
		this.getCoord();
		this.elems      = elems;
		this.num        = this.elems.length;
		this.move       = 0;
		this.tbMove     = 0;
		this.speed      = - 1 / this.options.inertie;
		this.deg        = [];
		this.diff       = 0;
		this.tbDiff     = 0;

		this.firstPlace();

		this.container.addEvents({
			'mouseenter'    : function(){
				if (this.enRoute) $clear(this.enRoute);
				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);
				if (this.options.reset == true) {
					this.deg = [];
					this.diff = 0;
					this.tbDiff = 0;
					this.firstPlace();
				}
			}.bind(this)
		});
		window.addEvent('resize', this.getCoord.bind(this));
	},

	getCoord: function(){
		this.contCoord  = this.container.getCoordinates();
		this.ziLeft     = this.contCoord.left;
		this.ziTop      = this.contCoord.top;
		this.ziWidth    = this.contCoord.width;
		this.ziHeight   = this.contCoord.height;
		this.ch         = this.ziWidth / 2; //centre horizontal
		this.cv         = this.ziHeight / 2; //centre vertical
	},

	firstPlace: function(){
		this.elems.each(function(el, i){
			this.deg.push(360 * i / this.num);
			el.setStyles({
				'display'  : 'block',
				'float'    : 'none',
				'position' : 'absolute',
				'opacity'  : this.options.opacity
			});
			this.place(el, i);
		}.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);
	}
});
