var IPCMap = function() {
  var oThis = this;
  return {
/*
  var map;
  var params = {};
  var callback;
  var tick;
*/
    init: function() {
      if (this.oMap) { return; }
      if (! this.buildmap()) {
//        jQuery.scope(this, function() {jQuery.timer(1000, function (timer) { this.buildmap(); }) });
        var executer = new PeriodicalExecuter(this.buildmap.bind(this), 1);
      }
    },
    buildmap: function(timer) {
      var oContentMap = $(this.map).contentWindow;
      if ( ! oContentMap || ! oContentMap.loaded || ! oContentMap.CreateMap ) {
        return false;
      }
      this.oContentMap = oContentMap;
      if (timer) { timer.stop(); }
      var oMap = oContentMap.CreateMap(this.callback, this.tick);
      this.oMap = oMap;
      oContentMap.CreateSlider(oMap);
      oMap.icon_type = this.params.icon_type || 1;
      oMap.Scale = this.params.map_scale || 1;
      oMap.Cursor = false;
      if (this.params.icon_lon && this.params.icon_lat) {
        this.setIcon(this.params.icon_lon, this.params.icon_lat,
          ! (this.params.map_lon && this.params.map_lat));
        oMap.Scroll( this.params.icon_lon, this.params.icon_lat );
      } else if (this.params.map_lon && this.params.map_lat) {
        oMap.Scroll( this.params.map_lon, this.params.map_lat );
      }
      oMap.ExtParams = 'COL=1';
      this.AdjustToView(oContentMap);
      return true;
    },
    callback: function(e) {},
    change_icon: function(icon_type) {
      this.oMap.icon_type = icon_type;
    },
    getParams: function() {
      return this.oMap ? {
        map_lon: this.oMap.Longitude,
        map_lat: this.oMap.Latitude,
        map_scale: this.oMap.Scale
      } : this.params;
    },
    setIcon: function(lon, lat, scroll) {
      var oLonLat = this.oMap.CreateLonLatInt( lon, lat );
      this.oContentMap.CreateFlag(this.oMap, oLonLat);
      if (scroll) { this.oMap.Scroll( lon, lat ); }
      this.callback({Longitude: lon, Latitude: lat});
      this.AdjustToView(this.oContentMap);
    },
    AdjustToView: function(map) {
      var oMap = this.oMap;
      var param = $H( {
        MAP: this.oMap.LonLatIntToStr10(oMap.CreateLonLatInt(oMap.Longitude, oMap.Latitude)),
        SZ: map.CMN_GetWindowWidth(oMap.window) + ',' + map.CMN_GetWindowHeight(oMap.window),
        ZM: 13 - oMap.Scale
      }).toQueryString();
      var ajaxRequest = new Ajax.Request('./mapexist.php', {
        method:'get',
        parameters: param,
        asynchronous: true,
        onComplete: function(r){
          oMap.Scale = parseInt(r.responseText);
          if (! this.adjusted) { oMap.AdjustToView(); }
          this.adjusted = true;
        }
      });
    },
    getLonLatStr10: function(lon, lat) {
      return this.oMap.LonLatIntToStr10(this.oMap.CreateLonLatInt(lon, lat));
    }
  };
}();
