function Overlay(wsnav){
    this.wsnav = wsnav;
    this.wsnav.addEventListener(this);
    this.max = 100;
    this.url = "";
    this.imageDir = "";
    this.symbol = "circle";
    this.numbered = false;
    this.nn6=document.getElementById&&!document.all;
    this.imgOverlayOver = "images/overlay/overlay_over.gif";
    this.imgOverlayOut  = "images/circle.gif"; //"images/overlay/overlay_out.gif";
    this.xmlParams = "";
    this.isDirectorySearch = true;
}

Overlay.prototype.mapevent=function(name){
    if(name=="map_init_end"){
        //document.getElementById("divOverlay").style.width = this.wsnav.mapWidth;//resout bug firefox, agilbert
        //document.getElementById("divOverlay").style.height = this.wsnav.mapHeight;//resout bug firefox, agilbert

    }
    if(name=="map_loaded"){
        if (this.wsnav.isMapInstanceKeyValid()){
            var url = "";
            if (this.isDirectorySearch) {
                url = this.wsnav.baseHREF+"/wsnav/WebServiceCaller.do?service=CommonService&method=getOverlays&mapInstanceKey="+this.mapInstanceKey+
                                                                    "&methodology=directory"+
                                                                    "&maxResult="+this.max+
                                                                    "&radius=15.5"+
                                                                    "&contentType=text/xml"+
                                                                    "&xmin="+this.wsnav.xmin+
                                                                    "&ymin="+this.wsnav.ymin+
                                                                    "&xmax="+this.wsnav.xmax+
                                                                    "&ymax="+this.wsnav.ymax+
                                                                    "&srs="+this.wsnav.srs+
                                                                    "&unit="+this.wsnav.distanceUnit+
                                                                    "&mapWidth="+this.wsnav.mapWidth+
                                                                    "&mapHeight="+this.wsnav.mapHeight+
                                                                    "&refresh="+Math.random(); 

                var x = createXmlHttpRequest();
                x.open("POST",url,true);
                var temp = this;
                x.onreadystatechange = function(){ 
                    if(x.readyState == 4) {
                        temp.writeOverLay(x);
                    }
                }
                x.send(xmlParams);
            } else {
                url = this.wsnav.baseHREF+"/showPoi.do?mapWidth="+this.wsnav.mapWidth+"&mapHeight="+this.wsnav.mapHeight+"&refresh="+Math.random();
                //alert('Calling showPoi at: ' + url);
                var x = createXmlHttpRequest();
                x.open("POST",url,true);
                var temp = this;
                x.onreadystatechange = function(){ 
                    if(x.readyState == 4) {
                        temp.showPOIs(x);
                        //alert('Done showPoi...');
                    }
                }
                x.send(null);
            }
        }
    }
    if(name=="map_drawing"){
        //document.getElementById("divOverlay").innerHTML = "";
    }
    if(name=="map_refresh"){
        document.getElementById("divOverlay").innerHTML = "";
    }
    if(name=="map_loaded"){
        document.getElementById("divOverlay").innerHTML = "";
    }
}

Overlay.prototype.showPOIs=function(x){
    var sHtml = "";
    var offsetX = this.wsnav.getGlobalImgX();
    var offsetY = this.wsnav.getGlobalImgY();
    //document.getElementById("testq").value = x.responseText;
    if (x.responseXML == null || x.responseXML.xml == "") {
        //alert("No data...");
        return;
    }
    var monXML = x.responseXML;
//    alert(x.responseText);
    var divOverlay = document.getElementById("divOverlay");
    var nbItems = monXML.getElementsByTagName("item").length;
    if(nbItems>this.max) nbItems = this.max;
    if(!this.wsnav.isLoading) for(var i=0;i<nbItems;i++){
        var item_ = monXML.getElementsByTagName("item").item(i);

        var id = item_.getElementsByTagName("id").item(0).firstChild.nodeValue;
        //alert(id);
        var tooltip = getNodeValue("tooltip", item_); // item_.getElementsByTagName("tooltip").item(0).firstChild.nodeValue;
        var category = getNodeValue("category", item_); // item_.getElementsByTagName("category").item(0).firstChild.nodeValue;
        var imgURL = this.imgOverlayOut;
        try{
            if (id == -1) {
                imgURL = this.imageDir + item_.getElementsByTagName("imgURL").item(0).firstChild.nodeValue + ".gif";
                tooltip = youAreHere;
            }
            //imgURL = this.imageDir + this.symbol + item_.getElementsByTagName("imgURL").item(0).firstChild.nodeValue + ".gif";
        }catch(exception){}
        var x = item_.getElementsByTagName("x").item(0).firstChild.nodeValue;
        var y = item_.getElementsByTagName("y").item(0).firstChild.nodeValue;
        var url = this.url;
        try{
            //url = item_.getElementsByTagName("url").item(0).firstChild.nodeValue;
            url +=  "\"" + i + "\",\"" + this.wsnav.baseHREF + "\");"
        }catch(exception){}
        var uid = "'" + id + "'";

        sHtml+="<div id='Layer_"+id+"' style='position:absolute;z-index: 2000;' class='"+category+"'>";
        if (id == -1) {
            sHtml+="<span onmouseover=\"overlay.showInfo("+uid+");\" onmouseout=\"overlay.hideInfo("+uid+");\">";
	} else {
	    sHtml+="<a href='"+url+"' target='_self' onmouseover=\"overlay.showInfo("+uid+");highlightList("+uid+", true)\" onmouseout=\"overlay.hideInfo("+uid+");highlightList("+uid+", false)\">";		
	}	
        sHtml+="<img id='pointimg_"+id+"' src='"+imgURL+"' onload=\"initOverlay(this, "+uid+", "+x+", "+y+");\" style='visibility:hidden' border='0' class='pointimg'>";
        if (id == -1) {
            sHtml+="</span>";
        } else {
            sHtml+="</a>";
        }
        if (this.numbered && id != -1) {
            var poiNumber = i + 1;
            sHtml+="<div class='overlayTextDiv'><a class='overlayText' href='"+url+"' target='_self' onmouseover=\"overlay.showInfo("+uid+");highlightList("+uid+", true)\" onmouseout=\"overlay.hideInfo("+uid+");highlightList("+uid+", false)\">"+poiNumber+"</a></div>";
        }
        sHtml+="</div>";
        //sHtml+="<div id='pointInfo_"+id+"' style='position:absolute;visibility:hidden;'><table border='0' cellpadding='1' cellspacing='0'><tr><td class='tooltipBox'>"+tooltip+"</td></tr></table></div>";
        sHtml+="<div id='pointInfo_"+id+"' style='position:absolute;visibility:hidden;' class='tooltipBox'>"+tooltip+"</div>";
        //alert(sHtml);
    }

    divOverlay.innerHTML = sHtml;
    divOverlay.style.visibility = 'visible';
}
function getNodeValue(elName, item_) {
    if (item_.getElementsByTagName(elName) && item_.getElementsByTagName(elName).item(0) && item_.getElementsByTagName(elName).item(0).firstChild) {
        return item_.getElementsByTagName(elName).item(0).firstChild.nodeValue;
    }
    return "";
}

Overlay.prototype.writeOverLay=function(x){
    var tempString = "";
    var offsetX = this.wsnav.getGlobalImgX();
    var offsetY = this.wsnav.getGlobalImgY();
    document.getElementById("testq").value = x.responseText;

    var monXML = x.responseXML;
//    alert(x.responseText);
    var nbItems = monXML.getElementsByTagName("item").length;
    if(nbItems>this.max) nbItems = this.max;
    if(!this.wsnav.isLoading) for(var i=0;i<nbItems;i++){
        var item_ = monXML.getElementsByTagName("item").item(i);

        var id = item_.getElementsByTagName("id").item(0).firstChild.nodeValue;
        var imgURL = this.imgOverlayOut;
        try{
            imgURL = item_.getElementsByTagName("imgURL").item(0).firstChild.nodeValue;
        }catch(exception){}
        var x = item_.getElementsByTagName("x").item(0).firstChild.nodeValue;
        var y = item_.getElementsByTagName("y").item(0).firstChild.nodeValue;
        var tooltip = item_.getElementsByTagName("tooltip").item(0).firstChild.nodeValue;
        var url = this.url;
        try{
            url = item_.getElementsByTagName("url").item(0).firstChild.nodeValue;
        }catch(exception){}

        tempString+="<div id='Layer_"+i+"' style='position:absolute; '>";
        tempString+="<a href=\"javascript:void(0);overlay.clickPoint('"+url+"', '"+id+"')\" onmouseover='overlay.showInfo("+i+")' onmouseout='overlay.hideInfo("+i+")'><img id='pointimg_"+i+"' src='"+imgURL+"' onload=\"initOverlay(this, "+i+", "+x+", "+y+");\" style='visibility:hidden' border='0'></a>";
        tempString+="<div class='overlayTextDiv'><a class='overlayText' href=\"javascript:void(0);overlay.clickPoint('"+url+"', '"+id+"')\" onmouseover='overlay.showInfo("+i+")' onmouseout='overlay.hideInfo("+i+")'>"+i+"</a></div>";
        tempString+="</div>";
        tempString+="<div id='pointInfo_"+i+"' style='position:absolute;visibility:hidden;'><table border='0' cellpadding='1' cellspacing='0'><tr><td class='tooltipBox'>"+tooltip+"</td></tr></table></div>";
    }

    document.getElementById("divOverlay").innerHTML = tempString;
    document.getElementById("divOverlay").style.visibility = 'visible';
}

function initOverlay(img, id, x, y){
    imgx = x-(img.width/2);
    imgy = y-(img.height/2);
    document.getElementById("Layer_"+id).style.left=imgx +'px';
    document.getElementById("Layer_"+id).style.top=imgy +'px';  

    tooltipDiv = document.getElementById("pointInfo_"+id);
    w = tooltipDiv.offsetWidth;
    if(w>(this.wsnav.mapWidth/2-img.width))
        w = this.wsnav.mapWidth/2-img.width;
    tooltipDiv.style.width = w + "px";
    h = tooltipDiv.offsetHeight;
    tx = imgx+img.width; //+2;
    ty = imgy-h; //-2
    if((tx+w)>(this.wsnav.mapWidth-2)){
        tx = imgx-w-2;
    }
    if((ty)<2){
        ty = imgy+img.height+2;    
    }
    tooltipDiv.style.left=tx +'px';
    tooltipDiv.style.top=ty +'px';
    tooltipDiv.style.zIndex=10002;
    img.style.visibility = "visible";
    //alert('initOverlay');
}

Overlay.prototype.selectPoint=function(id){
}
Overlay.prototype.unselectPoint=function(id){
}
Overlay.prototype.clickPoint=function(url, id){
    window.open(url+id);
}



Overlay.prototype.showInfo=function(id){
    if (document.getElementById("Layer_"+id)) {
        document.getElementById("Layer_"+id).style.zIndex = 10001;
//    document.getElementById("pointimg_"+id).style.visibility = "visible";
        document.getElementById("pointInfo_"+id).style.zIndex = 10001;
        document.getElementById("pointInfo_"+id).style.visibility="visible";
    }
}

Overlay.prototype.hideInfo=function(id){
    if (document.getElementById("Layer_"+id)) {
        document.getElementById("Layer_"+id).style.zIndex = 10000;
//    document.getElementById("pointimg_"+id).style.visibility = "hidden";
        document.getElementById("pointInfo_"+id).style.zIndex = 10000;
        document.getElementById("pointInfo_"+id).style.visibility="hidden";
    }
}



