if( typeof gCLOOKTRAINS == 'undefined' ){
  var gCLOOKTRAINS = 'defined';
  
  /**
   * @brief Visualization of Trains
   **/
  var CLookTrains = Class.create();
  CLookTrains.prototype = Object.extend(new ALookLocation(), {
    initialize: function() {
      ALookLocation.prototype.initialize.call( this );
      this.prodclasses = 0;
      this.shownames = false;
      this.showdestination = false;
    },
  
    setMainDiv: function( aDiv ){
      ALookLocation.prototype.setMainDiv.call( this, aDiv );
      // Add Div elements to mainelem and give them to traindrawer
      this.traindrawer = new CLookTrainDrawerMap();
      this.traindrawer.init( this.addDivElem( this.getElemName('traindiv'), this.maindiv ),
                            this.addDivElem( this.getElemName('detailsdiv'), this.maindiv ),
                            this.width, this.height );
      if( this.mousemanager ) this.traindrawer.setMouseManager( this.mousemanager );
    },
    setMouseManager: function( aMouseManager ){
      this.mousemanager = aMouseManager;
      if( this.traindrawer ) this.traindrawer.setMouseManager( this.mousemanager );
    },
    setShowNames: function( boolval ){
      this.shownames = boolval;
    },
  
    setShowDestination: function( boolval ){
      this.showdestination = boolval;
    },
  
    setProductClass: function( prodindex, prodboolval ){
      if( prodboolval == true )
        this.prodclasses |= Math.pow(2,prodindex-1);
      else
        this.prodclasses &= ~(Math.pow(2,prodindex-1));
    },
  
    startanimation: function(){
      if( this.aktiv ){
        this.aktiv.stop();
        delete this.aktiv;
      }
      this.callback();
      this.aktiv = new PeriodicalExecuterForObjects(this, 3);
    },
    callback: function(){
      this.makeLocatingRequest();
    },
    stopanimation: function(){
      if( this.aktiv ){
        this.aktiv.stop();
        delete this.aktiv;
      }
    },
    makeLocatingRequest: function(){
      if( typeof this.traindrawer == 'undefined' ){ alert( 'CLookTrains::makeLocatingRequest(): traindrawer missing' ); return; }
      var myAjax = new Ajax.Request(
        this.getUrl(),
        {
          method: 'post',
          onComplete: this.handleLocatingResult.bind(this)
        }
      );
    },
  
    handleLocatingResult: function(ajaxResponse){
      // Interpret and deserialise response
      var response = eval('(' + ajaxResponse.responseText + ')');
      var l = response.look;
      if( typeof l.look_minx == 'undefined' || l.look_minx=='' ){ alert( 'CLookTrains::handleLocatingResult(): look_minx missing' ); return; }
      if( typeof l.look_maxx == 'undefined' || l.look_maxx=='' ){ alert( 'CLookTrains::handleLocatingResult(): look_maxx missing' ); return; }
      if( typeof l.look_miny == 'undefined' || l.look_miny=='' ){ alert( 'CLookTrains::handleLocatingResult(): look_miny missing' ); return; }
      if( typeof l.look_maxy == 'undefined' || l.look_maxy=='' ){ alert( 'CLookTrains::handleLocatingResult(): look_maxy missing' ); return; }
      this.setBoundingBox(parseInt(l.look_minx),parseInt(l.look_maxx),parseInt(l.look_miny),parseInt(l.look_maxy));
      // draw stops
      this.drawtrains( l.trains );
    },
  
    drawtrains: function( trains ){
      if( typeof this.transformer == 'undefined' ){
        this.transformer = new CMapTransformer( this.width, this.height,
                                                this.mapmingeox, this.mapmaxgeox,
                                                this.mapmingeoy, this.mapmaxgeoy );
        this.traindrawer.setTransformer(this.transformer);
      }
      this.traindrawer.drawalltrains( trains, this.shownames, this.showdestination );
      this.traindrawer.show();
    },
  
  	getUrl: function (){
	  if( typeof gGoogleBoundingBox != 'undefined' ){
		 var url = 
				  gUrlTravelPlannerAbsolute+
				  look_gettpl('catenateRedirectorParameter')+
				  look_gettpl('catenateDefaultParameters')+
				  'mapDisplay=look' +
				  '&_looktrains=yes' +
				  '&look_minx='+gGoogleminx+
				  '&look_maxx='+gGooglemaxx+
				  '&look_miny='+gGoogleminy+
				  '&look_maxy='+gGooglemaxy+
    			  '&look_productclass='+this.prodclasses.toString(10)+
				  '&look_json=yes'+
				  '&performLocating=1&'+
				  '&mapPixelWidth=' + look_gettpl('mapPixelWidth') +
				  '&mapPixelHeight=' + look_gettpl('mapPixelHeight') +
				  '&mapActiveZoomLevel=' + look_gettpl('mapActiveZoomLevel');
		  return url;
	  }
      return look_gettpl('urlLookTrainUpdate')+
			    'look_productclass='+this.prodclasses.toString(10)+
			    '&look_json=yes'+
			    '&performLocating=1&';
    }
  
  });
}

