
var cursor_1 = 0;
var cursor_2 = 0;
var old_cursor = 0;

var all_width = 200;

var cur_pos_cursor_1 = 0;
var delta_cursor_1 = 13;
var cur_pos_cursor_2 = 200;

var kof = new Array();

function elements()
{
	this.id;
	this.kof;
	this.log_n;
}

function show_bar(id)
{
	var el = document.getElementById(id);	
	//alert(el);
	if(el!=null)
	{
		var str = '<div style="position: relative; height: 12px; width: ' + all_width + 'px;">'+
						'<div style="position: absolute; left: -3px; font-size: 10px;">0</div>'+
						'<div style="position: absolute; left: 80px; font-size: 10px;">10000</div>'+
						'<div style="position: absolute; left: 180px; font-size: 10px;">20000</div>'+
						'<div style="position: absolute; left: 0px; top:15px;">' +
							'<div style="position: relative; height: 5px; width: ' + all_width + 'px; background: url(/img/images/bar_1.png) repeat-x;">' + 
						  		'<div id="' + id + '_diapazon" style="position: absolute; left: ' + cur_pos_cursor_1 + 'px; height: 5px; width: ' + all_width + 'px; background: url(/img/images/bar_2.png) repeat-x; float: right;"></div>' +
						  	'</div>' + 
						'</div>'+
						
						'<div style="position: absolute; left: 0px; top: 12px;"><img src="/img/images/palka.gif"></div>'+
						'<div style="position: absolute; left: 100px; top: 12px;"><img src="/img/images/palka.gif"></div>'+
						'<div style="position: absolute; left: 200px; top: 12px;"><img src="/img/images/palka.gif"></div>'+
						
						'<div style="position: absolute; left: 0px; top:20px;">' +
						  	'<div style="position: relative; height: 12px; width: ' + all_width + 'px;">'+
							  	'<div OnMouseDown="mousedown(event, 1, \'' + id + '\')" id="' + id + '_cursor_1" style="position: absolute; left: ' + (cur_pos_cursor_1 - delta_cursor_1) +'px; height: 12px; width: 13px; background: url(/img/images/cursor_1.png) no-repeat; cursor: pointer;"></div>'+
							  	'<div OnMouseDown="mousedown(event, 2, \'' + id + '\')" id="' + id + '_cursor_2" style="position: absolute; left: ' + cur_pos_cursor_2 + 'px; height: 12px; width: 13px; background: url(/img/images/cursor_2.png) no-repeat; cursor: pointer;"></div>'+
						    '</div>'+
						'</div>'+
				  '</div>';

		el.innerHTML = str;

		var body = document.getElementsByTagName('body');
		
		//alert(body['0']);
		//body['0'].onclick = endmove;
		body['0'].onmouseup = endmove;
		body['0'].onmousemove = mousemove;
		//addHandler(body['0'], "click", endmove);
		//addHandler(body['0'], 'MouseUp', endmove);
		//addHandler(body['0'], 'OnMouseMove', mousemove);
	}
}
//http://webew.ru/articles/174.webew
function mousedown(e, cursor, id)
{//alert(id);
	if(!e)
		e = event;
		
	if(cursor==1)
	{
		cursor_1 = id;
		cursor_2 = 0;
		
		cur_pos_cursor_1 = parseInt(document.getElementById(cursor_1 + '_cursor_1').style.left) + delta_cursor_1;
		cur_pos_cursor_2 = parseInt(document.getElementById(cursor_1 + '_cursor_2').style.left);
	}
	else
	{
		cursor_2 = id;
		cursor_1 = 0;
		
		cur_pos_cursor_1 = parseInt(document.getElementById(cursor_2 + '_cursor_1').style.left) + delta_cursor_1;
		cur_pos_cursor_2 = parseInt(document.getElementById(cursor_2 + '_cursor_2').style.left);
	}
	old_cursor = e.screenX;
	
}

function mousemove(e)
{
	if(!e)
		e = event;

	if(cursor_1!=0)
	{
		var k=0;
		var log_n = 0;
		for(var i=0; i<kof.length; i++)
		{
			if(kof[i].id==cursor_1)
			{
				k = kof[i].kof;
				log_n = kof[i].log_n;
				break;
			}
		}
		delta_cursor = old_cursor - e.screenX;
		old_cursor = e.screenX;
		
		cur_pos_cursor_1 = cur_pos_cursor_1 - delta_cursor;
		if(cur_pos_cursor_1<0)
			cur_pos_cursor_1 = 0;
		
		if(cur_pos_cursor_1>cur_pos_cursor_2)
			cur_pos_cursor_1 = cur_pos_cursor_2;
			
		document.getElementById(cursor_1 + '_cursor_1').style.left = (cur_pos_cursor_1 - delta_cursor_1);	
		
		var diapazon = document.getElementById(cursor_1 + '_diapazon');
		diapazon.style.left = cur_pos_cursor_1 + 'px';
		diapazon.style.width = (cur_pos_cursor_2 - cur_pos_cursor_1) + 'px';
		
		if(log_n==null)
			document.getElementById(cursor_1 + '_val_1').value = cur_pos_cursor_1 * k;
		else
			document.getElementById(cursor_1 + '_val_1').value = parseInt(k * Math.pow(cur_pos_cursor_1, log_n));
	}
	
	if(cursor_2!=0)
	{
		var k=0;
		var log_n = 0;
		for(var i=0; i<kof.length; i++)
		{
			if(kof[i].id==cursor_2)
			{
				k = kof[i].kof;
				log_n = kof[i].log_n;
				break;
			}
		}
		
		delta_cursor = old_cursor - e.screenX;
		old_cursor = e.screenX;
		
		cur_pos_cursor_2 = cur_pos_cursor_2 - delta_cursor;
		if(cur_pos_cursor_2>all_width)
			cur_pos_cursor_2 = all_width;
		
		if(cur_pos_cursor_2<cur_pos_cursor_1)
			cur_pos_cursor_2 = cur_pos_cursor_1;
			
		document.getElementById(cursor_2 + '_cursor_2').style.left = cur_pos_cursor_2 + 'px';	
		
		var diapazon = document.getElementById(cursor_2 + '_diapazon');
		diapazon.style.width = (cur_pos_cursor_2 - cur_pos_cursor_1) + 'px';
		
		if(log_n==null)
			document.getElementById(cursor_2 + '_val_2').value = cur_pos_cursor_2 * k;
		else
			document.getElementById(cursor_2 + '_val_2').value = parseInt(k * Math.pow(cur_pos_cursor_2, log_n));	
	}
}

function endmove(e)
{
	if(!e)
		e = event;
		
	cursor_1 = 0;
	cursor_2 = 0;
}

function init_bar(id, name, start_value, end_value, ed, cur_val_1, cur_val_2, k, log_n)
{
	var avg_value = end_value/2;
	if(log_n==null)
	{
		val_1 = cur_val_1*k;
		val_2 = cur_val_2*k;
	}
	else
	{
		if(cur_val_1>0)
			val_1 = Math.pow(cur_val_1/k, 1/log_n);
		else
			val_1 = 0;
		
		if(cur_val_2>0)
			val_2 = Math.pow(cur_val_2/k, 1/log_n);
		else
			val_2 = 0;	
			
		//avg_value = parseInt(k * Math.pow(100, log_n));	
		avg_value = 10000;
	}
	var str=name + "<br>от <input class='no_border' id='" + id + "_val_1' type='text' value='" + cur_val_1 + "' size='4' name='data[Find][" + id + "_val_1]'/>"+
					" до "+
					"<input class='no_border' id='" + id + "_val_2' type='text' value='" + cur_val_2 + "' size='4' name='data[Find][" + id + "_val_2]'/>"+
					 " " +ed +
					"<div style='margin: 0px;'>"+
						'<div style="position: relative; height: 12px; width: ' + all_width + 'px;margin: 0px;">'+
						'<div style="position: absolute; left: -3px; font-size: 10px;margin: 0px;">' + start_value + '</div>'+
						'<div style="position: absolute; left: 80px; font-size: 10px;margin: 0px;">' + avg_value + '</div>'+
						'<div style="position: absolute; left: 180px; font-size: 10px;margin: 0px;">' + end_value + '</div>'+
						'<div style="position: absolute; left: 0px; top:15px;margin: 0px;">' +
							'<div style="position: relative; height: 5px; width: ' + all_width + 'px; background: url(/img/images/bar_1.png) repeat-x;margin: 0px;">' + 
						  		'<div id="' + id + '_diapazon" style="position: absolute; left: ' + val_1 + 'px; height: 5px; width: ' + (val_2-val_1) + 'px; background: url(/img/images/bar_2.png) repeat-x; float: right;margin: 0px;"></div>' +
						  	'</div>' + 
						'</div>'+
						
						'<div style="position: absolute; left: 0px; top: 12px;margin: 0px;"><img src="/img/images/palka.gif"></div>'+
						'<div style="position: absolute; left: 100px; top: 12px;margin: 0px;"><img src="/img/images/palka.gif"></div>'+
						'<div style="position: absolute; left: 200px; top: 12px;margin: 0px;"><img src="/img/images/palka.gif"></div>'+
						
						'<div style="position: absolute; left: 0px; top:20px;margin: 0px;">' +
						  	'<div style="position: relative; height: 12px; width: ' + all_width + 'px;margin: 0px;">'+
							  	'<div OnMouseDown="mousedown(event, 1, \'' + id + '\')" id="' + id + '_cursor_1" style="position: absolute; left: ' + (val_1 - delta_cursor_1) +'px; height: 12px; width: 13px; background: url(/img/images/cursor_1.png) no-repeat; cursor: pointer;margin: 0px;"></div>'+
							  	'<div OnMouseDown="mousedown(event, 2, \'' + id + '\')" id="' + id + '_cursor_2" style="position: absolute; left: ' + val_2 + 'px; height: 12px; width: 13px; background: url(/img/images/cursor_2.png) no-repeat; cursor: pointer;margin: 0px;"></div>'+
						    '</div>'+
						'</div>'+
				  '</div>';
					
					
	var el = document.getElementById(id);	

	if(el!=null)
	{
		el.innerHTML = str;

		var body = document.getElementsByTagName('body');
		
		body['0'].onmouseup = endmove;
		body['0'].onmousemove = mousemove;
	}
	var l = kof.length;
	kof[l] = new elements();
	kof[l].id = id;
	if(log_n==null)
		kof[l].kof = end_value / all_width;
	else
		kof[l].kof = k;
	kof[l].log_n = log_n;
}
