if( typeof gCHAFASCONNECTIONMAP == 'undefined' ){
  var gCHAFASCONNECTIONMAP = 'defined';
  var gCHafasConnectionMapInstanceCounter = 0;
  var gCHafasConnectionMapInstances = new Array;
  var CHafasConnectionMap = Class.create();
  CHafasConnectionMap.prototype = Object.extend(new AHafasMap(), {
    localmapurl : gJSMapImagePath+"CHafasConnectionMap/",
    initialize: function( aMap, connjson, params ) {
      gCHafasConnectionMapInstances[gCHafasConnectionMapInstanceCounter]=this;
      this.instance=gCHafasConnectionMapInstanceCounter++;
      this.param=params;
      this.opf=1.0;
      if(typeof params.opacityfactor!='undefined')
        this.opf=params.opacityfactor;
      this.showGisRouteType0=true;
      if(typeof params.showGisRouteType0!='undefined')
        this.showGisRouteType0=params.showGisRouteType0;
      if(typeof connjson=="string")
        this.conn = eval('('+connjson+')');
      else
        this.conn = connjson;
      this.map = aMap;
//      this.map.show();
      this.showingobjects = false;
      this.sections=new Array;
      this.mincenterzoom=2000;
      this.map.setMinCenterZoom(this.mincenterzoom);
      this.createObjects();
    },
    show: function(onshow){
      if(onshow!=null) this.onshow=onshow;
      this.showingcounter=0;
      this.showingObjectSum=this.polylineobject.length+
                            this.routeobject.length+
                            this.locationobject.length;
      for(var i=0;i<this.polylineobject.length;i++)
        this.map.showContent( this.polylineobject[i] );
      for(i=0;i<this.routeobject.length;i++)
        this.map.showContent( this.routeobject[i] );
      for(i=0;i<this.locationobject.length;i++)
        this.map.showContent( this.locationobject[i].loc );
    },
    checkShowing: function(){
      this.showingcounter++;
      if(this.showingcounter==this.showingObjectSum){
        if(typeof this.onshow!='undefined'&&this.onshow!=null){
          this.onshow();
        }
        this.showingcounter=0;
        this.showingObjectSum=-1;
      }
    },
    hide: function(){
      for(var i=0;i<this.polylineobject.length;i++)
        this.map.hideContent( this.polylineobject[i] );
      for(i=0;i<this.routeobject.length;i++)
        this.map.hideContent( this.routeobject[i] );
      for(i=0;i<this.locationobject.length;i++)
        this.map.hideContent( this.locationobject[i].loc );
    },
    centerRoute: function(){
      if(typeof this.allpolyline=='undefined') return;
      this.map.centerToContent(this.allpolyline);
    },
    centerSection: function(s,smooth){
      if(typeof this.sections[s]=='undefined') return;
      if(typeof this.sections[s].pol!='undefined')
        this.map.centerToContent(this.sections[s].pol,smooth);
      else if(typeof this.sections[s].route!='undefined')
        this.map.centerToContent(this.sections[s].route,smooth);
    },
    animateSection: function(s,onfinished){
      if(typeof this.Animation!='undefined'){
        window.clearInterval(this.Animation.Interval);
        this.map.hideContent(this.Animation.Location);
        this.map.removeContent(this.Animation.Location);
        delete this.Animation;
      }
      var an=new Object;
      an.onfinished=onfinished;
      var se=this.conn.sections[s];
      if(typeof se.productcode!='undefined'){
        an.LocParams=this.clone_contentparams( gLocParamsConnectionMap[ "prod" ] );
        an.LocParams.imageurl=this.localmapurl+"prod_"+se.productcode+".gif";
        an.LocParams.text=this.decode(se.name);
      }
      else{
        an.LocParams=this.clone_contentparams( gLocParamsConnectionMap[ "walk" ] );
      }
      var D=0;
      var lc;
      for(lc=0;lc<se.locations.length;lc++)
        if(typeof se.locations[lc].coord=='undefined')
          se.locations[lc].coord=new CCoord({lon:se.locations[lc].x,lat:se.locations[lc].y});

      for(lc=0;lc<se.locations.length-1;lc++){
        if(typeof se.locations[lc].dist=='undefined')
          se.locations[lc].dist=this.getDist(
            se.locations[lc].coord,
            se.locations[lc+1].coord);
        D+=se.locations[lc].dist;
      }
      var A=100;
      if(D<13000)A=20+Math.round(A*D/13000);
      var a=0;
      var d=0;
      var d1=0;
      var l0;
      var l1=se.locations[0];
      an.coords=new Array;
      an.displaytime=new Array;
      for(lc=0;lc<se.locations.length-1;lc++){
        d=d1;
        l0=l1;
        l1=se.locations[lc+1];
        d1+=l0.dist;
        var x0=l0.x;
        var x1=l1.x;
        var y0=l0.y;
        var y1=l1.y;
        var t0=l0.dep;
        var t1=l1.arr;
        for(;a<A&&a*D/A<d1;a++){
          var ac=a-(A*d/D);
          var al=A*l0.dist/D;
          var x=(ac*x1+(al-ac)*x0)/al;
          var y=(ac*y1+(al-ac)*y0)/al;
          an.coords[a]=new CCoord({latitude:y,longitude:x});
//          an.displaytime[s]
        }
      }
      an.c=0;
      an.LocParams.coord=an.coords[0];
      an.Location = this.map.createContent(an.LocParams);
      this.map.showContent(an.Location);
      an.Interval = window.setInterval("gCHafasConnectionMapInstances["+this.instance+"].animate()", 100);
      this.Animation=an;
    },
    getDist:function(c1,c2){
      return Math.sqrt(Math.pow(c1.getLon()-c2.getLon(),2)+Math.pow(c1.getLat()-c2.getLat(),2));
    },
    animate: function(){
      var c=++this.Animation.c;
      if(c>=this.Animation.coords.length){
        window.clearInterval(this.Animation.Interval);
        delete this.Animation.Interval;
        this.map.hideContent(this.Animation.Location);
        this.map.removeContent(this.Animation.Location);
        if(typeof this.Animation.onfinished!='undefined'&&this.Animation.onfinished!=null)
          this.Animation.onfinished();
        delete this.Animation;
        return;
      }
      this.map.moveLocation(this.Animation.Location,this.Animation.coords[c]);
    },
    centerLocation: function(s,l,smooth){
      if(typeof this.sections[s]=='undefined') return;
      if(typeof this.conn.sections[s].locations[l]!='undefined'){
        this.map.setZoom(this.mincenterzoom);
        this.map.centerToGeo(new CCoord({longitude:this.conn.sections[s].locations[l].x,
                                         latitude:this.conn.sections[s].locations[l].y}),smooth);
      }
    },
    centerStart:function(smooth){
      this.centerLocation(0,0,smooth);
    },
    centerEnd:function(smooth){
      this.centerLocation(this.sections.length-1,this.conn.sections[this.sections.length-1].locations.length-1,smooth);
    },
    createObjects: function(){
      if( this.showingobjects == false ){
        if( typeof this.conn.sections != 'undefined' ){
          this.locationobject = new Array;
          this.polylineobject = new Array;
          this.routeobject = new Array;
          var allpoly = new Object;
          allpoly.coords = new Array;
          allpoly.opacity = 1;
          allpoly.width = 1;
          allpoly.color = "black";
          allpoly.type = "polyline";
          var pc = 0;
          var rc = 0;
          var ac = 0;
          var lc = 0;
          var sl = this.conn.sections.length;

          for( var s1 = 0; s1 < sl; s1++ ){
            var s = s1+1;if(s==sl)s=0;
            this.sections[s] = new Object;
            var polylineparams = new Object;
            polylineparams.coords = new Array;
            var rgexists = false;
            if( typeof this.conn.sections[s].polyline != 'undefined' ){
              var ll = this.conn.sections[s].polyline.length;
              rgexists = true;
              for( var i = 0; i < ll; i++ ){
                var x = this.conn.sections[s].polyline[i].x;
                var y = this.conn.sections[s].polyline[i].y;
                polylineparams.coords[i] = new CCoord({latitude:y,longitude:x});
                allpoly.coords[ac++] = new CCoord({latitude:y,longitude:x});
              }
            }
            if( typeof this.conn.sections[s].locations != 'undefined' ){
              var ll = this.conn.sections[s].locations.length;
              for( var i1 = 0; i1 < ll; i1++ ){
                var i=i1;if(s==0){i=ll-(i1+1)}
                var x = this.conn.sections[s].locations[i].x;
                var y = this.conn.sections[s].locations[i].y;
                var t = this.conn.sections[s].locations[i].typ;
                if(!rgexists){
                  polylineparams.coords[i] = new CCoord({latitude:y,longitude:x});
                  allpoly.coords[ac++] = new CCoord({latitude:y,longitude:x});
                }
//                if(this.opf==1.0){
                  var locparams;
                  if( typeof t=='undefined'){
                    if( i == 0 && s == 0 ){
                      if(this.conn.sections[s].type == "GIS_ROUTE" && this.conn.sections[s].gisRouteType == "0") {
                         locparams = this.clone_contentparams( gLocParamsConnectionMap[ "start_walk" ] );
                      }else{
                         locparams = this.clone_contentparams( gLocParamsConnectionMap[ "start" ] );
                      }
                      locparams.zIndex=2;
                    }else if( i == ll - 1 && s == sl - 1 ){
                      if(this.conn.sections[s].type == "GIS_ROUTE" && this.conn.sections[s].gisRouteType == "0") {
                        locparams = this.clone_contentparams( gLocParamsConnectionMap[ "end_walk" ] );
                      }else{
                        locparams = this.clone_contentparams( gLocParamsConnectionMap[ "end" ] );
                      }
                      locparams.zIndex=2;
                    }
  
                    else if( i == 0 || i == ll - 1){
                      if ( s == 0 && this.conn.sections[0].type == "GIS_ROUTE" && this.conn.sections[0].gisRouteType == "0" ) {
                        if(i==0) {
                          locparams = this.clone_contentparams( gLocParamsConnectionMap[ "start" ] );
                        }else{
                          continue;
                        }
                      } else if ( s == sl-1 && this.conn.sections[sl-1].type == "GIS_ROUTE" && this.conn.sections[sl-1].gisRouteType == "0" ) {
                        locparams = this.clone_contentparams( gLocParamsConnectionMap[ "end" ] );
                      } else {
                        if(i==0) {
                          if(s==1 && this.conn.sections[0].type == "GIS_ROUTE" ) {
                            locparams = this.clone_contentparams( gLocParamsConnectionMap[ "start" ] );
                          }else{
                            locparams = this.clone_contentparams( gLocParamsConnectionMap[ "change" ] );
                          }
                        }else{
                          continue;
                        }
                      }
                      locparams.zIndex=1;
                    }else{
                      locparams = this.clone_contentparams( gLocParamsConnectionMap[ "station" ] );
                      locparams.zIndex=0;
                    }
                  }else{
                    if( (i == 0 && s == 0)||(i == ll - 1 && s == sl - 1) ){
                      locparams = this.clone_contentparams( gLocParamsConnectionMap[ "sbb"+t ] );
                      locparams.zIndex=2;
                    }else if(i == 0 || i == ll - 1 ){
                      locparams = this.clone_contentparams( gLocParamsConnectionMap[ "sbb"+t ] );
                      locparams.zIndex=1;
                    }else{
                      locparams = this.clone_contentparams( gLocParamsConnectionMap[ "station" ] );
                      locparams.zIndex=0;
                    }
                  }
                  locparams.text = this.decode( this.conn.sections[s].locations[i].name );
                  locparams.infotitle='<img align="absmiddle" hspace="3" vspace="1" src="'+gImagePath+this.conn.sections[s].icon+'_pic.gif"/>'+locparams.text;
                  var typnr = 2;
                  switch(t){
                  case 'STATION':
                    typnr=1;
                    break;
                  case 'ADDRESS':
                    typnr=2;
                    break;
                  case 'POI':
                    typnr=2;
                    break;
                  }
                  var szid="A="+typnr+"@O="+this.conn.sections[s].locations[i].name+"@X="+x+"@Y="+y;
                  // Show arrival times on every location except the first of each section or when it's the last section
                  if((i != 0) || ((s == (this.conn.sections.length-1)) && (i == this.conn.sections[s].locations.length-1))) {
                    locparams.infocontent="<div style='padding:2px;text-align:left'>"+gTArrival + ' '+ this.conn.sections[s].locations[i].arr+"</div>";
                  }else{
                    var time = (this.conn.sections[s].locations[i].arr != '')?this.conn.sections[s].locations[i].arr:this.conn.sections[s].locations[i].dep;
                    var label = (this.conn.sections[s].locations[i].arr != '')?gTArrival:gTDeparture;
                    if(((this.conn.sections[this.conn.sections.length-1].type == 'GIS_ROUTE')&&(s == this.conn.sections.length-1))) {
                      locparams.infocontent="<div style='padding:2px;text-align:left'>"+gTArrival + ' '+ this.conn.sections[s-1].locations[this.conn.sections[s-1].locations.length-1].arr+"</div>";
                    }else{
                      locparams.infocontent="<div style='padding:2px;text-align:left'>"+gTDeparture + ' '+ time  + "</div>";
                    }
                  }
                  if(t=='STATION')
                    locparams.infocontent+=
                      "<div style='padding:2px;text-align:left'>"+
                      "<a href='"+this.param.stationboardurl+"start=1&boardType=dep&input="+locparams.text+"'>"+
                      this.param.tDeparture+"</a></div>"+
                      "<div style='padding:2px;text-align:left'>"+
                      "<a href='"+this.param.stationboardurl+"start=1&boardType=arr&input="+locparams.text+"'>"+
                      this.param.tArrival+"</a></div>";
                  locparams.coord = new CCoord({latitude:y,longitude:x});
                  this.locationobject[lc] = new Object;
                  this.locationobject[lc++].par = this.clone_contentparams(locparams);
                  //                }
              }
            }
            polylineparams.opacity = 0.8*this.opf;
            polylineparams.width = 5;
            polylineparams.onshow=this.checkShowing.bind(this);
            switch(this.conn.sections[s].type){
            case "GIS_ROUTE":
              if(this.showGisRouteType0||(this.conn.sections[s].gisRouteType!=0&&this.conn.sections[s].gisRouteType!=1&&this.conn.sections[s].gisRouteType!=2&&this.conn.sections[s].gisRouteType!=3&&this.conn.sections[s].gisRouteType!=4&&this.conn.sections[s].gisRouteType!=5)){
                polylineparams.color = "red";
                if(this.opf==1.0){
//                  polylineparams.type = "route";
                  polylineparams.type = "polyline";
                }
                else
                  polylineparams.type = "polyline";
                this.routeobject[rc] = this.map.createContent(
                     this.clone_contentparams( polylineparams ) );
                this.sections[s].route=this.routeobject[rc++];
              }
              break;
            case "WALK":
            case "TRANSFER":
              polylineparams.color = "red";
              polylineparams.type = "polyline";
              this.polylineobject[pc] = this.map.createContent(
                   this.clone_contentparams( polylineparams ) );
              this.sections[s].pol=this.polylineobject[pc++];
              break;
            case "JOURNEY":
              polylineparams.color = "0BA7D9";
              polylineparams.type = "polyline";
              this.polylineobject[pc] = this.map.createContent(
                   this.clone_contentparams( polylineparams ) );
              this.sections[s].pol=this.polylineobject[pc++];
//              this.map.showContent( this.polylineobject[pc++] );
            }
          }
          this.allpolyline = this.map.createContent(
               this.clone_contentparams( allpoly ) );
//          this.map.centerToContent( this.allpolyline );
//          if(this.opf==1.0){
            for(z=0;z<=2;z++){
              for(i=0;i<this.locationobject.length;i++){
                if(this.locationobject[i].par.zIndex==z){
                  this.locationobject[i].par.onshow=this.checkShowing.bind(this);
                  this.locationobject[i].loc=this.map.createContent( this.locationobject[i].par );
//                  this.map.showContent( this.locationobject[i].loc );
                }
              }
//            }
          }
        }
        this.showingobjects = true;
      }
    }
  });
}

