function WsNav(){
    this.mapInstanceKey = null;
    this.zoom = 0;
    this.mapBounds = null;
    this.xmin = 0;
    this.xmax = 0;
    this.ymin = 0;
    this.ymax = 0;
    this.xminInitial = 0;
    this.xmaxInitial = 0;
    this.yminInitial = 0;
    this.ymaxInitial = 0;
    this.initialized = false;
    this.workspaceKey = null;
    this.baseHREF = null;
    this.baseHREFpns = null;
    this.tools = new Object();
    this.eventListeners = new Array();
    this.distanceUnit = "";
    this.srs = "";
    this.nn6=document.getElementById&&!document.all;
    this.mapWidth = 640;
    this.mapHeight = 480;
    this.isLoading = false;
    this.imgseq = 0;
}
WsNav.prototype.getGlobalImgX=function(){
    return findPosX(this.getImg())+(this.nn6?0:2);
}
WsNav.prototype.getGlobalImgY=function(){
    return findPosY(this.getImg())+(this.nn6?0:2);
}
WsNav.prototype.getImg=function(){
    return document.getElementById("imgMap"+this.imgseq);
}
WsNav.prototype.switchImg=function(){
    if(this.imgseq==1)
        this.imgseq = 0;
    else
        this.imgseq = 1;
}
WsNav.prototype.getImgAlt=function(){
    var temp = 0;
    if(this.imgseq==0)
        temp=1;
    return document.getElementById("imgMap"+temp);
}
WsNav.prototype.modifyImage=function(x,y,w,h){
    this.getImg().width = w;
    this.getImg().height = h;
    this.getImg().style.left = x;
    this.getImg().style.top = y;
}
WsNav.prototype.init=function(){
    this.dispatch("map_init_start");

    this.getImg().width = this.mapWidth;
    this.getImg().height = this.mapHeight;

    this.getImgAlt().width = this.mapWidth;
    this.getImgAlt().height = this.mapHeight;

    document.getElementById("divDraw").style.width = this.mapWidth;//resout bug firefox, agilbert
    document.getElementById("divDraw").style.height = this.mapHeight;//resout bug firefox, agilbert
    document.getElementById("divMap").style.width = this.mapWidth;
    document.getElementById("divMap").style.height = this.mapHeight;
    this.loadingAction(true);
    if(this.mapInstanceKey==null)
        this.createMapInstanceKey();
    if(this.distanceUnit!="")
        this.setDistanceUnit(this.distanceUnit);
    if(this.srs!="")
        this.setSRS(this.srs);
    var temp = this;
    document.getElementById("divMap").onmousedown = function(e){ 
        if (!e) var e = window.event;
        if(!temp.isLoading&&((!temp.nn6&&e.button==1)||(temp.nn6&&e.which==1))){
            for(id in temp.tools){      
                if(temp.tools[id].toggle&&temp.tools[id].selected){
                    if(temp.tools[id].maponmousedown){
                        temp.tools[id].maponmousedown(e);
                    }
                }
            }
        }
        return false;
    } 
    document.getElementById("divMap").onmouseup = function(e){ 
        if (!e) var e = window.event;
        if(!temp.isLoading&&((!temp.nn6&&e.button==1)||(temp.nn6&&e.which==1))){
            for(id in temp.tools){      
                if(temp.tools[id].toggle&&temp.tools[id].selected){
                    if(temp.tools[id].maponmouseup){
                        temp.tools[id].maponmouseup(e);
                    }
                }
            }
        }
        return false;
    } 
    document.getElementById("divMap").onmousemove = function(e){
        if(!temp.isLoading){
            for(id in temp.tools){      
                if(temp.tools[id].toggle&&temp.tools[id].selected){
                    if(temp.tools[id].maponmousemove){
                        if(temp.tools[id].drawing){
                            temp.dispatch("map_drawing");
                        }
                        temp.tools[id].maponmousemove(e);
                    }
                }
            }
        }
        return false;
    }
    document.getElementById("divMap").onmouseout = function(e){ 
        if(!temp.isLoading){
            for(id in temp.tools){      
                if(temp.tools[id].toggle&&temp.tools[id].selected){
                    if(temp.tools[id].maponmouseout){
                        temp.tools[id].maponmouseout(e);
                    }
                }
            }
        }
        return false;
    }
    this.dispatch("map_init_end");
    this.initialized = true;
}


WsNav.prototype.click=function(id){
    if(this.tools[id].toggle){
        if(this.tools[id].selected){
            this.unselect(id);
        }else{
            this.select(id);
        }
    }
    if(this.tools[id].click){
        this.tools[id].click();
    }
}

WsNav.prototype.select=function(id){
    this.clearselect();
    this.tools[id].selected = true;
    if(this.tools[id].select != null) 
        this.tools[id].select();
    var img = document.getElementById("tool_"+id+"_img");
    if(img!=null){
        document.getElementById("tool_"+id+"_img").className = "tool_"+id+"_selected";
    }
}

WsNav.prototype.unselect=function(id){
    this.clearselect();
}

WsNav.prototype.clearselect=function(){
    for(z in this.tools){      
        if(this.tools[z].selected){
            if(this.tools[z].toggle){     
                if(this.tools[z].unselect != null) 
                    this.tools[z].unselect();
                this.tools[z].selected=false;
                var img = document.getElementById("tool_"+z+"_img");
                if(img!=null){
                    document.getElementById("tool_"+z+"_img").className = "tool_"+z;
                }
            }
        }
    }
}

WsNav.prototype.loadingAction=function(response){
    this.isLoading = response;
    document.getElementById("divLoading").style.left = (this.mapWidth/2)-(document.getElementById("imgLoading").width/2);
    document.getElementById("divLoading").style.top =  (this.mapHeight/2)-(document.getElementById("imgLoading").height/2);	
    if (response==true)
        document.getElementById("divLoading").style.display =  'block';
    else
        document.getElementById("divLoading").style.display =  'none';	
}
WsNav.prototype.mapLoaded=function(){
    if(this.initialized){
        this.getImg().style.left=0;
        this.getImg().style.top=0;
        this.getImg().width = this.mapWidth;
        this.getImg().height = this.mapHeight;
        this.getImg().style.visibility = 'visible';
        this.getImgAlt().style.visibility = 'hidden';

        this.updateZoom();
        this.updateBounds();
        this.loadingAction(false);
        this.dispatch("map_loaded");
    }
}

WsNav.prototype.isMapInstanceKeyValid=function(){
    var re = new RegExp("^[0-9]+$");
    return re.test(this.mapInstanceKey);
}

WsNav.prototype.createMapInstanceKey=function(){
    var url = this.baseHREF+"/wsnav/WebServiceCaller.do?pnsURL="+this.baseHREFpns+"&service=MappingSessionService&method=createMapInstanceKey&p.1.string="+this.workspaceKey+"&refresh="+Math.random(); 
    var x = createXmlHttpRequest();
    x.open("GET",url,false);
    x.send(null);
    this.mapInstanceKey =  x.responseText.replace(/^\s*|\s*$/g,"");
    if(this.isMapInstanceKeyValid() == false)
        alert("Unable to create mapInstanceKey : "+x.responseText);
}

WsNav.prototype.updateZoom=function(){
    var url = this.baseHREF+"/wsnav/WebServiceCaller.do?pnsURL="+this.baseHREFpns+"&service=MapService&method=getZoom&p.1.string="+this.mapInstanceKey+"&refresh="+Math.random(); 
    var x = createXmlHttpRequest();
    x.open("GET",url,false);
    x.send(null);
    this.zoom =  Number(x.responseText);
}


WsNav.prototype.updateBounds=function(){
    var url = this.baseHREF+"/wsnav/WebServiceCaller.do?pnsURL="+this.baseHREFpns+"&service=MapService&method=getBounds&p.1.string="+this.mapInstanceKey+"&contentType=text/xml&refresh="+Math.random(); 
    var x = createXmlHttpRequest();
    x.open("GET",url,false);
    x.send(null);
    this.bounds = x.responseText;
    this.xmin = Number(x.responseXML.getElementsByTagName("X").item(0).firstChild.nodeValue);
    this.xmax = Number(x.responseXML.getElementsByTagName("X").item(1).firstChild.nodeValue);
    this.ymin = Number(x.responseXML.getElementsByTagName("Y").item(0).firstChild.nodeValue);
    this.ymax = Number(x.responseXML.getElementsByTagName("Y").item(1).firstChild.nodeValue);
    if(this.xminInitial==0&&this.xmaxInitial==0&&this.yminInitial==0&&this.ymaxInitial==0){
        this.xminInitial = this.xmin;
        this.xmaxInitial = this.xmax;
        this.yminInitial = this.ymin;
        this.ymaxInitial = this.ymax;
    }
}

WsNav.prototype.setZoom=function(zoom){
    url = this.baseHREF+"/wsnav/WebServiceCaller.do?pnsURL="+this.baseHREFpns+"&service=MapService&method=setZoom&p.1.string="+this.mapInstanceKey+"&p.2.double="+zoom+"&refresh="+Math.random(); 
    x = createXmlHttpRequest();
    x.open("GET",url,false);
    x.send(null);
}

WsNav.prototype.setDistanceUnit=function(unit){
    url = this.baseHREF+"/wsnav/WebServiceCaller.do?pnsURL="+this.baseHREFpns+"&service=MapService&method=setDistanceUnits&p.1.string="+this.mapInstanceKey+"&p.2.string="+unit+"&refresh="+Math.random(); 
    x = createXmlHttpRequest();
    x.open("GET",url,false);
    x.send(null);
}

WsNav.prototype.setSRS=function(srs){
    url = this.baseHREF+"/wsnav/WebServiceCaller.do?pnsURL="+this.baseHREFpns+"&service=MapService&method=setNumericCoordSys&p.1.string="+this.mapInstanceKey+"&p.2.string="+srs+"&refresh="+Math.random(); 
    x = createXmlHttpRequest();
    x.open("GET",url,false);
    x.send(null);
}

WsNav.prototype.setCenter=function(x,y){
    var url = this.baseHREF+"/wsnav/WebServiceCaller.do?pnsURL="+this.baseHREFpns+"&service=MapService&method=setCenter&p.1.string="+this.mapInstanceKey+"&p.2.double="+x+"&p.3.double="+y+"&refresh="+Math.random(); 
    var x = createXmlHttpRequest();
    x.open("GET",url,false);
    x.send(null);
}

WsNav.prototype.setZoomAndCenter=function(zoom,x,y){
    var url = this.baseHREF+"/wsnav/WebServiceCaller.do?pnsURL="+this.baseHREFpns+"&service=MapService&method=setZoomAndCenter&p.1.string="+this.mapInstanceKey+"&p.2.double="+zoom+"&p.3.double="+x+"&p.4.double="+y+"&refresh="+Math.random(); 
    var x = createXmlHttpRequest();
    x.open("GET",url,false);
    x.send(null);
}

WsNav.prototype.setZoomAndCenterExt=function(zoom,x,y,srsName,unit){
    var url = this.baseHREF+"/wsnav/WebServiceCaller.do?pnsURL="+this.baseHREFpns+"&service=MapService&method=setZoomAndCenter&p.1.string="+this.mapInstanceKey+"&p.2.double="+zoom+"&p.3.double="+x+"&p.4.double="+y+"&p.5.string="+srsName+"&p.6.string="+unit+"&refresh="+Math.random(); 
    var x = createXmlHttpRequest();
    x.open("GET",url,false);
    x.send(null);
}

WsNav.prototype.addPoint=function(layername, id, x, y, rendition, srsName){
    var url = this.baseHREF+"/wsnav/WebServiceCaller.do?pnsURL="+this.baseHREFpns+"&service=AnnotationService&method=addPoint&p.1.string="+this.mapInstanceKey+"&p.2.string="+layername+"&p.3.string="+id+"&p.4.double="+x+"&p.5.double="+y+"&p.6.string="+rendition+"&p.7.string=&p.8.string=&p.9.string="+srsName+"&refresh="+Math.random(); 
    var x = createXmlHttpRequest();
    x.open("GET",url,false);
    x.send(null);
}
WsNav.prototype.removePoint=function(layername, id){
    var url = this.baseHREF+"/wsnav/WebServiceCaller.do?pnsURL="+this.baseHREFpns+"&service=AnnotationService&method=removeFeature&p.1.string="+this.mapInstanceKey+"&p.2.string="+layername+"&p.3.string="+id+"&refresh="+Math.random(); 
    var x = createXmlHttpRequest();
    x.open("GET",url,false);
    x.send(null);
}

WsNav.prototype.setBounds=function(x1,y1,x2,y2){
    url = this.baseHREF+"/wsnav/WebServiceCaller.do?pnsURL="+this.baseHREFpns+"&service=MapService&method=setBounds&p.1.string="+this.mapInstanceKey+"&p.2.double="+x1+"&p.3.double="+y1+"&p.4.double="+x2+"&p.5.double="+y2+"&refresh="+Math.random(); 
    x = createXmlHttpRequest();
    x.open("GET",url,false);
    x.send(null);
}

WsNav.prototype.stretchFromBounds=function(x1,y1,x2,y2){
    var r = (this.xmax-this.xmin)/(x2-x1);
    var w = r*this.mapWidth;
    var h = r*this.mapHeight;
    var x = (x1-this.xmin)/(this.xmax-this.xmin)*w*-1;
    var y = (this.ymax-y2)/(this.ymax-this.ymin)*h*-1;
    this.modifyImage(x,y,w,h);
}

WsNav.prototype.setInitialBounds=function(){
    this.stretchFromBounds(this.xminInitial, this.yminInitial, this.xmaxInitial, this.ymaxInitial);
    this.setBounds(this.xminInitial, this.yminInitial, this.xmaxInitial, this.ymaxInitial);
}

WsNav.prototype.refreshMap=function(){        
    if (this.mapInstanceKey!=null){
        this.dispatch("map_refresh");
        this.loadingAction(true);
        this.dispatch("mappreload");
        var url = this.baseHREF+"/wsnav/WebServiceCaller.do?pnsURL="+this.baseHREFpns+"&service=MapService&method=getImage&p.1.string="+this.mapInstanceKey+"&p.2.int="+this.mapWidth+"&p.3.int="+this.mapHeight+"&refresh="+Math.random(); 
        this.switchImg();
        this.getImg().src = url;	
    }
}

WsNav.prototype.addEventListener=function(listener){
    this.eventListeners[this.eventListeners.length]=listener;
}

WsNav.prototype.dispatch=function(name){
    for(var i=0;i<this.eventListeners.length;i++){
        if(this.eventListeners.length){
            if(this.eventListeners[i].mapevent){
                this.eventListeners[i].mapevent(name);
            }
        }
    }
}
