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

MappingKit.Control.PrintContext = 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('No title');
      }
      try {
        var text = wmc.write(this.map, {});
        
        // write text to local file
        uri = Drupal.settings.basePath + '?q=map2pdf';
        jQuery.ajax({'type': 'POST'
          , 'url': uri
          , 'data': {postBody: text}
          , 'success': function(response) {
            data = Drupal.parseJson(response);
            if (data.fileUrl) {
              window.open(data.fileUrl, 'PDF');
            }
            else {
              alert(data.data);
            }
          }
          , 'error': function (xmlhttp, textStatus, errorThrown) {
            // typically only one of textStatus or errorThrown 
            // will have info
            //this; // the options for this ajax request
            alert(xmlhttp.statusText);
          }
        });
      }
      catch (err) {
        alert('wmc error: ' + err.message + '\nuri: ' + uri);
      }
    }
  },

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

