
function TMapApp(containerId, controlContainerId, appOptions)
{
  this.actionCounter = 0;
  this.icons = new Object();
  this.containerId = containerId;
  this.controlContainerId = controlContainerId;
  this.options = appOptions;
  this.map = this.initMap_();
  this.markerManager = this.initMarkerManager_();
}

TMapApp.prototype.initMap_ = function ()
{
  var mapOptions;
  if (this.options != undefined && this.options.mapTypes != undefined) {
    mapOptions = {
  	  mapTypes : this.options.mapTypes
    }
  } else {
  	mapOptions = {}
  }
  var defaultZoom = 12;
  var defaultMapType = null;
  var map = new GMap2(document.getElementById(this.containerId), mapOptions);
  if (this.options != undefined) {
    if (this.options.doubleClickZoom != undefined) {
      if (this.options.doubleClickZoom) {
	      map.enableDoubleClickZoom();
	    } else {
	      map.disableDoubleClickZoom();
	    }
	  }
	  if (this.options.draggable != undefined) {
      if (this.options.draggable) {
	      map.enableDragging();
	    } else {
	      map.disableDragging();
	    }
    }
	  if (this.options.infoWindows != undefined) {
      if (this.options.infoWindows) {
  	    map.enableInfoWindow();
	    } else {
	      map.disableInfoWindow();
	    }
    }
	  if (this.options.continuousZoom != undefined) {
      if (this.options.continuousZoom) {
  	    map.enableContinuousZoom();
	    } else {
	      map.disableContinuousZoom();
	    }
    }
	  if (this.options.scrollWheelZoom != undefined) {
      if (this.options.scrollWheelZoom) {
	      map.enableScrollWheelZoom();
	    } else {
	      map.disableScrollWheelZoom();
	    }
    }
	  if (this.options.defaultMapType != undefined) {
      defaultMapType = this.options.defaultMapType;
    }
	  if (this.options.defaultZoom != undefined) {
      defaultZoom = this.options.defaultZoom;
    }
  }
  map.setCenter(new GLatLng(47.69615, 13.33245), 6);
  return map;
};

TMapApp.prototype.initMarkerManager_ = function() {
  var mmOptions = {
      borderPadding: 100,
      maxZoom: 15,
      trackMarkers: false };
  var markerManager = new MarkerManager(this.map, mmOptions, this);
  return markerManager;
};

TMapApp.prototype.initControls = function(controlOptions) {
  if (controlOptions.showMapControl) {
    if (controlOptions.mapControlType == undefined) {
      this.map.addControl(new GLargeMapControl());
    } else if (controlOptions.mapControlType == "ZOOM") {
      this.map.addControl(new GSmallZoomControl());
    } else if (controlOptions.mapControlType == "SMALL") {
      this.map.addControl(new GSmallMapControl());
    } else if (controlOptions.mapControlType == "LARGE"){
      this.map.addControl(new GLargeMapControl());
    }
  }
  
  if (controlOptions.showTypeControl) {
    if (controlOptions.mapTypeControlType === undefined) {
      this.map.addControl(new GMapTypeControl(controlOptions.useShortNames));
    } else if (controlOptions.mapTypeControlType == "NORMAL") {
      this.map.addControl(new GMapTypeControl(controlOptions.useShortNames));
    } else if (controlOptions.mapTypeControlType == "MENU") {
      this.map.addControl(new GMenuMapTypeControl(controlOptions.useShortNames)); 
    } else if (controlOptions.mapTypeControlType == "HIERARCHICAL") {
      this.map.addControl(new GHierarchicalMapTypeControl());
    }
  }
  
  if (controlOptions.showScale) {
    this.map.addControl(new GScaleControl());
  }
  if (controlOptions.showOverviewMap) {
    this.map.addControl(new GOverviewMapControl());
  }
  
  
  
  
  
  
  
  
  
  
  
  
  
};


TMapApp.prototype.addIcon = function(iconId, icon)
{
  this.icons[iconId] = icon;
};

TMapApp.prototype.addMarker = function(latitude, longitude, opt_title, opt_iconId, opt_infoBoxContents, opt_number)
{
  var marker;
  if (opt_number == undefined) {
    var markerOptions = {
      icon: this.icons[opt_iconId],
      title: opt_title,
      clickable: opt_infoBoxContents != undefined,
	  draggable: false
    }
    marker = new GMarker(new GLatLng(latitude, longitude), markerOptions);
  } else {
    var icon = this.icons[opt_iconId];
    var offset = icon.labelOffset1Char;
    var opt_labelClass = "LabeledMarker_markerLabel";
    if (("" + opt_number).length > 2 && icon.labelOffset3Char) {
      offset = icon.labelOffset3Char;
      opt_labelClass = "LabeledMarker_markerLabelSmall";
    } else if (("" + opt_number).length > 1 && icon.labelOffset2Char) {
      offset = icon.labelOffset2Char;
    }
    var markerOptions = {
      icon: icon,
      title: opt_title,
      clickable: opt_infoBoxContents != undefined,
	    draggable: false,
	    labelText: opt_number,
	    labelClass: opt_labelClass,
	    labelOffset: offset
    }
    marker = new TLabeledMarker(new GLatLng(latitude, longitude), markerOptions);
  }

  if (opt_infoBoxContents != undefined) {
    marker.bindInfoWindowHtml(opt_infoBoxContents);
  }
  //GEvent.addListener(marker, "click", function() {
    
  //});
    
  this.markerManager.addMarker(marker, 0);
  return marker;
};

TMapApp.prototype.addMarkerCallback = function(latitude, longitude, opt_title, opt_iconId, opt_infoBoxContentsURL, opt_layer) 
{
  var marker;
  var markerOptions = {
    icon: this.icons[opt_iconId],
    title: opt_title,
    clickable: (opt_infoBoxContentsURL != undefined),
	  draggable: false
  }
    
  marker = new GMarker(new GLatLng(latitude, longitude), markerOptions);
  if (opt_infoBoxContentsURL != undefined) {
    GEvent.addListener(marker, "click", function() {
      if (!marker.infoWindowLoaded) {
        GDownloadUrl(opt_infoBoxContentsURL, function(data, responseCode) {
          marker.infoWindowLoaded = true;
          marker.bindInfoWindowHtml(data);
          marker.openInfoWindowHtml(data);
        });
      } 
    });
  }
  this.markerManager.addMarker(marker, 0);
  return marker;
}

TMapApp.prototype.createMarker = function(latitude, longitude, opt_title, opt_iconId, opt_infoBoxContents, opt_number) 
{
  var marker;
  var offset = this.icons[opt_iconId].labelOffset1Char;
  if (opt_number) { 
    var opt_labelClass = "LabeledMarker_markerLabel";
    if (("" + opt_number).length > 2 && this.icons[opt_iconId].labelOffset3Char) {
      offset = this.icons[opt_iconId].labelOffset3Char;
      opt_labelClass = "LabeledMarker_markerLabelSmall";
    } else if (("" + opt_number).length > 1 && this.icons[opt_iconId].labelOffset2Char) {
      offset = this.icons[opt_iconId].labelOffset2Char;
    }
    var markerOptions = {
      icon: this.icons[opt_iconId],
      title: opt_title,
      clickable: opt_infoBoxContents != undefined,
	    draggable: false,
	    labelText: opt_number,
	    labelClass: opt_labelClass,
	    labelOffset: offset
    }
    marker = new TLabeledMarker(new GLatLng(latitude, longitude), markerOptions);
    if (opt_infoBoxContents) {
      marker.bindInfoWindowHtml(opt_infoBoxContents);
    }
  } else {
    var markerOptions = {
      icon: this.icons[opt_iconId],
      title: opt_title,
      clickable: opt_infoBoxContents != undefined,
	    draggable: false
    }
    marker = new TLabeledMarker(new GLatLng(latitude, longitude), markerOptions);
  }
  
  return marker;
}

TMapApp.prototype.createMarkerCallback = function(latitude, longitude, opt_title, opt_iconId, opt_infoBoxContentsURL) 
{
  var marker;
  var markerOptions = {
    icon: this.icons[opt_iconId],
    title: opt_title,
    clickable: (opt_infoBoxContentsURL != undefined),
	  draggable: false
  }
  marker = new GMarker(new GLatLng(latitude, longitude), markerOptions);
  if (opt_infoBoxContentsURL != undefined) {
    GEvent.addListener(marker, "click", function() {
      if (!marker.infoWindowLoaded) {
        GDownloadUrl(opt_infoBoxContentsURL, function(data, responseCode) {
          marker.infoWindowLoaded = true;
          marker.bindInfoWindowHtml(data);
          marker.openInfoWindowHtml(data);
        });
      } 
    });
  }
  return(marker);
}

TMapApp.prototype.addPointToBounds = function(latitude, longitude) {
  if (this.bounds == undefined) {
    this.bounds = new GLatLngBounds();
  }
  this.bounds.extend(new GLatLng(latitude, longitude));
}

TMapApp.prototype.adaptZoomAndCenterToBounds = function() {
  this.map.checkResize();
  if (this.bounds == undefined) {
    return false;
  }
  this.map.setCenter(this.bounds.getCenter());
  this.map.setZoom(this.map.getBoundsZoomLevel(this.bounds));
}

TMapApp.prototype.setCenterAndZoom = function(latitude, longitude, zoom) {
  var newCenter = new GLatLng(latitude, longitude);
  this.map.setCenter(newCenter, zoom);
}

TMapApp.prototype.toggleLayer = function(layerId, opt_subLayerId, opt_variant, opt_adaptZoom) {
  var that = this;
  if (!that.markerManager.layerConfig.layers[layerId].loaded) {
    that.loadLayer(layerId, null, null, opt_adaptZoom);
  } else {
    that.startAction();
    that.markerManager.toggleLayer(layerId, opt_subLayerId, opt_variant);
    that.stopAction();
  }
}

TMapApp.prototype.loadLayer = function(layerId, opt_sublayerId, opt_variantId, opt_adaptZoom) {
  this.startAction();
  var that = this;
  var getParams = "?zoom=" + this.map.getZoom()
    + "&langId="   + this.datasource.languageId 
    + "&layoutId=" + this.datasource.layoutId
    + "&configId=" + this.config
    + "&layerId="  + layerId
    + "&objectId="  + this.datasource.objectId
    + "&nolink=" + this.datasource.nolinks;
  if (this.origin) { 
    getParams += ( "&gisXCoord=" + this.origin.lat()
    + "&gisYCoord=" + this.origin.lng());
  }
    
    
  var url = this.datasource.url + getParams;
  var xmlLoader = new TGeoXml(url, this, null);
  xmlLoader.parse(layerId, "INIT", function() {
    that.markerManager.layerConfig.layers[layerId].loaded = true;
    that.toggleLayer(layerId, opt_sublayerId, opt_variantId);
    if (opt_adaptZoom) {
      that.adaptZoomAndCenterToBounds();
    } 
    that.stopAction();
  });
}

TMapApp.prototype.addLayer = function(layerId, subLayerId, variantId, isDefault) {
  if (!subLayerId && !variantId) {
    var layer = new Object();
    layer.layerId = layerId;
    layer.visible = false;
    layer.loaded = false;
    layer.hasSublayers = false;
    layer.hasVariants = false;
    layer.markers = [];
    this.markerManager.layerConfig.layers[layerId] = layer;
  } else if (subLayerId) {
    var layer;
    for (id in this.markerManager.layerConfig.layers) {
      if (layerId == this.markerManager.layerConfig.layers[id].layerId) { 
        layer = this.markerManager.layerConfig.layers[id];
      }
    }
    if (layer) {
      if (!layer.sublayers) {
        layer.hasSublayers = true;
        layer.sublayers = new Object();;
      }
      var sublayer = new Object();
      sublayer.layerId = subLayerId;
      sublayer.visible = false;
      sublayer.markers = [];
      layer.sublayers[subLayerId] = sublayer;
    } else {
      alert("No layer for sublayer found");
    } 
  } else if (variantId) {
    var layer;
    for (id in this.markerManager.layerConfig.layers) {
      if (layerId == this.markerManager.layerConfig.layers[id].layerId) { 
        layer = this.markerManager.layerConfig.layers[id];
      }
    }
    if (layer) {
      if (!layer.variants) {
        layer.hasVariants = true;
        layer.variants = new Object();
      }
      var variant = new Object();
      variant.variantId = variantId;
      variant.visible = false;
      variant.markers = [];
      if (isDefault) {
        variant.isDefault = true;
      }
      layer.variants[variantId] = variant;
    } else {
      alert("No layer for variant found");
    }
  }
}

TMapApp.prototype.syncronizeWithControl = function() 
{
  //alert("Synchronizing with control panel");
  for (id in this.markerManager.layerConfig.layers) {
    var alertText = null;
    alertText = "Layer: " + id;
    alertText += document.getElementsByName(id)[0];
    if (id == "ACC") {
      for (subId in this.markerManager.layerConfig.layers[id].sublayers) {
        if (document.getElementsByName(subId)[0]) {
          if (!document.getElementsByName(subId)[0].checked) {
          }
        }
      }
    } else if (document.getElementsByName(id)[0]) {
      alertText += " :: " + document.getElementsByName(id)[0].checked;
      if (document.getElementsByName(id)[0].checked) {
        this.toggleLayer(id);
      }
    }
  }
}
 
TMapApp.prototype.startAction = function() 
{
  this.actionCounter++;
  
  if (this.actionCounter > 0) {
    var elem = document.getElementById("busyIcon");
    if (elem) {
      elem.style.visibility = "visible";
    }
  }
}

TMapApp.prototype.stopAction = function() 
{
  this.actionCounter--;
  if (this.actionCounter <= 0) {
    var elem = document.getElementById("busyIcon");
    if (elem) {
      elem.style.visibility = "hidden";
    }
  }
}


