if( typeof gAPTVMAP == 'undefined' ){
  var gAPTVMAP = 'defined';

  var sizeCenterZoom2BBox = function(width, height, center, zoom) {
    var supp = com.ptvag.webcomponent.map.CoordUtil.getSmartUnitsPerPixel(zoom);
    var bb = {left:center.x - supp*width/2, top:center.y + supp*height/2,
    right:center.x + supp*width/2, bottom:center.y - supp*height/2};
    return bb;
  }

  var sizeCenterZoomInSUPP2BBox = function(width, height, center, supp) {
    var bb = {left:center.x - supp*width/2, top:center.y + supp*height/2,
    right:center.x + supp*width/2, bottom:center.y - supp*height/2};
    return bb;
  }
  var APTVMap = Class.create();
  APTVMap.prototype = Object.extend(new AAjaxMap(), {
    localurl : gJSMapImagePath+"APTVMap/",
    getURLParams: function(){
      var par=
        "&REQMapWidth="+this.mapDiv.clientWidth+
        "&REQMapHeight="+this.mapDiv.clientHeight+
        "&REQMapCenterX="+this.getCenter().getLon()+
        "&REQMapCenterY="+this.getCenter().getLat()+
        "&REQMapScaling="+this.getZoom();
      var r=0;
      for(var c=this.contcount;c>0;c--){
        var p=this.content.get(c.toString());
        if(p.show){
          if(p.type=='polyline' || p.type=='route'){
            par+="&REQMapRoute"+r+".DisplayMode=POLYGON";
            par+="&REQMapRoute"+r+".Color="+this.getColorFFFFFF(p.color);
            par+="&REQMapRoute"+r+".LineWidth="+p.width;
            for(var l=0;l<p.coords.length;l++){
              par+="&REQMapRoute"+r+".Location"+l+".X="+p.coords[l].getLon();
              par+="&REQMapRoute"+r+".Location"+l+".Y="+p.coords[l].getLat();
              if(l==0||l==p.coords.length-1)
                par+="&REQMapRoute"+r+".Location"+l+".Marker=TRANSPARENT";
            }
            r++;
          }
          else if(p.type=='location'){
            par+="&REQMapRoute"+r+".DisplayMode=LOCATIONS";
            par+="&REQMapRoute"+r+".Location0.X="+p.coord.getLon();
            par+="&REQMapRoute"+r+".Location0.Y="+p.coord.getLat();
//            par+="&REQMapRoute"+r+".Location0.Name="+p.text;
            var imageptv=p.imageurl.substring(p.imageurl.lastIndexOf("/")+1,p.imageurl.lastIndexOf(".")).toUpperCase();
            par+="&REQMapRoute"+r+".Location0.Marker="+imageptv;
            r++;
          }
        }
      }
      return par;
    },
    additionalMapInitialisations:function(){
    },
    decodeUniCode:function(u){
      var d=this.addElem("div","dummy",{display:"none"},this.mapDiv);
      d.innerHTML=u;
      var a=d.innerHTML;
      this.mapDiv.removeChild(d);
      return a;
    },
    updateToolbarButton: function(but,imgurl,next,width,height,tooltip,clickHandler){
      var b=this.toolbar.removeElement(but);
      b.imgUrl=imgurl;
      b.width=width;
      b.height=height;
      if(tooltip!=null) b.tooltip=this.decodeUniCode(tooltip);
      if(clickHandler!=null) b.clickHandler=clickHandler;
      this.toolbar.addElement(b,next);
    },
    newToolbarButton: function(imgurl,next,width,height,tooltip,clickHandler,enabled,id){
      this.toolbar.addElement({width:2,id:"spacing_after_"+id},next);
      var b={imgUrl:imgurl,width:width,height:height,tooltip:this.decodeUniCode(tooltip),enabled:enabled,id:id,clickHandler:clickHandler};
      this.toolbar.addElement(b,"spacing_after_"+id);
    },
    removeToolbarElements: function(e){
      for(var i=0;i<e.length;i++)this.toolbar.removeElement(e[i]);
    },
    show: function() {

      var w=(typeof this.mapParams.width!='undefined'?this.mapParams.width:this.mapDiv.clientWidth);
      var h=(typeof this.mapParams.height!='undefined'?this.mapParams.height:this.mapDiv.clientHeight);
      var bbox = sizeCenterZoom2BBox( w,h,
                                      this.CCoord2SmartUnit(this.mapParams.coord),
                                      this.AjaxMap2PTVZoom( this.mapParams.zoom ) );

      this.map = new com.ptvag.webcomponent.map.Map( this.mapDiv, bbox );
      this.toolbar=this.map.getLayer("toolbar");
      this.toolbar.switchToMoveMode();
      this.toolbar.setAreaOpacity(0.01);
      this.toolbar.setBlendingOpacityOut(0.01);
      this.toolbar.setBlendingOpacityOver(0.3);
      this.toolbar.setButtonOpacityDisabled(0.4);
      this.toolbar.setButtonOpacityOut(0.7);
      this.toolbar.setButtonOpacityOver(1.0);
      this.updateToolbarButton("zoom-out",this.localurl+"zoomout.png","spacing-between-zoom",20,20,this.mapParams.tZoomOut,null);
      this.updateToolbarButton("zoom-in",this.localurl+"zoomin.png","spacing-after-zoom",20,20,this.mapParams.tZoomIn,null);

      this.map.setAnimate(true);
      if(typeof this.mapParams.mode!='undefined'&&this.mapParams.mode=="simple"){
        this.removeToolbarElements(["hybrid-view","spacing-between-view-1","aerial-view","spacing-between-view-2","map-view","spacing-after-view"]);
        this.removeToolbarElements(["spacing-after-zoom","zoom-mode","spacing-between-mode","move-mode","spacing-after-mode",
                                    "measurement","spacing-after-measurement","spacing-after-address-lookup","overview","spacing-after-overview",
                                    "reset","spacing-after-reset","history-back","spacing-between-history","history-forward"]);
        this.map.removeLayer("zoomslider");
        this.map.removeLayer("scale");
        this.map.removeLayer("compass");
      }else{
        this.map.setProfileGroup("terrain");
        this.map.getLayer("position").setEnabled(true);
        this.updateToolbarButton("zoom-mode",this.localurl+"lupe.png","spacing-between-mode",20,20,this.mapParams.tZoomMode,function(){
          this.toolbar.switchToZoomMode();
          this.updateToolbarButton("move-mode",this.localurl+"hand.png","spacing-after-mode",20,20,null,null);
          this.updateToolbarButton("zoom-mode",this.localurl+"lupe1.png","spacing-between-mode",20,20,null,null);
          return false;
        }.bind(this));
        this.updateToolbarButton("move-mode",this.localurl+"hand1.png","spacing-after-mode",20,20,this.mapParams.tMoveMode,function(){
          this.toolbar.switchToMoveMode();
          this.updateToolbarButton("move-mode",this.localurl+"hand1.png","spacing-after-mode",20,20,null,null);
          this.updateToolbarButton("zoom-mode",this.localurl+"lupe.png","spacing-between-mode",20,20,null,null);
          return false;
        }.bind(this));
        this.updateToolbarButton("measurement",this.localurl+"measure.png","spacing-after-address-lookup",20,20,null,function(){
          this.toolbar.toggleMeasurement();
          if(typeof this.map.getLayer("measurement")!='undefined'&&this.map.getLayer("measurement").isEnabled())
            this.updateToolbarButton("measurement",this.localurl+"measure1.png","spacing-after-address-lookup",20,20,null,null);
          else
            this.updateToolbarButton("measurement",this.localurl+"measure.png","spacing-after-address-lookup",20,20,null,null);
          return false;
        }.bind(this));
        //this.updateToolbarButton("overview",this.localurl+"minimap.png","spacing-after-overview",20,20,this.mapParams.tOverview,function(){
        //  this.toolbar.toggleOverview();
        //  if(typeof this.map.getLayer("overview")!='undefined'&&this.map.getLayer("overview").isEnabled())
        //    this.updateToolbarButton("overview",this.localurl+"nominimap.png","spacing-after-overview",20,20,null,null);
        //  else
        //    this.updateToolbarButton("overview",this.localurl+"minimap.png","spacing-after-overview",20,20,null,null);
        //  return false;
        //}.bind(this));
        this.updateToolbarButton("reset",this.localurl+"reset.png","spacing-after-reset",20,20,this.mapParams.tReset,null);
        this.updateToolbarButton("history-back",this.localurl+"left.png","spacing-between-history",20,20,this.mapParams.tHistoryBack,null);
        this.updateToolbarButton("history-forward",this.localurl+"right.png",null,20,20,this.mapParams.tHistoryForward,null);
        this.map.getLayer("overview").setAreaWidth(0.25);
        this.map.getLayer("overview").setAreaHeight(0.25);
        this.map.getLayer("overview").setAreaRight(1);
        this.map.getLayer("overview").setAreaBottom(1);
        this.map.getLayer("overview").setAreaLeft(null);
        this.map.getLayer("overview").setLayerOpacity(0.95);
        this.map.getLayer("overview").setAreaBorderWidth(1);
        this.map.getLayer("zoomslider").setAreaRight(null);
        this.map.getLayer("zoomslider").setAreaLeft(7);
        this.map.getLayer("scale").setAreaRight(null);
        this.map.getLayer("scale").setAreaLeft(100);
        this.map.getLayer("scale").setAreaBottom(1);
        this.map.removeLayer("compass");
        this.map.removeLayer("position");

        // suppress satellite view mode
        this.removeToolbarElements(["zoom-mode","move-mode","measurement","reset","history-forward","history-back","zoom-in","zoom-out","hybrid-view","spacing-between-view-1","aerial-view","spacing-between-view-2","map-view","spacing-after-view","overview","spacing-after-overview"]);
      }
      if(typeof this.mapParams.language=='undefined')this.mapParams.language="d";
//      this.map.setAllowMouseWheelZoom(false);
      //this.map.getLayer('debug').setEnabled(true);

      if(navigator.userAgent.toLowerCase().indexOf('msie')!=-1 && navigator.userAgent.indexOf('6.0')!=-1) {
        this.InfoBoxFactory = com.ptvag.webcomponent.map.vector.InfoBoxElementFactoryDefault.getInstance();
      }else{
        this.InfoBoxFactory = com.ptvag.webcomponent.map.vector.InfoBoxElementFactoryRound.getInstance();
      }
           if((this.mapParams.printmode != 'undefined') && (this.mapParams.printmode == true)) {
        this.InfoBoxFactory = com.ptvag.webcomponent.map.vector.InfoBoxElementFactoryDefault.getInstance();
      }

//      this.InfoBoxFactory = com.ptvag.webcomponent.map.vector.InfoBoxElementFactoryDefault.getInstance();
      this.InfoBoxFactory.setAllowWrap(true);
      com.ptvag.webcomponent.map.vector.InfoBoxElementFactory =
        this.InfoBoxFactory;
      this.addTileLayers(this.mapParams.layers);
      this.additionalMapInitialisations();
      if(typeof this.mapParams.onshow!='undefined')
        this.mapParams.onshow();
      this.map.addEventListener("changeZoom", this.cbChangeZoom.bind(this));
      this.map.addEventListener("changeCenter", this.cbChangeCenter.bind(this));
      this.vectorlayer=this.map.getLayer('vector');
      this.leftclickarea=new com.ptvag.webcomponent.map.vector.ClickArea(null,null, null, 0,
        this.cbLeftClick.bind(this));
      this.rightclickarea=new com.ptvag.webcomponent.map.vector.RightClickArea(null,null, null, 0,
        this.cbRightClick.bind(this));
      this.vectorlayer.addElement(this.leftclickarea);
      this.vectorlayer.addElement(this.rightclickarea);
      this.map.getLayer('overview').setApplyZoomRectToMainMap(false);
      this.map.hideZoomBox();
    },
    addTileLayers:function(layerdescriptions){
      if(typeof layerdescriptions!='undefined' && layerdescriptions!=null){
        this.toolbar.addElement({width:6,id:"spacing_after_ptv"},null);
        for(var l=0;l<layerdescriptions.length;l++){
          if(typeof layerdescriptions[l].viewtyp=='undefined') layerdescriptions[l].viewtyp='static';
          if(layerdescriptions[l].viewtyp=='static')
            this.addTileLayer(layerdescriptions[l],l);
          else
            this.addTileLayerButton(layerdescriptions[l],l);
        }
      }
    },
    addTileLayerButton:function(desc,l){
      if(typeof desc.imgfile=='undefined')desc.imgfile="layer.png";
      if(typeof desc.img1file=='undefined')desc.img1file="layer1.png";
      if(typeof desc.imgwidth=='undefined')desc.imgwidth=20;
      if(typeof desc.imgheight=='undefined')desc.imgheight=20;
      this.newToolbarButton(this.localurl+desc.imgfile,null,desc.imgwidth,desc.imgheight,desc.name,function(){
        if(typeof this.map.getLayer(desc.id)=='undefined'){
          this.updateToolbarButton(desc.id,this.localurl+desc.img1file,"spacing_after_"+desc.id,desc.imgwidth,desc.imgheight,null,null);
          this.addTileLayer(desc,l);
        }
        else{
          this.updateToolbarButton(desc.id,this.localurl+desc.imgfile,"spacing_after_"+desc.id,desc.imgwidth,desc.imgheight,null,null);
          this.removeTileLayer(desc.id);
        }
        return false;
      }.bind(this),true,desc.id);
    },
    removeTileLayer:function(name){
      if(typeof this.map.getLayer(name)=='undefined' || this.map.getLayer(name)==null) return;
      this.map.removeLayer(name);
    },
    addTileLayer:function(desc,l){
      if(typeof desc.url=='undefined') return;
      if(typeof desc.urlsubpath=='undefined')desc.urlsubpath='';
      if(typeof desc.tiletyp=='undefined') desc.tiletyp='static';
      if(typeof desc.opacity=='undefined') desc.opacity=1.0;
      if(typeof desc.id=='undefined') desc.id='MyTileLayer'+l.toString();
      if(typeof desc.name=='undefined') desc.name=desc.id;
      var MyRequestBuilder=new com.ptvag.webcomponent.map.RequestBuilder(this.map,true);
      MyRequestBuilder.defaultRB=new com.ptvag.webcomponent.map.RequestBuilder(this.map,true);
      MyRequestBuilder.buildRequest = function(left,top,right,bottom,width,height,loginfo,mapVersion){
        var r=this.defaultRB.buildRequest(left,top,right,bottom,width,height,loginfo,mapVersion);
        var diff = Math.abs(left - right);
        var z = 33 - Math.round(Math.log(diff) / Math.log(Math.pow(4, (1 / 3))));
        var x = Math.round((20015087 + Math.round(left)) / diff);
        var y = Math.round((20015085 + Math.round(bottom)) / diff);
        if(desc.tiletyp=='static')
          r.url=desc.url+gStaticTileDirPTV+desc.urlsubpath+z+"/"+x+"/"+y+".png";
        else
          r.url=desc.url+gDynamicTileParameterPTV+"z="+z+"&x="+x+"&y="+y;
        return r;
        };
      var MyTiles=new com.ptvag.webcomponent.map.layer.TileMapLayer(MyRequestBuilder);
      MyTiles.setLayerOpacity(desc.opacity);
      if(typeof MyTiles.setRemoveUnusedElements!='undefined') {
        MyTiles.setRemoveUnusedElements(true);
      }
      MyTiles.setIsRelative(true);
      MyTiles.setNeedsOpacityHack(true);
      this.map.addLayer(MyTiles,desc.id,0,this.map.getLayer("label"));
    },
    addContentLayer:function(name){
      if(typeof this.map.getLayer(name)!='undefined' && this.map.getLayer(name)!=null) return;
      l=new com.ptvag.webcomponent.map.layer.VectorLayer(this.map.getLayer("floater"));
      this.map.addLayer(l, name, 0, this.map.getLayer("overview") );
    },
    removeLayer:function(name){
      if(typeof this.map.getLayer(name)=='undefined' || this.map.getLayer(name)==null) return;
      this.map.getLayer(name).removeAllElements ();
      this.map.removeLayer(name);
    },
    geo2scr:function(g){
      var su = this.CCoord2SmartUnit(g);
      var zoom = this.map.getZoom();
      var pix = com.ptvag.webcomponent.map.CoordUtil.smartUnit2Pixel( su, zoom );
      var ctrpix = this.map.getCenterInPixel();
      pix.x = (this.map.getWidth()/2) + pix.x - ctrpix.x;
      pix.y = (this.map.getHeight()/2) + ctrpix.y - pix.y;
      return pix;
    },
    setOnChange: function(listener){
      if(typeof this.mapParams.onchange=='undefined')
        this.mapParams.onchange=new Array;
      this.mapParams.onchange[this.mapParams.onchange.length]=listener;
    },
    setOnZoom: function(listener){
      if(typeof this.mapParams.onzoom=='undefined')
        this.mapParams.onzoom=new Array;
      this.mapParams.onzoom[this.mapParams.onzoom.length]=listener;
    },
    cbonViewChange: function(){
      this.activeTimeoutChange=null;
      if(typeof this.mapParams.onchange!='undefined'){
        for(var i=0;i<this.mapParams.onchange.length;i++){
          this.mapParams.onchange[i]();
        }
      }
    },
    cbonZoom: function(){
      this.activeTimeoutZoom=null;
      if(typeof this.mapParams.onzoom!='undefined'){
        for(var i=0;i<this.mapParams.onzoom.length;i++){
          this.mapParams.onzoom[i]();
        }
      }
    },
    cbChangeZoom: function(){
      window.clearTimeout(this.activeTimeoutChange);
      this.activeTimeoutChange=window.setTimeout("g"+this.classtype+"Instances["+this.instance+"].cbonViewChange()",500);
      window.clearTimeout(this.activeTimeoutZoom);
      this.activeTimeoutZoom=window.setTimeout("g"+this.classtype+"Instances["+this.instance+"].cbonZoom()",200);
    },
    cbChangeCenter: function(){
      window.clearTimeout(this.activeTimeoutChange);
      this.activeTimeoutChange=window.setTimeout("g"+this.classtype+"Instances["+this.instance+"].cbonViewChange()",500);
    },
    getBoundingBox: function(){
      var bb = this.map.getRect();
      return {
        sw:this.SmartUnit2CCoord({x:bb.left,y:bb.bottom}),
        ne:this.SmartUnit2CCoord({x:bb.right,y:bb.top})
      }
    },
    setBoundingBox: function(bb){
      var newbb = {
        sw: this.CCoord2SmartUnit(bb.sw),
        ne: this.CCoord2SmartUnit(bb.ne)
      };
      this.map.setRect(newbb.sw.x,newbb.ne.y,newbb.ne.x,newbb.sw.y);
    },
    showInfoboxGeo:function(geo,titletext,content){
      this.showInfoboxSU(this.CCoord2SmartUnit(geo),titletext,content);
    },
    showInfoboxSU:function(su,titletext,content, titelid,p){
      //this.hideAllInfoBoxes();
      if(typeof titelid=='undefined')titelid='PTVInfoBoxTitle';
      if(typeof p == 'undefined') {
         var title    = this.wrapTitle(titletext);
      }else{
        var title = "<div class='ptvinfoboxtitle_small'>"+titletext+"</div>"
      }
      if(typeof p == 'undefined') {
      var infoBoxClass = 'ptvinfoboxtitle';
      var titlebut = "<img id='PTVInfoboxClosebuttonImage_"+this.instance+"' src='" +
                     this.localurl+"closebutton.png' width='14' height='14' alt='' " +
                     "onclick='g"+this.classtype+"Instances["+this.instance+"].hideAllInfoBoxes();return false'>";
      }else{
        var titlebut = '';
        var infoBoxClass = 'ptvinfoboxtitle_small';
      }
      if(content != '') {
        var extcontent = "<div class='ptvinfoboxcontent'>"+content+"</div>";
      }else{
        var extcontent = '';
      }
      var allcontent = "<table class='ptvinfobox' cellspacing='0' width='100%'><tr>"+
                       "<td id='"+titelid+"' class='"+infoBoxClass+"'>"+title+"</td>"+
                       "<td class='"+infoBoxClass+"' style='width:16px;'>"+titlebut+"</td></tr>"+
                       "<tr><td colspan='2'>"+extcontent+"</td></tr></table>";
      if((p == null) || (typeof p == 'undefined')) {
        this.currentInfoBox=new com.ptvag.webcomponent.map.vector.InfoBox(
                          su.x, su.y,allcontent, 34, 1, "Infobox", false);
        this.vectorlayer.addElement(this.currentInfoBox);
      }else{
        p.currentBox = new com.ptvag.webcomponent.map.vector.InfoBox(
                          su.x, su.y,allcontent, 34, 1, "Infobox_"+titelid, true);
        this.vectorlayer.addElement(p.currentBox);
      }

      this.map.getLayer("floater").setSwallowHoverEvents(true);
    },
    wrapTitle: function(titletext){
      return "<div class='ptvinfoboxtitle'>"+titletext+"</div>";//.replace(/\ /g,'&nbsp;')+"</div>";
    },
    replaceCurrentInfoboxTitle: function(TitelContainerId,newTitle){
      var c=document.getElementById(TitelContainerId);
      if(typeof c!='undefined' && c!= null){
        c.innerHTML = this.wrapTitle(newTitle);
// Leider wird die Infobox nicht neu platziert. 1. gescheiterter Versuch :
//        alert("x:"+this.currentInfoBox.getX()+",y:"+this.currentInfoBox.getY());
//        this.InfoBoxFactory.positionInfoBoxElement(this.currentInfoBox,this.currentInfoBox.getX(),this.currentInfoBox.getY());
//        this.InfoBoxFactory.positionInfoBoxElement(this.vectorlayer.getElement("Infobox"),this.currentInfoBox.getX(),this.currentInfoBox.getY());
      }
    },
    cbLeftClick: function(p){
      var geo=this.SmartUnit2CCoord({x:p.clickX,y:p.clickY});
      if(typeof this.mapParams.leftinfoclicktitle!='undefined'){
        var titelid="PTVInfoboxTitelContainer_"+geo.getLon()+"_"+geo.getLat();
        this.showInfoboxSU({x:p.clickX,y:p.clickY},
                            this.mapParams.leftinfoclicktitle,
                            this.mapParams.leftinfoclickcontent({x:geo.getLon(),y:geo.getLat(),titleid:titelid}),
                            titelid );
      }
    },
    cbRightClick: function(p){
      var geo=this.SmartUnit2CCoord({x:p.clickX,y:p.clickY});
      if(typeof this.mapParams.rightinfoclicktitle!='undefined'){
        var titelid="PTVInfoboxTitelContainer_"+geo.getLon()+"_"+geo.getLat();
        this.showInfoboxSU({x:p.clickX,y:p.clickY},
                            this.mapParams.rightinfoclicktitle,
                            this.mapParams.rightinfoclickcontent({x:geo.getLon(),y:geo.getLat(),titleid:titelid}),
                            titelid );
      }
    },
    getBoundingBox: function(){
      var bb = this.map.getRect();
      return {
        sw:this.SmartUnit2CCoord({x:bb.left,y:bb.bottom}),
        ne:this.SmartUnit2CCoord({x:bb.right,y:bb.top})
      }
    },
    isCoordVisible:function(c){
      var bb = this.map.getRect();
      var s = this.CCoord2SmartUnit(c);
      return s.x>=bb.left&&s.x<=bb.right&&s.y>=bb.bottom&&s.y<=bb.top;
    },
    getCenter: function(){
      return this.SmartUnit2CCoord(this.map.getCenter());
    },
    centerToGeo:function(g){
      this.map.setCenter(this.CCoord2SmartUnit(g));
    },
    setZoom:function(zoom){
      this.map.setZoom(this.AjaxMap2PTVZoom( zoom ));
    },
    createContent:function(params){
      if(typeof params.type=='undefined')return null;
      this.contcount++;
      var cc=this.contcount.toString();
      var inst=this.instance.toString();
      var p=this.clone_contentparams(params)
      p.cc=cc;
      p.inst=inst;
      p.show = false;
      this.content.set(cc,p);
      if(typeof p.layername=='undefined')
        p.layername='vector';
      p.vectorlayer = this.map.getLayer(p.layername);
      if(typeof p.vectorlayer == 'undefined' || p.vectorlayer==null)
        p.vectorlayer = this.map.getLayer('vector');
      switch(p.type){
      case 'location':this.createLocation(p);break;
      case 'route':
      case 'point':this.createPoint(p);break;
      case 'polyline':this.createPolyline(p);break;
      case 'polygon':this.createPolygon(p);break;
      case 'circle':this.createCircle(p);break;
      case 'container':this.createContainer(p);break;
      default:return null;
      }
      return cc;
    },
    showContent:function(content){
      if(typeof content=='undefined')return;
      if(content==null)return;
      var p=this.content.get(content);
      if(typeof p=='undefined')return;
      switch(p.type){
      case 'location':this.showLocation(p);break;
      case 'route':
      case 'point':this.showPoint(p);break;
      case 'polyline':this.showPolyline(p);break;
      case 'polygon':this.showPolygon(p);break;
      case 'circle':this.showCircle(p);break;
      case 'container':this.showContainer(p);break;
      }
      p.show = true;
    },
    hideContent:function(content){
      if(typeof content=='undefined')return;
      if(content==null)return;
      var p=this.content.get(content);
      if(typeof p=='undefined')return;
      p.show = false;
      switch(p.type){
      case 'location':this.hideLocation(p);break;
      case 'point':
      case 'circle':
        this.hideContent(p._poly);
        break;
      case 'route':
      case 'polyline':
      case 'polygon':
        if(typeof this.vectorlayer.removeElement!='undefined')
          p.vectorlayer.removeElement(p.cc);
        else
          p.vectorlayer.hideElement(p.cc);
        delete p.content;
        break;
      }
    },
    removeContent:function(content){
      if(typeof content=='undefined')return;
      if(content==null)return;
      var p=this.content.get(content);
      if(typeof p=='undefined')return;
      switch(p.type){
      case 'location':
      case 'route':
      case 'polyline':
      case 'polygon':
      case 'container':
        break;
      case 'circle':
      case 'point':
        this.removeContent(p._poly);
        break;
      }
      this.content.unset(content);
      delete this.content[content];
    },
    changeContent:function(content,parnew){
       if(typeof content=='undefined')return;
      if(content==null)return;
      var p=this.content.get(content);
      if(typeof p=='undefined')return;
      switch(p.type) {
      case 'location':this.changeIcon(p.cc,parnew);
                      break;
      case 'polyline':this.changePolyline(p,parnew);
                      break;
      }
    },
    centerToContent: function(content){
      if(typeof content=='undefined')return;
      if(content==null)return;
      var par=this.content.get(content);
      switch(par.type){
      case "circle":
        this.map.setCenter(this.CCoord2SmartUnit(par.coord));
        this.map.setZoom(this.AjaxMap2PTVZoom(this.mincenterzoom));
      case "location":
        this.map.setCenter(this.CCoord2SmartUnit(par.coord));
        this.map.setZoom(this.AjaxMap2PTVZoom(this.mincenterzoom));
        break;
      case "polyline":
      case "polygon":
      case "route":
        if(typeof par.su != 'undefined')
          this.map.setViewToPoints(par.su, false, false);
        break;
      }
    },

    centerToGeoCoords: function(geoArray, smooth)
    {
      if((typeof(geoArray) == "undefined") || geoArray.length == 0)
        return;
      var smartUnitArray = new Array();
      for (var i = 0; i < geoArray.length; i++)
        {
        smartUnitArray[i] = this.CCoord2SmartUnit(geoArray[i]);
        }
     this.map.setViewToPoints(smartUnitArray, false, false);
    },
    openContentInfobox: function(content){
      if(typeof content=='undefined')return;
      if(content==null)return;
      var par=this.content.get(content);
      if(typeof par.onshowinfobox!='undefined')
        this.showInfoboxSU(par.su,par.infotitle,par.infocontent);
    },

    // Hilfsmethoden
    createIconHTML:function(par){
      par.shad = false;
      if( typeof par.shadowheight!='undefined' &&
          typeof par.shadowwidth!='undefined' &&
          typeof par.shadowurl!='undefined')
        par.shad = true;
      var w = par.shad?Math.max( par.shadowwidth,par.imagewidth ):par.imagewidth;
      var h = par.shad?Math.max( par.shadowheight,par.imageheight ):par.imageheight;
      par.html = "<div id='ptvlocation" + par.inst + '-' + par.cc + "maindiv' style='position:absolute;" +
          "width:" + w + "px;" +
          "height:" + h + "px;" +
          "left:" + (-par.hotspot.x).toString() + "px;" +
          "top:" + (-par.hotspot.y).toString() + "px;" +
          "'>";
      if(par.shad)
        par.html+= "<img id='ptvlocation" + par.inst + '-' + par.cc + "shadowimg' style='position:absolute;left:"+ ((-par.hotspot.x)+7).toString() +"px;top:"+ ((-par.hotspot.y)+22).toString() +"px;" +
          "' " +
          "src='" + par.shadowurl + "' " +
          "width='" + par.shadowwidth + "' " +
          "height='" + par.shadowheight + "' alt=''>";
      par.html+="<img id='ptvlocation" + par.inst + '-' + par.cc + "iconimg' style='position:absolute;" +
          "' " +
          "src='" + par.imageurl + "' " +
          "width='" + par.imagewidth + "' " +
          "height='" + par.imageheight + "' alt=''>" +
          "</div>";
//      this.setOnZoom(this.resizeLocation.bind(this,par));
    },
    resizeLocation: function(par){
      var maindiv=$('ptvlocation'+par.inst+'-'+par.cc+'maindiv');
      if(typeof maindiv=='undefined'||maindiv)return;
      var w = shad?Math.max( par.shadowwidth,par.imagewidth ):par.imagewidth;
      var h = shad?Math.max( par.shadowheight,par.imageheight ):par.imageheight;
      maindiv.style.width="10px";
      maindiv.style.height="10px";
      var iconimg=$('ptvlocation'+par.inst+'-'+par.cc+'iconimg');
      if(typeof iconimg=='undefined'||iconimg)return;
      iconimg.width="10";
      iconimg.height="10";
      var shadowimg=$('ptvlocation'+par.inst+'-'+par.cc+'shadowimg');
      if(typeof shadowimg!='undefined'&&shadowimg){
        shadowimg.width="10";
        shadowimg.height="10";
      }
    },
    createLocation: function( par ){
      this.createIconHTML(par);
      if(typeof par.flexible=='undefined')par.flexible=false;
      par.su=this.CCoord2SmartUnit(par.coord);
      par.su.x+=0.0001*parseInt(par.cc)%100-0.005;
    },
    changeTooltip: function(content,param){
      var par=this.content.get(content);
      par.text=param.text;
      if(typeof par.tooltip!='undefined'){
        var t=this.vectorlayer.getElement(par.cc+"_tooltip");
        t.setText(par.text.replace(/\ /g,'&nbsp;'));
      }
    },
    changeInfo: function(content,param){
      var par=this.content.get(content);
      par.infotitle=param.infotitle;
      par.infocontent=param.infocontent;
      par.onshowinfobox=function(){
        this.showInfoboxSU(par.su,par.infotitle,par.infocontent);
      };
      var t=this.vectorlayer.getElement(par.cc+"_clickarea");
      t.setHandler(par.onshowinfobox.bind(this));
    },
    changeIcon: function(content,param){
      if(typeof com.ptvag.webcomponent.map.vector.HTML!='undefined'){
        var par=this.content.get(content);
        this.vectorlayer.removeElement(par.cc);
        par.shadowheight= param.shadowheight;
        par.shadowwidth = param.shadowwidth;
        par.shadowurl   = param.shadowurl;
        par.shadowhotspot = param.shadowhotspot;
        if(typeof param.imageurl != 'undefined') {
          par.imageheight = param.imageheight;
          par.imagewidth  = param.imagewidth;
          par.imageurl    = param.imageurl;
        }
        this.createIconHTML(par);
        par.content=new com.ptvag.webcomponent.map.vector.HTML(
          par.su.x, par.su.y, 17, par.html, null, par.cc, par.flexible);
        par.vectorlayer.addElement(par.content);
      }
    },
/*    createLocation: function( par ){
      var shad = false;
      if( typeof par.shadowheight!='undefined' &&
          typeof par.shadowwidth!='undefined' &&
          typeof par.shadowurl!='undefined' )
        shad = true;
      var w = shad?Math.max( par.shadowwidth,par.imagewidth ):par.imagewidth;
      var h = shad?Math.max( par.shadowheight,par.imageheight ):par.imageheight;
      par.html = "<div style='position:absolute;" +
          "width:" + w + "px;" +
          "height:" + h + "px;" +
          "left:" + (-par.hotspot.x).toString() + "px;" +
          "top:" + (-par.hotspot.y).toString() + "px;" +
          "'>";
      if(shad)
        par.html+= "<img style='position:absolute;" +
          "' " +
          "src='" + par.shadowurl + "' " +
          "width='" + par.shadowwidth + "' " +
          "height='" + par.shadowheight + "' alt=''>";
      par.html+="<img style='position:absolute;" +
          "' " +
          "src='" + par.imageurl + "' " +
          "width='" + par.imagewidth + "' " +
          "height='" + par.imageheight + "' " +
          "alt=''>" +
          "</div>";
      if(typeof par.flexible=='undefined')par.flexible=false;
      par.su=this.CCoord2SmartUnit(par.coord);
      par.su.x+=0.0001*parseInt(par.cc)%100-0.005;
    },          */
    showLocation: function( par ){
      // Marker
      if(typeof com.ptvag.webcomponent.map.vector.HTML!='undefined')
        par.content=new com.ptvag.webcomponent.map.vector.HTML(
            par.su.x, par.su.y, 17, par.html, null, par.cc, par.flexible);
      else
        par.content=new com.ptvag.webcomponent.map.vector.ImageMarker(
            par.su.x, par.su.y, par.imageurl,34, null, par.cc, par.flexible);
      //par.content.positionFlexible = true;
      //par.content.flexX = 10;
      //par.content.flexY = 10;
      par.vectorlayer.addElement(par.content);

      // HoverArea for draggable items
      par.hoverArea = new com.ptvag.webcomponent.map.vector.HoverArea(par.su.x,par.su.y,null,5,par.hover,par.unhover,0,par.cc+"_hoverArea", par.flexible);
      par.vectorlayer.addElement(par.hoverArea);

      // Tooltip (on hover)
      if ( typeof par.htmlText!='undefined') {
        par.tooltip=new com.ptvag.webcomponent.map.vector.Tooltip(
          par.su.x, par.su.y, 17, Math.max(par.imagewidth,par.imageheight),
           "<div style='white-space:nowrap;'>"+par.htmlText+"</div>", 34, 2, par.cc+"_tooltip", par.flexible);
        par.vectorlayer.addElement(par.tooltip);
      }
      else if ( typeof par.text!='undefined') {
        par.tooltip=new com.ptvag.webcomponent.map.vector.Tooltip(
          par.su.x, par.su.y, 17, Math.max(par.imagewidth,par.imageheight),
          "<div style='white-space:nowrap;'>"+par.text+"</div>", 34, 2, par.cc+"_tooltip", par.flexible);
        par.vectorlayer.addElement(par.tooltip);
      }
      if ( typeof par.onclick!='undefined') {
        par.clickArea=new com.ptvag.webcomponent.map.vector.ClickArea(
            par.su.x, par.su.y, null, 8, par.onclick, 0, par.cc+"_clickArea", par.flexible);
        par.vectorlayer.addElement(par.clickArea);
      }
      // Infobox (on left click)
      if( typeof par.infotitle!='undefined'&&typeof par.infocontent!='undefined') {
        par.onshowinfobox=function(){
          this.showInfoboxSU(par.su,par.infotitle,par.infocontent);
          if(typeof par.tooltip != 'undefined') {
             par.tooltip.clear();
          }
        };

        par.clickarea=new com.ptvag.webcomponent.map.vector.ClickArea(
          par.su.x, par.su.y, null, Math.max(par.imagewidth,par.imageheight),
          par.onshowinfobox.bind(this), 0, par.cc+"_clickarea", par.flexible);
        par.vectorlayer.addElement(par.clickarea,true);
      }
      else if ( typeof par.info!='undefined') {
        par.onshowinfobox=function(){
          //this.hideAllInfoBoxes();
          var infotop = "<div style='float:right;text-align:right'>" +
                        "<img id='PTVInfoboxClosebuttonImage_"+this.instance+"' src='" +
                        this.localurl+"closebutton.png' width='16' height='16' alt=''" +
                        "onclick='g"+this.classtype+"Instances["+this.instance+"].hideAllInfoBoxes();return false'></div>";
          this.currentInfoBox=new com.ptvag.webcomponent.map.vector.InfoBox(
            par.su.x, par.su.y, infotop+par.info, 34, 1, "Infobox", false);
          par.vectorlayer.addElement(this.currentInfoBox);
        };
        par.clickarea=new com.ptvag.webcomponent.map.vector.ClickArea(
          par.su.x, par.su.y, null, Math.max(par.imagewidth,par.imageheight),
          par.onshowinfobox.bind(this), 0, par.cc+"_clickarea", par.flexible);
        par.vectorlayer.addElement(par.clickarea);
      }
      if((typeof par.showinfobox != 'undefined') && (par.showinfobox == true)) {
          this.showInfoboxSU(par.su,par.infotitle,par.infocontent,par.text,par);
      }
    },
    hideLocation: function(p){
      this.hideAllInfoBoxes();
      if(typeof this.vectorlayer.removeElement!='undefined')
      {
        p.vectorlayer.removeElement(p.cc);
        p.vectorlayer.removeElement(p.cc+"_tooltip");
        p.vectorlayer.removeElement(p.cc+"_clickarea");
        p.vectorlayer.removeElement(p.cc+"_clickArea");
        p.vectorlayer.removeElement(p.cc+"_hoverArea");
      }
      else
      {
        p.vectorlayer.hideElement(p.cc);
        p.vectorlayer.hideElement(p.cc+"_tooltip");
        p.vectorlayer.hideElement(p.cc+"_clickarea");
        p.vectorlayer.hideElement(p.cc+"_clickArea");
        p.vectorlayer.removeElement(p.cc+"_hoverArea");
      }
      delete p.content;
      delete p.tooltip;
      delete p.clickarea;
      delete p.clickArea;
    },
    hideAllInfoBoxes: function(){
      if(this.currentInfoBox!=null){
        if(typeof this.vectorlayer.removeElement!='undefined')
          this.vectorlayer.removeElement("Infobox");
        else
          this.vectorlayer.hideElement("Infobox");
        delete this.currentInfoBox;
        this.currentInfoBox=null;
        this.map.getLayer("floater").setSwallowHoverEvents(false);
      }
    },
    createCircle: function( par ){
      if(typeof par.color=='undefined')par.color="red";
      if(typeof par.opacity=='undefined')par.opacity=0.7;
      if(typeof par.radius=='undefined')par.radius=16;
      if(typeof par.flexible=='undefined')par.flexible=false;
//      this.vectorlayer = this.map.getLayer('vector');
//      par.su=this.CCoord2SmartUnit(par.coord);
//      par.rgba=this.getRGBA(par.color,par.opacity);
      // Create polyline
      var lat = par.coord.getLat();
      var lon = par.coord.getLon();
      var radfact = Math.PI/180;
      var latoff = par.radius / 60 / 1852 * 1e6;
      var lonoff = par.radius / 60 / 1852 * 1e6 / Math.cos(lat/1e6*radfact);
      var n = 32;
      var crds = [];
      for( var i=0; i<n; i++ ) {
        var rad = 360 * i / n * radfact;
        var latfact = Math.cos(rad);
        var lonfact = Math.sin(rad);
        crds[i] = new CCoord({ lat: lat+latfact*latoff, lon: lon+lonfact*lonoff });
      }
      crds.push(crds[0]);
      par._poly = this.createContent({
        type: 'polyline',
        coords: crds,
        color: par.color,
        width: 4
      });
    },
    showCircle: function( par ){
      this.showContent( par._poly );
    },
    createPoint: function( par ){
      if(typeof par.color=='undefined')par.color="red";
      if(typeof par.opacity=='undefined')par.opacity=0.7;
      if(typeof par.radius=='undefined')par.radius=16;
      if(typeof par.flexible=='undefined')par.flexible=false;
      var lat = par.coord.getLat();
      var lon = par.coord.getLon();
      var radfact = Math.PI/180;
      var latoff = par.radius / 60 / 1852 * 1e6;
      var lonoff = par.radius / 60 / 1852 * 1e6 / Math.cos(lat/1e6*radfact);
      var n = 16;
      var crds = [];
      for( var i=0; i<n; i++ ) {
        var rad = 360 * i / n * radfact;
        var latfact = Math.cos(rad);
        var lonfact = Math.sin(rad);
        crds[i] = new CCoord({ lat: lat+latfact*latoff, lon: lon+lonfact*lonoff });
      }
      crds.push(crds[0]);
      par._poly = this.createContent({
        type: 'polygon',
        coords: crds,
        color: par.color,
        opacity: par.opacity,
        width: 1
      });
    },
    showPoint: function( par ){
      this.showContent( par._poly );
    },
    createPolyline: function( par ){
      if(typeof par.color=='undefined')par.color="blue";
      if(typeof par.opacity=='undefined')par.opacity=0.7;
      if(typeof par.width=='undefined')par.width=4;
      par.su=new Array();
      for(var i=0;i<par.coords.length;i++)
        par.su[i]=this.CCoord2SmartUnit(par.coords[i]);
      par.rgba=this.getRGBA(par.color,par.opacity);
    },
    showPolyline: function( par ){
      par.content=new com.ptvag.webcomponent.map.vector.Line(
          par.rgba, par.width, par.su, null, par.cc);
      par.vectorlayer.addElement(par.content);
    },
    createPolygon: function( par ){
      if(typeof par.color=='undefined')par.color="green";
      if(typeof par.opacity=='undefined')par.opacity=0.3;
      if(typeof par.width=='undefined')par.radius=4;
      par.su=new Array();
      for(var i=0;i<par.coords.length;i++)
        par.su[i]=this.CCoord2SmartUnit(par.coords[i]);
      par.rgba=this.getRGBA(par.color,par.opacity);
    },
    changePolyline:function(par,parnew){
        if(typeof parnew.color != 'undefined') {
          par.content.setColor(parnew.color);
        }
        par.content.refresh();
    },
    showPolygon: function( par ){
      par.content=new com.ptvag.webcomponent.map.vector.Poly(
          par.rgba, par.su);
      par.vectorlayer.addElement(par.content);
    },
    createContainer: function( par ){
      par.su=this.CCoord2SmartUnit(par.coord);
      par.content = '<div style="position:absolute; left:' +
        (-par.hotspot.x) + 'px; top:' + (-par.hotspot.y) + 'px;">' +
        par.content + '</div>'
    },
    showContainer: function( par ){
      par.content=new com.ptvag.webcomponent.map.vector.HTML(
          par.su.x, par.su.y, 2, par.content, null, par.cc, par.flexible);
      par.vectorlayer.addElement(par.content);
    },
    CCoord2SmartUnit: function( coord ){
      return com.ptvag.webcomponent.map.CoordUtil.geoDecimal2SmartUnit(
        {x:coord.getLon()/10, y:coord.getLat()/10});
    },
    SmartUnit2CCoord: function( su ){
      var gd = com.ptvag.webcomponent.map.CoordUtil.smartUnit2GeoDecimal(su);
      return new CCoord({lon:Math.round(gd.x*10),lat:Math.round(gd.y*10)});
    },
    AjaxMap2PTVZoom: function( aZoom ){
      if (typeof aZoom == 'undefined') {
        return 10;
      }
      var maxpix = Math.max( this.mapDiv.clientWidth, this.mapDiv.clientHeight );
      return Math.max( 0, Math.min( 23, Math.round( Math.log( aZoom/(maxpix * 0.3) ) / Math.log( 1.59 ) )));
    },
    PTV2AjaxMapZoom: function( aPTVZoom ){
      if (typeof aPTVZoom == 'undefined') {
        return 10000;
      }
      var maxpix = Math.max( this.mapDiv.clientWidth, this.mapDiv.clientHeight );
      return Math.round( Math.exp(aPTVZoom*Math.log(1.59)) * (maxpix * 0.3) );
    },
    getZoom: function(){
      return this.PTV2AjaxMapZoom( this.map.getZoom() );
    },
    getRGBA: function(c,o){
      if(typeof o=='undefined')o=1;
      if(typeof c=='undefined')c="red";
      if(c.length == 6)
        {
        var rV = parseInt(c.substring(0,2),16);
        var gV = parseInt(c.substring(2,4),16);
        var bV = parseInt(c.substring(4),16);
        if(!(isNaN(rV*gV*bV)))
           return 'rgba('+rV+','+gV+','+bV+','+o+')';
        }
      switch(c){
      case 'red':return 'rgba(255,0,0,'+o+')';
      case 'green':return 'rgba(0,255,0,'+o+')';
      case 'blue':return 'rgba(0,0,255,'+o+')';
      case 'yellow':return 'rgba(255,255,0,'+o+')';
      case 'cyan':return 'rgba(0,255,255,'+o+')';
      case 'purple':return 'rgba(255,0,255,'+o+')';
      case 'white':return 'rgba(255,255,255,'+o+')';
      case 'black':return 'rgba(0,0,0,'+o+')';
      case 'grey':return 'rgba(128,128,128,'+o+')';
      default:return 'rgba(255,0,0,'+o+')';
      }
    },
    getColorFFFFFF: function(c){
      if(typeof c=='undefined')c="red";
      switch(c){
      case 'red':return 'FF0000';
      case 'green':return '00FF00';
      case 'blue':return '0000FF';
      case 'yellow':return 'FFFF00';
      case 'cyan':return '00FFFF';
      case 'purple':return 'FF00FF';
      case 'white':return 'FFFFFF';
      case 'black':return '000000';
      case 'grey':return '888888';
      default:return 'FF0000';
      }
    },
    getPTVMapType: function( type ){
      switch( type ){
      case "satellite":
        return G_SATELLITE_MAP;
      case "road":
        return G_NORMAL_MAP;
      case "hybrid":
        return G_HYBRID_MAP;
      default:
        return G_NORMAL_MAP;
      };
    }
  });
}

