/*
Script : mooSauterAideChoix.js
04/2009 - massacred by chris wait aka virtualgadjo
tourne avec mootools 1.2.1
*/
var mooSauterAideChoix = new Class ({

	Implements: [Events, Options],

	options: {
		onReset      : Class.empty,
		onComplete   : Class.empty,
		choixClass   : 'acChoix', //class de la liste contenant les items à classer
		reponsesClass: 'acDrops', //class de la liste contenant les résultats
		duration     : 400,
		transition   : Fx.Transitions.linear,
		actifClass   : 'acActif', //css class pour signaler qu'un élément est en position de remplir sa drop zone
		inactifClass : 'acInactif',
		resetButt    : 'resetButt', //class des boutons reset s'il y en a
		zIndex       : 50
	},

	initialize: function(ziContainer, options){
		this.setOptions(options);
		this.c             = $(ziContainer);
		this.drags         = this.c.getFirst('ul.' + this.options.choixClass).getChildren('li').getFirst('div');
		this.drops         = this.c.getFirst('ul.' + this.options.reponsesClass).getChildren('li');
		this.sort          = this.c.getFirst('ul.' + this.options.reponsesClass);
		this.resetButts    = this.c.getElements('.' + this.options.resetButt);
		this.questRep      = this.c.getFirst('input[type=hidden]');
		this.reponse       = [];

		this.formerabs     = [];
		this.formerord     = [];
		this.largeur       = [];
		this.hauteur       = [];
		this.choice        = [];

		this.c.setStyle('position', this.c.getStyle('position') == 'absolute' ? 'absolute' : 'relative');

		this.drags.each(function(el, i){

			this.absi      = el.getCoordinates(this.c).left;
			this.ord       = el.getCoordinates(this.c).top;
			this.dWidth    = el.getCoordinates(this.c).width;
			this.dHeight   = el.getCoordinates(this.c).height;
			this.newElem   = el.clone().inject(this.c, 'top');

			this.formerabs.push(this.absi);
			this.formerord.push(this.ord);
			this.largeur.push(this.dWidth);
			this.hauteur.push(this.dHeight);

			this.newElem.setStyles({
				'position'  : 'absolute',
				'top'       : this.ord,
				'left'      : this.absi,
				'z-index'   : this.options.zIndex,
				'cursor'    : 'move'
			});
			this.choice.push(this.newElem);
			this.choiceNum = this.choice.length;

		}.bind(this));

		this.firstChoice();

		if (this.resetButts.length > 0){
			this.resetButts.each(function(el){
				el.addEvent('click', function(e){
					e.stop();
					this.reset();
				}.bind(this));
			}.bind(this));
		}

	},

	firstChoice: function(){
		var toto = 0;

		this.choice.each(function(elem, i) {
			var zidrag = new Drag.Move(elem, {

				droppables: this.drops,

				onEnter : function(el, droppable){
					if (droppable.get('text') == '') el.addClass(this.options.actifClass);
				}.bind(this),

				onLeave : function(el){
					el.removeClass(this.options.actifClass);
				}.bind(this),

				onDrop : function(el, droppable) {
					var backInPlace = new Fx.Morph(el, {
						duration: this.options.duration,
						transition: this.options.transition
					});
					if (!droppable) backInPlace.start({'left': this.formerabs[i], 'top': this.formerord[i]});
					else{
						if (!droppable.hasClass('busy')) {
							toto ++;
							droppable.addClass('busy').set('rel', i);
							el.setStyles({
								'left'   : droppable.getCoordinates(this.c).left,
								'top'    : droppable.getCoordinates(this.c).top,
								'z-index': droppable.getStyle('z-index'),
								'cursor' : 'auto'
							})
							this.drags[i].addClass(this.options.inactifClass);
							zidrag.detach();
						}
						//backInPlace.start({'left': this.formerabs[i], 'top': this.formerord[i]});
						if (toto == this.choiceNum) { this.setInput(); this.fireEvent('onComplete'); }
					}
				}.bind(this)
			});
		}.bind(this));
	},

	setInput: function(){
		this.drops.each(function(el, i){
			this.reponse.push(el.get('rel'));
		}.bind(this));
		this.repString = this.reponse.toString();
		this.questRep.set('value', this.repString);
	},

	reset: function(){
		this.reponse.empty();
		this.repString = "";
		this.questRep.set('value', '');
		this.drops.each(function(el){
			el.removeClass('busy');
			el.set('rel', '');
		});
		this.choice.each(function(el, i){
			el.removeClass(this.options.actifClass); //pour cette burne d'ie...
			el.setStyles({
				'position'  : 'absolute',
				'top'       : this.formerord[i],
				'left'      : this.formerabs[i],
				'z-index'   : this.options.zIndex,
				'cursor'    : 'move'
			});
		}.bind(this));
		this.drags.each(function(el, i){
			el.removeClass(this.options.inactifClass);
		}.bind(this));
		this.fireEvent('onReset');
		this.firstChoice();
	}

});