var gmaps_is_over = false;
var gmaps_is_big  = false;
var to_html = '';
var from_html = '';
var gmarkers = '';



/* region */

function gmaps_load(reg_choice,zoom) {
  if(GBrowserIsCompatible()) {

    var map = new GMap2(document.getElementById("gmaps"));
    map.setMapType(G_PHYSICAL_MAP);
    map.addMapType(G_PHYSICAL_MAP);
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    geocoder = new GClientGeocoder();
    geocoder.getLatLng(reg_choice, function(point) {
      if(point) { map.setCenter(point, zoom); }
    });
    return map;

  } else {
    document.getElementById('gmaps').innerHTML = 'Ihr Browser unterst&uuml;tzt die Kartenfunktion nicht. Um die Karte zu nutzen, verwenden einen anderen Browser.';
  }
}

var point2marker = function(anzeige,i,mid,bild,link,iconcolor) {
  return function(point) {
    if(point) {

      var newIcon = new GIcon(G_DEFAULT_ICON);
      if(iconcolor==1) {
        newIcon.image = "http://www.thuelsfelder-talsperre.de/DE/images/green_icon.png";
      } else {
        newIcon.image = "http://www.thuelsfelder-talsperre.de/DE/images/blue_icon.png";
      }

      var marker = new GMarker(point, {icon:newIcon});

      var htmlcode = '<div style="width:280px;">';
      if(bild!='') { htmlcode += '<div style="float:left; margin-right:5px;"><img src="' + bild + '" alt="" /></div>'; }
      htmlcode += '<div style="float:left; width:160px;">' + anzeige + '<br />';
      if(link!='') { htmlcode += '<a href="' + link + '">Zum Angebot</a></div>'; }
      htmlcode += '<div style="clear:both; margin-bottom:25px;"></div></div>';

      GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(htmlcode);
      });

      map.addOverlay(marker);
      visible_marks[mid][i] = marker;
    }
  }
};

function point2marker_coord(loc_info,iconcolor,bild,anzeige,link) {
  var latlen = loc_info.split(',');
  var point = new GLatLng(latlen[0],latlen[1]);

  var newIcon = new GIcon(G_DEFAULT_ICON);
  if(iconcolor==1) {
    newIcon.image = "http://www.thuelsfelder-talsperre.de/DE/images/green_icon.png";
  } else {
    newIcon.image = "http://www.thuelsfelder-talsperre.de/DE/images/blue_icon.png";
  }

  var marker = new GMarker(point, {icon:newIcon});

  var htmlcode = '<div style="width:280px;">';
  if(bild!='') { htmlcode += '<div style="float:left; margin-right:5px;"><img src="' + bild + '" alt="" /></div>'; }
  htmlcode += '<div style="float:left; width:160px;">' + anzeige + '<br />';
  if(link!='') { htmlcode += '<a href="' + link + '">Zum Angebot</a></div>'; }
  htmlcode += '<div style="clear:both; margin-bottom:25px;"></div></div>';

  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(htmlcode);
  });
  map.addOverlay(marker);

  return marker;
}

function gmaps_setmarker(mid,map,iconcolor) {
  var i = 0;

  if(gicon_flags[mid]==false) {
    geocoder = new GClientGeocoder();

    for(i=0; i<mark_array[mid].length; i++) {
      var anzeige = mark_array[mid][i][1];
      var bild = mark_array[mid][i][2];
      var link = mark_array[mid][i][3];
      if(mark_array[mid][i][4]==1) {
        visible_marks[mid][i] = point2marker_coord(mark_array[mid][i][0],iconcolor,bild,anzeige,link);
      } else {
        geocoder.getLatLng(mark_array[mid][i][0], point2marker(anzeige,i,mid,bild,link,iconcolor));
      }
    }
    document.getElementById('gicon'+mid).className = 'gicon_selected';
    gicon_flags[mid] = true;

  } else {

    for(i=0; i<visible_marks[mid].length; i++) {
      marker = visible_marks[mid][i];
      if(marker) {
        map.removeOverlay(marker);
      }
    }
    visible_marks[mid] = new Array();
    document.getElementById('gicon'+mid).className = 'gicon';
    gicon_flags[mid] = false;

  }
  
  return map;
}

function gmaps_route(map,whichone) {

  if(akt_poly) {
    map.removeOverlay(akt_poly);
  }
  if(whichone.length>0) {
    map = gmaps_make_polyline(map,poly_source[whichone],poly_info[whichone]);
  }

  document.getElementById('gicon9').className = 'gicon';
  var theEl = document.getElementById('gmaps_nordic_walking');
  theEl.selectedIndex = 0;
  for(i=0; i<theEl.length; i++) {
    if(theEl[i].value==whichone) {
      theEl.selectedIndex = i;
      document.getElementById('gicon9').className = 'gicon_selected';
    }
  }

  document.getElementById('gicon10').className = 'gicon';
  var theEl = document.getElementById('gmaps_radtouren');
  theEl.selectedIndex = 0;
  for(i=0; i<theEl.length; i++) {
    if(theEl[i].value==whichone) {
      theEl.selectedIndex = i;
      document.getElementById('gicon10').className = 'gicon_selected';
    }
  }

  return map;
}

function gmaps_make_polyline(map,poly,info) {

  var poly_coord = new Array();
  for(i=0; i<poly.length; i++) {
    poly_coord[i] = new GLatLng(poly[i][0],poly[i][1]);
  }

  var polyline = new GPolyline(poly_coord, info[1], 3, 0.65);
  map.addOverlay(polyline);
  akt_poly = polyline;

  map.setCenter(new GLatLng(info[0][0],info[0][1]), info[0][2]);

  return map;
}



/* detailseite */

function detail_gmaps_load(adresse,anzeige) {
  if(GBrowserIsCompatible()) {

    map = new GMap2(document.getElementById("detail_gmaps2"));
    map.setCenter(new GLatLng(52.84337220006248,8.040962219238281), 14);
    if(gmaps_is_coord==1) {
      var latlng_coord = adresse.split(',');
      var point = new GLatLng(latlng_coord[0],latlng_coord[1]);
      map.setCenter(point, 14);
      var marker = createMarker(point,anzeige);
      map.addOverlay(marker);
    } else {
      geocoder = new GClientGeocoder();
      geocoder.getLatLng(adresse, function(point) {
        if(point) {
          map.setCenter(point, 14);
          var marker = createMarker(point,anzeige);
          map.addOverlay(marker);
        } else {
          document.getElementById('detail_gmaps_main').style.display = 'none';
        }
      });
    }
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.addMapType(G_PHYSICAL_MAP);
    map.enableDoubleClickZoom();
    var pano_layer = new PanoramioLayer(map);
    pano_layer.enable();

    var do_big = function(e) {
      if(gmaps_is_over==true) {
        gmaps_morph(760,650,0,0);
        gmaps_is_big = true;
      }
    }
    var do_small = function(e) {
      if(gmaps_is_over==false) {
        gmaps_morph(285,285,-235,-185);
        gmaps_is_big = false;
      }
    }
    var m_in  = function(e) {
      gmaps_is_over = true;
      if(gmaps_is_big==false) {
        window.setTimeout(do_big, 500);
      }
    }
    var m_out = function(e) {
      gmaps_is_over = false;
      if(gmaps_is_big==true) {
        window.setTimeout(do_small, 300);
      }
    }

    var kl = new YAHOO.util.KeyListener(document, { keys:27 }, { fn:do_small, scope:document, correctScope:true } );
    kl.enable();

    YAHOO.util.Event.addListener("detail_gmaps", "mouseover", m_in);
    YAHOO.util.Event.addListener("detail_gmaps", "mouseout", m_out);
    YAHOO.util.Event.addListener("mainbody", "click", do_small);

  } else {
    document.getElementById('detail_gmaps').innerHTML = 'Ihr Browser unterst&uuml;tzt die Kartenfunktion nicht. Um die Karte zu nutzen, verwenden einen anderen Browser.';
  }
}

function createMarker(point,anzeige) {
  var marker = new GMarker(point);

  // von hier
  to_html = '<h4>Adresse:</h4>' +
            '<div style="margin-bottom:7px;">' + anzeige + '</div>' +
            '<div><span style="font-weight:bold;">Route berechnen:</span> Hierher - <a style="color:#333;" href="javascript:fromhere();">Von hier</a></div>' +
            '<div style="margin-top:5px;font-size:10px;color:#999;">Startadresse</div><form action="http://maps.google.com/maps" method="get" target="_blank" style="display:inline;">' +
            '<div style="padding-bottom:10px;"><input type="text" maxlength="40" name="saddr" id="saddr" value="" style="width:150px;"/>' +
            '<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() + '" />' +
            '<input type="submit" value="Los!" style="margin-left:3px;" /></form></div>';

  // hierher
  from_html = '<h4>Adresse:</h4>' +
              '<div style="margin-bottom:7px;">' + anzeige + '</div>' +
              '<div><span style="font-weight:bold;">Route berechnen:</span> <a style="color:#333;" href="javascript:tohere();">Hierher</a> - Von hier</div>' +
              '<div style="margin-top:5px;font-size:10px;color:#999;">Zieladresse</div><form action="http://maps.google.com/maps" method="get" target="_blank" style="display:inline;">' +
              '<div style="padding-bottom:10px;"><input type="text" maxlength="40" name="daddr" id="daddr" value="" style="width:150px;"/>' +
              '<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() + '" />' +
              '<input type="submit" value="Los!" style="margin-left:3px;" /></form></div>';

  // uebersicht
  html = '<h4>Adresse:</h4>' +
         '<div style="margin-bottom:7px;">' + anzeige + '</div>' +
         '<div style="padding-bottom:10px;">Route berechnen: <a style="color:#333;" href="javascript:tohere();">Hierher</a> - <a style="color:#333;" href="javascript:fromhere();">Von hier</a></div>';

  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml(html);
  });
  gmarkers = marker;

  return marker;
}

function tohere() {
  gmarkers.openInfoWindowHtml(to_html);
}
function fromhere() {
  gmarkers.openInfoWindowHtml(from_html);
}

function gmaps_morph(w,h,r,b) {
  var posi = {
    right: { to: r },
    bottom: { to: b }
  };
  var size = {
    width: { to: w },
    height: { to: h }
  };
  var posi_a = new YAHOO.util.Anim('detail_gmaps2', posi, 0.2);
  var size_a = new YAHOO.util.Anim('detail_gmaps', size, 0.2);
  posi_a.animate(); 
  size_a.animate();
}


/* veranstaltungen */

function va_gmaps_load() {
  if(GBrowserIsCompatible()) {

    map = new GMap2(document.getElementById("va_gmap"));
    map.setCenter(new GLatLng(52.84337220006248,8.040962219238281), 14);
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.addMapType(G_PHYSICAL_MAP);
    map.enableDoubleClickZoom();
    map.enableScrollWheelZoom();

    return map;
  }
}

function va_gmap_set(map,adresse,anzeige) {

  map.clearOverlays();

  geocoder = new GClientGeocoder();
  geocoder.getLatLng(adresse, function(point) {
    if(point) {
      map.setCenter(point, 14);
      var marker = createMarker(point,anzeige);
      map.addOverlay(marker);
    }
  });

  gmap.show();

  return map;
}

