var $centerX;
var $centerY;
var $extend;
var $route;
var $poi;
var $startX;
var $startY;
var $destinationX;
var $destinationY;
var minimapstatus = 0;
var $sessionkey;
var $xml_server = 'http://xml.klicktel.de';
var aerial = 0;

//var tragetid = document.getElementById ("singlemap").src;
//var tragetid = mapwindow.location.href;

maplinks = new Array();

function switch_aerial (b){
		switch (b){
		case 0:
			mapwindow.location.href = "/map_scout.php?"+$session_name+"="+$session_id+"&id="+$xml_scout_id+"&route="+$route+"&tilex="+$centerX+"&tiley="+$centerY+"&extend="+$extend+"&poi="+$poi+"&startx="+$startX+"&starty="+$startY+"&destinationx="+$destinationX+"&destinationy="+$destinationY+"&sessionkey="+$sessionkey+"&aerial=0";
			break;
		case 1:
			mapwindow.location.href = "/map_scout.php?"+$session_name+"="+$session_id+"&id="+$xml_scout_id+"&route="+$route+"&tilex="+$centerX+"&tiley="+$centerY+"&extend="+$extend+"&poi="+$poi+"&startx="+$startX+"&starty="+$startY+"&destinationx="+$destinationX+"&destinationy="+$destinationY+"&sessionkey="+$sessionkey+"&aerial=1";
			break;
	}
}

var zoomlevels = new Array (2500, 5000, 10000, 20000, 40000, 80000, 160000, 320000, 640000);

/*
function showMiniMap (obj, number, route){
	$id = "minimap_"+number;	
	$map = document.getElementById ($id);
	if ($map.style.display=="inline"){
		$map.style.display = "none";
		var Text = document.createTextNode('Karte einblenden');
  	document.getElementById(obj).replaceChild(Text, document.getElementById(obj).firstChild);
	}
	else {
		//$map.src = "http://xml.klicktel.de/scripts/image.dll?getmapdirect&left=0&top=1000&right=1000&bottom=0&routid="+route+"&w=575&h=200&staubst=0&mmap="+(number - 1)+"&customer=1502";
		$map.src = "http://ppm.klicktel.de/scripts/image.dll?GetXMapDirect&left=0&top=1000&right=1000&bottom=0&out=3&w=575&h=200&routid="+route+"&aerial=2&mmap="+(number - 1)+"";
		$map.style.display = "inline";
		var Text = document.createTextNode('Karte ausblenden');
  		document.getElementById(obj).replaceChild(Text, document.getElementById(obj).firstChild);
	}
}
*/

function showMiniMap (obj, number, route, options, icon){
	$id = "minimap_"+number;	
	$map = document.getElementById ($id);
	if ($map.style.display=="block"){
		$map.style.display = "none";
		$map.parentNode.style.display = "none";
		var Text = document.createTextNode(document.getElementById("SHOWMAP").value);
  	document.getElementById(obj).replaceChild(Text, document.getElementById(obj).firstChild);
	}
	else {
		aerial=0;
		if (mapObject != null) {
			if (mapObject.aerial == true) {
				if (mapObject.hybrid == true) {
					aerial = 2;
				} else {
					aerial = 1;
				}
				
			}
		}
		
		//$map.src = "http://ppm.klicktel.de/scripts/image.dll?GetXMapDirect&left=0&top=1000&right=1000&bottom=0&out=3&w=575&h=200&routid="+route+"&aerial="+aerial+"&mmap="+(number - 1);
		$map.src = "http://nmap0"+(Math.round(1 + (Math.random() * 3)))+".ppm-ng.de/scripts/image.dll?getxmapdirect&left=0&top=0&w=575&h=200&zoom=2000&routeid="+route+"&trafficjam=0&layer=16&mmap="+(number - 1)+"&filetype=2";
		
		if (icon == 'abfahrt.gif' || icon == 'ankunft.gif') {
			var flag = document.createElement ("img");
			if (icon == 'abfahrt.gif')
				flag.src="/kt_map/images/flagge_start.gif";
			else
				flag.src="/kt_map/images/flagge_ziel.gif";
			flag.style.position = "absolute";
			flag.style.left = "286px";
			flag.style.top = "46px";
			flag.style.zIndex = 100;
			$map.parentNode.appendChild (flag);
		} 
		
		$map.style.display = "block";
		$map.parentNode.style.display = "block";
		var Text = document.createTextNode(document.getElementById("HIDEMAP").value);
  		document.getElementById(obj).replaceChild(Text, document.getElementById(obj).firstChild);
	}
}

function updateMinimaps(){
	log("updateMinimaps");
	var maps=document.getElementsByClassName("minimap");
	log("updateMinimaps: maps.length="+maps.length);
	aerial=0;
	if (mapObject != null) {
		if (mapObject.aerial == true) {
			if (mapObject.hybrid == true) {
				aerial = 2;
			} else {
				aerial = 1;
			}
			
		}
	}
	for(i = 0; i < maps.length; i++){
		var map=maps[i];
		log("updateMinimaps: maps["+i+"].src="+map.src);
		if(map.src!=""){
			var src=map.src;
			var new_src=src.replace(/aerial=[0-2]/,"aerial="+aerial);
			map.src=new_src;
		}
		
		//var input = 'minimap_'+maplinks[i];	
		//var d=$(input).style.display;
		//log("updateMinimaps: input="+input+" :: "+d);
	}
}

function initMap (centerX, centerY, extend, route, poi, startX, startY, destinationX, destinationY, sessionkey, pin){
	$centerX = centerX;
	$centerY = centerY;
	$extend = extend;
	$route = route;
	$startX = startX;
	$startY = startY;
	$destinationX = destinationX;
	$destinationY = destinationY;
	$poi = poi;
	$sessionkey = sessionkey;
	$xml_scout_id = pin;
	markupZoomLevel($extend);
}

function showAll(obj){	
	if(minimapstatus==0)
	{
		for(i = 0; i < maplinks.length; i++)
 		{
			var input = 'minimap_'+maplinks[i];
			document.getElementById(input).style.display='block';
			document.getElementById(input).src = document.getElementById(input).name;
			input = 'maplink_'+maplinks[i];
			var maplinktext = document.createTextNode('Karte ausblenden');
			document.getElementById(input).replaceChild(maplinktext, document.getElementById(input).firstChild);
		}
		var Text = document.createTextNode('Alle Karten ausblenden');
		document.getElementById(obj).replaceChild(Text, document.getElementById(obj).firstChild);
		document.getElementById('pic_allmaps').src = '/images/workbar/icon_minimaps_hide.gif';
		minimapstatus = 1;
	}
	else if(minimapstatus==1)
	{
		for(i = 0; i < maplinks.length; i++)
 		{
			var input = 'minimap_'+maplinks[i];
			document.getElementById(input).style.display='none';
			input = 'maplink_'+maplinks[i];
			var maplinktext = document.createTextNode('Karte einblenden');
			document.getElementById(input).replaceChild(maplinktext, document.getElementById(input).firstChild);
		}
		var Text = document.createTextNode('Alle Karten einblenden');
		document.getElementById(obj).replaceChild(Text, document.getElementById(obj).firstChild);
		document.getElementById('pic_allmaps').src = '/images/workbar/icon_minimaps_show.gif';
		minimapstatus = 0;
	}
}



function setZoomLevel(level){
		
	
	$extend = zoomlevels[level-1];
	mapwindow.location.href = "/map_scout.php?"+$session_name+"="+$session_id+"&id="+$xml_scout_id+"&route="+$route+"&tilex="+$centerX+"&tiley="+$centerY+"&extend="+$extend+"&poi="+$poi+"&startx="+$startX+"&starty="+$startY+"&destinationx="+$destinationX+"&destinationy="+$destinationY+"&sessionkey="+$sessionkey;
	
	$zoomlevel=level;


	markupZoomLevel ($extend);
	return (false);
}

function markupZoomLevel (extend){
	for (var i=0;i<=7;i++){
		if (extend >= zoomlevels[i] && extend<zoomlevels[i+1]){
			document.getElementById("zoombutton"+(i+1)).style.backgroundColor="#"+$colorBorderArrows;
		}
		else {
			document.getElementById("zoombutton"+(i+1)).style.backgroundColor="#"+$colorBorderEdge;
		}
	}
}

function zoomIn (){
	$extend=Math.round($extend/2);
	mapwindow.location.href = "/map_scout.php?"+$session_name+"="+$session_id+"&id="+$xml_scout_id+"&route="+$route+"&tilex="+$centerX+"&tiley="+$centerY+"&extend="+$extend+"&poi="+$poi+"&startx="+$startX+"&starty="+$startY+"&destinationx="+$destinationX+"&destinationy="+$destinationY+"&sessionkey="+$sessionkey;
	markupZoomLevel($extend);
	return(false);
}

function zoomOut(){
	$extend=$extend*2;
	mapwindow.location.href = "/map_scout.php?"+$session_name+"="+$session_id+"&id="+$xml_scout_id+"&route="+$route+"&tilex="+$centerX+"&tiley="+$centerY+"&extend="+$extend+"&poi="+$poi+"&startx="+$startX+"&starty="+$startY+"&destinationx="+$destinationX+"&destinationy="+$destinationY+"&sessionkey="+$sessionkey;
	markupZoomLevel($extend);
	return(false);
}


function moveMap (direction){
	//Make integers from all center values
	$centerX = 1*$centerX;
	$centerY = 1*$centerY;
	
	switch (direction){
		case 'n':
		$centerY += Math.floor($extend/2);
		break;
		case 's':
		$centerY -= Math.floor($extend/2);
		break;
		case 'e':
		$centerX += Math.floor($extend/2);
		break;
		case 'w':
		$centerX -= Math.floor($extend/2);
		break;
		case 'ne':
		$centerY += Math.floor($extend/2);
		$centerX += Math.floor($extend/2);
		break;
		case 'nw':
		$centerY += Math.floor($extend/2);
		$centerX -= Math.floor($extend/2);
		break;
		case 'se':
		$centerY -= Math.floor($extend/2);
		$centerX += Math.floor($extend/2);
		break;
		case 'sw':
		$centerY -= Math.floor($extend/2);
		$centerX -= Math.floor($extend/2);
		break;
	}
	mapwindow.location.href = "/map_scout.php?"+$session_name+"="+$session_id+"&id="+$xml_scout_id+"&route="+$route+"&tilex="+$centerX+"&tiley="+$centerY+"&extend="+$extend+"&poi="+$poi+"&startx="+$startX+"&starty="+$startY+"&destinationx="+$destinationX+"&destinationy="+$destinationY+"&sessionkey="+$sessionkey+"&id="+$xml_scout_id;
	
	
	
	//document.getElementById("singlemap").src = "/mapserver.php?route="+$route+"&tilex="+$centerX+"&tiley="+$centerY+"&extend="+$extend+"&poi="+$poi+"&startx="+$startX+"&starty="+$startY+"&destinationx="+$destinationX+"&destinationy="+$destinationY;
	//document.getElementById ("framemap").src = "/framemap_scout.php?id="+$xml_scout_id+"&route="+$route+"&tilex="+$centerX+"&tiley="+$centerY+"&extend="+$extend+"&poi="+$poi+"&startx="+$startX+"&starty="+$startY+"&destinationx="+$destinationX+"&destinationy="+$destinationY;
return (false);

}

function log(msg){
	Logger.log(msg);
}