var BtnDiv = null;

function CSlide(MinValue, MaxValue, Width, Height)
{
	this.MinRange = MinValue;
	this.MaxRange = MaxValue;
	this.Width = Width;
	this.Height = Height;

	this.BtnLock = null;
	this.BtnId = 0;

	this.Btn1 = null;
	this.Btn2 = null;
	this.Inner = null;

	this.Btn1Pos = 0;
	this.Btn2Pos = Width - Height;

	this.StartX + 0;

	this.UnitPerPixel = (MaxValue - MinValue + 1) / (Width - Height * 2);
	this.Btn1Value = MinValue;
	this.Btn2Value = MaxValue;
}

CSlide.BtnLock = function(Ev)
{
	if (typeof Ev.currentTarget == 'undefined')
	{
		BtnDiv = window.event.srcElement;
		this.StartX = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
	}
	else
	{
		BtnDiv = Ev.currentTarget;
		this.StartX = Ev.clientX + window.scrollX;
	}

	SlideDiv = BtnDiv.parentNode;
	if (SlideDiv.SlideClass == 'undefined')
		return;

	Slide = SlideDiv.SlideClass;
	Slide.BtnLock = BtnDiv;
	if (Slide.BtnLock == Slide.Btn1)
		Slide.BtnId = 1;
	else
		Slide.BtnId = 2;

	// Ajout monitoring des evenement
	Slide.AddEvent(document, 'mouseup', CSlide.BtnUnlock);
	Slide.AddEvent(document, 'mousemove', CSlide.BtnMove);
}


CSlide.BtnUnlock = function(Ev)
{
	// Retrait monitoring des evenement
	Slide.RemoveEvent(document, 'mouseup', CSlide.BtnUnlock);
	Slide.RemoveEvent(document, 'mousemove', CSlide.BtnMove);

	SlideDiv = BtnDiv.parentNode;
	if (SlideDiv.SlideClass == 'undefined')
		return;

	SlideDiv.SlideClass.StartX = 0;
	SlideDiv = null;

	if(Slide.onchanged != 'undefined')
		Slide.onchanged(Math.floor(Slide.Btn1Value), Math.ceil(Slide.Btn2Value));
}


CSlide.BtnMove = function(Ev)
{
	if (!BtnDiv)
		return;

	if (typeof Ev.currentTarget == 'undefined')
	{
		CurrentX = window.event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
	else
	{
		CurrentX = Ev.clientX + window.scrollX;
		Ev.preventDefault();
		Ev.stopPropagation();
	}

	SlideDiv = BtnDiv.parentNode;
	if (SlideDiv.SlideClass == 'undefined')
		return;

	Slide = SlideDiv.SlideClass;

	if (!Slide.StartX)
		Slide.StartX = CurrentX;

	Diff = CurrentX - Slide.StartX;
	if (!Diff)
		return;

	if (Slide.BtnId == 1)
	{
		if (Slide.Btn1Pos + Diff < 0)
			Slide.Btn1Pos = 0;
		else if (Slide.Btn1Pos + Diff + Slide.Height > Slide.Btn2Pos - 1)
			Slide.Btn1Pos = Slide.Btn2Pos - Slide.Height - 1;
		else
		{
			Slide.Btn1Pos = Slide.Btn1Pos + Diff;
			Slide.StartX = CurrentX;
		}

		Slide.Btn1.style.marginLeft = Slide.Btn1Pos + 'px';
		Slide.Btn1Value = Slide.Btn1Pos * Slide.UnitPerPixel + Slide.MinRange;
	}
	else
	{
		if (Slide.Btn2Pos + Diff + Slide.Height > Slide.Width)
			Slide.Btn2Pos = Slide.Width - Slide.Height;
		else if (Slide.Btn2Pos + Diff <= Slide.Btn1Pos + Slide.Height)
			Slide.Btn2Pos = Slide.Btn1Pos + Slide.Height + 1;
		else
		{
			Slide.Btn2Pos = Slide.Btn2Pos + Diff;
			Slide.StartX = CurrentX;
		}

		Slide.Btn2.style.marginLeft = Slide.Btn2Pos + 'px';
		Slide.Btn2Value = (Slide.Btn2Pos - Slide.Height) * Slide.UnitPerPixel + Slide.MinRange;
	}

	Slide.Inner.style.marginLeft = (Slide.Btn1Pos + Slide.Height) + 'px';
	Slide.Inner.style.width = (Slide.Btn2Pos - (Slide.Btn1Pos + Slide.Height)) + 'px';

	Slide.UpdateLabel();
}


CSlide.prototype.Create = function (DivID, CallBack)
{
	this.DivObj = document.getElementById(DivID);
	if (this.DivObj == null)
		return false;

	this.DivObj.SlideClass = this;
	this.DivObj.style.background = 'URL(./css/pictures/SlideBg.gif) ';

	this.DivID = DivID;
	this.CallBack = CallBack;

	this.Btn1 = document.createElement("div");
	this.Btn1.style.position = 'absolute';
	this.Btn1.style.marginLeft = this.Btn1Pos + 'px';
	this.Btn1.style.backgroundColor = '#000000';
	this.Btn1.style.background = 'url(./css/pictures/FilterArrow1.gif) no-repeat';
	this.Btn1.style.width = this.Height + 'px';
	this.Btn1.style.height = this.Height + 'px';
	this.Btn1.style.cursor = 'pointer';
	this.AddEvent(this.Btn1, 'mousedown', CSlide.BtnLock);

	this.Btn2 = document.createElement("div");
	this.Btn2.style.position = 'absolute';
	this.Btn2.style.marginLeft = this.Btn2Pos + 'px';
	this.Btn2.style.backgroundColor = '#000000';
	this.Btn2.style.background = 'url(./css/pictures/FilterArrow2.gif) no-repeat';
	this.Btn2.style.width = this.Height + 'px';
	this.Btn2.style.height = this.Height + 'px';
	this.Btn2.style.cursor = 'pointer';
	this.AddEvent(this.Btn2, 'mousedown', CSlide.BtnLock);

	this.Inner = document.createElement("div");
	this.Inner.style.position = 'absolute';
	this.Inner.style.marginLeft = (this.Btn1Pos + this.Height) + 'px';
	this.Inner.style.height = this.Height + 'px';
	this.Inner.style.width = this.Btn2Pos - (this.Btn1Pos + this.Height) + 'px';
	this.Inner.style.background = '#F3F3F3';
	this.Inner.style.textAlign = 'center';
	this.Inner.style.overflow = 'hidden';
	this.Inner.style.whiteSpace = 'nowrap';
	this.Inner.style.color = '#666666';
	this.Inner.innerHTML = 'Faites glisser les curseurs';

	this.Label = document.createElement("span");
	this.Label.style.position = 'absolute';
	this.Label.style.marginLeft = (this.Width + 10) + 'px';
	this.Label.innerHTML = 'test';

	this.Hidden = document.createElement("input");
	this.Hidden.type = 'hidden';
	this.Hidden.id = DivID + 'Value';

	this.DivObj.appendChild(this.Inner);
	this.DivObj.appendChild(this.Btn1);
	this.DivObj.appendChild(this.Btn2);
	this.DivObj.appendChild(this.Label);
	this.DivObj.appendChild(this.Hidden);

	this.UpdateLabel();

	return true;
}

CSlide.prototype.SetValues = function(MinVal, MaxVal)
{
	if (!MinVal && !MaxVal)
	{
		this.Btn1Pos = 0;
		this.Btn2Pos = this.Width - this.Height;
	}
	else
	{
		this.Btn1Pos = MinVal;
		this.Btn2Pos = MaxVal;
	}

	this.Btn1.style.marginLeft = this.Btn1Pos + 'px';
	this.Btn2.style.marginLeft = this.Btn2Pos + 'px';
	this.Inner.style.marginLeft = (this.Btn1Pos + this.Height) + 'px';
	this.Inner.style.width = this.Btn2Pos - (this.Btn1Pos + this.Height) + 'px';

	this.Btn1Value = this.Btn1Pos * this.UnitPerPixel + this.MinRange;
	this.Btn2Value = (this.Btn2Pos - this.Height) * this.UnitPerPixel + this.MinRange;

	this.UpdateLabel();
}

CSlide.prototype.AddEvent = function(Elem, EventName, Func)
{
	// IE
	if (Elem.attachEvent)
		Elem.attachEvent("on" + EventName, Func);
	// Gecko
	else if (Elem.addEventListener)
		Elem.addEventListener(EventName, Func, false);
	else
		Elem["on" + EventName] = Func;
}


CSlide.prototype.RemoveEvent = function(Elem, EventName, Func)
{
	// IE
	if (Elem.detachEvent)
		Elem.detachEvent("on" + EventName, Func);
	// Gecko
	else if (Elem.removeEventListener)
		Elem.removeEventListener(EventName, Func, false);
	else
		Elem["on" + EventName] = null;
}

CSlide.prototype.UpdateLabel = function()
{
	// Mise à jour du label
	this.Label.innerHTML = 'De ' + Math.floor(Slide.Btn1Value) + ' &agrave; ' + Math.ceil(Slide.Btn2Value) + ' &euro;';
	this.Hidden.value = Math.floor(Slide.Btn1Value) + ';' + Math.ceil(Slide.Btn2Value) + ';' + Slide.Btn1Pos + ';' + Slide.Btn2Pos;

}

