if( typeof gCShowAjaxContent == 'undefined' ){
  var gCShowAjaxContent = 'defined';
  var CShowAjaxContent = Class.create();
  CShowAjaxContent.prototype = Object.extend(new AJSMapBaseClass(), {
    localurl : gJSMapImagePath+"CShowAjaxContent/",
    initialize: function(params){
      this.params=params;
      this.params.map=this.checkParameter(this.params.map);
      this.params.requesturl=this.checkParameter(this.params.requesturl);
      this.params.x=this.checkParameter(this.params.x,null);
      this.params.y=this.checkParameter(this.params.y,null);
      this.params.showclosestops=this.checkParameter(this.params.showclosestops,false);
      this.params.showtrainlane=this.checkParameter(this.params.showtrainlane,false);
      this.params.showchain=this.checkParameter(this.params.showchain,false);
      this.params.trainclass=this.checkParameter(this.params.trainclass,1);
      this.params.center=this.checkParameter(this.params.center,false);
      this.params.globalContentReminder=this.checkParameter(this.params.globalContentReminder,typeof gRememberContentOnMap=='undefined'?null:gRememberContentOnMap);
      this.content=new Array;
      this.hide=false;
      this.doAjaxRequest();
    },
    doAjaxRequest:function(){
      this.request = new Ajax.Request(
        this.params.requesturl,
        {
          method: 'get',
          onComplete: this.handleAjaxResult.bind(this)
        }
      );
    },
    prodClass2Icon:function(prodclass){
      var imageurl=this.localurl+"12x12/prod_";
      switch(parseInt(prodclass)){
        case 1: imageurl+="ice";break;
        case 2: imageurl+="ic";break;
        case 4: imageurl+="ir-d";break;
        case 8: imageurl+="re-rb";break;
        case 16: imageurl+="s";break;
        case 32: imageurl+="bus";break;
        case 64: imageurl+="ship";break;
        case 128: imageurl+="u";break;
        case 256: imageurl+="tram";break;
        case 512: imageurl+="taxi";break;
        default: imageurl+="bus";
      }
      imageurl+=".gif";
      return {type:"location",imageurl:imageurl,imagewidth:12,imageheight:12,hotspot:{x:6,y:6}};
    },
    showTrainLane:function(){
      var stoparray=new Array;
      var allcoords=new Array;
      for(var i=0; i < this.res.stops.length; i++){
        var locparam=this.prodClass2Icon(this.res.trains[0].trainclass);
        locparam.coord=new CCoord({latitude:parseInt(this.res.stops[i].y),longitude:parseInt(this.res.stops[i].x)});
        locparam.text=this.res.stops[i].name+" ("+(typeof this.res.stops[i].dep!="undefined"?this.res.stops[i].dep:this.res.stops[i].arr)+")";
        var l=this.content.length;
        this.content[l] = this.params.map.createContent(locparam);
        this.params.map.showContent(this.content[l]);
        if(this.params.globalContentReminder)this.params.globalContentReminder(this.content[l]);
        stoparray[parseInt(this.res.stops[i].id)]=this.res.stops[i];
        allcoords[allcoords.length]=locparam.coord;
      }
      var colorfctprod=this.getColorSetFunction("trainclass");
      var colorfctback=this.getColorSetFunction("linebackground");
      for(var i=0; i < this.res.edges.length; i++){
        this.res.edges[i].stop1=stoparray[parseInt(this.res.edges[i].id1)];
        this.res.edges[i].stop2=stoparray[parseInt(this.res.edges[i].id2)];
        this.res.edges[i].coords=[new CCoord({latitude:parseInt(this.res.edges[i].stop1.y),longitude:parseInt(this.res.edges[i].stop1.x)}),
                                  new CCoord({latitude:parseInt(this.res.edges[i].stop2.y),longitude:parseInt(this.res.edges[i].stop2.x)})]
        var l=this.content.length;
        this.content[l] = this.params.map.createContent({
                  type: "polyline",
                  coords:this.res.edges[i].coords,
                  color:colorfctback(),
                  width:8,
                  opacity:1.0});
        this.content[l+1] = this.params.map.createContent({
                  type: "polyline",
                  coords:this.res.edges[i].coords,
                  color:colorfctprod(this.res.trains[0]),
                  width:4,
                  opacity:1.0});
        this.params.map.showContent(this.content[l]);
        if(this.params.globalContentReminder)this.params.globalContentReminder(this.content[l]);
        this.params.map.showContent(this.content[l+1]);
        if(this.params.globalContentReminder)this.params.globalContentReminder(this.content[l+1]);
      }
      if(this.params.center)this.params.map.centerToContent(this.params.map.createContent({type: "polyline",coords:allcoords}));
    },
    showCloseStops:function(){
      for(var i=0; i < this.res.stops.length; i++){
        var l=this.content.length;
        this.content[l] = this.params.map.createContent({
                 type: "location",
                 coord: new CCoord({latitude:parseInt(this.res.stops[i].y),longitude:parseInt(this.res.stops[i].x)}),
                 text: this.res.stops[i].name+" ("+this.res.stops[i].dist+"m)",
                 imageurl:this.localurl+"icon_stopsection0"+(i%5+1).toString()+".png",  
                 imagewidth:16,                 
                 imageheight:16,               
                 hotspot:{x:8,y:8},
                 infotitle:this.res.stops[i].name+" ("+this.res.stops[i].dist+"m)",
                 infocontent:infotextstation({map:this.params.map,
                                              extId:this.res.stops[i].extId,
                                              puic:this.res.stops[i].puic,
                                              name:this.res.stops[i].urlname,
                                              x:this.res.stops[i].x,
                                              y:this.res.stops[i].y})
                 });
        this.params.map.showContent(this.content[l]);
        if(this.params.globalContentReminder)this.params.globalContentReminder(this.content[l]);
        if(this.params.x&&this.params.y){
          var l=this.content.length;
          this.content[l] = this.params.map.createContent({
                  type: "polyline",
                  coords:[new CCoord({latitude:parseInt(this.res.stops[i].y),longitude:parseInt(this.res.stops[i].x)}),
                          new CCoord({latitude:parseInt(this.params.y),longitude:parseInt(this.params.x)})],
                  color:"888888",
                  width:2,
                  opacity:0.4});
          this.params.map.showContent(this.content[l]);
          if(this.params.globalContentReminder)this.params.globalContentReminder(this.content[l]);
        }
      }
    },
    hideContent:function(){
      for(var l=0;l<this.content.length;l++){
        this.params.map.hideContent(this.content[l]);
        this.params.map.removeContent(this.content[l]);
      }
      this.hide=true;
      delete this.content;
    },
    handleAjaxResult:function(json){
      if(typeof json.responseText!='undefined'&&json.responseText!=null&&json.responseText!=""){
        this.res=eval('('+json.responseText+')');
//        if(!confirm("Es wurden "+res.stops.length+" Haltestellen ermittelt. Wollen Sie sie anzeigen?"))return;;
        if(!this.hide){
          if(this.params.showclosestops) this.showCloseStops();
          if(this.params.showtrainlane)  this.showTrainLane();
        }
      }
      else{
        alert('fehlerAJAX');
      }
    }
  });
}

