function Location(top_marker,right_marker,bottom_marker,left_marker,top,right,bottom,left,id,top_minus,left_minus){
  this.id = id
	this.top_minus = top_minus
	this.left_minus = left_minus
  this.clip = "rect("+top_marker+"px "+right_marker+"px "+bottom_marker+"px "+left_marker+"px)";
  for(var i=left; i<=right; i++){
    this.coords.x[i] = this.coords.x[i]||[];
    this.coords.x[i].push(this)
  }
  for(var i=top; i<=bottom; i++){
    this.coords.y[i] = this.coords.y[i]||[];
    this.coords.y[i].push(this)
  }
}
Location.prototype.coords = {x:[],y:[]};
Event.observe(window, "load", function(){
  Location.prototype.hovermap = $("location-hover-map");
  Location.prototype.clipmap = $("location-clip-map");
  Location.prototype.over = null;
  Location.prototype.current = null;
  Location.prototype.popShow = function(e){
    this.popwindow = $(this.id);
    var offsetx = (window.innerWidth-25||document.body.clientWidth) - (e.clientX + this.popwindow.offsetWidth);
    var offsety = ((window.innerHeight||document.body.clientHeight)-25) - (e.clientY + this.popwindow.offsetHeight);
    if(offsetx>0) offsetx = 0;
    if(offsety>0) offsety = 0;
		this.popwindow.style.left = (e.layerX||e.x) + offsetx - this.left_minus + "px";
    this.popwindow.style.top = (e.layerY||e.y) + offsety - this.top_minus + "px";
    this.popwindow.style.visibility = "visible";
  }
  Location.prototype.popHide = function(e){
    var node = Event.element(e);
    while(node){
			if(node.className == "map-popup-close"){
				break;
			}
      if(node.className == "map-popup") return false;
      else node = node.parentNode;
    }
    this.popwindow.style.visibility = "hidden";
    return true;
  }
  Location.prototype.getLocation = function(e){
	var xcoord = this.coords.x[e.layerX||e.x]||[];
    var ycoord = this.coords.y[e.layerY||e.y]||[];
    var location = null;
    xcoord.each(function(xlocal){
      ycoord.each(function(ylocal){
        if(xlocal == ylocal) location = xlocal;
      });
    });
    return location;
  }
  Event.observe(Location.prototype.hovermap, "mousemove", function(e){
    var location = this.getLocation(e);
    if(this.over == location) return;
    this.over = location;
	  if(this.over){
			this.clipmap.style.cursor = "pointer";
			this.clipmap.style.clip = this.over.clip;
    }else{
      this.clipmap.style.cursor = "default";
			this.clipmap.style.clip = "rect(0px 0px 0px 0px)";
    }
  }.bind(Location.prototype));
  Event.observe(Location.prototype.hovermap, "click", function(e){
      if(this.over){
        this.over.popShow(e);
        this.current = this.over;
      }
  }.bind(Location.prototype));
  Event.observe(Location.prototype.clipmap, "click", function(e){
      if(this.over){
        this.over.popShow(e);
        this.current = this.over;
      }
  }.bind(Location.prototype));
  Event.observe(document, "mousedown", function(e){
    if(this.current){
      if(this.current.popHide(e))
        this.current = null;
    }
  }.bind(Location.prototype));
});