var RSPlug_Mover2 = function (container, object, sens){ 
	this.init(container, object, sens); 
};
RSPlug_Mover2.prototype.init = function(container, object, sens){
	this.container = (typeof(container) == 'string' ? document.getElementById(container) : container);
	this.object = (typeof(object) == 'string' ? document.getElementById(object) : object);
	if (this.container.style.position != 'absolute'){
		this.container.style.position = 'relative';
	}
	this.object.style.position = 'absolute';
	this.object.style.left = 0;
	this.object.style.top = 0;
	this.onDeplaceCallback = null;
	this.onInverseCallback = null;
	this.timeouts = {};
	this.vars = {};
	this.vars.deplacementAuto = false;
	this.vars.inversementAuto = true;
	this.vars.pause = false;
	this.vars.sens = null;
	this.vars.vitesse = 30;
	this.vars.pas = 1.5;
	this.sensDeplacement(sens);
};
RSPlug_Mover2.prototype.deplace = function(sens, pas){
	pas = (null === pas || undefined === pas) ? this.vars.pas : pas;
	if (sens == 'gauche'){
		this.object.style.left = (parseFloat(this.object.style.left) +pas) +'px';
	} else if (sens == 'droite'){
		this.object.style.left = (parseFloat(this.object.style.left) -pas) +'px';
	} else if (sens == 'haut'){
		this.object.style.top = (parseFloat(this.object.style.top) -pas) +'px';
	} else if (sens == 'bas'){
		this.object.style.top = (parseFloat(this.object.style.top) +pas) +'px';
	}
	this.peutDeplacer(sens); // Ajuste les sorties
	if (null !== this.onDeplaceCallback && typeof(this.onDeplaceCallback) == 'function'){
		this.onDeplaceCallback(this);
	}
};
RSPlug_Mover2.prototype.peutDeplacer = function(sens){
	if (sens == 'gauche'){
		if ((parseFloat(this.object.style.left)) < 0){
			return true;
		}
		this.object.style.left = 0;
	} else if (sens == 'droite'){
		if (-(parseInt(this.object.offsetWidth)) < (parseFloat(this.object.style.left) - parseInt(this.container.offsetWidth))){
			return true;
		}
		this.object.style.left = -(parseInt(this.object.offsetWidth) - parseInt(this.container.offsetWidth)) +'px';
	} else if (sens == 'bas'){
		if ((parseFloat(this.object.style.top)) < 0){
			return true;
		}
		this.object.style.top = 0;
	} else if (sens == 'haut'){
		if (-(parseInt(this.object.offsetHeight)) < (parseFloat(this.object.style.top) - parseInt(this.container.offsetHeight))){
			return true;
		}
		this.object.style.top = -(parseInt(this.object.offsetHeight) - parseInt(this.container.offsetHeight)) +'px';
	}
	return false;
};
RSPlug_Mover2.prototype.deplacementAuto = function(deplacementAuto){
	if (null !== deplacementAuto && undefined !== deplacementAuto){
		this.vars.deplacementAuto = (deplacementAuto ? true : false);
		if (this.vars.deplacementAuto){
			var inst = this;
			this.ajouteTimeout('deplacementAuto', function(){ inst.callbackDeplacementAuto(); }, this.vars.vitesse, true);
		} else {
			this.detruitTimeout('deplacementAuto');
		}
	}
	return this.vars.deplacementAuto;
};
RSPlug_Mover2.prototype.inversementAuto = function(inversementAuto){
	if (null !== inversementAuto && undefined !== inversementAuto){
		this.vars.inversementAuto = (inversementAuto ? true : false);
	}
	return this.vars.inversementAuto;
};
RSPlug_Mover2.prototype.callbackDeplacementAuto = function(){
	if (!this.vars.pause){
		if (this.peutDeplacer(this.vars.sens)){
			this.deplace(this.vars.sens);
		} else if (this.vars.inversementAuto){
			if (null !== this.onInverseCallback && typeof(this.onInverseCallback) == 'function'){
				this.onInverseCallback(this);
			}
			this.sensDeplacement(this.sensOppose(this.vars.sens));
			if (this.peutDeplacer(this.vars.sens)){
				this.deplace(this.vars.sens);
			}
		}
	}
};
RSPlug_Mover2.prototype.pause = function(pause){
	if (null !== pause && undefined !== pause){
		this.vars.pause = (pause ? true : false);
	}
	return this.vars.pause;
};
RSPlug_Mover2.prototype.vitesse = function(vitesse){
	if (null !== vitesse && undefined !== vitesse && !isNaN(vitesse) && vitesse > 0){
		this.vars.vitesse = vitesse;
	}
	return this.vars.vitesse;
};
RSPlug_Mover2.prototype.sensDeplacement = function(sens){
	if (null !== sens && undefined !== sens && (sens == 'haut' || sens == 'bas' || sens == 'gauche' || sens == 'droite')){
		this.vars.sens = sens;
	}
	return this.vars.sens;
};
RSPlug_Mover2.prototype.sensOppose = function(sens){
	if (sens == 'gauche'){
		return 'droite';
	} else if (sens == 'droite'){
		return 'gauche';
	} else if (sens == 'haut'){
		return 'bas';
	} else if (sens == 'bas'){
		return 'haut';
	}
	return null;
};
RSPlug_Mover2.prototype.declareOnDeplace = function(callback){
	if (typeof(callback) == 'function'){
		this.onDeplaceCallback = function(inst){ callback.call(inst); };
	}
};
RSPlug_Mover2.prototype.declareOnInverse = function(callback){
	if (typeof(callback) == 'function'){
		this.onInverseCallback = function(inst){ callback.call(inst); };
	}
};
RSPlug_Mover2.prototype.declareOver = function(callback){
	if (typeof(callback) == 'function'){
		var inst = this;
		this.object.onmouseover = function(e){ var evt = window.event || e; callback.call(inst, evt); };
	}
};
RSPlug_Mover2.prototype.declareOut = function(callback){
	if (typeof(callback) == 'function'){
		var inst = this;
		this.object.onmouseout = function(e){ var evt = window.event || e; callback.call(inst, evt); };
	}
};
RSPlug_Mover2.prototype.declareZone = function(zone, callbackOver, callbackOut){
	if (typeof(callbackOver) == 'function'){
		var inst = this;
		zone.onmouseover = function(e){ var evt = window.event || e; callbackOver.call(inst, evt, zone); };
	}
	if (typeof(callbackOut) == 'function'){
		var inst = this;
		zone.onmouseout = function(e){ var evt = window.event || e; callbackOut.call(inst, evt, zone); };
	}
};
RSPlug_Mover2.prototype.ajouteTimeout = function(idTimeout, callbackTimeout, vitesseTimeout, recursif){
	if (typeof(callbackTimeout) == 'function'){
		this.detruitTimeout(idTimeout);
		var inst = this;
		var recursif = (recursif ? true : false);
		if (recursif){
			var vitesseTimeoutRecursif = (typeof(vitesseTimeout) == 'function' ? vitesseTimeout() : vitesseTimeout); 
			this.timeouts[idTimeout] = setTimeout(function(){ callbackTimeout(); inst.ajouteTimeout(idTimeout, callbackTimeout, vitesseTimeout, recursif); }, vitesseTimeoutRecursif);
		} else {
			this.timeouts[idTimeout] = setTimeout(function(){ callbackTimeout(); }, vitesseTimeout);
		}
	}
};
RSPlug_Mover2.prototype.detruitTimeout = function(idTimeout){
	if (this.timeouts[idTimeout]){
		clearTimeout(this.timeouts[idTimeout]);
		this.timeouts[idTimeout] = null;
	}
};



RSPlug_Mover2.prototype.createZone = function(idZone, sens, zoneObj){
	var zone = new RSPlug_Mover2.zone(idZone, sens, zoneObj);
	zone.mover = this;
	return zone;
};
RSPlug_Mover2.zone = function(idZone, sens, zoneObj){
	this.id = idZone;
	this.sens = sens;
	this.zoneObj = zoneObj;
	this.coefZone = 2;
	this.multiplicateur = 10;
};
RSPlug_Mover2.zone.prototype.declareOver = function(callbackOver){
	if (typeof(callbackOver) == 'function'){
		var inst = this;
		this.zoneObj.onmouseover = function(e){ var evt = window.event || e; callbackOver.call(inst, inst.id, inst.sens, evt, inst.zoneObj); };
	}
};
RSPlug_Mover2.zone.prototype.declareOut = function(callbackOut){
	if (typeof(callbackOut) == 'function'){
		var inst = this;
		this.zoneObj.onmouseout = function(e){ var evt = window.event || e; callbackOut.call(inst, inst.id, inst.sens, evt, inst.zoneObj); };
	}
};
RSPlug_Mover2.zone.prototype.coef = function(){
	return this.coefZone; 
};
RSPlug_Mover2.zone.prototype.calculCoef = function(e, inverse){
	var evt = window.event || e;
	if (evt.pageX){
		var curleft = 0;
		var obj = this.zoneObj;
		if (obj.offsetParent) {
			do {
				curleft += obj.offsetLeft;
			} while (obj = obj.offsetParent);
		}
		var posX = (evt.pageX -curleft);
	} else {
		var posX = evt.offsetX;
	}
	if (inverse){
		this.coefZone = (((parseInt(this.zoneObj.offsetWidth) - posX) * this.multiplicateur / parseInt(this.zoneObj.offsetWidth)) +1);
	} else {
		this.coefZone = ((posX * this.multiplicateur / parseInt(this.zoneObj.offsetWidth)) +1);
	}
};
