imx.Mapwork = imx.Mapwork || {};

imx.Mapwork = {
  /**
   * Constants defining the different maptypes, that can be shown
   * 
   * @type object
   */
  mapTypeId: {
    ROADMAP: 1,
    SATELLITE: 2,
    HYBRID: 3,
    TERRAIN: 4
  },
  
  /**
   * These vendors are currently supported by the mapwork.
   * 
   * @type object
   */
  vendorId: {
    GOOGLE: 1,
    ALPSTEIN: 2
  },
  
  /**
   * Herein lies the basepath to this file. Needed for including other files.
   * 
   * @type String
   */
  _basePath: null,

  /**
   * Includes the file corresponding to given "namespace"
   * 
   * @param {String} filePath the namespace of the desired object
   */
  require: function(filePath) {
    var basePath = imx.Mapwork._basePath;
    if (null == basePath) {
      basePath = imx.Mapwork._findBasePath();
    }
  
    imx.include_once(basePath + filePath + '.js');
  },

  /**
   * Tries to detect the base path of the Mapwork.js file
   * 
   * @private
   * @return the base path to this file, if successful
   * @type String
   */
  _findBasePath: function() {
    jQuery('script').each(function() {
      var src = jQuery(this).attr('src');

      if (!imx.isTypeOf('undefined', src)) {
        var questionMarkIndex = src.lastIndexOf('?');
        var lastCharOfPathIndex = questionMarkIndex == -1 ? src.length : questionMarkIndex;

        if (src.substr(lastCharOfPathIndex - 10, 10) == 'Mapwork.js') imx.Mapwork._basePath = src.substr(0, lastCharOfPathIndex - 10);
      }
    });

    return imx.Mapwork._basePath;
  },

  /**
   * Creates and returns the map object for further implementation
   * 
   * @param {String} id in which element this map should be rendered into
   * @param {imx.Mapwork.Configuration} configuration the configuration object for this map
   * @param {Boolean} [isMiniMap=false] if this map is very small, use this to hide all controls
   * @return the vendor specific map object
   * @type imx.Mapwork.Map
   */
  createMap: function(id, configuration, isMiniMap) {
    var map = null;
    isMiniMap = isMiniMap || false;

    switch (configuration.getVendorId()) {
      case imx.Mapwork.vendorId.GOOGLE:
        imx.Mapwork.require('map/Google');
        map = new imx.Mapwork.Map.Google(id, configuration);
        break;

      case imx.Mapwork.vendorId.ALPSTEIN:
        imx.Mapwork.require('map/Alpstein');
        map = new imx.Mapwork.Map.Alpstein(id, configuration);
        break;

      default:
        imx.error('vendor not yet implemented: ' + configuration.configuration.getVendorId());
        break;
    }

    map.loadMap();
    map.isMiniMap(isMiniMap);
    
    return map;
  },
  
  /**
   * Easter-Egg for Axel.
   * You requested it, now you got it!
   * 
   * @param {String} what type of the cupholder
   */
  cupholder: function(what) {
    var uri = 'http://www.google.com/images?q=cupholder';
    if ('' != what) {
      uri += '+' + what.replace(' ', '+');
    }
    window.open(uri, '_blank').focus();
  }
};

imx.Mapwork.require('Configuration');

if ('object' != typeof(imx)) alert('GNAAAAAAH! MISSING imx!!!');
