//globals
var mapManager;
var map;
var geocoder;
var start;
var wz1;
var wz2;
var graphicDiv;
var markerDiv;
var tabDiv;
var optionsSelector;
var markerOptions;
var graphText;
var SWIM;
var BIKE;
var RUN;
var input_time_h;
var input_time_m;
var input_time_s;
var input_speed;
var input_alt;
var input_sum_alt;
var input_dist;
var form_details;
var form_sum;



function initialize() {
  if (GBrowserIsCompatible()) {
    map = new google.maps.Map2(document.getElementById("map"));
    map.addControl(new google.maps.LargeMapControl());
    map.addControl(new google.maps.MapTypeControl());
    map.enableDoubleClickZoom();
    map.enableContinuousZoom();
    map.enableScrollWheelZoom();
    map.addMapType(G_SATELLITE_3D_MAP);
    map.addMapType(G_PHYSICAL_MAP);
    map.setMapType(G_SATELLITE_MAP);
    geocoder = new google.maps.ClientGeocoder();
    directions = new google.maps.Directions(map);
    start = new google.maps.LatLng(49.1698, 8.617);
    wz1 = new google.maps.LatLng(49.17057, 8.61795);
    wz2 = new google.maps.LatLng(49.216, 8.6581);
    graphicDiv = document.getElementById("graphic");
    graphText = document.getElementById("graph_text");
    markerDiv = document.getElementById("marker");
    tabDiv = document.getElementById("tab");
    optionsSelector = document.getElementById("options");
    input_speed = document.getElementById("speed");
    input_time_h = document.getElementById("time_h");
    input_time_m = document.getElementById("time_m");
    input_time_s = document.getElementById("time_s");
    input_dist = document.getElementById("distance");
    input_alt = document.getElementById("altitude");
    input_sum_alt = document.getElementById("sum_altitude");
    form_details = document.getElementById("position_details");
    form_sum = document.getElementById("form_sum");
    selectOverview();
    markerOptions = {title:"START"};
    var marker = new google.maps.Marker(start, markerOptions);
    map.addOverlay(marker);
    markerOptions = {title:"WZ1"};
    marker = new google.maps.Marker(wz1, markerOptions);
    map.addOverlay(marker);
    markerOptions = {title:"WZ2"};
    marker = new google.maps.Marker(wz2, markerOptions);
    map.addOverlay(marker);
    SWIM = createIcon("Schwimmer.gif", 17, 60);
    BIKE = createIcon("Fahrrad.gif", 36, 42);
    RUN = createIcon("Laeufer.gif", 26, 43);
  }
}

function createMapManager(){
	return new MapManager(this.map, this.jg, this.graphicDiv, this.markerDiv, this.optionsSelector, this.input_time_h, this.input_time_m, this.input_time_s, this.input_speed, this.input_alt, this.input_sum_alt, this.input_dist, this.form_details, this.form_sum, this.graphText);
}

function createIcon(fileName, height, width){
  var baseIcon = new google.maps.Icon();
  //baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
  if(height != null && width !=null)
    baseIcon.iconSize = new GSize(60, 60);
  baseIcon.shadowSize = new GSize(height, width);
  baseIcon.iconAnchor = new GPoint(0, 34);
  baseIcon.infoWindowAnchor = new GPoint(9, 2);
//  baseIcon.infoShadowAnchor = new GPoint(0, 30);
  baseIcon.image = "http://test.challenge-kraichgau.com/images/map/" + fileName;
//  baseIcon.image = "http://localhost/images/map/" + fileName;
  return baseIcon;
}

function selected(method, script){
  tabDiv.style.cursor= "wait";
  unload();
  if(!method)
    loadjsfile("/js/"+script);
  else
    method();
}

function setMapManager(manager){
  this.mapManager = manager;
  tabDiv.style.cursor= "default";
}

function unload(){
	if(mapManager!= null){
	 mapManager.unload();
	 mapManager = null;
	}
}

function bodyUnload(){
	unload();
	GUnload();
	mapManager = null;
  map = null;
  geocoder = null;
  start = null;
  wz1 = null;
	wz2 = null;
	graphicDiv = null;
	markerDiv = null;
	optionsSelector = null;
	markerOptions = null;
	SWIM = null;
	BIKE = null;
	RUN = null;
}

function optionSelected(event){
	mapManager.selectionChanged(optionsSelector.selectedIndex);
}

function overviewSelected(){
	unload();
	selectOverview();
}

function selectOverview(){
  map.setCenter(new google.maps.LatLng(49.192, 8.643), 13);
}

function positionChanged(event){
	mapManager.positionChanged(event);
}

function loadjsfile(filename){
  var fileref=document.createElement('script');
  fileref.setAttribute("type","text/javascript");
  fileref.setAttribute("src", filename);
  if (typeof fileref!="undefined")
    document.getElementsByTagName("head")[0].appendChild(fileref)
}

function speedChanged(){
	mapManager.speedChanged();
}
