var moveByTimer = 0;
function getClientWidth()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}
flg = (document.all) ? 0 : 1;
var obj;
var polzunokX = 0;
if (flg) var psize=151;
else var psize=152;

function mousedown(ev) 
{
 if (flg) 
 { 
 	if (ev.target.id == 'Polzunok') {

		obj = document.getElementById('a').style;
		//ev.target.style;
		X=ev.clientX;
		Y=ev.y;
//		alert(X);
		moveByTimer = 0;
		return false;
 	}
  
 }
 else 
 {
 	if (event.srcElement.parentElement.id == 'a') {
		obj = event.srcElement.parentElement.style;
		X=event.offsetX;
		Y=event.offsetY;
		moveByTimer = 0;
 	}
 }
 return true;
}

function mousemove(ev) 
{
 if (obj) 
 {
 	cliWidth = getClientWidth();
  	cellSize = cliWidth/5;
  	chekPoints = new Array();
  	point = Math.round(cellSize/2 - psize/2);
  	width = cliWidth-cellSize;

  if (flg) 
  {
   //obj.moveTo((ev.pageX-X), (ev.pageY-Y));
	if (ev.clientX-psize/2<cliWidth-psize && ev.clientX-psize/2>0) {
	   obj.left = (ev.clientX-psize/2)+'px';
   		polzunokX = (ev.clientX-psize/2);
  		curPos = (ev.clientX-psize/2)-cellSize/2+psize/2;
  		
  		percent = (curPos/width)*(6400-1280);
  		document.getElementById('myBg').style.backgroundPosition = (-percent) + 'px';
	}

  }
  else 
  {
  	
  	
  	
  	for (i=0; i<5; i++) {
  		point += cellSize;
  		chekPoints[i] = point;
  	}
  	if (event.clientX-X + document.body.scrollLeft<cliWidth-psize && event.clientX-X + document.body.scrollLeft>0) {
//  		obj.pixelLeft = event.clientX-X + document.body.scrollLeft;
  		obj.left = event.clientX-X + document.body.scrollLeft;
  		polzunokX = event.clientX-X + document.body.scrollLeft;
  		curPos = obj.pixelLeft-cellSize/2+psize/2;
  		percent = (curPos/width)*(6400-1280);
  		document.getElementById('myBg').style.backgroundPosition = -percent + 'px';
//  		document.getElementById('testText1').innerHTML = curPos+' - '+width;
  	}
//   obj.pixelTop = event.clientY-Y + document.body.scrollTop;
   return false;
  }
 }
// return true;
}

function mouseup() 
{
	if (obj) {
		obj = null;
		moveByTimer = 1;
		moveStep = 5;
		window.setTimeout(mbt, 100);
	}
	return true;
}

if (flg) 
{
//  document.addEventListener(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
}
//alert(getClientWidth());
document.onmousedown = mousedown;
document.onmousemove = mousemove;
document.onmouseup = mouseup;
/*var moveStep = 5;
moveByTimer = 1;*/
function mbt() {
	myObj = document.getElementById('a').style;
  	cliWidth = getClientWidth();
  	cellSize = cliWidth/5;
  	point = Math.round(cellSize/2 - psize/2);
  	min = 1000;
  	curDif = 0;
  	goTo = 0;
  	suxx = 0;
  	for (i=0; i<5; i++) {
  		if (Math.round(Math.abs(polzunokX-point))<min) {
  			curDif = Math.abs(point-polzunokX)/(point-polzunokX);
//  			document.getElementById('testText'+i).innerHTML = curDif;
  			min = Math.round(Math.abs(polzunokX-point));
  			goTo = point;
  			suxx = i;
  			
  		} else {
//  			document.getElementById('testText'+i).innerHTML = 0;
  		}
  		point += cellSize;
  	}
  	if (curDif<0) {
  		document.getElementById('Polzunok').className = 'truckback';
  	} else {
  		document.getElementById('Polzunok').className = 'truck';
  	}

  	if (Math.abs(myObj.left.replace('px', '')-goTo)<5) {
  		moveByTimer = 0;
  		myObj.left = goTo+'px';
  		curPos = goTo-cellSize/2+psize/2;
  		width = cliWidth-cellSize;
  		percent = (curPos/width)*(6400-1280);
  		document.getElementById('myBg').style.backgroundPosition = (-percent) + 'px';
  		doLoad(document.getElementById('menuAlias'+suxx).value);
  	} else {
		nextX = curDif*moveStep+(myObj.left.replace('px',''))*1;
	  	myObj.left=nextX+'px';
  		curPos = nextX-cellSize/2+psize/2;
  		width = cliWidth-cellSize;
  		percent = (curPos/width)*(6400-1280);
  		document.getElementById('myBg').style.backgroundPosition = (-percent) + 'px';

  	}
	if (moveByTimer) {
		window.setTimeout(mbt, 10);
	}
}

function polzunokMoveTo(num) {
	
	myObj = document.getElementById('a').style;
  	cliWidth = getClientWidth();
  	cellSize = cliWidth/5;
  	point = Math.round(cellSize/2 - psize/2);
  	min = 1000;
  	curDif = 0;
  	goTo = 0;
  	for (i=0; i<5; i++) {
  		if (i==num) {
  			goTo = point;
  		}			
  		point += cellSize;
  	}
  		moveByTimer = 0;
  		myObj.left = goTo+'px';
  		curPos = goTo-cellSize/2+psize/2;
  		width = cliWidth-cellSize;
  		percent = (curPos/width)*(6400-1280);
  		document.getElementById('myBg').style.backgroundPosition = (-percent) + 'px';

}
var goToNum = 0;
function moveTruck() {
	
	myObj = document.getElementById('a').style;
  	cliWidth = getClientWidth();
  	cellSize = cliWidth/5;
  	point = Math.round(cellSize/2 - psize/2);
  	min = 1000;
  	curDif = 0;
  	suxx = 0;
  	moveStep = 5;
  	goTo = point + (cellSize*goToNum);
  	curDif = Math.abs(goTo-myObj.left.replace('px', ''))/(goTo-myObj.left.replace('px', ''));
//  	document.getElementById('Polzunok').innerHTML = '123';
  	if (curDif<0) {
  		document.getElementById('Polzunok').className = 'truckback';
  	} else {
  		document.getElementById('Polzunok').className = 'truck';
  	}
  	if (Math.abs(myObj.left.replace('px', '')-goTo)<5) {
  		moveByTimer = 0;
  		myObj.left = goTo+'px';
  		curPos = goTo-cellSize/2+psize/2;
  		width = cliWidth-cellSize;
  		percent = (curPos/width)*(6400-1280);
  		document.getElementById('myBg').style.backgroundPosition = (-percent) + 'px';
  	} else {
		nextX = curDif*moveStep+(myObj.left.replace('px',''))*1;
	  	myObj.left=nextX+'px';
  		curPos = nextX-cellSize/2+psize/2;
  		width = cliWidth-cellSize;
  		percent = (curPos/width)*(6400-1280);
  		document.getElementById('myBg').style.backgroundPosition = (-percent) + 'px';

  	}
	if (moveByTimer) {
		window.setTimeout(moveTruck, 10);
	}
}
