// Damit ein SyntaxHighlighting vorhanden ist
// <script type="text/javascript">

var ve_mapping = new Array();
var layer_pins;
var layer_lines;

function IEDebug(){
	if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent)) {
		var elems = document.getElementsByTagName("div");
		for(var i = 0; i < elems.length; i++){
			var elem = elems[i];
			var cls = elem.className;
			if(cls=='ero-shadow'){
				var ie_abdecker = d.createElement("iframe");
				ie_abdecker.setAttribute('id','VEabdecker');
				ie_abdecker.style.position = 'absolute';
				ie_abdecker.style.width = '0px';
				ie_abdecker.style.height = '0px';
				ie_abdecker.style.top = '0px';
				ie_abdecker.style.zIndex = '85';
				elem.appendChild(ie_abdecker);
			}
		}
	}
}

function IEDebug_U(width,height){
	if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent)) {
		get_element('VEabdecker').style.width = parseInt(width)+29;
		get_element('VEabdecker').style.height = parseInt(height)+44;
	}
}

function AddPin(p){
	if(typeof(adressmarker[p.k])=='undefined'){
		if(p.i==0) p.i = icon_default;
		if(p.it=='ort'){
			width = 12;
			height = 12;
		}else if(p.it=='icon_default_img'){
			width = 57;
			height = 51;
		}else{
			width = 31;
			height = 33;
		}
		adressmarker[p.k] = new VEShape(VEShapeType.Pushpin, new VELatLong(p.b,p.l));
		adressmarker[p.k].SetCustomIcon("<img width='"+width+"' height='"+height+"' class='"+p.it+"' src='"+p.i+"' />");
		layer_pins.AddShape(adressmarker[p.k]);
		var id = adressmarker[p.k].GetID();
		ve_mapping[id] = new Array('am',p.k,p.c);
	}
}

function AddOrt(p,icon){
	if(typeof(ortmarker[p.k])=='undefined'){
		ortmarker[p.k] = new VEShape(VEShapeType.Pushpin, new VELatLong(p.b,p.l));
		if(icon == 'rot') {
			ortmarker[p.k].SetCustomIcon("<img class='icon_ort' src='http://www.timm4.de/include/icons/google_maps_marker/ort_rot.png' title='"+p.t+"' alt='"+p.t+"' />");
		} else {
			ortmarker[p.k].SetCustomIcon("<img class='icon_ort' src='http://www.timm4.de/include/icons/google_maps_marker/ort.png' title='"+p.t+"' alt='"+p.t+"' />");
		}
		layer_pins.AddShape(ortmarker[p.k]);
		var id = ortmarker[p.k].GetID();
		ve_mapping[id] = new Array('om',p.k,p.c);
	}
}

function AddContentID(p,icon){
	if(typeof(contentmarker[p.k])=='undefined'){
		contentmarker[p.k] = new VEShape(VEShapeType.Pushpin, new VELatLong(p.b,p.l));
		if(icon == 'rot') {
			contentmarker[p.k].SetCustomIcon("<img class='icon_ort' src='http://www.timm4.de/include/icons/google_maps_marker/ort_rot.png' />");
		} else {
			contentmarker[p.k].SetCustomIcon("<img class='icon_ort' src='http://www.timm4.de/include/icons/google_maps_marker/ort.png' />");
		}
		layer_pins.AddShape(contentmarker[p.k]);
		var id = contentmarker[p.k].GetID();
		ve_mapping[id] = new Array('cm',p.k,p.c);
	}
}

function ClickPin(e){
	if(e.elementID!=null){
		id = e.elementID.substr(0,18);
		if(typeof(ve_mapping[id])!='undefined'){
			if(ve_mapping[id][0]=='am'){ // Adressmarker
				open_marker(ve_mapping[id][1],ve_mapping[id][2]);
			}else if(ve_mapping[id][0]=='om'){ // Ortmarker
				open_marker_ort(ve_mapping[id][1]);
			}else if(ve_mapping[id][0]=='cm'){ // Contentmarker
				open_marker_content(ve_mapping[id][1]);
			}else if(ve_mapping[id][0]=='ad'){ // Adresse
				open_adresse(ve_mapping[id][1]);
			}
		}
	}
}

// Funktion gibt den Query mit den aktuellen Grenzen zurueck
function bounds_query(as_str) {
	if(typeof(map)!='undefined'){
		var zoom = map.GetZoomLevel();
		var eckdaten = map.GetMapView();
		var nw = eckdaten.TopLeftLatLong;
		var se = eckdaten.BottomRightLatLong;
	
		var bounds = new Array;
		bounds['l_min'] = nw.Longitude;
		bounds['l_max'] = se.Longitude;
		bounds['b_min'] = se.Latitude;
		bounds['b_max'] = nw.Latitude;
		if(as_str==1) {
			return 'lgrad_min='+bounds['l_min']+'&lgrad_max='+bounds['l_max']+'&bgrad_min='+bounds['b_min']+'&bgrad_max='+bounds['b_max']+'&zoom='+zoom;
		} else {
			return bounds;
		}
	}else{
		return '';
	}
}

// Funktion die die aktuelle Kartenposition im Cookie speichert
function map_save_position() {
	set_cookie(map_id+"zoom",map.GetZoomLevel());
	set_cookie(map_id+"bgrad",map.GetCenter().Latitude);
	set_cookie(map_id+"lgrad",map.GetCenter().Longitude);
	set_cookie(map_id+"maptype",map.GetMapStyle());

	bounds = bounds_query(0);
	set_cookie(map_id+"lgrad_min",bounds['l_min']);
	set_cookie(map_id+"lgrad_max",bounds['l_max']);
	set_cookie(map_id+"bgrad_min",bounds['b_min']);
	set_cookie(map_id+"bgrad_max",bounds['b_max']);
}

// Öffnet einen Marker
function open_marker(id,cluster) {
	if(adressmarker[id] != undefined) {
		// Fuer Media fixe Fenstergroesse
		if(id.substring(0,1)=='m'){
			width = '320';
			height = '240';
		}else{
			width = '350';
			height = '200';
		}
     
		adressmarker[id].SetDescription('<div id="openmarker" style="width:'+width+'px;height:'+height+'px;"><img src="/layout/loader.gif" alt="" /></div>');
		map.HideInfoBox();
		map.ShowInfoBox(adressmarker[id]);
		IEDebug_U(width,height);
				
		set_cookie(map_id+"marker_open",id);
		set_cookie(map_id+"marker_open_cluster",cluster);

		// Wegline entfernen
		for(var key in weglinie) map.DeleteShape(weglinie[key]);
		weglinie = new Array;

		// Weg-Linie anzeigen
		if(id.substring(0,1)=='w'){
			create_element('/timm4maps/show_weg.php?id='+id+'&maps_id='+map_id+'&db='+db);
		}
	
		var query = gm_query();
		query = '&'+query;
		query += '&id='+id;
		query += '&cluster='+cluster;
		query += '&maps_id='+map_id;
		query += '&db='+db;
		
		if(typeof jQuery == "function"){
			$('#openmarker').load('/timm4maps/ajax_show_marker.php', { postdata : query }, 
				function(){
					if(id.substring(0,1)=='m'){
						create_element('/timm4maps/show_video.php?id='+id);
					}
				});
			
		}else{
			var ajax = createRequest();
			ajax.open('post','/timm4maps/ajax_show_marker.php',true);
			ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			ajax.send(query);
			
			ajax.onreadystatechange = function() {
				if(ajax.readyState == 4) {
					get_element("openmarker").innerHTML = ajax.responseText;
					// Video starten
					if(id.substring(0,1)=='m'){
						create_element('/timm4maps/show_video.php?id='+id);
					}
					adressmarker[id].SetDescription('');
				}
			};
		}
	}
}

function open_marker_2(id,gps_lgrad,gps_bgrad) {
	marker_found = 0;
  document.getElementById("map").scrollIntoView(true);
	for(var i in visible_marker){
		if(visible_marker[i].k==id || visible_marker[i].cluster.indexOf(id)>=0){
			open_marker(visible_marker[i].k,visible_marker[i].cluster);
			marker_found = 1;
			marker_to_open = 0;
			break;
		}
	}

	// Wenn der gesuchte Marker auserhalb der Anzeige ist
	if(!marker_found && gps_bgrad && gps_lgrad){
		marker_to_open = id;
		bounds = bounds_query(0);
		offset = (bounds['b_max']-bounds['b_min'])/4;
		map.HideInfoBox();
		map.PanToLatLong(new VELatLong(gps_bgrad+offset,gps_lgrad));
	}
}

// Öffnet eine Adresse
function open_adresse(id) {
	if(adresse[id] != undefined) {
		adresse[id].SetDescription('<div id="openmarker_small" style="width:250px;height:160px;"><img src="/layout/loader.gif" alt="" /></div>');
		map.HideInfoBox();
		map.ShowInfoBox(adresse[id]);
		IEDebug_U(250,160);
					
		if(typeof jQuery == "function"){
			$('#openmarker_small').load('/timm4maps/ajax_show_adresse.php?id='+id, {});
		
		}else{
			var ajax = createRequest();
			ajax.open('get', '/timm4maps/ajax_show_adresse.php?id='+id,true);
			ajax.onreadystatechange = function() {
				if(ajax.readyState == 4) {
					get_element("openmarker_small").innerHTML = ajax.responseText;
					adresse[id].SetDescription('');
				}
			};
			ajax.send(null);
		}
	}
}

// Öffnet einen Ort
function open_marker_ort(id){
	if(ortmarker[id] != undefined) {
		ortmarker[id].SetDescription('<div id="openmarker" style="width:350px;height:200px;"><img src="/layout/loader.gif" alt="" /></div>');
		map.HideInfoBox();
		map.ShowInfoBox(ortmarker[id]);
		IEDebug_U(350,200);
					
		if(typeof jQuery == "function"){
			$('#openmarker').load('/timm4maps/ajax_show_ort.php?id='+id, {});
		
		}else{
			var ajax = createRequest();
			ajax.open('get', '/timm4maps/ajax_show_ort.php?id='+id,true);
			ajax.onreadystatechange = function() {
				if(ajax.readyState == 4) {
					get_element("openmarker").innerHTML = ajax.responseText;
					ortmarker[id].SetDescription('');
				}
			};
			ajax.send(null);
		}
	}
}

// Debug VE
function open_marker_ort_2(string) {

	vars = string.split(',');
	
	map_lgrad = Math.round(map.GetCenter().Longitude*10)/10;
	map_bgrad = Math.round(map.GetCenter().Latitude*10)/10;
	gps_lgrad = Math.round(vars[1]*10)/10;
	gps_bgrad = Math.round(vars[2]*10)/10;
	
	if(map_bgrad==gps_bgrad && map_lgrad==gps_lgrad){
		open_marker_ort(vars[0]);
	}else{
		map.SetCenter(new VELatLong(gps_bgrad,gps_lgrad));
		window.setTimeout("open_marker_ort_2('"+string+"')",300);
	}
}

// Öffnet einen Ort
function open_marker_content(id){
	if(contentmarker[id] != undefined) {
		contentmarker[id].SetDescription('<div id="openmarker" style="width:350px;height:200px;"><img src="/layout/loader.gif" alt="" /></div>');
		map.HideInfoBox();
		map.ShowInfoBox(contentmarker[id]);
		IEDebug_U(350,200);
					
		if(typeof jQuery == "function"){
			$('#openmarker').load('/timm4maps/ajax_show_content.php?id='+id, {});
		
		}else{
			var ajax = createRequest();
			ajax.open('get', '/timm4maps/ajax_show_content.php?id='+id,true);
			ajax.onreadystatechange = function() {
				if(ajax.readyState == 4) {
					get_element("openmarker").innerHTML = ajax.responseText;
					contentmarker[id].SetDescription('');
				}
			};
			ajax.send(null);
		}
	}
}

// Loescht den Marker
function DelPin(p) {
  if(typeof(adressmarker[p.k])!='undefined'){
    map.DeleteShape(adressmarker[p.k]);
    delete adressmarker[p.k];
  }
}

// Loescht alle Marker und Linien von der Karte
function DelAllPins() {
	map.DeleteAllShapes();
	adressmarker = new Array;
	weglinie = new Array;
}

function GUnload() {}
