var menuTimerID = null;
var mouseOverTimerID = null;
var infoDiv = null;
var infoIndex = 0;
var infoVisible = false;
//document.onMouseMove = trackMouse;
onload = init;
var useXML = false;
var http_request = null;
//var xmlURL = 

function init()
{
  infoDiv = createInfoBox("infobox");
  document.body.appendChild(infoDiv);
  //infoDiv.style.visibility = 'hidden';
  /*
  a = document.getElementsByTagName("area");
  alen = a.length;
  for (i = 0;i < alen; i++)
  {
    if (a[i].getAttribute("alt")) a[i].removeAttribute("alt");
  }
  */
}
var intrack = 0;
function trackMouse(e)
{
//  if(!infoVisible) return;
//alert("zzz");
  if (window.event)
  {
  infoDiv.style.left = parseInt(window.event.clientX) + parseInt(document.body.scrollLeft) + 8 + "px";
  infoDiv.style.top  = parseInt(window.event.clientY) + parseInt(document.body.scrollTop) + 8 + "px";
  }
  else
  {
  infoDiv.style.left = parseInt(e.pageX) + 8 + "px";
  infoDiv.style.top  = parseInt(e.pageY) + 8 + "px";
  }
}

function createInfoBox(divName)
{
  obj = document.createElement("div");
  obj.id = divName;
  obj.innerHTML = 'empty';
  obj.style.visibility = 'hidden';

  return obj;
}

function initInfo(divName)
{
  infoDiv = document.getElementById(divName);
}

function displayInfo()
{
  mouseOverTimerID = null;
  //alert('left=' + infoDiv.style.left + ',top' + infoDiv.style.top);
  if (!useXML)
  {
  	infoDiv.innerHTML = createInfoText();
  }
  else 
  {
  	infoDiv.innerHTML = "loading XML ...";
  	getXMLInfo();
  }
    
  if (infoDiv.style.visibility != "visible")
  {
    infoDiv.style.visibility = "visible";
  }

}

function showInfo(up,evt,idx)
{
  if (infoDiv == null) return;
  
  infoIndex = idx;
  mouseOverTimerID = window.setTimeout("displayInfo()",300);

  //trackMouse(evt);
  document.onmousemove = trackMouse;
  window.onscroll = hideInfo; 
  
}

function infoOut()
{
  if (mouseOverTimerID != null) 
  {
    window.clearTimeout(mouseOverTimerID);
    mouseOverTimerID = null;
//    infoDiv = null;
  }
  else
    menuTimerID = window.setTimeout("hideInfo()", 200 );
}

function infoOver(o)
{
  if (menuTimerID!=null)
  {
    window.clearTimeout(menuTimerID);
    menuTimerID = null;
  }
}

function hideInfo()
{
  if (mouseOverTimerID != null)
  {
    window.clearTimeout(mouseOverTimerID);
    mouseOverTimerID = null;
  } 
  if (infoDiv == null) return;
  infoDiv.style.visibility = "hidden";
  //infoDiv.style.left = "0";
  //infoDiv.style.top = "0";
  document.onmousemove = null;
  window.onscroll = null;
//  infoDiv = null;
}

function createInfoText()
{
  
  var infohtml = "" + infoIndex + "<br/>";
  
  if (info[infoIndex] == undefined)
  {
  	infohtml += "andmed puuduvad";
  }
  else
  {
	  infohtml += "<b>" + info[infoIndex][0] + "</b><br/>";
	  if (info[infoIndex][1] != '')
	    infohtml += "" + info[infoIndex][1] + "<br/>";
	  if (info[infoIndex][2] != '')
	    infohtml += "" + info[infoIndex][2] + "<br/>";
  }
  
  return infohtml;
}

function createInfoTextFromXML(xmldoc)
{
    var html = "";
    var room = xmldoc.firstChild;
    var children;
    if (room.hasChildNodes())
    {
        children = room.childNodes;
        len = children.length;
        //html += "has " + room.childNodes.length + " nodes";
        for(i = 0;i < len;i++)
        {
            if (children[i].nodeName=="name")
                html += "<b>" + children[i].firstChild.nodeValue + "</b><br/>";
            else
            if (children[i].hasChildNodes())
                html += children[i].firstChild.nodeValue + "<br/>";
        }

    }
    else
    {
        html += "" + infoIndex + "<br/>is empty";
    }
    return html;
}
 
function getXMLInfo()
{
    http_request = newXMLHttpReq();
    http_request.onreadystatechange = updateInfoTextFromXML;
    http_request.open('GET', xmlURL+infoIndex, true);
    http_request.send(null);
}

function updateInfoTextFromXML()
{
    if (http_request.readyState == 4) {
    // everything is good, the response is received        
        infoDiv.innerHTML = createInfoTextFromXML(http_request.responseXML);
    } else {
    
    }
}

function newXMLHttpReq()
{
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        http_request = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return http_request;
}
