function get(id)
{
	return document.getElementById(id) || false;
}

PriceForm = function (interval, line, min, max, min_value, max_value, min_field, max_field, price_min, price_max, block, alter, step)
{
	this.eInterval = get(interval);
	this.eMin = get(min);
	this.eMax = get(max);
	this.eMinValue = get(min_value);
	this.eMaxValue = get(max_value);
	this.eLine = get(line);
	
	this.maxValueSet = false;
	
	this.eMinField = get(min_field);
	this.eMaxField = get(max_field);
	
	this.priceMin = price_min;
	this.priceMax = price_max;
	
	this.shift = 0
	
	this.step = step;
	
	this.block = get(block);
	this.alter = get(alter);

	this.prepare = function ()
	{
		this.alter.style.display = 'none';
		this.block.style.display = '';
		this.defaultLeftBorder = 0;
		this.defaultRightBorder = this.eLine.offsetWidth;

		
		this.eMinField.value = this.eMinField.value || this.priceMin;
		if (this.eMaxField.value == '')
		{
			this.maxValueSet = true;
		}
		else
		{
			this.eMaxField.value = this.eMaxField.value || this.priceMax;
		}
		this.eMinField.value = this.eMinField.value < this.priceMin ? this.priceMin : this.eMinField.value;
		
		if (this.eMaxField.value > this.priceMax)
		{
			this.maxValueSet = true;
			this.eMaxField.value = '';
		}
		this.resize(this.eMinField.value, this.eMaxField.value);

		var money = this.eMinField.value;
		money = money.toString();
		switch(money.length)
		{
			case 4: money = money.substr(0, 1) + ',' + money.substr(1, 3);break;
			case 5: money = money.substr(0, 2) + ',' + money.substr(1, 3);break;
			case 6: money = money.substr(0, 3) + ',' + money.substr(1, 3);break;
			case 7: money = money.substr(0, 1) + ',' + money.substr(1, 3) + ',' + money.substr(4, 3);break;
			case 8: money = money.substr(0, 2) + ',' + money.substr(1, 3) + ',' + money.substr(4, 3);break;
			case 9: money = money.substr(0, 3) + ',' + money.substr(1, 3) + ',' + money.substr(4, 3);break;
		}
		this.eMinValue.innerHTML = '$' + money;
		if (this.maxValueSet)
		{
			this.eMaxValue.innerHTML = 'max';
		}
		else
		{
			var money = this.eMaxField.value;
			money = money.toString();
			switch(money.length)
			{
				case 4: money = money.substr(0, 1) + ',' + money.substr(1, 3);break;
				case 5: money = money.substr(0, 2) + ',' + money.substr(1, 3);break;
				case 6: money = money.substr(0, 3) + ',' + money.substr(1, 3);break;
				case 7: money = money.substr(0, 1) + ',' + money.substr(1, 3) + ',' + money.substr(4, 3);break;
				case 8: money = money.substr(0, 2) + ',' + money.substr(1, 3) + ',' + money.substr(4, 3);break;
				case 9: money = money.substr(0, 3) + ',' + money.substr(1, 3) + ',' + money.substr(4, 3);break;
			}
			this.eMaxValue.innerHTML = '$' + money;
		}
	}
	
	this.resize = function (min, max)
	{
		this.defaultRightBorder = this.eLine.offsetWidth ? this.eLine.offsetWidth : window.defaultRightBorder;
		if (this.defaultRightBorder)
		{
			if (parseInt(min) >= 0)
			{
				var money =  parseInt(min);
			}
			else 
			{
				var minValue = this.eMinField.value;
				if (minValue >= 0)
				{
					var money = parseInt(minValue.toString().replace(',', '').replace('$', ''))
				}
			}
			if (money >= 0)
			{
				this.eMin.style.left = Math.round((money - this.priceMin) * (this.defaultRightBorder - this.eMax.offsetWidth) / (this.priceMax - this.priceMin)) + 'px';
			}

			if (parseInt(max) >= 0)
			{
				var money2 =  parseInt(max);
			}
			else 
			{
				var maxValue = this.eMaxField.value;
				if (maxValue)
				{
					//var money2 = parseInt(maxValue.toString().replace(',', '').replace('$', ''))
					var money2 = parseInt(maxValue);
				}
			}
			if (money2)
			{
				this.eMax.style.left = Math.round((money2 - this.priceMin) * (this.defaultRightBorder - this.eMax.offsetWidth) / (this.priceMax - this.priceMin)) + 'px';
			}
			else if (this.maxValueSet)
			{
				this.eMax.style.left = this.defaultRightBorder - this.eMax.offsetWidth + 'px';
			}
		}
		this.setPosition();
	}
	
	this.activator = function () {/* рыба */}
	
	this.drag = function (event)
	{
		if (!event)
        {
            // For IE.
            event = window.event;
        }
		var target = event.target ? event.target : event.srcElement;
        if (target)
        {
            if (target.id == this.eMin.id) 
			{
				this.canDragMin = true;
				this.shift = event.clientX - parseInt(this.eMin.style.left);
			}
			if (target.id == this.eMax.id) 
			{
				this.canDragMax = true;
				this.shift = event.clientX - parseInt(this.eMax.style.left);
			}
        }
		if (this.canDragMin || this.canDragMax)
		{
			this.blockEvent(event);
		}
        return false;
	}
	
	this.drop = function ()
	{
		this.canDragMin = false;
		this.canDragMax = false;
		return false;
	}
	
	this.move = function (event)
	{
        if (!event)
        {
            event = window.event;
        }
		this.setPosition(event.clientX - this.shift);
		if (this.canDragMin || this.canDragMax)
		{
			this.blockEvent(event);
		}
        return false;
	}
	
    this.blockEvent = function (event)
    {
        if(event.stopPropagation) event.stopPropagation();
        else event.cancelBubble = true;
        if(event.preventDefault) event.preventDefault();
        else event.returnValue = false;
    }
	
	this.setPosition = function (newPosition)
	{
		if (this.canDragMin)
		{
			var elem = this.eMin;
			var value = this.eMinValue;
			var leftBorder = this.defaultLeftBorder;
			var rightBorder = parseInt(this.eMax.style.left) - this.eMax.offsetWidth;
			var field = this.eMinField;
		}
		if (this.canDragMax)
		{
			var elem = this.eMax;
			var value = this.eMaxValue;
			var leftBorder = parseInt(this.eMin.style.left) + this.eMin.offsetWidth;
			var rightBorder = this.defaultRightBorder - this.eMax.offsetWidth;
			var field = this.eMaxField;
		}
        if (this.canDragMin || this.canDragMax)
		{
			if(newPosition > rightBorder && this.canDragMax)
			{        
				newPosition = rightBorder;
				this.maxValueSet = true;
			}
			else if (newPosition <= rightBorder && this.canDragMax)
			{
				this.maxValueSet = false;
			}
			else if (newPosition > rightBorder)
			{
				newPosition = rightBorder;
			}
			if(newPosition < leftBorder)
			{
				newPosition = leftBorder;
			}
			elem.style.left = newPosition + "px";
			if (this.maxValueSet && this.canDragMax)
			{
				field.value = '';
				value.innerHTML = 'max';
			}
			else
			{
				var money = (this.priceMin + Math.round(newPosition / (this.defaultRightBorder - this.eMax.offsetWidth) * (this.priceMax - this.priceMin))).toString();
				money = Math.round(money / step) * step;
				field.value = money;
				money = money.toString();
				switch(money.length)
				{
					case 4: money = money.substr(0, 1) + ',' + money.substr(1, 3);break;
					case 5: money = money.substr(0, 2) + ',' + money.substr(1, 3);break;
					case 6: money = money.substr(0, 3) + ',' + money.substr(1, 3);break;
					case 7: money = money.substr(0, 1) + ',' + money.substr(1, 3) + ',' + money.substr(4, 3);break;
					case 8: money = money.substr(0, 2) + ',' + money.substr(1, 3) + ',' + money.substr(4, 3);break;
					case 9: money = money.substr(0, 3) + ',' + money.substr(1, 3) + ',' + money.substr(4, 3);break;
				}
				value.innerHTML = '$' + money;
			}
		}
		this.eInterval.style.left = parseInt(this.eMin.style.left) + 7 + 'px';
		if (parseInt(this.eMax.style.left) - parseInt(this.eMin.style.left) - 7 > 0)
		{
			this.eInterval.style.width = (parseInt(this.eMax.style.left) - parseInt(this.eMin.style.left) - 7) + 'px';
		}
		else
		{
			this.eInterval.style.width = '1px';
		}
        return false;
	}
}
