if( typeof gCLOOKSTOPS == 'undefined' ){
  var gCLOOKSTOPS = 'defined';
  
  var gLookLocationReferenceCount = gLookLocationReferenceCount?gLookLocationReferenceCount:0;
  
  /**
   * @brief Abstract basis class for visualization of static Locations on a Map
   **/
  var ALookLocation = Class.create();
  ALookLocation.prototype = {
    initialize: function() {
      this.refcount = gLookLocationReferenceCount;
//      alert( 'ALookLocation::initialize(): this.refcount:' + this.refcount );
      gLookLocationReferenceCount++;
      this.mapmingeox = 0;
      this.mapmaxgeox = 0;
      this.mapmaxgeoy = 0;
      this.mapmingeoy = 0;
    },
  
    getElemName: function( shortname ){
      return 'ALookLocation_' + this.refcount + '_' + shortname;
    },
    
    setBoundingBox: function( minx, maxx, miny, maxy ){
      this.mapmingeox = minx;
      this.mapmaxgeox = maxx;
      this.mapmingeoy = miny;
      this.mapmaxgeoy = maxy;
    },
  
    setMainDiv: function( aDiv ){
      this.maindiv = aDiv;
      this.width = parseInt(this.maindiv.style.width);
      this.height = parseInt(this.maindiv.style.height);
      // Remove all Content of maindiv
      CNodes.removeAllChilds( this.maindiv );
    },

    setMouseManager: function( aMouseManager ){
      this.mousemanager = aMouseManager;
    },
  
    show: function(){
      if( typeof this.maindiv == 'undefined' ){ alert( 'ALookLocation::show(): maindiv missing' ); return; }
      this.makeLocatingRequest();
    },
  
    addDivElem: function( divname, parentelem ){
      var elem = CNodes.getElem( 'div', divname, 'absolute', 'block',
                                 0, 0, this.width, this.height );
      parentelem.appendChild(elem);
      return $( divname );
    }
  }
  
  /**
   * @brief Visualization of Stops
   **/
  var CLookStops = Class.create();
  CLookStops.prototype = Object.extend(new ALookLocation(), {
    initialize: function() {
      ALookLocation.prototype.initialize.call( this );
      this.prodclasses = 0;
      this.showstopnames = false;
    },
  
    setMainDiv: function( aDiv ){
      ALookLocation.prototype.setMainDiv.call( this, aDiv );
      // Add Div elements to mainelem and give them to stopdrawer
      this.stopdrawer = new CLookStopDrawerMap();
      this.stopdrawer.init( this.addDivElem( this.getElemName('stopdiv'), this.maindiv ),
                            this.addDivElem( this.getElemName('stopdetailsdiv'), this.maindiv ),
                            this.addDivElem( this.getElemName('detailsdiv'), this.maindiv ),
                            this.addDivElem( this.getElemName('abovealldiv'), this.maindiv ),
                            this.width, this.height );
      if( this.mousemanager ) this.stopdrawer.setMouseManager( this.mousemanager );
    },
  
    setMouseManager: function( aMouseManager ){
      this.mousemanager = aMouseManager;
      if( this.stopdrawer ) this.stopdrawer.setMouseManager( this.mousemanager );
    },
  
    setShowStopNames: function( boolval ){
      this.showstopnames = boolval;
    },
  
    setProductClass: function( prodindex, prodboolval ){
      if( prodboolval == true )
        this.prodclasses |= Math.pow(2,prodindex-1);
      else
        this.prodclasses &= ~(Math.pow(2,prodindex-1));
    },
  
    makeLocatingRequest: function(){
      if( typeof this.stopdrawer == 'undefined' ){ alert( 'CLookStops::makeLocatingRequest(): stopdrawer 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( 'CLookStops::handleLocatingResult(): look_minx missing' ); return; }
      if( typeof l.look_maxx == 'undefined' || l.look_maxx=='' ){ alert( 'CLookStops::handleLocatingResult(): look_maxx missing' ); return; }
      if( typeof l.look_miny == 'undefined' || l.look_miny=='' ){ alert( 'CLookStops::handleLocatingResult(): look_miny missing' ); return; }
      if( typeof l.look_maxy == 'undefined' || l.look_maxy=='' ){ alert( 'CLookStops::handleLocatingResult(): look_maxy missing' ); return; }
      this.setBoundingBox(parseInt(l.look_minx),parseInt(l.look_maxx),parseInt(l.look_miny),parseInt(l.look_maxy));
      // clear all
      this.stopdrawer.clearAllDivs();
      // draw stops
      this.drawstops( l.stops );
    },
  
    drawstops: function( stops ){
      if( typeof this.transformer == 'undefined' ){
        this.transformer = new CMapTransformer( this.width, this.height,
                                                this.mapmingeox, this.mapmaxgeox,
                                                this.mapmingeoy, this.mapmaxgeoy );
      }
      this.stopdrawer.drawallstops( this.transformer, stops, this.showstopnames );
    },
  
  	getUrl: function (){
      var stopname='';
      if(this.showstopnames==true){
          stopname='look_stopname=yes&';
      }
		if( typeof gGoogleBoundingBox != 'undefined' ){
		  var url = 
		         gUrlTravelPlannerAbsolute+
			      look_gettpl('catenateRedirectorParameter')+
			      look_gettpl('catenateDefaultParameters')+
			      'mapDisplay=look' +
			      '&lookstops=yes' +
			      '&look_minx='+gGoogleminx+
			      '&look_maxx='+gGooglemaxx+
     			   '&look_miny='+gGoogleminy+
			      '&look_maxy='+gGooglemaxy+
			      '&look_stopclass='+this.prodclasses.toString(10)+
			      '&look_json=yes'+
			      '&performLocating=2&'+
			      '&mapPixelWidth=' + look_gettpl('mapPixelWidth') +
     			   '&mapPixelHeight=' + look_gettpl('mapPixelHeight') +
			      '&mapActiveZoomLevel=' + look_gettpl('mapActiveZoomLevel') + 
			      '&' + stopname;
			return url;
		}
      return look_gettpl('urlLookStopUpdate')+'look_stopclass='+this.prodclasses.toString(10)+'&look_json=yes'+'&performLocating=2&'+stopname;
    }
  
  });
  
  
  
  /**
   * @brief Visualization of Pois
   **/
  var CLookPois = Class.create();
  CLookPois.prototype = Object.extend(new ALookLocation(), {
    initialize: function() {
      ALookLocation.prototype.initialize.call( this );
      this.maxpoi = 16;
    },
  
    setMainDiv: function( aDiv ){
      ALookLocation.prototype.setMainDiv.call( this, aDiv );
      // Add Div elements to mainelem and give them to stopdrawer
      var poidiv = new Array;
      var abovealldiv = new Array;
      for( var i = 1; i <= this.maxpoi; i++ ){
        poidiv[i] = this.addDivElem( this.getElemName('poidiv' + i ), this.maindiv )
      }
      var detailsdiv = this.addDivElem( this.getElemName('detailsdiv' ), this.maindiv )
      for( var i = 1; i <= this.maxpoi; i++ ){
        abovealldiv[i] = this.addDivElem( this.getElemName('abovealldiv' + i ), this.maindiv )
      }
      this.stopdrawer = new Array;
      for( var i = 1; i <= this.maxpoi; i++ ){
        this.stopdrawer[i] = new CLookPoiDrawerMap( i );
        this.stopdrawer[i].init( poidiv[i], detailsdiv, abovealldiv[i],
                                 this.width, this.height );
        if( this.mousemanager ) this.stopdrawer[i].setMouseManager( this.mousemanager );
      }
    },
  
    setMouseManager: function( aMouseManager ){
      this.mousemanager = aMouseManager;
      for( var i = 1; i <= this.maxpoi; i++ ){
        if( this.stopdrawer[i] ) this.stopdrawer[i].setMouseManager( this.mousemanager );
      }
    },
  
    makeLocatingRequest: function( poiclass ){
      if( typeof this.stopdrawer[poiclass] == 'undefined' ){ alert( 'CLookPois::makeLocatingRequest(): stopdrawer missing' ); return; }
      var myAjax = new Ajax.Request(
        this.getUrl( poiclass ),
        {
          method: 'post',
          onComplete: this.handleLocatingResult.bind(this, poiclass)
        }
      );
    },
  
    handleLocatingResult: function(poiclass,ajaxResponse){
      // Interpret and deserialise response
      var response = eval('(' + ajaxResponse.responseText + ')');
      var l = response.look;
      if( typeof l.look_minx == 'undefined' || l.look_minx=='' ){ alert( 'CLookPois::handleLocatingResult(): look_minx missing' ); return; }
      if( typeof l.look_maxx == 'undefined' || l.look_maxx=='' ){ alert( 'CLookPois::handleLocatingResult(): look_maxx missing' ); return; }
      if( typeof l.look_miny == 'undefined' || l.look_miny=='' ){ alert( 'CLookPois::handleLocatingResult(): look_miny missing' ); return; }
      if( typeof l.look_maxy == 'undefined' || l.look_maxy=='' ){ alert( 'CLookPois::handleLocatingResult(): look_maxy missing' ); return; }
      this.setBoundingBox(parseInt(l.look_minx),parseInt(l.look_maxx),parseInt(l.look_miny),parseInt(l.look_maxy));
      // clear all
      this.stopdrawer[poiclass].clearAllDivs();
      // draw pois
//      var vorher = new Date();
      this.drawpois( l.pois, poiclass );
//      var nachher = new Date();
//      alert('Draw Time for ' + l.pois.length + ' objects:' + (nachher.getTime()-vorher.getTime()) +
//            'ms (' + ((nachher.getTime()-vorher.getTime())/l.pois.length).toFixed(2) + 'ms per object)' );
    },
  
    drawpois: function( pois, poiclass ){
      if( typeof this.transformer == 'undefined' ){
        this.transformer = new CMapTransformer( this.width, this.height,
                                                this.mapmingeox, this.mapmaxgeox,
                                                this.mapmingeoy, this.mapmaxgeoy );
      }
      this.stopdrawer[poiclass].drawallstops( this.transformer, pois );
    },
  
    show: function( poiclass ){
      if( typeof this.maindiv == 'undefined' ){ alert( 'CLookPois::show(): maindiv missing' ); return; }
      this.makeLocatingRequest( poiclass );
    },
  
    clear: function( poiclass ){
      if( typeof this.stopdrawer == 'undefined' ){ alert( 'CLookPois::clear(): stopdrawer missing' ); return; }
      this.stopdrawer[poiclass].clearAllDivs();
    },
  
  	getUrl: function (poiclass){
       if( typeof gGoogleBoundingBox != 'undefined' ){
         var url = 
                gUrlTravelPlannerAbsolute+
               look_gettpl('catenateRedirectorParameter')+
               look_gettpl('catenateDefaultParameters')+
               'mapDisplay=look' +
               '&lookpois=yes' +
               '&look_minx='+gGoogleminx+
               '&look_maxx='+gGooglemaxx+
                '&look_miny='+gGoogleminy+
               '&look_maxy='+gGooglemaxy+
               '&look_pois='+poiclass+
               '&look_json=yes'+
               '&performLocating=4&'+
               '&mapPixelWidth=' + look_gettpl('mapPixelWidth') +
               '&mapPixelHeight=' + look_gettpl('mapPixelHeight') +
               '&mapActiveZoomLevel=' + look_gettpl('mapActiveZoomLevel') + 
               '&';
         return url;
       }
      return look_gettpl('urlLookPoiUpdate')+'look_pois='+poiclass+'&look_json=yes'+'&performLocating=4&';
    }
  });
}

