

var PPDLSliders = {
	sliders: [],
	register: function(slider) {
		this.sliders.push(slider);
	},
	getLastId: function() {
	    return this.sliders.length - 1;
	},
	unregister: function(slider) {
		this.drags = this.drags.reject(function(d) { return d==slider });
	}
};

var PPDLSlider = Class.create();
PPDLSlider.prototype = {

	initialize: function(n, sx, sy) {
	    this.speed = 0.1;
	    this.name = n;
	    this.xsize = sx;
	    this.ysize = sy;
	    this.xshift = 0;
	    this.yshift = 0;
	    this.idle = false;
	    this.nbEvent = 1;
		this.slicer = false;
		this.drag = false;
	    this.scroll = this.wheel.bindAsEventListener(this);
	    Event.observe(window, "DOMMouseScroll", this.scroll);
	    Event.observe(document, "mousewheel", this.scroll);
	    PPDLSliders.register(this);
	    this.id = PPDLSliders.getLastId();
	},

	getSlicerXShift: function() {
	    if (this.xsize == 0)
	        return 0;
		L = (this.xsize * this.nbEvent - 10);
		le = this.xsize / (this.xsize * (this.parent.childNodes.length - this.nbEvent)) * (this.xsize * this.nbEvent);
		return parseInt( (L - le) / (this.parent.childNodes.length - this.nbEvent) );
	},

	isMax: function() {
		var length = this.parent.childNodes.length - this.nbEvent +1;
		return (length == this.slider.value);
	},
	
	isMin: function() {
	    return (this.slider.value == 1);
	},

	getSlicerYShift: function() {
	    if (this.ysize == 0)
	        return 0;
		L = (this.ysize * this.nbEvent - 10);
		le = this.ysize / (this.ysize * (this.parent.childNodes.length - this.nbEvent)) * (this.ysize * this.nbEvent);
		return parseInt( (L - le) / (this.parent.childNodes.length - this.nbEvent) );
	},

	addSlicer: function(visible) {
	    if ( !this.parent )
	        this.parent = $(this.name);
		
		var scrollbar = document.createElement("div");
		Element.setStyle(scrollbar, {padding: '0px', margin: '0px', position: 'relative', height: '16px', width: (this.xsize * this.nbEvent - 10) });

		this.slicer = document.createElement("div");
		if ( Prototype.Browser.IE )
			Element.setStyle(this.slicer, {zoom: '1', display: 'inline', height: '16px', width: (this.xsize * this.nbEvent - 10 - 32)+'px', position: 'relative', background: 'url(/shared/skins/images/bg-ascenseur2.jpg) repeat-x top #dcdcdc'});
		else
			Element.setStyle(this.slicer, {margin: '0px', display: 'inline-block', height: '16px', width: (this.xsize * this.nbEvent - 10 - 32)+'px', position: 'relative', background: 'url(/shared/skins/images/bg-ascenseur2.jpg) repeat-x top #dcdcdc'});
		
		//var X = this.xsize / (this.xsize * (this.parent.childNodes.length - this.nbEvent)) * (this.xsize * this.nbEvent);
		//var X = this.getSlicerXShift();
		var X = 20;

		var s = document.createElement("div");
		s.id = this.name + "_slidermove";
		var cursortype = (this.xsize != 0 ? "e-resize" : "n-resize");
		Element.setStyle(s, {cursor: cursortype, margin: '1px', height: '13px', width: parseInt( X )+'px', position: 'relative', background: 'url(/shared/skins/images/ascenseur-bullet.jpg) no-repeat top transparent'});
		this.slicer.id = this.name + "_slider";
		this.slicer.appendChild(s);
		
		var buttonleft = document.createElement("img");
		buttonleft.src = '/shared/skins/images/img-ascenseur-left.jpg';
		if ( Prototype.Browser.IE )
			buttonleft.align='absmiddle';
		this.ondownclick = this.down.bindAsEventListener(this);
		buttonleft.onclick = this.ondownclick;
		scrollbar.appendChild(buttonleft);

		scrollbar.appendChild(this.slicer);

		var buttonright = document.createElement("img");
		buttonright.src = '/shared/skins/images/img-ascenseur-right.jpg';
		if ( Prototype.Browser.IE )
			buttonright.align = 'absmiddle';
		this.onupclick = this.up.bindAsEventListener(this);
		buttonright.onclick = this.onupclick;
		
		scrollbar.appendChild(buttonright);
		
		this.parent.parentNode.appendChild(scrollbar);
		
		
		if (!visible)
			Element.setStyle(scrollbar, {display: 'none'});

		this.onslide = this.move.bindAsEventListener(this);

		var length = this.parent.childNodes.length - this.nbEvent +1;
		valuesList = new Array();
		for(var i=1; i<=length; i++)
		    valuesList.push(i);

		this.slider = new Control.Slider(this.slicer.firstChild.id, this.slicer.id, {
		    range: $R(1, length),
		    values: valuesList,
		    onSlide: this.onslide,
		    onChange: this.onslide
		});

	},

	move: function(value) {
        var xshift = 0, yshift = 0, diff;
		if ( this.idle )
		    return;
        if (this.xsize != 0) // horizontal
        {
            var nx = Math.abs( parseInt(this.xshift / this.xsize) ) +1;
            if (value == nx)
                return;
			if (value < nx)
			    xshift = this.xsize;
			else
			    xshift = -this.xsize;

			diff = Math.abs(value - nx);
			this.xshift += xshift*diff;

		}
        if (this.ysize != 0) // vertical
        {
            var ny = Math.abs( parseInt(this.yshift / this.ysize) ) +1;
            if (value == ny)
                return;
			if (value < ny)
			    yshift = this.ysize;
			else
			    yshift = -this.ysize;

			diff = Math.abs(value - ny);
			this.yshift += yshift*diff;
		}
		this.shift(xshift, yshift, true, diff);
	},

	down: function() {
		if ( this.slicer )
		    this.slider.setValueBy( -1 );
	},
	
	up: function() {
		if ( this.slicer )
		    this.slider.setValueBy( 1 );
	},

	eventDisplay: function(n) {
	    this.nbEvent = n;
	},

	shift: function(xshift, yshift, queued, number) {
	    if ( !number )
	        number = 1;
	    xshift = parseInt(xshift) * number;
	    yshift = parseInt(yshift) * number;
	    speed = this.speed;
	    id = this.id;
		name = this.name;
		if (xshift > 0)
		    slicerxshift = -this.getSlicerXShift() * Math.abs(number);
		else
		    slicerxshift = this.getSlicerXShift() * Math.abs(number);
		if (yshift > 0)
		    sliceryshift = -this.getSlicerYShift() * Math.abs(number);
		else
		    sliceryshift = this.getSlicerYShift() * Math.abs(number);
		if ( queued )
		{
			new Effect.Move( $(this.parent.id), {
				x: xshift,
				y: yshift,
				duration: speed,
				beforeStart: function() {
					PPDLSliders.sliders[id].idle = true;
				},
				afterFinish: function() {
					PPDLSliders.sliders[id].idle = false;
				},
				queue: {position: 'end', scope: name}
			});
		}
		else
		{
			new Effect.Move( $(this.parent.id), {
				x: xshift,
				y: yshift,
				duration: speed,
				beforeStart: function() {
					PPDLSliders.sliders[id].idle = true;
				},
				afterFinish: function() {
					PPDLSliders.sliders[id].idle = false;
				}
			});
		}

	},

	handle: function(element, delta) {
	    var currentElement = -1;
	    // recuperation de la div container
	    var parent = element.parentNode;
		var nbElements = parent.childNodes.length;
		for(var i=0; i<nbElements; i++)
		{
		    if (parent.childNodes[i] == element)
		    {
		        currentElement = i;
		        break;
		    }
		}
		if (currentElement == -1)
		    return;

		var xshift = 0;
		var yshift = 0;

	    if (delta < 0)
	    {
	        if ( ( this.xshift == -(nbElements-this.nbEvent) * this.xsize && this.xsize != 0 ) || ( this.yshift == -(nbElements-this.nbEvent) * this.ysize && this.ysize != 0 ) )
	            return;
			this.xshift = this.xshift - this.xsize;
            xshift = -this.xsize;
			this.yshift = this.yshift - this.ysize;
            yshift = -this.ysize;
	    }
	    else
	    {
	        if ( ( this.xshift == 0 && this.xsize != 0 ) || ( this.yshift == 0 && this.ysize != 0 ) )
	            return;
			this.xshift = this.xshift + this.xsize;
            xshift = this.xsize;
			this.yshift = this.yshift + this.ysize;
            yshift = this.ysize;
	    }

		if ( this.slicer )
		    this.slider.setValueBy( -xshift / this.xsize );
		this.shift(xshift, yshift, true);

	},

    wheel: function(event) {

	    if ( !event )
	        event = window.event;

		myclass = this;

		var element = event.target || event.srcElement;
		child = true;
		while( child && element.parentNode)
		{
		    if (element.parentNode.id != this.name)
				element = element.parentNode;
			else
			    child = false;
		}
		if ( !this.parent )
		    this.parent = element.parentNode;
		if (!element.parentNode)
		    return;

		var delta = 0;
		if (!event) // pour IE
			event = window.event;
		if (event.wheelDelta)
		{
			// IE/Opera.
			delta = event.wheelDelta/120;
			// Opera 9, delta differs in sign as compared to IE.
			//if (window.opera)
			//	delta = -delta;
		}
		else if (event.detail)
		{
			// Mozilla
			// delta est multiplié par 3.
			delta = -event.detail/3;
		}

		if (delta)
			this.handle(element, delta);
		/** Prevent default actions caused by mouse wheel.
		 * That might be ugly, but we handle scrolls somehow
		 * anyway, so don't bother here..
		 */
		if (event.preventDefault)
			event.preventDefault();
		event.returnValue = false;
	}
};
//var slider1 = new PPDLSlider('bloc', 500, 0);
//var slider2 = new PPDLSlider('grumble', 0, 150);
