// $Id: MapContext.Control.js,v 1.1.2.3 2008/10/24 10:11:06 openwereld Exp $

MappingKit.Control.MapContext = OpenLayers.Class(OpenLayers.Control, {
  /**
   * Property: type
   * TYPE_BUTTON.
   */
  type: OpenLayers.Control.TYPE_BUTTON,
  
  /*
   * Method: trigger
   * Return web map context.
   */
  trigger: function() {
    if (this.map) {
      var wmc_options = {'layerOptions': {buffer: 0}};
      var wmc = new OpenLayers.Format.MapContext(wmc_options);
      var uri = '';
      if (!this.map.title) {
        this.map.title = Drupal.t('Unknown');
      }
      try {
        var text = wmc.write(this.map, {});
        
        // write text to local file
        if (!MappingKit.settings.fileDirectoryTemp) {
          alert(Drupal.t('The temporary directory of this site is not accessible.'));
        }
        else {
          uri = Drupal.settings.basePath + 'sites/all/modules/mappingkit/serializer.php?'
            + 'file_directory_temp=' + MappingKit.settings.fileDirectoryTemp
            + '&base_path=' + Drupal.settings.basePath;
          jQuery.post(uri, {postBody: text}
            , function(response) {
                data = Drupal.parseJson(response);
                window.open(data.fileUrl, 'WMC');
              });
        }
      }
      catch (err) {
        alert('wmc error: ' + err.message + '\nuri: ' + uri);
      }
    }
  },

  CLASS_NAME: "mk.Control.MapContext"
});

