function trace(val)
{
 //obj=document.getElementById("trace");
 //obj.innerHTML=val;
 //window.status=val;
}
var pi=3.1415926535;
var automove=null;
var movezone=50;
var movex=0;
var movey=0;
var amovex=0;
var amovey=0;
var aspeed=0;
var adirection=0;
var autoanchor=0;
var movedelay=0;
var movearea=0;
var actmove=0;
var mouseactmove=0;

window.document.onmouseout=function(event) { if (event!==undefined) { if (event.clientX<=0 || event.clientX>=bodyobj.offsetWidth || event.clientY<=0 || event.clientY>=bodyobj.offsetHeight ) { movex=0; movey=0; movearea=0; } } else { movex=0; movey=0; movearea=0; } };

function gocoords(x,y,anchorid)
{
 if (automove==null)
 {
  autoanchor=anchorid;
  automove=setInterval("automovescreen(" + x + "," + y + ")",30);
 }
}
function getlentotarget(x,y,x2,y2)
{
 tx=x2-x;
 ty=y2-y;
 if (tx<0) { tx=-tx; }
 if (ty<0) { ty=-ty; }
 return Math.round(Math.sqrt(tx*tx+ty*ty),0);
}
function arcsinus(x,y)
{
 ret=0;
 if (y>0)
 {
  if (x<0)
  {
   if (x==0 && y==0)
   {
    ret=0;
   }
   else
   {
    ret=90*(y/Math.sqrt(x*x+y*y));
   }
  }
  else
  {
   if (x==0 && y==0)
   {
    ret=180;
   }
   else
   {
    ret=180-90*(y/Math.sqrt(x*x+y*y));
   }
  }
 }
 else
 {
  if (x>=0)
  {
   if (x==0 && y==0)
   {
    ret=180;
   }
   else
   {
    ret=180+90*((-y)/Math.sqrt(x*x+y*y));
   }
  }
  else
  {
   if (x==0 && y==0)
   {
    ret=360;
   }
   else
   {
    ret=360-90*((-y)/Math.sqrt(x*x+y*y));
   }
  }
 }
 return ret;
}
function automovescreen(x,y)
{
 if ( offMoveFlag == 1 ) {
  return false;
 }
 bodyobj=document.getElementById("body");
 cx=bodyobj.scrollLeft+Math.round(bodyobj.offsetWidth/2,0);
 cy=bodyobj.scrollTop+Math.round(bodyobj.offsetHeight/2,0);
 lentotarget=getlentotarget(x,y,cx,cy);
 tdirection=arcsinus(cx-x,cy-y);
 ddirection=Math.round(adirection-tdirection,0);
 while (ddirection>=360)
 {
  ddirection=ddirection-360;
 }
 while (ddirection<0)
 {
  ddirection=ddirection+360;
 }
 if (ddirection!=0)
 {
  if (ddirection>180)
  {
   adirection = adirection + ((180 - Math.abs(180 - ddirection)) / 40 + 3);
  }
  else
  {
   adirection = adirection - ((180 - Math.abs(180 - ddirection)) / 40 + 3);
  }
 }
 amovex=Math.cos(adirection/180*pi)*aspeed;
 amovey=-Math.sin(adirection/180*pi)*aspeed;
 if (isNaN(amovex)) { amovex=0; }
 if (isNaN(amovey)) { amovey=0; }
 amovex=Math.round(amovex,0);
 amovey=Math.round(amovey,0);
 ltt1=lentotarget;
 ltt2=aspeed;
 ltt3=unknownmathfunction(aspeed);
 if (lentotarget > unknownmathfunction(aspeed) + 100)
 {
  if (aspeed<100)
  {
   aspeed++;
  }
 }
 else
 {
  if (aspeed < 0)
  {
   amovex=0;
   amovey=0;
   clearInterval(automove);
   automove=null;
   if (autoanchor!="undefined")
   {
    anchorobj=document.getElementById("anchorlogo" + autoanchor);
    anchorobj.focus();
   }
  }
  else
  {
   aspeed--;
  }
 }
 if (adirection>360) { adirection=adirection-360; }
 if (adirection<0) { adirection=adirection+360; }
 if (amovex==0 && amovey==0)
 {
  return false;
 }
 maindivobj=document.getElementById("maindiv");
 if (amovex!=0)
 {
  if (bodyobj.scrollLeft+amovex<0)
  {
   bodyobj.scrollLeft=0;
  }
  else
  {
   if (bodyobj.scrollLeft+amovex>maindivobj.offsetWidth-bodyobj.offsetWidth)
   {
    bodyobj.scrollLeft=maindivobj.offsetWidth-bodyobj.offsetWidth;
   }
   else
   {
    bodyobj.scrollLeft=bodyobj.scrollLeft+amovex;
   }
  }
 }
 if (amovey!=0)
 {
  if (bodyobj.scrollTop+amovey<0)
  {
   bodyobj.scrollTop=0;
  }
  else
  {
   if (bodyobj.scrollTop+amovey>maindivobj.offsetHeight-bodyobj.offsetHeight)
   {
    bodyobj.scrollTop=maindivobj.offsetHeight-bodyobj.offsetHeight;
   }
   else
   {
    bodyobj.scrollTop=bodyobj.scrollTop+amovey;
   }
  }
 }
}

function movescreen()
{
 if ( offMoveFlag == 1 ) {
  return false;
 }
 if (movearea==0)
 {
  movedelay=0;
 }
 if ((movex==0 && movey==0) || automove!=null)
 {
  bodyobj=document.getElementById("body");
  if ( bodyobj != null ) {
	if ( bodyobj.style.cursor != '' && mouseactmove!=1) {
		bodyobj.style.cursor = '';
	}
  }
  return false;
 }
 if (movedelay<15)
 {
  movedelay++;
  return false;
 }
 bodyobj=document.getElementById("body");
 maindivobj=document.getElementById("maindiv");
 if (movex!=0)
 {
  if (bodyobj.scrollLeft+movex<0)
  {
   bodyobj.scrollLeft=0;
  }
  else
  {
   if (bodyobj.scrollLeft+movex>maindivobj.offsetWidth-bodyobj.offsetWidth)
   {
    bodyobj.scrollLeft=maindivobj.offsetWidth-bodyobj.offsetWidth;
   }
   else
   {
    bodyobj.scrollLeft=bodyobj.scrollLeft+movex;
   }
  }
 }
 if (movey!=0)
 {
  if (bodyobj.scrollTop+movey<0)
  {
   bodyobj.scrollTop=0;
  }
  else
  {
   if (bodyobj.scrollTop+movey>maindivobj.offsetHeight-bodyobj.offsetHeight)
   {
    bodyobj.scrollTop=maindivobj.offsetHeight-bodyobj.offsetHeight;
   }
   else
   {
	// Вертикаль
    bodyobj.scrollTop=bodyobj.scrollTop+movey;
   }
  }
 }
 /*******************
	Стили курсора
 *******************/
 
 // Если движемся только по вертикали
 if ( movex == 0 ) {
	if ( document.getElementById("body").style.cursor != 'n-resize' ) {
		document.getElementById("body").style.cursor = 'n-resize';
	}
 }
 // Если движемся только горизонтали
 if ( movey == 0 ) {
	if ( document.getElementById("body").style.cursor != 'e-resize' ) {
		document.getElementById("body").style.cursor = 'e-resize';
	}
 }
 // Если вправо и вверх
 if ( movex > 0 && movey < 0 ) {
	if ( document.getElementById("body").style.cursor != 'ne-resize' ) {
		document.getElementById("body").style.cursor = 'ne-resize';
	}
 }
 // Если вправо и вверх
 if ( movex > 0 && movey > 0 ) {
	if ( document.getElementById("body").style.cursor != 'nw-resize' ) {
		document.getElementById("body").style.cursor = 'nw-resize';
	}
 }
 // Если влево и вверх
 if ( movex < 0 && movey < 0 ) {
	if ( document.getElementById("body").style.cursor != 'se-resize' ) {
		document.getElementById("body").style.cursor = 'se-resize';
	}
 }
  // Если влево и вниз
 if ( movex < 0 && movey > 0 ) {
	if ( document.getElementById("body").style.cursor != 'ne-resize' ) {
		document.getElementById("body").style.cursor = 'ne-resize';
	}
 }
}




function unknownmathfunction(val)
{
 ret = 0;
 for(i=1;i<val;i++)
 {
  ret = ret + i;
 }
 return ret;
}
function bodymousemove(event)
{
 bodyobj=document.getElementById("body");
 if (mouseactmove==1)
 {
  dx=mouseactmovex-event.clientX;
  mouseactmovex=event.clientX;
  dy=mouseactmovey-event.clientY;
  mouseactmovey=event.clientY;
  bodyobj.scrollLeft=bodyobj.scrollLeft+dx;
  bodyobj.scrollTop=bodyobj.scrollTop+dy;
  mousex=0;
  mousey=0;
  return false;
 }
 actmove=0;
 if (event.clientX>0 && event.clientX<movezone)
 {
  actmove=1;
  // Скорость движения
  movex=event.clientX-movezone;
 }
 if (event.clientX>bodyobj.offsetWidth-movezone && event.clientX<bodyobj.offsetWidth)
 {
  actmove=1;
  movex=movezone+event.clientX-bodyobj.offsetWidth;
 }
 if (event.clientY>0 && event.clientY<movezone)
 {
  actmove=1;
  movey=event.clientY-movezone;
 }
 if (event.clientY>bodyobj.offsetHeight-movezone && event.clientY<bodyobj.offsetHeight)
 {
  actmove=1;
  movey=movezone+event.clientY-bodyobj.offsetHeight;
 }
 if (event.clientX>=movezone && event.clientX<=bodyobj.offsetWidth-movezone) { movex=0; }
 if (event.clientY>=movezone && event.clientY<=bodyobj.offsetHeight-movezone) { movey=0; }
 movearea=actmove;
}

function bodymouseup(event)
{
 document.getElementById("body").className = 'bodyAlt';
 hidecontext();
 mouseactmove=0;
}

function bodymousedown(event)
{
 if ( offMoveFlag == 1 ) {
  mouseactmove=0;
  return false;
 }
 if (event.button==0 || event.button==1)
 {
  document.getElementById("body").className = "bodyAltMover";
  mouseactmove=1;
  mouseactmovex=event.clientX;
  mouseactmovey=event.clientY;
 }
}

function clearmove(tst)
{
 //document.getElementById("body").style.cursor = 'auto';
 mouseactmove=0;
 if (tst!=1) { setTimeout("clearmove(1)",1); }
 return false;
}

function bodykeyup(event)
{
 if (event.keyCode>=37 && event.keyCode<=40)
 {
  actmove=0;
  movex=0;
  movey=0;
  movearea=0;
 }
}
function bodykeydown(event)
{
 if (event.keyCode==37)
 {
  if (movex>-20)
  {
   actmove=1;
   movex=movex-1;
  }
 }
 if (event.keyCode==39)
 {
  if (movex<20)
  {
   actmove=1;
   movex=movex+1;
  }
 }
 if (event.keyCode==38)
 {
  if (movey>-20)
  {
   actmove=1;
   movey=movey-1;
  }
 }
 if (event.keyCode==40)
 {
  if (movey<20)
  {
   actmove=1;
   movey=movey+1;
  }
 }
 movearea=actmove;
}

function addmarker(event)
{
/*
 obj=document.getElementById('markerdiv');
 bodyobj=document.getElementById("body");
 x=event.clientX+bodyobj.scrollLeft;
 y=event.clientY+bodyobj.scrollTop;
 obj.innerHTML=obj.innerHTML + "<div style='left:" + x + "px;top:" + y + "px;' class='markerdivcls'>left:" + x + "px;top:" + y + "px; (" + x + "," + y + ")</div>";
*/
	obj=document.getElementById('markerdiv');
	bodyobj=document.getElementById("body");
	x=bodyobj.scrollLeft + 1/2 * bodyobj.clientWidth  ;
	y=bodyobj.scrollTop + 1/2 * bodyobj.clientHeight;
	obj.innerHTML=obj.innerHTML + "<div style='left:" + x + "px;top:" + y + "px;' class='markerdivcls'>left:" + x + "px;top:" + y + "px; (" + x + "," + y + ")</div>";

}