


document.onkeydown = ondocumentkeydown;
document.onkeyup = ondocumentkeyup;

var fldlang=1;
// label drag marker icon
var glabelmarkericon = new GIcon();
glabelmarkericon.image = "/dev/gmap/greencube.gif";
glabelmarkericon.iconAnchor = new GPoint(3, 3);
glabelmarkericon.iconSize = new GSize(15, 15);

var poly = new Array(1);
var inpfldonfocus=null;
var inpfldontxt="";
var glabeldivhtml="<div id='glabeldivid_id' onclick='glabelclick(this)' class='style1'></div>";
var glabelinphtml="<table cellpadding='0' cellspacing='0' border='0'><tr><td>de </td><td><input type='text' id='glabelinpid1_id' value='' onchange='glabelchange(this)' onclick='ginpfldclick(this)' onfocus='ginpfldclick(this)'></td></tr><tr><td>fr</td><td><input type='text' id='glabelinpid2_id' value='' onchange='glabelchange(this)' onclick='ginpfldclick(this)' onfocus='ginpfldclick(this)'></td></tr><tr><td>en</td><td><input type='text' id='glabelinpid3_id' value='' onchange='glabelchange(this)' onclick='ginpfldclick(this)' onfocus='ginpfldclick(this)'></td></tr></table>";
var glabeldefaulttext="Info Text";

var arrowlineweight=100;
var arrowheadlength=100;
var arrowwingsize=85;
var arrowwingshrink=100;
var arrowlinecolor="#006699";
var arrowfillcolor="#ffffff";


// **********************************************************************************************************
function garrowsave() {
//debug("garrowsave "+poly);
	if(!poly) return;
	garrowcancel();
	map.checkResize();
	mcenpix=map.fromLatLngToDivPixel(map.getCenter());
	polys = new Array(1);
	polyscnt=-1;
	for(polycnt = 0; polycnt < poly.length-1; polycnt++){
		if(poly[polycnt]!=null){
			polyscnt=polyscnt+1;
			polys[polyscnt]=new Object();
			polys[polyscnt]["glabeltext"]=poly[polycnt]["glabeltext"]
			polys[polyscnt]["pts1"]=poly[polycnt]["pts"][1].toString();
			polys[polyscnt]["origpix"]=poly[polycnt]["origpix"].toString();
			polys[polyscnt]["labelpix"]=poly[polycnt]["labelpix"].toString();
			pt1pix=map.fromLatLngToDivPixel(poly[polycnt]["pts"][1]);
			dx=pt1pix.x-mcenpix.x;
			dy=pt1pix.y-mcenpix.y;
			polys[polyscnt]["peakpix"]=new GPoint(dx,dy).toString();
//debug("text "+polyscnt+"  "+polys[polyscnt]["glabeltext"]);
//debug("peak "+polys[polycnt]["pts1"]);
//debug("orig "+polys[polycnt]["origpix"]);
//debug("label "+polys[polycnt]["labelpix"]);
		}
	}
	mappar=new Object();
	mappar["gcenter"]=map.getCenter().toString();
	mappar["gmaptype"]=getmaptypenumber(map);
//	mappar["gsize"]=map.getSize().toString();	// the pixel size of the map window
	mappar["gsize"]="("+parseInt(document.getElementById("gmap").style.width)+", "+parseInt(document.getElementById("gmap").style.height)+")";
	mappar["gzoom"]=map.getZoom().toString();
	mappar["polys"]=polys;
	mappar["placeid"]=actplaceid;
	mappar["hintid"]=acthintid;
	mappar["imageid"]=actimageid;
	pmarker=map.fromLatLngToDivPixel(markerD2.getPoint());
	dx=pmarker.x-mcenpix.x;
	dy=pmarker.y-mcenpix.y;
	mappar["placecenterpix"]=new GPoint(dx,dy).toString();

debug("garrowsave placeid "+mappar["placeid"]+" hintid "+mappar["hintid"]+" imageid "+mappar["imageid"]+" gmaptype "+mappar["gmaptype"]+" placecenterpix "+mappar["placecenterpix"]);
//debug("maptype "+mappar["gmaptype"]);
//debug("size "+mappar["gsize"]);
debug("center "+mappar["gcenter"]);
//debug("zoom "+mappar["gzoom"]);
	//serialize and submit to server
	wddxSerializer = new WddxSerializer();
	mapparwddx = wddxSerializer.serialize(mappar);
//debug("mapparwddx "+mapparwddx);
//debug("dockeydown "+dockeydown);
	if(dockeydown==17){		// ctrl key
		mapparwddx=escape(mapparwddx);
		DWREngine._execute(ajaxurl, null, "savemappar", mapparwddx, garrowsaveresponse);
	}else{
debug("update opener hintid "+acthintid);
		if(opener.document.getElementById("fImagesMappar_"+acthintid)){
debug("update opener now ");
			opener.document.getElementById("fImagesMappar_"+acthintid).value=mapparwddx;
			window.close();
		}
	}
	
}
// **********************************************************************************************************
function garrowsaveresponse(resp){ 
	debug("save response "+resp);
} 
// **********************************************************************************************************
function getmaptypenumber(map){ var type=-1; for(var ix=0;ix<map.getMapTypes().length;ix++){ if(map.getMapTypes()[ix]==map.getCurrentMapType()) type=ix; } return type; } 
function setmaptypenumber(map, num){ var type=-1; for(var ix=0;ix<map.getMapTypes().length;ix++){ if(ix==num){ map.setMapType(map.getMapTypes()[ix]);}}} 
// **********************************************************************************************************
function arrowclick() {
//debug("click "+this.getVertexCount()+this.getVertex(3));
	ptref=this.getVertex(3).toString();
	for(polycnt = 0; polycnt < poly.length-1; polycnt++){
		arrowpeakll=poly[polycnt]["pts"][1];
		if(arrowpeakll==ptref) break;
	}
//debug("found "+polycnt+"  "+arrowpeakll);
	labelvis="hidden"; if(poly[polycnt]["glabeltext"][0]=="hidden") labelvis="visible";
	poly[polycnt]["glabeltext"][0]=labelvis;
	redraw();
}
// **********************************************************************************************************
function redraw(actlang) {
	if(!poly || poly[0]==null) return;
	if(!actlang)actlang=1;fldlang=parseInt(actlang);fldlang=Math.min(3,fldlang);fldlang=Math.max(1,fldlang);
	garrowcancel();
	for(polycnt = 0; polycnt < poly.length-1; polycnt++){
debug("redraw "+polycnt);
		if(poly[polycnt]!=null){
			// remove existing overlays
//debug("redraw "+polycnt+"  "+poly[polycnt]["glabeloverlay"]);
			if(poly[polycnt]["glabeloverlay"]) map.removeOverlay(poly[polycnt]["glabeloverlay"]);
			if(poly[polycnt]["glabelmarker"]) map.removeOverlay(poly[polycnt]["glabelmarker"]);
			if(poly[polycnt]["polygon"]) map.removeOverlay(poly[polycnt]["polygon"]);
			// calculate arrow origin using pixel offset
			arrowpeakll=poly[polycnt]["pts"][1];
			arrowpeakpix=map.fromLatLngToDivPixel(arrowpeakll);
//debug("arrowpeakpix "+arrowpeakpix);
			arroworigoffsetpix=poly[polycnt]["origpix"];
			arroworigpix=new GPoint(arrowpeakpix.x+arroworigoffsetpix.x,arrowpeakpix.y+arroworigoffsetpix.y);
			arroworigll=map.fromDivPixelToLatLng(arroworigpix);
			arrowact=new Array(1);
			arrowact[0]=arroworigll;
			arrowact[1]=arrowpeakll;
			// calculate the arrow's vertices and draw polygon
			ptsarrow=garrowcalc(arrowact);
			poly[polycnt]["polygon"] = new GPolygon(ptsarrow, arrowlinecolor, 1, 1, arrowfillcolor, 1);
//debug("add poly "+polycnt+"  "+poly[polycnt]["polygon"]);
			map.addOverlay(poly[polycnt]["polygon"]);
			if(gmapedit) GEvent.addListener(poly[polycnt]["polygon"], "click", arrowclick);
			// calculate the labels position using pixel offset and draw label
			labeloffsetpix=poly[polycnt]["labelpix"];
			labelpix=new GPoint(arrowpeakpix.x+labeloffsetpix.x,arrowpeakpix.y+labeloffsetpix.y);
			labelll=map.fromDivPixelToLatLng(labelpix);
			labeloffsetpix=new GSize(0,0);
//debug("labelpix "+labelpix);
//debug("labelll "+labelll);
			glabeldraw(map,polycnt, labelll);
		}
	}
//debug("redraw end ");
}

// **********************************************************************************************************
function garrowset(overlay,point){				// launched by GEvent.addListener(map,"click",function(overlay,point){
	polycnt=poly.length-1;
	pt=point;
	if(poly[polycnt] && poly[polycnt]["pts"][0]!=null){		// if arrow origin already is defined
		if(poly[polycnt]["polygon"])map.removeOverlay(poly[polycnt]["polygon"]);
		// calculate the arrow's vertices and draw the polygon
		ptsarrow=garrowcalc(poly[polycnt]["pts"]);
		poly[polycnt]["polygon"] = new GPolygon(ptsarrow, arrowlinecolor, 1, 1, arrowfillcolor, 1);
		map.addOverlay(poly[polycnt]["polygon"]);
		GEvent.addListener(poly[polycnt]["polygon"], "click", arrowclick);
		// add a label div near the arrow's origin
		poly[polycnt]["glabeltext"]=new Array(1);
		poly[polycnt]["glabeltext"][1]=glabeldefaulttext;
		poly[polycnt]["glabeltext"][2]=glabeldefaulttext;
		poly[polycnt]["glabeltext"][3]=glabeldefaulttext;
		glabeldraw(map, polycnt, poly[polycnt]["pts"][0]);
		// calc the pixel offset between the label and the arrow's peak
		pt0pix=map.fromLatLngToDivPixel(poly[polycnt]["pts"][0]);
		pt1pix=map.fromLatLngToDivPixel(poly[polycnt]["pts"][1]);
		dx=pt0pix.x-pt1pix.x;
		dy=pt0pix.y-pt1pix.y;
		poly[polycnt]["origpix"]=new GPoint(dx,dy);

		pt=null;
		polycnt = polycnt+1;
	}
	if(!poly[polycnt]){			// init a new polygon object
		poly[polycnt]=new Object();
		poly[polycnt]["polygon"]=null;
		poly[polycnt]["pts"]=new Array(1);
		poly[polycnt]["pts"][1]=null;
	}
	if(inpfldonfocus == null){		// no arrow drawing if label input fields are active
		poly[polycnt]["pts"][0]=pt;	// set arrow origin
	}
}
// **********************************************************************************************************
function garrowdrag(point){			// launched by GEvent.addListener(map, 'mousemove', function(point){
	polycnt=poly.length-1;
	if(poly[polycnt] && poly[polycnt]["pts"][0]!=null){
		if(poly[polycnt]["polygon"])map.removeOverlay(poly[polycnt]["polygon"]);
		polygon = new GPolygon([poly[polycnt]["pts"][0],point], arrowlinecolor, 5, 1, arrowfillcolor, 0.2);
		map.addOverlay(polygon);
		poly[polycnt]["polygon"]=polygon;
		poly[polycnt]["pts"][1]=point;
	}
}
// **********************************************************************************************************
function garrowcalc(ptsc) {
	// calc the angle of the arrows origin and peak in pixels
	ptsx=new Array(1);
	ptsx[0]=map.fromLatLngToDivPixel(ptsc[0]);
	ptsx[1]=map.fromLatLngToDivPixel(ptsc[1]);
//debug("orig "+ptsx[0]);
//debug("dest "+ptsx[1]);
	dx=ptsx[1].x-ptsx[0].x;
	dy=ptsx[1].y-ptsx[0].y;
	ang=dy/dx; if(dx==0)ang=10000;
//debug("ang "+ang+"  dx "+dx+"  dy "+dy);
	// calc the length of the arrow head
	asize=0.3*Math.sqrt(dx*dx+dy*dy);
//debug("asize "+asize);
	asize=Math.min(asize,55);
	asize=Math.max(asize,35);
	asize=35;
	asize=asize*arrowheadlength/100;
	adyq=(asize*asize)*(ang*ang)/(1+ang*ang);
	ady=Math.sqrt(adyq);
	sgn=1;if(dy<0)sgn=-1;if(dy==0 && dx<0)sgn=-1;
	// the ref position of the arrow base (point on the line, where the wing-peaks are)
	abasey=ptsx[1].y-sgn*ady;
	abasex=ptsx[1].x-sgn*ady/ang; if(ang==0)abasex=ptsx[1].x-sgn*asize;
//debug("abasex "+abasex+"  abasey "+abasey); 
	// the ref position of the arrow wing base (point on the line, where the wings join the line)
	abasewy=ptsx[1].y-sgn*ady*0.8/arrowwingshrink*100;
	abasewx=ptsx[1].x-sgn*ady/ang*0.8/arrowwingshrink*100; if(ang==0)abasewx=ptsx[1].x-sgn*asize*0.8/arrowwingshrink*100;
//debug("abasewx "+abasewx+"  abasewy "+abasewy); 

	angn=-1/ang; if(ang==0)angn=10000;
	sgnx=-1;if(dx<0)sgnx=1;
	// calc the 'dy' of the wing peaks and the wing roots
	asizewing=asize*0.5; asizewing=asizewing*arrowwingsize/100;
	adywingq=(asizewing*asizewing)*(angn*angn)/(1+angn*angn);
	adywing=Math.sqrt(adywingq);
	asizewingroot=4;if(asize==35)asizewingroot=3;if(asize==55)asizewingroot=5; asizewingroot=asizewingroot*arrowlineweight/100
	adywingrootq=(asizewingroot*asizewingroot)*(angn*angn)/(1+angn*angn);
	adywingroot=Math.sqrt(adywingrootq);
//debug("adywing "+adywing+"  adywingroot "+adywingroot); 
	// wing peaks
	awingy=abasey+sgnx*adywing;
	awingx=abasex+sgnx*adywing/angn;
//debug("awingx "+awingx+"  awingy "+awingy); 
	gwingpoint=new GPoint(awingx,awingy);
	gwingleft=map.fromDivPixelToLatLng(gwingpoint);
	awingy=abasey-sgnx*adywing;
	awingx=abasex-sgnx*adywing/angn;
//debug("awingx "+awingx+"  awingy "+awingy); 
	gwingpoint=new GPoint(awingx,awingy);
	gwingright=map.fromDivPixelToLatLng(gwingpoint);
	// wing roots
	gwingpoint=new GPoint(abasewx+sgnx*adywingroot/angn,abasewy+sgnx*adywingroot);
//debug("gwingpoint "+gwingpoint);
	gwingrootleft=map.fromDivPixelToLatLng(gwingpoint);
	gwingpoint=new GPoint(abasewx-sgnx*adywingroot/angn,abasewy-sgnx*adywingroot);
	gwingrootright=map.fromDivPixelToLatLng(gwingpoint);
	// arrow orig positions
	gwingpoint=new GPoint(ptsx[0].x+sgnx*adywingroot/angn,ptsx[0].y+sgnx*adywingroot);
	gorigleft=map.fromDivPixelToLatLng(gwingpoint);
	gwingpoint=new GPoint(ptsx[0].x-sgnx*adywingroot/angn,ptsx[0].y-sgnx*adywingroot);
	gorigright=map.fromDivPixelToLatLng(gwingpoint);
	// define the arrow's vertices array
    ptsarrow = new Array(1);i=-1;
    i=i+1;ptsarrow[i] = gorigleft;
    i=i+1;ptsarrow[i] = gwingrootleft;
    i=i+1;ptsarrow[i] = gwingleft;
    i=i+1;ptsarrow[i] = ptsc[1];
    i=i+1;ptsarrow[i] = gwingright;
    i=i+1;ptsarrow[i] = gwingrootright;
    i=i+1;ptsarrow[i] = gorigright;

	return ptsarrow;
}
// **********************************************************************************************************
function garrowcancel() {
	if(!poly) return;
	polycnt=poly.length-1;
//debug("cancel "+polycnt);
	if(poly[polycnt]!=null){
		if(poly[polycnt]["polygon"])map.removeOverlay(poly[polycnt]["polygon"]);
		poly[polycnt]=new Object();
		poly[polycnt]["polygon"]=null;
		poly[polycnt]["pts"]=new Array(1);
		poly[polycnt]["pts"][0]=null;
		poly[polycnt]["pts"][1]=null;
	}
}
// **********************************************************************************************************
function glabeldraw(map,polycnt, glabellatlng) {
//debug("glabeldraw "+polycnt+" lang "+fldlang+" txt "+poly[polycnt]["glabeltext"][fldlang]+" hidden "+poly[polycnt]["glabeltext"][0]);
	if(poly[polycnt]["glabeltext"][0]=="hidden") return;
	glabelpix=map.fromLatLngToDivPixel(glabellatlng);
	glabeloffsetpix=new GSize(7.5,7.5);
	glabelhtmlact=glabeldivhtml.replace("_id", polycnt);
	txt=poly[polycnt]["glabeltext"][fldlang]; if(txt.length<2) txt=txt+"&nbsp;";
	glabelhtmlact=glabelhtmlact.replace("></div>", ">"+txt+"</div>");
	// draw the label
	poly[polycnt]["glabeloverlay"] = new ELabel(glabellatlng, glabelhtmlact, "elabelstyle", glabeloffsetpix, 85 );
	map.addOverlay(poly[polycnt]["glabeloverlay"]); 
	// calc pixel offset between the label-drag marker and the arrow's peak
	poslabelpix=map.fromLatLngToDivPixel(glabellatlng);
	pt1pix=map.fromLatLngToDivPixel(poly[polycnt]["pts"][1]);
	dx=poslabelpix.x-pt1pix.x;
	dy=poslabelpix.y-pt1pix.y;
	poly[polycnt]["labelpix"]=new GPoint(dx,dy);
	// add label-drag marker
	if(gmapedit){
		poly[polycnt]["glabelmarker"] = new GMarker(glabellatlng, {icon:glabelmarkericon, draggable: true, title:"Hier klicken und schieben"}); 
		map.addOverlay(poly[polycnt]["glabelmarker"]);
		poly[polycnt]["glabelmarker"].enableDragging();
	
		// label dragging using the marker as handle
		GEvent.addListener(poly[polycnt]["glabelmarker"], "drag", function(){
			// get the marker's current position
			actlatlng=poly[polycnt]["glabelmarker"].getLatLng();
			labeloffsetpix=new GSize(7.5,7.5);
			// define the label text
			txt=document.getElementById("glabeldivid"+polycnt).innerHTML;
			if(txt.indexOf("glabelinpid")>=0){
				txt=document.getElementById("glabelinpid"+fldlang+polycnt).value;
			}
			glabelhtmlact=glabeldivhtml.replace("_id", polycnt); 
			glabelhtmlact=glabelhtmlact.replace("></div>", ">"+txt+"</div>");
			// redraw the label at the marker's current position
			map.removeOverlay(poly[polycnt]["glabeloverlay"]);
			poly[polycnt]["glabeloverlay"] = new ELabel(actlatlng, glabelhtmlact, "elabelstyle", labeloffsetpix, 75 );
			map.addOverlay(poly[polycnt]["glabeloverlay"]); 
			// calc pixel offset between the label-drag marker and the arrow's peak
			poslabelpix=map.fromLatLngToDivPixel(actlatlng);
			pt1pix=map.fromLatLngToDivPixel(poly[polycnt]["pts"][1]);
			dx=poslabelpix.x-pt1pix.x;
			dy=poslabelpix.y-pt1pix.y;
			poly[polycnt]["labelpix"]=new GPoint(dx,dy);
		});
	}
	return;
}
// **********************************************************************************************************
function ginpfldclick(inpfld) {
	fldid=inpfld.id.substr(12,6);
//debug("ginpfldclick "+inpfld.id);
	inpfld.focus();inpfld.select();
	inpfldonfocus=inpfld.id;
	inpfldontxt=inpfld.value;

	return true;
}
// **********************************************************************************************************
function glabelclick(actdiv) {				// launched by the onclick DOM event registered by the label's 'div'
	if(!gmapedit) return;
	fldid=parseInt(actdiv.id.substr(11,6));
//debug("glabelclick top "+fldid);
	// get the current text
	txt=document.getElementById("glabeldivid"+fldid).innerHTML;
	// if no input field is present in the label then add one
	if(txt.indexOf("glabelinpid1"+fldid)<0){ 
		glabelinphtmlact=glabelinphtml.replace(/_id/g, fldid); 
		document.getElementById("glabeldivid"+fldid).innerHTML=glabelinphtmlact;
//debug("glabelinpid"+fldlang+fldid);
		if(poly[fldid]["glabeltext"][1]==null){ 
//debug("glabelchange init labeltext ");
			poly[fldid]["glabeltext"]=new Array(1);	
			for(cnt=0;cnt<4;cnt++){poly[fldid]["glabeltext"][cnt]=glabeldefaulttext;}
		}
		for(cnt=1;cnt<4;cnt++){
//debug("fill filds "+cnt+"  ");
			document.getElementById("glabelinpid"+cnt+fldid).value=poly[fldid]["glabeltext"][cnt];
		}
		inpfld=document.getElementById("glabelinpid"+fldlang+fldid);
		inpfld.focus();inpfld.select();
		inpfldonfocus=inpfld.id;
		inpfldontxt=txt;
	}
	// cancel the new arrow initiated by that click
	garrowcancel();
	return true;
}
// **********************************************************************************************************
function glabelchange(inpfld) {				// launched by the onchange DOM event registered by the label's 'input' field
//debug("glabelchange "+inpfld.id);
	inpfldonfocus=null;
	fldid=parseInt(inpfld.id.substr(12,6));
	fldlang=parseInt(inpfld.id.substr(11,1));
	// delete this arrow if the label's text has no length
	if(inpfld.value.length==0){
		map.removeOverlay(poly[fldid]["glabeloverlay"]);
		map.removeOverlay(poly[fldid]["glabelmarker"]);
		map.removeOverlay(poly[fldid]["polygon"]);
		poly[fldid]=null; 
	}else{
		poly[fldid]["glabeltext"][fldlang]=inpfld.value;
		for(cnt=1;cnt<4;cnt++){ 
			if(cnt!=fldlang){
				fldtmp=document.getElementById("glabelinpid"+cnt+fldid);
				if(fldtmp.value==glabeldefaulttext){
					poly[fldid]["glabeltext"][cnt]=poly[fldid]["glabeltext"][fldlang];
					fldtmp.value=poly[fldid]["glabeltext"][fldlang];
				}
			}
		}
	}
	return;
}
// **********************************************************************************************************
dockeydown=0;
function ondocumentkeydown(e)
{
	// from http://www.quirksmode.org/js/events_properties.html
	var keydowncode;
	if (!e) var e = window.event;
	if (e.keyCode) keydowncode = e.keyCode;
	else if (e.which) keydowncode = e.which;
	dockeydown=keydowncode;
//debug("dockeydown "+dockeydown);
	if(keydowncode==27){	// escape
		garrowcancel();
		if(document.getElementById(inpfldonfocus)){
			fldid=parseInt(inpfldonfocus.substr(12,6));
debug("escape key [glabeldivid"+fldid+"] = "+inpfldontxt);
			document.getElementById("glabeldivid"+fldid).innerHTML=inpfldontxt;
			inpfldontxt="";
		}
		inpfldonfocus=null;
	}
	if(keydowncode==13){	// return
debug("enter key focus "+inpfldonfocus);
		if(inpfldonfocus != null){
			inpfld=document.getElementById(inpfldonfocus);
			fldid=parseInt(inpfldonfocus.substr(12,6));
//debug("enter key change "+fldid);
			glabelchange(inpfld);
			// remove the input field and display pure 'text in div'
//debug("enter key set "+inpfld.value);
			if(document.getElementById("glabeldivid"+fldid))document.getElementById("glabeldivid"+fldid).innerHTML=inpfld.value;
			inpfldonfocus=null;
		}
	}
}
function ondocumentkeyup(e)
{
	dockeydown=0;
}
// **********************************************************************************************************
var mapsizerefwidth=0;
var mapsizerefheight=0;
var mapsizemaxwidth=0;
var mapsizemaxheight=0;
function mapresizestart(ev, objid, func){
	mapsizerefwidth=parseInt(document.getElementById("gmap").style.width);
	mapsizerefheight=parseInt(document.getElementById("gmap").style.height);
	mapsizemaxwidth=parseInt(document.getElementById("gmapparent").style.width)-10;
	mapsizemaxheight=parseInt(document.getElementById("gmapparent").style.height)-10;
	
	map.showControls();
	dragStart(ev, objid, 'mapresizego');
}
function mapresizego(dx, dy){
	dx=dx+mapsizerefwidth;
	dy=dy+mapsizerefheight;
	actwidth=Math.min(dx,mapsizemaxwidth);
	actheight=Math.min(dy,mapsizemaxheight);
	actwidth=Math.max(actwidth,235);
	actheight=Math.max(actheight,110);
	if(actwidth>mapsizemaxwidth || actheight>mapsizemaxheight){
		document.getElementById("gmapparent").style.background="red";
	}else{
		document.getElementById("gmapparent").style.background="transparent";
		if(actheight<300){
			if(controlzoomactive!=controlzoomsmall){
				map.removeControl(controlzoomlarge);
				map.addControl(controlzoomsmall);
				controlzoomactive=controlzoomsmall;
			}
		}else{
			document.getElementById("gmapparent").style.background="transparent";
			if(controlzoomactive!=controlzoomlarge){
				map.removeControl(controlzoomsmall);
				map.addControl(controlzoomlarge);
				controlzoomactive=controlzoomlarge;
			}
		}
	}
	document.getElementById("mapsize").style.top=actheight+"px";
	document.getElementById("mapsize").style.left=actwidth+"px";
	document.getElementById("gmap").style.height=actheight+"px";
	document.getElementById("gmap").style.width=actwidth+"px";
	if(document.getElementById("mapsizelabel")){
		document.getElementById("mapsizelabel").innerHTML=actwidth+"x"+actheight;
	}
	map.checkResize();
}
// lib stuff -------------------------------------------------------------------
var timdebref=0;
function debug(txt) {
	if(!document.getElementById('debug')) return;
	if(typeof init == "undefined") init=0;
	tim=new Date();
	txtexist="";if(!init)txtexist=txtexist+document.getElementById('debug').innerHTML;
	s=tim.getSeconds();
	ms=tim.getMilliseconds();
	tmidebact=s+ms/1000;
	if(tmidebact<timdebref)	tmidebact=tmidebact+60;
	if(txtexist.length<10 || !timdebref || tmidebact<timdebref){
		timdebref=s+ms/1000;
	}
	tmidebact=tmidebact-timdebref;
	document.getElementById('debug').innerHTML=txtexist+"<br>"+tmidebact.toFixed(3)+": "+txt;
}
var mousecoords={x:0, y:0};
function ondocumentmousemove(ev){
	ev= ev || window.event;
	mousecoords = getmousecoords(ev);

	if (mouseposref!=null){
		dx=mousecoords.x-mouseposref.x;
		dy=mousecoords.y-mouseposref.y;
	}
	return true;
}
function getmousecoords(ev){
	if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};	//firefox
	}
	return {								//msie
		x:ev.clientX + document.body.scrollLeft + document.documentElement.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop + document.documentElement.scrollTop - document.body.clientTop
	};
}

