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

massacred by christopher wait aka virtualgadjo aka grizzly aka l' 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皦 de l'option reset pour faire plus propre... :) + petites am駘iorations de perf ・droite ・gauche

########## HOW TO ##########
oo -- les Options -- oo
- maxWidth   : la largeur maxi que prennent les 駘駑ents
- maxHeight  : devinez...
- topZ       : le z-index de l'駘駑ent du dessus (utile ・pas mal de titre, entre autres si vous avez des fen黎re modales histoire)
- hPad, vPad : attention, d駘icat, pseudo padding horizontal (hPad) et vertical (vPad).
               Plus vos 駘駑ents sont larges et haut par rapport ・la taille du conteneur d'origine, plus le chiffre doit 黎re grand
               pour ne pas qu'ils d駱assent des bords du conteneur. Un 駘駑ent de placement important
- inertie    : plus le nombre est grand moins forte est l'accel駻ation quand on s'駘oigne du centre
- opacity    : bon, ben 軋, inutile de vous faire un dessin... concerne les 駘駑ents 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    : 200,
		maxHeight   : 150,
		topZ        : 500,
		mode        : 'horizontal',
		hPad        : 1.6,
		vPad        : 2,
		/* Rotating spped configuration value. The less, the faster. */
		inertie     : 120,
		opacity     : 1,
		reset       : false  /* change 2009.12.29 */
	},
	
	initialize: function(container, elems, options){

		this.setOptions(options);
		this.container  = container;
		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
		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();
				} else { 
					/* add 2009.12.29 */
					this.diff = 0;
					this.tbDiff = 0;
					this.firstPlace();
				}
			}.bind(this)
		});

	},

	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);
	}
});