/* CLookMouse.js */

if( typeof gCLOOKMOUSE == 'undefined' ){
  var gCLOOKMOUSE = 'defined';

  var gLookMouseReferenceCount = gLookMouseReferenceCount?gLookMouseReferenceCount:0;
  /**
   * @brief Administration of Mouseover effects
   **/
  var CLookMouse = Class.create();
  CLookMouse.prototype = {
    // constructor
    initialize: function( mouseoverdiv ) {
      this.refcount = gLookMouseReferenceCount;
      gLookMouseReferenceCount++;
      this.mouseoverdiv = mouseoverdiv;
      this.mouseoverdiv.onmousemove = this.mouseevent.bind( this );
      this.mouseoverdiv.onmouseout = this.mouseevent.bind( this );
      this.activebodyfunctionindex = -1;
    },
    getElemName: function( shortname ){
      return 'CLookMouse_' + this.refcount + '_' + shortname;
    },
    showcurrentobjects: function(){
      if( this.currentobjects.length > 0 ){
        var elem = document.createElement( 'div' );
        elem.id=this.getElemName('popup');
        elem.className = 'look_popup';
        elem.style.top = (this.currenty - 1) + 'px';
        elem.style.left = (this.currentx - 1) + 'px';
        for( var i = 0; i < this.currentobjects.length; i++ ){
          elem.appendChild(this.getObjectTitle( this.currentobjects[i], i ));
        }
        this.mouseoverdiv.appendChild( elem );
        for( var i = 0; i < this.currentobjects.length; i++ ){
          $(this.getElemName( "popuptitle_"+i)).style.width = elem.clientWidth;
        }
        document.onmousedown = function(){};
        document.onmousemove = function(){};
        document.onmouseup = function(){};
      }
    },
    getIcon: function(obj, index){
      var poiimg = document.createElement('img');
      poiimg.width= 10;
      poiimg.height= 10;
      poiimg.style.padding= '1px';
      poiimg.style.position = 'relative';
      if(!ie7) poiimg.style.top = '-1px';
      poiimg.src= obj.icon;
      if( obj.bodyfunction ){
        if( obj.bodyfunction == "stboard" ){
          poiimg.onmouseover = this.startstboard.bind( this, index );
          poiimg.onmouseout = this.stopstboard.bind( this, index );
        }
      }else if( obj.train ){
        poiimg.onmouseover = this.showtraininfo.bind( this, index );
        poiimg.onmouseout = this.hidetraininfo.bind( this );
      }
      return poiimg;
    },
    getTitleText: function(obj, index){
      var titletext = document.createElement('span');
      titletext.className = "look_popup_titletext";
      titletext.innerHTML = obj.title;
      if( obj.destination && obj.startlocation ) titletext.style.paddingRight = "40px";
      else if( obj.train ) titletext.style.paddingRight = "16px";
      if( obj.bodyfunction ){
        if( obj.bodyfunction == "stboard" ){
          titletext.onmouseover = this.startstboard.bind( this, index );
          titletext.onmouseout = this.stopstboard.bind( this, index );
        }
      }else if( obj.train ){
        titletext.onmouseover = this.showtraininfo.bind( this, index );
        titletext.onmouseout = this.hidetraininfo.bind( this );
      }
      return titletext;
    },
    getTextDiv: function(text){
      var textdiv = document.createElement('div');
      textdiv.className = "look_popup_textdiv";
      textdiv.innerHTML = text;
      return textdiv;
    },
    showtraininfo: function( index ){
      if( this.currentobjects[index] && this.currentobjects[index].train ){
        var t = this.currentobjects[index].train;
        var elem = document.createElement( 'div' );
        elem.id=this.getElemName('popup_traininfo');
        elem.className = 'look_popup';
        elem.style.position = 'absolute';
        elem.style.top = $(this.getElemName('popup')).style.top;
        elem.style.left = (parseInt($(this.getElemName('popup')).style.left) +
                          parseInt($(this.getElemName('popup')).clientWidth)) + 'px';
        elem.appendChild(this.getObjectTitleTrainInfo( this.currentobjects[index], index ));
        elem.appendChild(this.getTextDiv( 'please wait' ));
        this.mouseoverdiv.appendChild( elem );
        var myAjax1 = new Ajax.Request(
          this.getSingleTrainUrl(t.trainid),
          {
            method: 'get',
            onComplete: this.handleSingleTrainLocatingResult.bind(this, t, index )
          }
        );
//        this.showSingleChain( t.trainid );
      }
    },
    getSingleChainUrl: function( trainid ){
      return look_gettpl('urlLookChainUpdate') +
             'enablelook=yes&look_json=yes&' +
             'look_trainid=' + trainid + '&' +
             'performLocating=16&';
    },
    getSingleTrainUrl: function( trainid ){
      return look_gettpl('urlLookSingleTrainUpdate') +
             'enablelook=yes&look_json=yes&' +
             'look_trainid=' + trainid + '&' +
             'performLocating=8&';
    },
    handleSingleTrainLocatingResult: function( train, index, ajaxResponse ){
      if( $(this.getElemName('popup_traininfo')) ){
        var elem = $(this.getElemName('popup_traininfo'));
        var response = eval('(' + ajaxResponse.responseText + ')');
        var st = response.look.singletrain[0];
        CNodes.removeAllChilds( elem );
        var title = this.getObjectTitleTrainInfo( this.currentobjects[index], index );
        elem.appendChild(title);
        if( st.narr && st.narr!='' && st.nstopname && st.nstopname != '' ){
          var next = this.getTextDiv( st.narr + ':' + st.nstopname );
          elem.appendChild(next);
        }
        if( st.larr && st.larr!='' && st.lstopname && st.lstopname != '' && st.lstopname != st.nstopname ){
          var dest = this.getTextDiv( st.larr + ':' + st.lstopname )
          elem.appendChild(dest);
        }
        title.style.width = elem.clientWidth;
        if( next ) next.style.width = elem.clientWidth;
        if( dest ) dest.style.width = elem.clientWidth;
      }
    },
    showSingleChain: function( trainid ){
      var elem = document.createElement( 'div' );
      elem.id=this.getElemName('chaindiv');
      elem.style.position = 'absolute';
      elem.style.top = '0px';
      elem.style.left = '0px';
      var height = 450;//parseInt(this.mouseoverdiv.clientHeight);
      var width = parseInt(this.mouseoverdiv.clientWidth);
      elem.style.height = height + 'px';
      elem.style.width = width + 'px';
      this.mouseoverdiv.insertBefore(elem, this.mouseoverdiv.firstChild );

      this.chain = new CLookChainWithForeignMap( this.getElemName('chaindiv'), width, height,
                                                 gLook_minx, gLook_maxx, gLook_miny, gLook_maxy );
      this.chain.startbytrain( trainid );
    },
    hidetraininfo: function(){
      if( $(this.getElemName('popup_traininfo')) )
        this.mouseoverdiv.removeChild( $(this.getElemName('popup_traininfo')) );
    },
    getMoreInfoIcon: function(obj, index){
      var moreinfoimgdiv = document.createElement('div');
      moreinfoimgdiv.style.position = 'absolute';
      moreinfoimgdiv.style.top = '0px';
      moreinfoimgdiv.style.right = '1px';
      var moreinfoimg = document.createElement('img');
      moreinfoimg.width= 10;
      moreinfoimg.height= 10;
      moreinfoimg.style.padding= '1px';
      moreinfoimg.src= gImagePath + 'look/moreinfo.gif';
      moreinfoimgdiv.appendChild(moreinfoimg);
      if( obj.bodyfunction ){
        if( obj.bodyfunction == "stboard" ){
          moreinfoimgdiv.onmouseover = this.startstboard.bind( this, index );
          moreinfoimgdiv.onmouseout = this.stopstboard.bind( this, index );
        }
      }else if( obj.train ){
        moreinfoimgdiv.onmouseover = this.showtraininfo.bind( this, index );
        moreinfoimgdiv.onmouseout = this.hidetraininfo.bind( this );
      }
      return moreinfoimgdiv;
    },
    getDestIcon: function(obj, index){
      var destimgdiv = document.createElement('div');
      destimgdiv.style.position = 'absolute';
      destimgdiv.style.top = '0px';
      destimgdiv.style.right = '13px';
      destimgdiv.onclick = this.takedestination.bind( this, index );
      var destimg = document.createElement('img');
      destimg.width= 10;
      destimg.height= 10;
      destimg.title= 'Take as destination';
      destimg.style.padding= '1px';
      destimg.src= gImagePath + 'look/destination.gif';
      destimgdiv.appendChild(destimg);
      return destimgdiv;
    },
    getStartIcon: function(obj, index){
      var startimgdiv = document.createElement('div');
      startimgdiv.style.position = 'absolute';
      startimgdiv.style.top = '0px';
      startimgdiv.style.right = '25px';
      startimgdiv.onclick = this.takestart.bind( this, index );
      var startimg = document.createElement('img');
      startimg.width= 10;
      startimg.height= 10;
      startimg.title= 'Take as start location';
      startimg.style.padding= '1px';
      startimg.src= gImagePath + 'look/start.gif';
      startimgdiv.appendChild(startimg);
      return startimgdiv;
    },
    getObjectTitle: function( obj, index ){
      var titlediv = document.createElement('div');
      if( obj.titleClassName )
        titlediv.className = obj.titleClassName;
      else
        titlediv.className = "look_popup_title";
      titlediv.id = this.getElemName( "popuptitle_"+index);
      if(obj.icon) titlediv.appendChild( this.getIcon(obj, index) );
      titlediv.appendChild( this.getTitleText(obj, index) );
      if(obj.bodyfunction || obj.train ) titlediv.appendChild( this.getMoreInfoIcon(obj, index) );
      if(obj.destination) titlediv.appendChild( this.getDestIcon(obj, index) );
      if(obj.startlocation) titlediv.appendChild( this.getStartIcon(obj, index) );
      return titlediv;
    },
    getObjectTitleTrainInfo: function( obj, index ){
      var titlediv = document.createElement('div');
      titlediv.className = "look_popup_title_traininfo";
      titlediv.id = this.getElemName( "popuptitle_"+index);
      titlediv.appendChild( this.getTitleText(obj, index) );
      return titlediv;
    },
    takestart: function( index ){
      initHafasMap();
      window.location.href = gUrlTravelPlannerAbsolute +
          gCatenateRequiredParameters +
          'performGis=1&' +
          'REQMapTask=MAPINPUT&' +
          'REQMapClickLocationType=' + (this.currentobjects[index].kind=='2_look_stop'?'STATION&':'POI&') +
          'REQMapClickPixelPosX='+this.currentobjects[index].x+'&' +
          'REQMapClickPixelPosY='+this.currentobjects[index].y+'&' +
          'REQMapClickAction=TAKE_AS_START0&' +
          '' ;
    },
    takedestination: function( index ){
      initHafasMap();
      window.location.href = gUrlTravelPlannerAbsolute +
          gCatenateRequiredParameters +
          'performGis=1&' +
          'REQMapTask=MAPINPUT&' +
          'REQMapClickLocationType=' + (this.currentobjects[index].kind=='2_look_stop'?'STATION&':'POI&') +
          'REQMapClickPixelPosX='+this.currentobjects[index].x+'&' +
          'REQMapClickPixelPosY='+this.currentobjects[index].y+'&' +
          'REQMapClickAction=TAKE_AS_DEST0&' +
          '' ;
    },
    startstboard: function( index ){
      if( this.activebodyfunctionindex == -1 ){
        if( this.currentobjects[index] ){
          var stationname = this.currentobjects[index].title.replace(/&ouml;/g, 'ö');
          var stationname = stationname.replace(/&auml;/g, 'ä');
          var stationname = stationname.replace(/&uuml;/g, 'ü');
          var stationname = stationname.replace(/&Auml;/g, 'Ä');
          var stationname = stationname.replace(/&Ouml;/g, 'Ö');
          var stationname = stationname.replace(/&Uuml;/g, 'Ü');
          var stationname = stationname.replace(/&apos;/g, '\'');
          var stationname = stationname.replace(/&#252;/g, 'ü');
          var stationname = stationname.replace(/&#246;/g, 'ö');
          var stationname = stationname.replace(/&#228;/g, 'ä');
          var stationname = stationname.replace(/&#223;/g, 'ß');
          var stationname = stationname.replace(/&#196;/g, 'Ä');
          var stationname = stationname.replace(/&#214;/g, 'Ö');
          var stationname = stationname.replace(/&#220;/g, 'Ü');
          var stationname = stationname.replace(/&#248;/g, 'ø');
          var stationname = stationname.replace(/&#216;/g, 'Ø');
          var stationname = stationname.replace(/&#229;/g, 'å');
          var stationname = stationname.replace(/&#197;/g, 'Å');
          var stationname = stationname.replace(/&#230;/g, 'æ');
          var stationname = stationname.replace(/&#198;/g, 'Æ');
          var stationname = stationname.replace(/&#233;/g, 'é');
          var stationname = stationname.replace(/&#232;/g, 'è');
          var stationname = stationname.replace(/&#156;/g, 'œ');
          var stationname = stationname.replace(/&#140;/g, 'Œ');
          var stationname = stationname.replace(/&#/g, '');
          createStBoardTicker3( this.currentobjects[index].extId, stationname, "dep", 1,
                                parseInt($(this.getElemName('popup')).style.left)
                                 + parseInt($(this.getElemName('popup')).clientWidth)
                                 - 18,
                                parseInt($(this.getElemName('popup')).style.top));
          this.activebodyfunctionindex = index;
        }
      }
    },
    stopstboard: function( index ){
      if( this.activebodyfunctionindex != -1 && stBoards[1] ){
        stBoards[1].deleteStBoard();
        this.activebodyfunctionindex = -1;
      }
    },
    posInObject: function(x,y,mousobject){
      if( parseInt(mousobject.x) - parseInt(mousobject.radius) <= x
       && parseInt(mousobject.x) + parseInt(mousobject.radius) >= x
       && parseInt(mousobject.y) - parseInt(mousobject.radius) <= y
       && parseInt(mousobject.y) + parseInt(mousobject.radius) >= y )
        return true;
      return false;
    },
    mouseevent: function( mouseEvent ){
      if( typeof gZoomByWinRunning != 'undefined' && gZoomByWinRunning ) return;
      if ( !mouseEvent ){
        mouseEvent = window.event;
      }
      this.currentx = mouseEvent.clientX - (absLeft(this.mouseoverdiv)-getScrollPos().x);
      this.currenty = mouseEvent.clientY - (absTop(this.mouseoverdiv)-getScrollPos().y);
//      if(ie6==true && ie7==false) this.currenty += 16;
//      $('look_statusdiv').innerHTML = this.currentx + ',' + this.currenty;
      if( $(this.getElemName('popup')) ){
        if( help.posInElementId( mouseEvent.clientX, mouseEvent.clientY, this.getElemName('popup') ) == true ){
          return;
        }
        this.mouseoverdiv.removeChild($(this.getElemName('popup')));
        this.hidetraininfo();
        this.stopstboard();
        initHafasMap();
      }
      this.currentobjects = new Array;
      if(this.Elems){
        var kindkeys = this.Elems.keys().sort();
        for( var i = 0; i < kindkeys.length; ++i ){
          this.Elems[kindkeys[i]].each( this.radiustest.bind( this ) );
        }
      }
      this.showcurrentobjects();
    },
    radiustest: function( radiuspair ){
      this.currentradius = parseInt( radiuspair.key );
      radiuspair.value.each( this.xtest.bind( this ) );
    },
    xtest: function( xpair ){
      var x = parseInt( xpair.key );
      if( ((x - this.currentradius) <= this.currentx) && ((x + this.currentradius) >= this.currentx )){
        xpair.value.each( this.ytest.bind( this ) );
      }
    },
    ytest: function( ypair ){
      var y = parseInt( ypair.key );
      if( (y - this.currentradius) <= this.currenty && (y + this.currentradius) >= this.currenty ){
        for( var i = 0; i < ypair.value.length; ++i ){
          this.currentobjects[this.currentobjects.length] = ypair.value[i];
        }
      }
    },

    addElem: function( mouseeventelem ){
      var x = mouseeventelem.x;
      var y = mouseeventelem.y;
      var radius = mouseeventelem.radius;
      var kind = mouseeventelem.kind;
      if( typeof this.Elems == 'undefined' )
        this.Elems = $H();
      if( typeof this.Elems[kind] == 'undefined' ){
        this.Elems[kind] = $H();
      }
      if( typeof this.Elems[kind][radius] == 'undefined' )
        this.Elems[kind][radius] = $H();
      if( typeof this.Elems[kind][radius][x] == 'undefined' )
        this.Elems[kind][radius][x] = $H();
      if( typeof this.Elems[kind][radius][x][y] == 'undefined' )
        this.Elems[kind][radius][x][y] = new Array;
      this.Elems[kind][radius][x][y][this.Elems[kind][radius][x][y].length] = mouseeventelem;
    },
    removeKind: function( kind ){
      if(this.Elems) this.Elems.remove( kind );
    }
/*    ,
    removeElem: function( mouseeventelem ){
      var x = mouseeventelem.x;
      var y = mouseeventelem.y;
      var radius = mouseeventelem.radius;
      for( var shift = false, i = 0, len = this.Elems[radius][x][y].length; i < len; ++i ){
        if( shift == true ){
          this.Elems[radius][x][y][i-1] = this.Elems[radius][x][y][i];
        } else if( this.Elems[radius][x][y][i] == mouseeventelem ){
          shift = true;
        }
      }
      this.Elems[radius][x][y].pop();
      if( len != this.Elems[radius][x][y].length+1 ) alert( 'Error');
      if( this.Elems[radius][x][y].length == 0 ){
        this.Elems[radius][x].remove(y);
      }
      if( this.Elems[radius][x].values().length == 0 ){
        this.Elems[radius].remove(x);
      }
      if( this.Elems[radius].values().length == 0 ){
        delete this.Elems[radius];
      }
    }  */
  }
}

