/* (C) 2008 Siva K Dirisala */
function MQPoint(id,x,y,title,r,imgurl) {
  this.id = id;
  this.x = x;
  this.y = y;
  this.title = title;
  this.r = r;
  this.imgurl = imgurl;
}

function MQ(mq,size,xaxis,yaxis,mqPoints) {
  this.mq = mq;
  this.xrange = [0,100];
  this.yrange = [0,100];
  this.imgurl = '../images/mqmarker.png';
  this.mouseover;
  var self = this;
  var depth = 1001;
  var mqelm = document.createElement("div");
  function init() {
  mqelm.style.width = size+"px";
  mqelm.style.height = size+"px";
  mqelm.style.position = 'relative';
  mqelm.style.overflow = 'hidden';
  mqelm.style.border = '1px solid #ffaaaa';
  mq.appendChild(mqelm);
  var hdiv = document.createElement('hr');
  hdiv.style.margin = '0px';
  hdiv.style.position = 'absolute';
  hdiv.style.width = '100%';
  hdiv.style.top = (size/2)+'px';
  hdiv.style.border = '1px solid #ffaaaa';
  mqelm.appendChild(hdiv);
  var vdiv = document.createElement('div');
  vdiv.style.position = 'absolute';
  vdiv.style.margin = '0px';
  vdiv.style.height = '100%';
  vdiv.style.left = (size/2)+'px';
  vdiv.style.border = '1px solid #ffaaaa';
  mqelm.appendChild(vdiv);
  }
  init();

  this.start = function() {
    for(var i=0;i<mqPoints.length;i++) {
      var p = mqPoints[i];
      var img = createImage(p);
    }
  }

  function createImage(p) {
    var img = document.createElement("IMG");
    if(p.title) {
      img.alt = p.title;
      img.title = p.title+" ("+p.x+","+p.y+")";
    }
    var image = new Image();
    image.onload = function(event) {
      img.src = image.src;
      if(p.r) {
        image.width = img.width = p.r;
	image.height = img.height = p.r;
      }
      var pdiv = document.createElement('span');
      pdiv.id = "marker"+p.id;
      pdiv.style.whiteSpace = 'nowrap';
      pdiv.style.position = 'absolute';
      pdiv.style.zIndex = 1000;
      pdiv.onmouseover = function(event) {
	pdiv.style.zIndex = depth++;
      }
      pdiv.appendChild(img);
      if(p.title) {
        var ptitle = document.createElement('span');
        ptitle.style.whiteSpace = 'nowrap';
        ptitle.innerHTML = p.title;
        pdiv.appendChild(ptitle);
      }
      var x = self.getX(p.x);
      var y = self.getY(p.y);
      pdiv.style.left = (x-image.width/2)+'px';
      pdiv.style.top = (y-image.height/2)+'px';
      mqelm.appendChild(pdiv);
    }
    if(self.mouseover && document.getElementById(p.id)) {
      img.onmouseover = function(event) { self.mouseover(event,p.id); }
    }
    image.src = p.imgurl ? p.imgurl : self.imgurl;
    return img;
  }
  this.getX = function(x) {
    return Math.round((x - self.xrange[0])*100/(self.xrange[1]-self.xrange[0])*size/100);
  }
  this.getY = function(y) {
    return size - Math.round((y - self.yrange[0])*100/(self.yrange[1]-self.yrange[0])*size/100);
  }

  this.focusOn = function(event,id) {
    var pdiv = document.getElementById("marker"+id); 
    pdiv.style.zIndex = depth++;
    if(self.mouseover)
      self.mouseover(event,id);
  }
}

