var toolMode=0;     //initialize toolMode to no selected tool
var thePrintMapWindow;  // refernce to the printmap window
/*
 * set tool mode dependent on click
 */
function clickFunction (toolName) {
    switch(toolName) {
        case "zoomin":      // zoom in mode
            toolMode = 1;
            break
        case "zoomout":     // zoom out mode
            toolMode = 2;
            break
        case "pan":         // pan mode
            toolMode = 3;
            break
        case "identify":    // get feature info
            toolMode = 4;
            break
        case "meeting":    // meeting point
            toolMode = 5;
            break
        case "create":
			toolMode = 6;	// create Object
			break
    }
    setToolPic ();  // set the button
    setToolModeCursor();    // set the cursor
}
/*
 * set the cursor dependent on the tool mode
 */
function setToolModeCursor () {
    var theDocument = MapFrame.document;
    switch(toolMode) {
        case 1:     // zoom in mode
            theDocument.getElementById("theMapDiv").style.cursor = "crosshair";
            break
        case 2:     // zoom out mode
            theDocument.getElementById("theMapDiv").style.cursor = "crosshair";
            break
        case 3:     // pan mode
            theDocument.getElementById("theMapDiv").style.cursor = "move";
            break
        case 4:     // get feature info
            if (isIE5)  { theDocument.getElementById("theMapDiv").style.cursor = "hand"; }
            else { theDocument.getElementById("theMapDiv").style.cursor = "pointer"; }
            break
        case 5:     // meeting point
            theDocument.getElementById("theMapDiv").style.cursor = "crosshair";
            break
        case 6:     // create object
            theDocument.getElementById("theMapDiv").style.cursor = "crosshair";
            break
        case 7:     // center coords
            theDocument.getElementById("theMapDiv").style.cursor = "hand";
            break
    }
}
/*
 * Replace the existing image with an image that represents a selected tool
 */
function setToolPic() {
    clearToolPic();         // set all tools to "none-selected"
    var theDocument = ToolFrame.document;
    switch(toolMode) {
        case 1:
            theDocument.DZoomIn.src="img/zoomin_2.gif";
            break;
        case 2:
            theDocument.DZoomOut.src="img/zoomout_2.gif";
            break;
        case 3:
            theDocument.DPan.src="img/pan_2.gif";
            break;
        case 4:
            if (typeof(theDocument.DIdentify) != 'undefined') { theDocument.DIdentify.src="img/identify_2.gif"; }
            break;
        case 5:
            theDocument.MeetingP.src="img/meetingpoint_2.gif";
            break;

    }
    return false;
}
/*
 * reset all tool images to none-selected
 */
function clearToolPic() {
    var theDocument = ToolFrame.document;
    theDocument.DZoomIn.src="img/zoomin_1.gif";
    theDocument.DZoomOut.src="img/zoomout_1.gif";
    theDocument.DPan.src="img/pan_1.gif";
    if (typeof(theDocument.DIdentify) != 'undefined') { theDocument.DIdentify.src="img/identify_1.gif";}
    if (typeof(theDocument.MeetingP) != 'undefined') { theDocument.MeetingP.src="img/meetingpoint_1.gif";}
}
/*
 * handle all mousedown events for map image
 */
function mapTool(e){
    if (!e) var e = MapFrame.event;
    hidePin();       // first hide the pin

    if (bSymbolClick != true){
        switch(toolMode) {
            case 1:     //call startZoomBox()
                startZoomBox(e);
                return false;
                break
            case 2:     // zoom out
                startZoomBox(e);
                return false;
                break
            case 3:     // pan
                startPan(e);
                return false;
                break
            case 4:     // feature info
                identifyByClick(e);
                return false;
                break
            case 5:     // meeting point
                showMeetingPoint(e);
                return false;
                break
            case 6:		// create Object
            	 parent.displayAdminMapCoords(e);
            	 return false;
            	 break
            case 9:
                //showLink (sSymbolURL);
                return false;
                break;
        }
    }
}
/*
 * handle events during move of mouse
 */
function getMouse(e){
    if (!e) var e = MapFrame.event;  // IE provides events in another way
    displayMapCoords(e);             // show the coords in statusbar
    switch(toolMode) {
        case 1:     // zoom in: resizeZoomBox()
            resizeZoomBox(e);
            return false;
            break
        case 2:     // zoom out: resizeZoomBox()
            resizeZoomBox(e);
            return false;
            break
        case 3:     // pan: move image
            panMap(e);
            return false;
            break
        case 5:     // meeting point: move pin
            panMpPin(e);
            return false;
            break
        case 9:     // cursor over symbol
            return false;
            break;
    }
}
/*
 * handle all mouseup events for map image
 */
function chkMouseUp(e){
    if (!e) var e = MapFrame.event;

    if (bSymbolClick != true) {
        switch(toolMode) {
            case 1:         //zoom in: stopZoomBox()
                stopZoomBox();
                stopZoomIn();
                return false;
                break
            case 2:         // zoom out
                stopZoomBox();
                stopZoomOut();
                return false;
                break
            case 3:         // pan
                stopPan(e);
                return false;
                break
        }
    }

    // reset ->
    bSymbolClick = false;
}
/*
 * feature by click
 */
function identifyByClick(e){
	var pixelTolerance = 3;
	var xIdentify = e.clientX;      // get current mouse coordinates
	var yIdentify = e.clientY;
	// apply the pixelTolerance to define the minimum and maximum x and y values for the search envelope
	pminxId = xIdentify - (pixelTolerance/2);
	pminyId = yIdentify + (pixelTolerance/2);
	pmaxxId = xIdentify + (pixelTolerance/2);
	pmaxyId = yIdentify - (pixelTolerance/2);
	// convert the minimum x and y values from pixel to map coordinates
	var thePointMin = getMapXY(pminxId,pminyId);
	// set global variables to define ENVELOPE in getfeatureinfo request
	minxId = thePointMin[0]
	minyId = thePointMin[1]
	// convert the maximum x and y values from pixel to map coordinates
	var thePointMax = getMapXY(pmaxxId,pmaxyId);
	// set global variables to define ENVELOPE in getfeatureinfo request
	maxxId = thePointMax[0]
	maxyId = thePointMax[1]
	// call function dependent on the associated feature_info
	var theFunction = zoomLevelFeatureInfo;
	// requested function is a url -> open a new window
	if (theFunction.indexOf('http://') != -1){
	    // add coords to the request
	    theFunction += '?minx='+minxId+'&miny='+minyId+'&maxx='+maxxId+'&maxy='+maxyId;
	    openWindow('FeatureInfo', theFunction);
	}
	else {
	    var theFunction = new Function(theFunction);
	    theFunction ();
	}
	// show the pin
	if (zoomLevelFeatureInfo.length > 0 && toolMode == 4 ) { displayPin ((minxId+maxxId)/2, (minyId+maxyId)/2); }
}
/*
 * feature info by gk-coords
 */
function identifyByXY(xIdentify, yIdentify, featureInfoFunction){
	var tolerance = 1;
	// apply the pixelTolerance to define the minimum and maximum x and y values for the search envelope
	minxId = xIdentify - (tolerance/2);
	minyId = yIdentify + (tolerance/2);
	maxxId = xIdentify + (tolerance/2);
	maxyId = yIdentify - (tolerance/2);

	if (featureInfoFunction){
	    theFunction = featureInfoFunction;
	}
	else {
	    // call function dependent on the associated feature_info
	    theFunction = zoomLevelFeatureInfo;
	}
	// requested function is a url -> open a new window
	if (theFunction.indexOf('http://') != -1){
	    // add coords
	    theFunction += '?minx='+minxId+'&miny='+minyId+'&maxx='+maxxId+'&maxy='+maxyId;
	    openWindow('FeatureInfo', theFunction);
	}
	else {
	    var theFunction = new Function(theFunction);
	    theFunction ();
	}
	// show the pin
	displayPin(xIdentify, yIdentify);
}

/*
 * show the help
 */
function showHelp (){
    openWindow ('help', top.helpURL);
}
/*
 * show the feedbackform
 */
function showFeedback() {
    openWindow ('feedback', top.feedbackURL);
}
/*
 * click Acction on Symbol
 */
function setSymbolClick() {
    bSymbolClick=true;
}

/*
 * show link function
 */
function showLink(theURL, Mode) {
    setSymbolClick();

    var flag = false;

    for(var i = 0; i < theURL.length; i++){
		var letter = theURL.charAt(i);
		if (letter == " "){
		}
		else{
			flag = true;
		}
	}

    if ( theURL.length > 0 && flag == true) {
        if (Mode){newwin = openWindow ('link', theURL, Mode); }
        else  { newwin = openWindow ('link', theURL); }
        newwin.focus();
    }
    else {
        var URL = 'http://' + hostName + '/'+ top.VirtualDir +'/nolink.jsp?nolink_message=' + top.sNoLinkMessage;
        noLinkWindow = openWindow('nolink',URL);
        //alert (top.sNoLinkMessage);
    }
    return false;
}
/*
 * show print map window
 */
function showPrintMap() {
    var URL = 'http://' + hostName + '/'+ top.VirtualDir +'/PrintMap.jsp';
    thePrintMapWindow = openWindow ('print', URL);
}
/*
 * show the meetingpoint-mail-form
 */
function showMeetingPoint(e) {

    var xIdentify = e.clientX;      // get current mouse coordinates
	var yIdentify = e.clientY;
    var thePoint = new Array();
    thePoint = getMapXY(xIdentify, yIdentify);

    var URL = 'http://' + hostName + '/'+ top.VirtualDir +'/jsp/' + top.meetingPointURL + '?x='+ thePoint[0]+ '&y=' + thePoint[1];
    openWindow ('meetingpoint', URL, "top=0,left=0,scrollbars=no,resizable=yes,status=no,toolbar=no,menubar=no,location=no,width=300,height=400");

    toolMode = 0;
    clearToolPic();         // set all tools to "none-selected"
    MapFrame.document.getElementById("mpPin").style.visibility = "hidden";
}
/*
 * common function to open a window
 */
function openWindow (Name, URL, Mode) {
    var winParams;
    if (Mode){
        winParams = Mode;
    }
    else {
        winParams = "top=0,left=0,scrollbars=yes,resizable=yes,status=no,toolbar=no,menubar=no,location=no,width=685,height=600";
    }
    newWin = window.open(URL, Name, winParams);
    newWin.focus(self);

    return newWin;
}
