var websisUrl = "http://" + top.hostName + "/"+ top.VirtualDir;

// array with symbol-information
var aSymbols = new Array();

// array with symbols to highlight
var aHighlightSymbols = new Array();

// global var if click-on symbol
var bSymbolClick = false;

var blinkTimer;
var blinkState = 0;
var blinkCount = 0;
var bRewriteMap = false;
var bRequestWithoutMap = false;

function writeMapWithSymbols(sMapURL, sScaleURL){

	var zoomLevelCopyrightURL;
	var zoomLevelCopyrightWebUrl;
	zoomLevelCopyrightURL = top.aZoomLevel[top.currentZoomLevel]["CopyrightURL"];
	zoomLevelCopyrightWebUrl = top.aZoomLevel[top.currentZoomLevel]["CopyrightWebUrl"];
	if (zoomLevelCopyrightURL.length == 0){
	    zoomLevelCopyrightURL = "copyright_layer_4.png";
	}

    var theDoc = MapFrame.document;

    // retrieve the current visibility of the pin and the coords
    var thePin = theDoc.getElementById("pin");
    var pinVisibility = thePin.style.visibility;
    var pinTop = thePin.style.top;
    var pinLeft = thePin.style.left;

    // write the document
    theDoc.writeln ('<html><head><title>MapFrame</title></head>');
    theDoc.writeln ('<body bgcolor="White" onResize="parent.refreshMap()" onload="parent.startUpEventHandler()">');

    //  Create the zoom box CSS
    theDoc.writeln ('<div id="zoomBox" style="border-bottom:red 2px solid; border-left:red 2px solid; border-right:red 2px solid; border-top:red 2px solid; overflow:hidden;position:absolute;z-index:100; visibility:hidden"></div>');
    theDoc.writeln ('<div id="mpPin" style="position:absolute; z-index:100; visibility:hidden"><img src="../img/mp_pin.gif" width="16" height="16" border="0"></div>');

    // create the pin
    theDoc.writeln ('<div id="pin" style="position:absolute; top:'+ pinTop +'; left:'+ pinLeft +'; visibility:'+pinVisibility+';z-index:10;width:80;height:80"><img src="../img/pin.gif"></div>');

    // tooltip for panning
    theDoc.writeln ('<div id="tip" style="z-index:20;color:#000000;font-family:Arial;font-size:12px;visibility:hidden;position:absolute;border-style:solid;border-width:1px;border-color:#000000;background-color:#FFFFCE;padding:2px;left:10;top:70;width:160;text-align:center">');
    theDoc.writeln ('Über den Verschiebe-Button<br><img src="../img/pan_1.gif" alt="verschieben" border="0"><br>können Sie komfortabel den Bildausschnitt verschieben!</div>');

    // Map image.  Display "Loading ..." message on first hit
    theDoc.writeln ('<form name="mapForm">')
    theDoc.writeln ('<div id="theMapDiv" name="theMapDiv" style="position:absolute; left:0; top:0; border:0px" onMouseOut="parent.stopDisplayMapCoords();">');
    theDoc.writeln (writeMapDiv(sMapURL, sScaleURL, minx, miny, maxx, maxy, mWidth, mHeight, "map"));
    theDoc.writeln ('</div></form>');

    // retrieving picture
    theDoc.writeln ('<div id="retrieving" style="position:absolute; top:0; left:0; visibility:visible;"><img src="'+websisUrl+'/img/retrieving.gif"></div>');

    // scale factor image
    //theDoc.writeln ('<div id="scale" style="z-index:20;color:#000000;font-family:Arial;font-size:12px;visibility:visible;position:absolute;padding:2px;right:0;bottom:0;width:400;height:30;text-align:center">');
    //theDoc.writeln ('<img id="theScaleImage" name="theScaleImage" src="img/scale_factor.png" align="right">');
    //theDoc.writeln ('</div>');

    // copyright image
    theDoc.writeln ('<div id="copyright" style="visibility:visible;position:absolute;left:0;bottom:0;text-align:center">');
    theDoc.writeln ('<a href="'+zoomLevelCopyrightWebUrl+'" id="copyrightWebUrl" name="theCopyrightWebUrl" target="_blank" style="visibility:visible">');
    theDoc.writeln ('<img id="copyrightImage" name="theCopyrightImage" src="../img/copyright/' + zoomLevelCopyrightURL + '" style="visibility:visible;border:0px">');
    theDoc.writeln ('</a>');
    theDoc.writeln ('</div>');

	//'./img/copyright/' + zoomLevelCopyrightURL
    //MapFrame.document.getElementById('theCopyrightImage').src = top.ImagePath + 'copyright/' + zoomLevelCopyrightURL;

    theDoc.writeln ('</body></html>');

    theDoc.close();

     // set the cursor
    setToolModeCursor();

    // reset the false;
    bRewriteMap = false;
}


function writeMapDiv (sMapURL, sScaleURL, llx, lly, urx, ury, iWidth, iHeight, sMode) {

    var theHTML = '<img id="theMapImage" name="theMapImage" src="'+sMapURL+'" border="0">';

    var upp =  iWidth/(Math.abs(llx-urx))/2;
    // now display the symbols
    for (var i=0; i<aSymbols.length; i++) {
        var id      = aSymbols[i]["ID"];
        var symbol  = aSymbols[i]["Symbol"];
        var tooltip = aSymbols[i]["ToolTip"];
        var url     = aSymbols[i]["URL"];
        var x       = aSymbols[i]["X"];
        var y       = aSymbols[i]["Y"];
        var widthSym  = aSymbols[i]["Width"];
        var heightSym =  aSymbols[i]["Height"];

        // transform the coords to pixel-coords
        var theCoord = getPixelXY(x,y,llx, lly, urx, ury, iWidth, iHeight);

        if (upp < 0.2) { upp=0.2; };
        if (upp > 0.5) { upp=0.5; };

        // scale the symbol dependent on map-scale
        widthSym =  parseInt(widthSym * (upp *2));
        heightSym = parseInt(heightSym * (upp *2));

        var widthDiv = widthSym + (top.SymbolBorderWidth*2);
        var heightDiv = heightSym + (top.SymbolBorderWidth*2);

        // calculate the position
        x = theCoord[0] - widthDiv/2;
        y = theCoord[1] - heightDiv/2;

        // write the symbol
        theHTML += "<div id=\""+id+"\" style=\"position:absolute;left:"+x+";top:"+y+";border:0px;z-index:5;width:"+widthDiv+";height:"+heightDiv+";\">";

        if (sMode == "print") {
            theHTML += "<img src=\"symbole/"+symbol+"\" alt=\""+tooltip+"\" border=\"0\" width=\""+widthSym+"\" height=\""+heightSym+"\" style=\"position:absolute;top:"+top.SymbolBorderWidth+";left:"+top.SymbolBorderWidth+"\">";
        }
        else {
            if (url != 'null'){ var jsFcn="parent.showLink('"+url+"');"; }
            else { var jsFcn="parent.showLink('');"; }

            theHTML += "<a onmouseup=\""+jsFcn+";\" onmousedown=\"parent.setSymbolClick();\" onmouseover=\"parent.hoverSymbol('"+id+"','"+top.MouseOverColorSymbol+"','over');\" onmouseout=\"parent.hoverSymbol('"+id+"','transparent','out');\">";

            var currentWebsisUrl = websisUrl + "/symbole/" + symbol;
            theHTML += "<img src=\"" + currentWebsisUrl + "\" alt=\""+tooltip+"\" border=\"0\" width=\""+widthSym+"\" height=\""+heightSym+"\" style=\"position:absolute;top:"+top.SymbolBorderWidth+";left:"+top.SymbolBorderWidth+"\"></a>";
        }

        theHTML += "</div>";
    }

    // scale factor image
    theHTML += '<div id="scale" style="z-index:20;color:#000000;font-family:Arial;font-size:12px;visibility:visible;position:absolute;padding:2px;right:0;bottom:0;width:400;height:30;text-align:center">';
    theHTML += '<img id="theScaleImage" name="theScaleImage" src="'+sScaleURL+'" align="right">';
    theHTML += '</div>';

    //alert (theHTML);
    return theHTML;
}

function getSymbols () {
    // delete old array
    aSymbols = new Array();

    var thePostForm = PostFrame.document.forms[0];
    var symCount = thePostForm.symCount.value;

    // store every symbol in the array "aSymbols"
    for(var i=0; i<symCount; i++) {
        theSym = PostFrame.document.getElementsByName("sym"+i)[0].value;

        aSymbols[i] = new Array();
        aSymbols[i]["ID"]= theSym.split("|||")[0];
        aSymbols[i]["Symbol"]= theSym.split("|||")[2];
        aSymbols[i]["Width"]= theSym.split("|||")[3];
        aSymbols[i]["Height"]= theSym.split("|||")[4];
        aSymbols[i]["ToolTip"]= theSym.split("|||")[5];
        aSymbols[i]["X"] = theSym.split("|||")[6];
        aSymbols[i]["Y"] = theSym.split("|||")[7];
        aSymbols[i]["URL"]= theSym.split("|||")[8];
    }
}


// find div with smybol-id and hightlight
function hoverSymbol (theID, theColor, theMode) {
    var theDiv =  MapFrame.document.getElementById(theID);
    theDiv.style.backgroundColor = theColor;

    if (theMode == 'out') {
      bSymbolClick = false;
    }
}

// find div with smybol-id and hightlight
function highlightSymbol () {

    var theID = aHighlightSymbols[0];
    var theDiv =  MapFrame.document.getElementById(theID);

    blinkTimer = window.setInterval("alternateBackground();",500);
}



function alternateBackground (theDiv){
    var theID = aHighlightSymbols[0];
    var theDiv =  MapFrame.document.getElementById(theID);

    if (theDiv){
        if (blinkState == 0) {
            blinkState = 1;
            theDiv.style.backgroundColor = top.HighlightColorSymbol;
        }
        else {
            blinkState = 0;
           theDiv.style.backgroundColor = '#FFFFFF';
        }
    }
    blinkCount++;

    if (blinkCount >= 6) { stopBlinkTimer(); }
}


function stopBlinkTimer() {

    window.clearInterval(blinkTimer);

    // leave background highlighted
    if (blinkState == 0) {
        // get the symbol
        var theID = aHighlightSymbols[0];
        var theDiv =  MapFrame.document.getElementById(theID);

        if (theDiv) {
            theDiv.style.backgroundColor = top.HighlightColorSymbol;
        }
    }
    blinkState = 0;
    blinkCount = 0;

    // reset the highlight array
    aHighlightSymbols = new Array();
}


// add a symbol to the requested symbols
function addSymbol (theID) {

    // check if symbol already exits
    var sTemp = "," + aObjectIDsToShow.join (",")+ ",";

    // if value not found -> add id
    if (sTemp.indexOf(","+theID+",") == -1) {
        // add id
        aObjectIDsToShow.push(theID);
        // force map to redraw
        bRewriteMap = true;
    }
}

// delete a symbol from the diplayed symbols
function deleteSymbol (theID) {

    var aTemp = new Array();
    for (var i=0, j=0; i<aObjectIDsToShow.length-1; i++) {
        if (aObjectIDsToShow[j] == theID){ j++; }
        aTemp[i] = aObjectIDsToShow[j];
        j++;
    }
    aObjectIDsToShow = aTemp;
    // force map to redraw
    bRewriteMap = true;
}

function turnOnSymbols (theGroupID) {
    // if symbols is already blinking stop timer
	stopBlinkTimer();

	// reset array with symbols
    aSymbols = new Array();

    // add id of group to show the symbols
    addSymbol(theGroupID);

    // request the new map
    bRequestWithoutMap = true;
    bRewriteMap = true;
    getMap();
}

function turnOffSymbols (theGroupID) {

    // reset array with symbols
    aSymbols = new Array();

    // if symbols is already blinking stop timer
	stopBlinkTimer();

    // add id of group to show the symbols
    deleteSymbol(theGroupID);

    // request the new map
    bRequestWithoutMap = true;
    bRewriteMap = true;
    getMap();
}
