function getEvent(evt)
{
 return evt || window.event;
}

function findObject(evt)
{
 evt = getEvent(evt);
 return evt.target || evt.srcElement || null;
}

function findSource(evt)
{
 var source = findObject(evt);
 if (source == null) return null;
 var re = /^h(\d+)$/;
 do {
    if (source.id != null && typeof(source.id) == 'string') {
       var matches = source.id.match(re);
       if (matches != null) return { obj:source, idx:matches[1] };
    }
    source = source.parentNode;
 }
 while (source != null);
 return null;
}

function topOffset(node)
{
 var offset = 0;
 while (node) {
    offset += node.offsetTop;
    node = node.offsetParent;
 }
 return offset;
}

function leftOffset(node)
{
 var offset = 0;
 while (node) {
    offset += node.offsetLeft;
    node = node.offsetParent;
 }
 return offset;
}

function showCap(evt)
{
 var source = findSource(evt);
 if (source == null) return;
 
 var caption = document.getElementById("caption");
 var pic = source.obj;
 ++pic.style.zIndex;
 caption.innerHTML = captions[source.idx].name + "<br>" + captions[source.idx].addr;
 caption.style.display = "block";

 var container = document.getElementById("mapdiv");
 var x = pic.offsetLeft + 18;
 var offsetX = container.offsetWidth - caption.offsetWidth;
 if (x > offsetX) x = offsetX;
 caption.style.left = x + "px";
 
 var y = pic.offsetTop + 18;
 var offsetY = container.offsetHeight - caption.offsetHeight;
 if (y > offsetY) y = offsetY;
 caption.style.top = y + "px";
}

function hideCap(evt)
{
 var source = findSource(evt);
 if (source == null) return;

 var caption = document.getElementById("caption");
 var pic = source.obj;
 --pic.style.zIndex;
 caption.style.display = "none";
}

function gotoComp(evt)
{
 var source = findSource(evt);
 if (source != null)
    location.href = "company.aspx?id=" + captions[source.idx].id + "&r=" + region;
}

// Removes all children of the given node
function clearNode(node)
{
 var cur, next;
 for (cur = node.firstChild; cur != null; ) {
     next = cur.nextSibling;
     node.removeChild(cur);
     cur = next;
 }
}
