//<![CDATA[
rub1 = 0; // Tourenführer Gipfel	   
rub2 = 0; // Hütten
rub3 = 0; // Gipfelbuch		

id1 = 0; // Tourenführer Gipfel	   
id2 = 0; // Hütten
id3 = 0; // Gipfelbuch	

target='GmapFull'
zoom = 7;
kox = 46.20;
koy = 9.06;
ovcont = 0;
olcont = 1;
lmcont = 0;
tpcont = 0;
listener = 1;
center=0;

var ge;

// Gipfel-Marker erstellen
var gipfel = new GIcon();
gipfel.image = "http://www.bergportal.ch/graphics/GM_selectmarker.png";
gipfel.shadow = "http://www.bergportal.ch/graphics/GM_selectmarker_s.png";
gipfel.iconSize = new GSize(45, 35);
gipfel.shadowSize = new GSize(45, 35);
gipfel.iconAnchor = new GPoint(15, 35);
gipfel.infoWindowAnchor = new GPoint(15, 1);

// Gipfel-Marker others
var marker1 = new GIcon();
marker1.image = "http://www.bergportal.ch/public/images/marker1.png";
marker1.iconSize = new GSize(25, 25);
marker1.shadowSize = new GSize(25, 25);
marker1.iconAnchor = new GPoint(5, 25);
marker1.infoWindowAnchor = new GPoint(5, 1);

// Gipfel-Marker active
var marker1selected = new GIcon();
marker1selected.image = "http://www.bergportal.ch/public/images/marker1selected.png";
marker1selected.iconSize = new GSize(50, 50);
marker1selected.shadowSize = new GSize(50, 50);
marker1selected.iconAnchor = new GPoint(10, 50);
marker1selected.infoWindowAnchor = new GPoint(10, 1);

// Hütten-Marker others
var marker2 = new GIcon();
marker2.image = "http://www.bergportal.ch/public/images/marker2.png";
marker2.iconSize = new GSize(25, 25);
marker2.shadowSize = new GSize(25, 25);
marker2.iconAnchor = new GPoint(5, 25);
marker2.infoWindowAnchor = new GPoint(5, 1);

// Hütten-Marker active
var marker2selected = new GIcon();
marker2selected.image = "http://www.bergportal.ch/public/images/marker2selected.png";
marker2selected.iconSize = new GSize(50, 50);
marker2selected.shadowSize = new GSize(50, 50);
marker2selected.iconAnchor = new GPoint(10, 50);
marker2selected.infoWindowAnchor = new GPoint(10, 1);

// Gipfelbuch-Marker others
var marker3 = new GIcon();
marker3.image = "http://www.bergportal.ch/public/images/marker3.png";
marker3.iconSize = new GSize(25, 25);
marker3.shadowSize = new GSize(25, 25);
marker3.iconAnchor = new GPoint(5, 25);
marker3.infoWindowAnchor = new GPoint(5, 1);

// Gipfelbuch-Marker active
var marker3selected = new GIcon();
marker3selected.image = "http://www.bergportal.ch/public/images/marker3selected.png";
marker3selected.iconSize = new GSize(50, 50);
marker3selected.shadowSize = new GSize(50, 50);
marker3selected.iconAnchor = new GPoint(10, 50);
marker3selected.infoWindowAnchor = new GPoint(10, 1);

// Center Marker
var marker4 = new GIcon();
marker4.image = "http://www.bergportal.ch/public/images/marker4.png";
marker4.iconSize = new GSize(25, 25);
marker4.shadowSize = new GSize(25, 25);
marker4.iconAnchor = new GPoint(12, 12);
marker4.infoWindowAnchor = new GPoint(12, 12);

// Karte (Anzeige) laden
function loadGM(tgt,zm,kx,ky,r1,r2,r3,i1,i2,i3,ov,lm,tp,ls,ce){
	rub1 = r1;
	rub2 = r2;
	rub3 = r3;
	
	id1 = i1;	   
	id2 = i2;
	id3 = i3;		

    if(tgt) target=tgt;
	if(zm) zoom=zm;
	if(kx) kox=kx;
	if(ky) koy=ky;
	if(ov) ovcont=ov;
	if(lm) lmcont=lm;
	if(tp) tpcont=tp;
	if(ls) listener=ls;
	if(ce) center=ce;
	
	Gload();
	getMarker();
}
/*
// Karte (Anzeige) laden
function loadGM2(tgt,zm,kx,ky,r1,r2,r3,i1,i2,i3,ov,lm,tp,ls,ce){
	rub1 = r1;
	rub2 = r2;
	rub3 = r3;
	
	id1 = i1;	   
	id2 = i2;
	id3 = i3;		

    if(tgt) target=tgt;
	if(zm) zoom=zm;
	if(kx) kox=kx;
	if(ky) koy=ky;
	if(ov) ovcont=ov;
	if(lm) lmcont=lm;
	if(tp) tpcont=tp;
	if(ls) listener=ls;
	if(ce) center=ce;
	
	Gload2();
	getMarker();
}
*/
function loadGM2(id,tgt,kx,ky,r1,r2,r3,i1,i2,i3,ov,lm,tp,ls,ce){
	Effect.toggle(id,'blind', {duration : 0.3});
	if($(id).style.display == 'none'){

		rub1 = r1;
		rub2 = r2;
		rub3 = r3;
		
		id1 = i1;	   
		id2 = i2;
		id3 = i3;		

		if(tgt) target=tgt;
		
		if(kx) kox=kx;
		if(ky) koy=ky;
		if(ov) ovcont=ov;
		if(lm) lmcont=lm;
		if(tp) tpcont=tp;
		if(ls) listener=ls;
		if(ce) center=ce;

		zoom = 12;
		
		setTimeout("Gload()", 500);
		
		setTimeout("getMarker()", 550);
	}
}




// Karte (Input) laden
function loadGMinput(id,tgt){
	if($(id).style.display == 'none'){
		Effect.toggle(id,'blind', {duration : 0.3});
		
		if(tgt) target=tgt;
		zoom=7;
		kox = 46.5;
		koy = 9.06;
		if($('int_koor_x_dec').value != "" && $('int_koor_y_dec').value != ""){
		  kox = $('int_koor_x_dec').value;
		  koy = $('int_koor_y_dec').value;
		  zoom = 12;
		} 
		ovcont=0;
		olcont=0;
		lmcont=1;
		tpcont=1;
		listener=0;
		
		setTimeout("Gload()", 500);
		
		setTimeout("map.addOverlay(createMarkerInput(new GLatLng(kox,koy), gipfel))", 550);
	}
}
/*
// karte erstellen
function Gload() {
  if (GBrowserIsCompatible()) {
	map = new GMap2($(target));
	map.setCenter(new GLatLng(kox,koy),zoom);
	if(lmcont){
		map.addControl(new GLargeMapControl());
	} else {
		map.addControl(new GSmallMapControl());
	}
	if(tpcont){
		map.addControl(new GMapTypeControl());
	}
	map.addMapType(G_PHYSICAL_MAP);
	map.removeMapType(G_HYBRID_MAP);
	map.setMapType(G_PHYSICAL_MAP);
	if(ovcont){
		map.addControl(new GOverviewMapControl(new GSize(100,100)));
	}
	if(olcont){
		map.addControl(new OverlayControl());
	}
	if(listener){
		GEvent.addListener(map, "dragend", getMarker);
		GEvent.addListener(map, "zoomend", getMarker);
		}
	}
  }
 */
 // karte erstellen
function Gload() {
  if (GBrowserIsCompatible()) {
	map = new GMap2($(target));
	map.setCenter(new GLatLng(kox,koy),zoom);
	if(lmcont){
		map.addControl(new GLargeMapControl());
	} else {
		map.addControl(new GSmallMapControl());
	}
	map.addControl(new GHierarchicalMapTypeControl());
	
	if(tpcont){
		map.addControl(new GMapTypeControl());
	}
	map.addMapType(G_PHYSICAL_MAP);
	map.removeMapType(G_HYBRID_MAP);
	map.setMapType(G_PHYSICAL_MAP);
	
    // add the Earth map type      
    map.addMapType(G_SATELLITE_3D_MAP);
	
    //map.setMapType(G_SATELLITE_3D_MAP);
    map.getEarthInstance(getEarthInstanceCB);
	
	if(ovcont){
		map.addControl(new GOverviewMapControl(new GSize(100,100)));
	}
	if(olcont){
		map.addControl(new OverlayControl());
	}
	if(listener){
		GEvent.addListener(map, "dragend", getMarker);
		GEvent.addListener(map, "zoomend", getMarker);
		}
	}
  }
  
function getEarthInstanceCB(pluginInstance) {
    ge = pluginInstance;
	if (ge) {
		var link = ge.createLink('');
		var href = 'http://www.bergportal.ch/'
				   + 'public/tourenfuehrer.kml'
		link.setHref(href);
		
		var networkLink = ge.createNetworkLink('');
		networkLink.set(link, true, false);
		ge.getFeatures().appendChild(networkLink);
	}
}


function getMarker(){
	if(map){
		var zoom = map.getZoom();
		var coords = map.getBounds();
		var northEast = coords.getNorthEast();
		var southWest = coords.getSouthWest();
		var action = 'tourenfuehrer';
		var url = "/gmoverlays/" + rub1 + "-" + rub2 + "-" + rub3 + "/" + id1 + "-" + id2 + "-" + id3 + "/" + zoom + "/" + northEast.lng() + "-" + northEast.lat() + "-" + southWest.lng() + "-" + southWest.lat();
		new Ajax.Request(url, { 
			onComplete: function (transport) {
				showMarker(transport);
			}
		});
	}
}

// Marker Funktion
function showMarker(value) {
  map.clearOverlays();
  if(center){
    map.addOverlay(createMarkerCenter(new GLatLng(kox,koy),marker4,'Markierter Punkt'));
  }
  if(value.responseText){
	  var mkrs = value.responseText.split("#BGP#");
	  for(var i=1; i <= mkrs.length; i++){ 
		 var mkr = mkrs[i].split("#|#");
		 var tpe = str_replace('marker', '', mkr[4]);
		 map.addOverlay(createMarker(new GLatLng(mkr[0], mkr[1]),eval(mkr[4]),mkr[2],mkr[3],tpe));
	  }
  }
}

// Marker Funktion
function createMarker(point, ic, id, tit, tpe) {
  var marker = new GMarker(point, {icon: ic, title: tit});
  var infof = '<div id=\"infowindow_content\"><img src=\"http://www.bergportal.ch/public/images/loading.gif\" width=\"68\" height=\"60\" class=\"loading\" alt=\"loading...\" title=\"loading...\" /></div>';
  GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(infof); setTimeout("getInfoWindowContent('" + id + "','" + tpe + "')", 500); });
  return marker;
  }
// Marker Funktion
function createMarkerCenter(point, ic, tit) {
  var marker = new GMarker(point, {icon: ic, title: tit});
  return marker;
  }
function createMarkerInput(point, ic) {
  var marker = new GMarker(point, {icon: ic, draggable: true});
  GEvent.addListener(marker, "dragend", function() { $('int_koor_x_dec').value= marker.getPoint().lat();$('int_koor_y_dec').value=marker.getPoint().lng(); dvWGSdectoCH($('int_koor_y_dec').value, $('int_koor_x_dec').value); dvWGSdectoWGS($('int_koor_y_dec').value, $('int_koor_x_dec').value); });
  return marker;
  }
  
function getInfoWindowContent (id,tpe){
	var url = "/gmoverlays/detail/id/" + id + "/type/" + tpe;
	new Ajax.Request(url, { 
		onComplete: function (transport) {
  			$('infowindow_content').innerHTML = transport.responseText;
		}
	});
}
 
// Marker Funktion
function addVals(marker) {
  $('int_koor_y_dec').value=marker.getPoint().lat();
  $('int_koor_x_dec').value=marker.getPoint().lng(); 
  dvWGSdectoCH($('int_koor_y_dec').value, $('int_koor_x_dec').value); 
  dvWGSdectoWGS($('int_koor_y_dec').value, $('int_koor_x_dec').value);
  }
function koorUpdateCH(){
	dvWGStoWGSdec($('int_koor_y').value,$('int_koor_x').value);
	dvWGSdectoCH($('int_koor_y_dec').value,$('int_koor_x_dec').value)
	if($('int_koor_x_dec').value && $('int_koor_y_dec').value){
	  kox = $('int_koor_x_dec').value;
	  koy = $('int_koor_y_dec').value;
	  zoom = 12;
	} 
	Gload();
	map.addOverlay(createMarkerInput(new GLatLng(kox,koy), gipfel))
}
function koorUpdateWGS(){
	dvCHtoWGSdec($('ch_koor_x').value,$('ch_koor_y').value);
	dvWGSdectoWGS($('int_koor_y_dec').value,$('int_koor_x_dec').value);
	if($('int_koor_x_dec').value > 0 && $('int_koor_y_dec').value > 0){
	  kox = $('int_koor_x_dec').value;
	  koy = $('int_koor_y_dec').value;
	  zoom = 12;
	} 
	Gload();
	map.addOverlay(createMarkerInput(new GLatLng(kox,koy), gipfel))
}
function dvWGSdectoCH(x_vl,y_vl){
	// in Sekunden umwandeln
	var sy = y_vl*3600;
	var sx = x_vl*3600;
	
	// Hilfsgrössen berechnen
	var y2 = (sy-169028.66)/10000;
	var x2 = (sx-26782.5)/10000;
	
	// Rechenschritte ausführen
	var y3 = Math.floor(600072.37 + 211455.93*x2 - 10938.51*x2*y2 - 0.36*x2*y2*y2 - 44.54*x2*x2*x2);
	var x3 = Math.floor(200147.07 + 308807.95*y2 + 3745.25*x2*x2 + 76.63*y2*y2 - 194.56*x2*x2*y2 + 119.79*y2*y2*y2);
	
	if(y3 < 450000 || x3 < 50000 || y3 > 850000 || x3 > 300000){
		y3 = "";
		x3 = "";
	}
	
	$('ch_koor_x').value = y3;
	$('ch_koor_y').value = x3;
	}
	
function dvCHtoWGSdec(x,y){
	// in Einheit (1000km) umwandeln
	var x1 = (x-600000)/1000000;
	var y1 = (y-200000)/1000000;
	
	// Rechenschritte ausführen
	var sy2 = 16.9023892+3.238272*y1-0.270978*x1*x1-0.002528*y1*y1-0.0447*x1*x1*y1-0.0140*y1*y1*y1;
	var sx2 = 2.6779094 + 4.728982*x1+0.791484*x1*y1+0.1306*x1*y1*y1-0.0436*x1*x1*x1;
	
	var hy1 = sy2*100/36;
	var hx1 = sx2*100/36;

	$('int_koor_y_dec').value = hx1;
	$('int_koor_x_dec').value = hy1;
}
	
function dvWGStoWGSdec(){
	if($('int_koor_x').value != "" && $('int_koor_y').value != ""){
		var spl_x = $('int_koor_x').value.split(":");
		var dec_x = parseInt(spl_x[0])+parseInt(spl_x[1])/60+parseInt(spl_x[2])/3600;
		if(spl_x[3] == "S" || spl_x[3] == "s"){
			$('int_koor_x_dec').value = "-" + dec_x;
		} else {
			$('int_koor_x_dec').value = dec_x;
		}
		var spl_y = $('int_koor_y').value.split(":");
		var dec_y = parseInt(spl_y[0])+parseInt(spl_y[1])/60+parseInt(spl_y[2])/3600;
		if(spl_y[3] == "W" || spl_y[3] == "w"){
			$('int_koor_y_dec').value = "-" + dec_y;
		} else {
			$('int_koor_y_dec').value = dec_y;
		}
	} else {
		$('int_koor_x_dec').value = "";
		$('int_koor_y_dec').value = "";
	}
}
	
function dvWGSdectoWGS(y_vl,x_vl){
  var hy = Math.floor(y_vl);
  var hx = Math.floor(x_vl);
  
  var my = (y_vl-hy)*60;
  var mx = (x_vl-hx)*60;
  var my1 = Math.floor(my);
  var mx1 = Math.floor(mx);
  
  var sy = (my-my1)*60;
  var sx =(mx-mx1)*60;
  var sy1 = Math.floor(sy);
  var sx1 = Math.floor(sx);
  
  var bezx = "N";
  if(hx < 0){
	  bezx = "S";
      hx = Math.abs(hx);
  }
  var bezy = "E";
  if(hy < 0){
	  bezy = "W";
  	  hy = Math.abs(hy);
  }
  
  $('int_koor_y').value = hy + ":" + my1 + ":" + sy1 + ":" + bezy;
  $('int_koor_x').value = hx + ":" + mx1 + ":" + sx1 + ":" + bezx;
  }
  
 ///////////////////////////////////////////////////////////////////
 
function OverlayControl() {
}

OverlayControl.prototype = new GControl();

OverlayControl.prototype.initialize = function(map) {
  var container = document.createElement("div");

  var TFdiv = document.createElement("div");
  TFdiv.setAttribute('id', 'TFoverlay'); 
  container.appendChild(TFdiv);
  TFdiv.appendChild(document.createTextNode("Gipfel"));
  setButtonStyle_default(TFdiv);
  if(rub1){
  	setButtonStyle_active(TFdiv,1);
  } else {
  	setButtonStyle_inactive(TFdiv,1);
  }
  GEvent.addDomListener(TFdiv, "click", toggleButton);

  var HUdiv = document.createElement("div");
  HUdiv.setAttribute('id', 'HUoverlay'); 
  container.appendChild(HUdiv);
  HUdiv.appendChild(document.createTextNode("Hütten"));
  setButtonStyle_default(HUdiv);
  if(rub2){
  	setButtonStyle_active(HUdiv,2);
  } else {
  	setButtonStyle_inactive(HUdiv,2);
  }
  GEvent.addDomListener(HUdiv, "click", toggleButton);

  var GBdiv = document.createElement("div");
  GBdiv.setAttribute('id', 'GBoverlay'); 
  container.appendChild(GBdiv);
  GBdiv.appendChild(document.createTextNode("Gipfelbuch"));
  setButtonStyle_default(GBdiv);
  if(rub3){
  	setButtonStyle_active(GBdiv,3);
  } else {
  	setButtonStyle_inactive(GBdiv,3);
  }
  GEvent.addDomListener(GBdiv, "click", toggleButton);

  map.getContainer().appendChild(container);
 return container;
}

function toggleButton(){
	switch(this.getAttribute('id')){
		case "TFoverlay":
			if(rub1 == 1){
				rub1 = 0;
				setButtonStyle_inactive(this,1);
			} else {
				rub1 = 1;
				setButtonStyle_active(this,1);
			}
		break;
		case "HUoverlay":
			if(rub2 == 1){
				rub2 = 0;
				setButtonStyle_inactive(this,2);
			} else {
				rub2 = 1;
				setButtonStyle_active(this,2);
			}
		break;
		case "GBoverlay":
			if(rub3 == 1){
				rub3 = 0;
				setButtonStyle_inactive(this,3);
			} else {
				rub3 = 1;
				setButtonStyle_active(this,3);
			}
		break;
	}
	getMarker();
}

OverlayControl.prototype.getDefaultPosition = function() {
  return new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(7, 7));
}

setButtonStyle_default = function(button,type) {
  button.style.textDecoration = "none";
  button.style.color = "#000000";
  button.style.font = "11px Verdana";
  button.style.border = "1px solid black";
  button.style.padding = "2px 2px 2px 50px";
  button.style.marginRight = "3px";
  button.style.width = "70px";
  button.style.height = "16px";
  button.style.cursor = "pointer";
  button.style.styleFloat = 'left';
  button.style.cssFloat = 'left';
}

setButtonStyle_inactive = function(button,type) {
  button.style.background = "#ffffff url(http://www.bergportal.ch/public/images/marker" + type + "_hide.png) left top no-repeat";
}

setButtonStyle_active = function(button,type) {
  button.style.background = "#ffffff url(http://www.bergportal.ch/public/images/marker" + type + "_show.png) left top no-repeat";
}
function str_replace(src, rpl, subject) {
	if(subject){
    	return subject.split(src).join(rpl);
	}
}
  
//]]>
