/**
	Methoden fuer die Kartendarstellung.
	
	Achtung: Die Datei muss mit einen ISO-8859-1 encoding 
			 abgespeichert werden da sonst die Umlaute nicht
			 richtig angezeigt werden.
*/

/*
	TODO:
	Label kapseln
	Mausbewegung zentral analysieren
	Deaktivieren des Labels / Interaktive Navigation auf der Karte
*/

// Grenze ab der eine Mausbewegung angenommen wird;
MIN_MOVE = 2;
/*
	Setzt ACE und Bounds der aktuellen Karte.
*/
function jgis_maploaded(){
	jgis.mapLoaded();
}

if (!jgis.mapHandle) jgis.mapHandle = new Object();

jgis.mapHandle.goto = function(props){
	if (props.x && props.y){
		// Point
		var t = new Transform();
		if ( props.worldCoordinates ){
			// Need to transform to pixel
			var img = dojo.byId("jgis.map.image");
			t.matchingBounds({xmin:0, ymin:0, xmax:img.width,ymax:img.height},jgis.bbox);
		}
		var c = t.inverseTransform(props);
		var frm = dojo.byId("jgis-map-form-navigate");
		if ( frm ){
			// Request requires x and y as integers	
			frm.action = frm.action+"&operation=map.center&x="+parseInt(c.x)+"&y="+parseInt(img.height-c.y);
			jgis.submit(frm)
			return;
		}
	}
} 

jgis.mapHandle.gotoBBox = function(props){
	if (props.bbox){		
		var frm = dojo.byId("jgis-map-form-navigate");
		if ( frm ){
			frm.action = frm.action+"&operation=goto";
			for ( var p in props){
				frm.action = frm.action+"&"+p+"="+props[p];
			} 
			jgis.submit(frm)
			return;
		}
	}
} 
/*
	Registers map bounds with jgis
*/
function jgis_register_bounds(bnds){
	// Bounds of the map	
	var coords = bnds.split(",");
	if ( coords.length != 4 ) return;
	
	// Convert the coords to floats
    for ( var i = 0; i < coords.length; i++){
    	coords[i] = parseFloat(coords[i]);
    }
	jgis.bbox = new Object();
	jgis.bbox.xmin = coords[0];
	jgis.bbox.ymin = coords[1];
	jgis.bbox.xmax = coords[2];
	jgis.bbox.ymax = coords[3];		
}
/*
	Callback des Doppelclicks in die Karte
*/
function jgis_map_select(target, formName, evt){
	if ( !evt ) evt = window.event;
	var evtUtils = new EventUtils();
	var coord = evtUtils.getCoord(evt,target);
	// form used to submit the request 	
	var frm = document.forms[formName];
	frm.action = frm.action+"&X="+coord.x+"&Y="+coord.y;
	jgis.preSubmit(frm);
	frm.submit();
}
/*
	Callback when user has pressed the mouse button within the map
*/
function jgis_map_down(target, formName, evt){
	if ( !evt ) evt = window.event;
	
	// Shift-Key?  --> Panning
	if ( evt.shiftKey ) return jgis_map_pan(target, formName, evt);
	if ( !evt.ctrlKey ) return ;
	var evtUtils = new EventUtils();
	var label = xbGetElementById(jgis.map_label);
	var labelStyle
	if ( label ) {
		label.firstChild.nodeValue = CENTER;
		labelStyle = new xbStyle(label)
		var o = evtUtils.getOffset();
		labelStyle.moveTo(evt.clientX+o.x, evt.clientY+o.y);		
		labelStyle.setVisibility('visible');
	}
	target.style.cursor='crosshair';
	var oldMousedown = target.onmousedown;
	// form used to submit the request 	
	var frm = document.forms[formName];

	// actual bounds of the image
	var bnds = jgis.bbox;		
	if ( !bnds || !frm) return;
	
	var start = evtUtils.getCoord(evt,target);

	target.onmousemove = move_handler;
	target.onmouseup = up_handler;
	target.onmousedown = null;
	
	if (label) label.onmouseup = target.onmouseup;
	
	evtUtils.stopEvent(evt);
	
	// Handles mouse up.
	function up_handler(evt){
		if ( !evt ) evt = window.event;
		evtUtils.stopEvent(evt);
		var mh = target.onmousemove;
		target.onmousemove = null;
		target.onmouseup = null;
		var c = evtUtils.getCoord(evt,target);
		var b = mh.dragBox;		
		// Not moved 
		if ( labelStyle ) labelStyle.setVisibility('hidden');
		if ( !b || (Math.abs(start.x -c.x) < MIN_MOVE && Math.abs(start.y -c.y) < MIN_MOVE )) {
			frm.action = frm.action+"&operation=map.center&x="+c.x+"&y="+c.y;
			jgis.submit(frm)
			return;
		}
		if ( start.x > c.x && start.y > c.y ) {
			frm.action = frm.action+"&operation=map.back";
			jgis.submit(frm)
			return;
		}
		var factor = 1;
		if ( start.x < c.x && start.y > c.y ) factor=-1;
		if ( start.x > c.x && start.y < c.y) {
			b.hide();
			target.onmousedown= oldMousedown;
			target.style.cursor='auto';
			mh.dragBox = null;
			return;
		}
		frm.action = frm.action+"&operation=goto-ibox&factor="+factor+"&ibox="+b.getBounds().asBoundsString();
		jgis.submit(frm)
	}
	
	// Handle move (drawing box)	
	function move_handler(evt){
		if ( !evt ) evt = window.event;
		var c = evtUtils.getCoord(evt,target);
		// Not moved !!
		if ( start.x == c.x && start.y == c.y ) return;
		var o = evtUtils.getOffset();
		labelStyle.moveTo(evt.clientX+o.x, evt.clientY+o.y);
		var x = Math.min(start.x, c.x);
		var y = Math.min(start.y, c.y);
		var w = Math.max(Math.abs(start.x - c.x),1);
		var h = Math.max(Math.abs(start.y - c.y),1);		
		if ( !move_handler.dragBox ){	
			move_handler.dragBox = new Box('jgis-map-layer', x,y,w,h,true, 'red',1);
		} else {
			var b = move_handler.dragBox;
			b.moveTo(x,y);
			b.setWidth(w);
			b.setHeight(h);
		}
		if ( !b ||(Math.abs(start.x-c.x)<MIN_MOVE && Math.abs(start.y-c.y)<MIN_MOVE )) label.firstChild.nodeValue = CENTER;
		if ( start.x < c.x && start.y > c.y ) label.firstChild.nodeValue = ZOOMOUT;
		if ( start.x < c.x && start.y < c.y ) label.firstChild.nodeValue = ZOOMIN;
		if ( start.x > c.x && start.y > c.y ) label.firstChild.nodeValue = BACK;
		if ( start.x > c.x && start.y < c.y ) label.firstChild.nodeValue = ABORT;
		evtUtils.stopEvent(evt);	
	}
}

/*
	Handles the normal mouse down. This will set a cursor and pans 
	if the user moves the pressed mouse
*/
function jgis_map_pan(target, formName, evt){


	// form used to submit the request 	
	var frm = document.forms[formName];
	// actual bounds of the image
	var bnds = jgis.bbox;
	var img = xbGetElementById("jgis.map.image")		

	if ( !bnds || !frm || !img) return;
	var evtUtils = new EventUtils();
	
	var mapStyle = new xbStyle(img);
	var oldMousedown = target.onmousedown;
	
	var start = evtUtils.getCoord(evt,target);
	var coordActivatorButton = xbGetElementById("coordActivate")		
	var inCoordMode = false; 
	if (coordActivatorButton) inCoordMode = coordActivatorButton.checked;
	if (!inCoordMode) { 
 		target.style.cursor='move';
 		target.onmousemove = move_handler;	
 	} else {
   		target.style.cursor='crosshair';
 	}
 	target.onmouseup = up_handler;
	target.onmousedown = null;	
	evtUtils.stopEvent(evt);

	// Handles mouse up.
	function up_handler(evt){
		target.style.cursor='default';
		if ( !evt ) evt = window.event;
		evtUtils.stopEvent(evt);
		target.onmousemove = null;
		target.onmouseup = null;
		var c = evtUtils.getCoord(evt,target);
		target.onmousedown = oldMousedown;
		if (!inCoordMode) {
			if (Math.abs(start.x -c.x) > MIN_MOVE || Math.abs(start.y -c.y) > MIN_MOVE ) {
				var dx = c.x - start.x;
				var dy = c.y - start.y;
				frm.action = frm.action+"&operation=map.center&x="+(img.width/2-dx)+"&y="+(img.height/2-dy);
				jgis.submit(frm)
			}
		}	
		return;
	}	
	function move_handler(evt){
	if ( !evt ) evt = window.event;
		var c = evtUtils.getCoord(evt,target);
		// Not moved !!
		if ( start.x == c.x && start.y == c.y ) {
  			evtUtils.stopEvent(evt);			
		 	return;
		 }
		var o = evtUtils.getOffset();
		mapStyle.moveTo(c.x-o.x-start.x, c.y-o.y-start.y);
		evtUtils.stopEvent(null);			
	}
}

function jgis_hide(id){
	var ele = xbGetElementById(id);
	if ( ele ) {
		var style = new xbStyle(ele);
		style.setVisibility("hidden");
	}
}

function jgis_show(id,evt){
	// Den Block auskommentieren wenn mehere Fenster angezeigt werden sollen.
	var divs = document.getElementsByTagName('div');
	for ( var i=0; i < divs.length; i++ ){
		var ele = divs[i];
		if ( ele.className == 'overlay-window' && ele.id != id){
			var style = new xbStyle(ele);
			style.setVisibility('hidden');
		} 
	}
	var ele = xbGetElementById(id);
	if ( ele ) {
		var style = new xbStyle(ele);
		style.setVisibility("visible");
		var x = evt.clientX;
		var y = evt.clientY;
		style.moveTo(x, y);	
	}
}