function suchinfo(e){
  e = e || event; 

  var tx = (e.clientX + viewx + mapx*tilewidth)/aktscale;
  var ty = (e.clientY + viewy + mapy*tileheight)/aktscale;

  var nr = locate( tx, ty );

  if( nr ){
    $("zacke").style.display="none";
    zeigelager( nr );
    if( e.clientX > winwidth / 2 )
      detailx = e.clientX - $("lagerdetails").clientWidth;
    else
      detailx = e.clientX + 5;

    if( e.clientY  > winheight / 2 )
      detaily = e.clientY - $("lagerdetails").clientHeight;
    else
      detaily = e.clientY + 5;
    $("lagerdetails").style.left = ""+detailx+"px";
    $("lagerdetails").style.top = ""+detaily+"px";
  } else {
    closedetails();
  }

  return false;
}

// Array mit sortierten koordinaten (X-Richtung)
var xs = Array();

function init_locator(){
  var i,j=0;
  for( i in x ) {
    xs[j++]=i;
  }
  qsort(0,xs.length-1);
}

// undef ist der Maximalwert.
function lt_undef( a, b ){
  if( a == undefined && b == undefined ) return false;
  if( b == undefined ) return true;
  if( a == undefined ) return false;
  return a < b; 
}

function qsort(l,r) {
  var i,j,m,w;
  i = l;
  j = r;
  m = x[xs[Math.round((l+r)/2)]];
  do {
    while (lt_undef(x[xs[i]],m)) {i++};
    while (lt_undef(m,x[xs[j]])) {j--};
    if (i<=j) {
      w = xs[i];
      xs[i] = xs[j];
      xs[j] = w;
      i++;
      j--;
    }
  } while (i<=j);
  if (l<j) {qsort(l,j)};
  if (i<r) {qsort(i,r)};
}



// gib aktuell angegeigtes Lager zurueck. Parameter sind Karten-Pixelkoordinaten.
function locate( tx, ty ){
  // suche X-Koordinate mit maximalem Index in Reichweite des Mauscursors
  var max_xs = -1;
  var i = binsearch( tx, 0, xs.length - 1 );
  if( i == undefined ){ return undefined; } 
  while( in_xreach( tx, x[xs[i-1]] ) ) i--;
  while( in_xreach( tx, x[xs[i]] ) ){
    if( in_yreach( ty, y[xs[i]] ) && parseInt(xs[i]) > parseInt(max_xs) ){
      max_xs = xs[i];
    }
    i++;
  }
  if( max_xs > -1 ) return max_xs;
  return undefined;
}

function in_xreach( tx, cx ){
  var dist = (tx - cx)*aktscale + anw/2;
  return dist >= 0 && dist <= anw;
}

function in_yreach( ty, cy ){
  var dist = (ty - cy)*aktscale + anh/2;
  return dist >= 0 && dist <= anh;
}


function binsearch( tx, l, r ){
  if( l > r ) return undefined; 
  var i = Math.round((l+r)/2);
  if( in_xreach( tx, x[xs[i]] )  )
    return i;
  else {
    if( l == r ) return undefined;
    if( tx > x[xs[i]] )
      return binsearch( tx, i+1, r );
    else
      return binsearch( tx, l, i-1 );
  }
}


