

/******************************************************************************

 *

 * Purpose: core p.mapper functions (init, user interaction, open popups) 

 * Author:  Armin Burger

 *

 ******************************************************************************

 *

 * Copyright (c) 2003-2006 Armin Burger

 *

 * This file is part of p.mapper.

 *

 * p.mapper is free software; you can redistribute it and/or modify

 * it under the terms of the GNU General Public License as published by

 * the Free Software Foundation; either version 2 of the License, or

 * (at your option) any later version. See the COPYING file.

 *

 * p.mapper is distributed in the hope that it will be useful,

 * but WITHOUT ANY WARRANTY; without even the implied warranty of

 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the

 * GNU General Public License for more details.

 *

 * You should have received a copy of the GNU General Public License

 * along with p.mapper; if not, write to the Free Software

 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

 *

 ******************************************************************************/





// Global for resize timer/setTimeout

var resize_timer;





/*

myScalebar = new ScaleBar(1);

myScalebar.divisions = 3;

myScalebar.subdivisions = 2;

myScalebar.minLength = 150;

myScalebar.maxLength = 250;

myScalebar.place('scalebar');

*/





/**************************************************** 

 * LOAD MAP IMAGE INTO PARENT WINDOW MAP DIV

 * Reset parameters of some DIV's

 ****************************************************/

function loadMapImg(mapImgSrc) {

    // SWAP MAP IMG

    var theMapImg = $("mapImg");

		 //Bei der Suche müssen die JavaScript "Pfade" angepasst werden, da die Attributeaus einem Popup-Window aufgerufen werden

		 //Sonst wird keine Karte geladen

		if (theMapImg==null)

		{

		var theMapImg = opener.$("mapImg");

		theMapImg.src = mapImgSrc;

		//alert (theMapImg.src+' themapimg.src pmapper.js');

		}

		else {

    theMapImg.src = mapImgSrc;

		//alert (theMapImg.src+' themapimg.src pmapper.js');

		}

}



function resetMapImgParams() {

    var theMapImgL = $("mapimgLayer");

    var theMapImg = $("mapImg");

		theMapImg.style.width = mapW+"px";

    theMapImg.style.height = mapH+"px";

    

    theMapImgL.style.top  = 0+"px";  

    theMapImgL.style.left = 0+"px";

    theMapImgL.style.width = mapW+"px";

    theMapImgL.style.height = mapH+"px";

    

    theMapImgL.style.clip = 'rect(auto auto auto auto)';  // NEEDED TO RESET DIV TO NON-CLIPPED AND ORIGINAL SIZE

    

    $("zoombox").style.visibility = 'hidden';

    $('loading').style.visibility = 'hidden';

    

    maploading = false;

    

    var varformMode = $("varform").mode.value;

    if (varformMode == 'measure') {

        resetMeasure();

        polyline = toPxPolygon(geoPolyline);

        if (polyline.getPointsNumber()>0) {

            drawPolyline(jg,polyline);

        }

    }

}





/**

 * Initialize function; called by 'onload' event of map.phtml

 * initializes several parameters by calling other JS function

 */

function pm_init() {

    // Add Resize Event to main window   

    window.onresize = function(){initLayout();};

    initLayout();



    // Add properties to mapImg

    var imgTmpMap = $("mapImg");

    imgTmpMap.onload = resetMapImgParams;

    imgTmpMap.onmouseover = startUp;

    //$('map').onmouseout = hideIQL;

    //imgTmpMap.onmouseover = pm_zoombox.startUp;

    

    // Initialize TOC/legend

    initToc();

    

    // Set zoombox class for Opera and Konqueror to non-semitransparent

    if (navigator.userAgent.match(/Opera|Konqueror/i)) {

        $("zoombox").className = 'zoombox_nontransp';

    }

    

    // If pm_master exists, update 'm_offs...' values

    if (document.getElementById('pm_master')) {

        m_offsX = Layout.MasterLeft;

        m_offsY = Layout.MasterTop;

    }

    

    createZSlider('zslider');

    //createSearchOptions();

    domouseclick('zoomin');

    setTbTDButton('zoomin');

    

}   







/**

 * Update s1 value for slider settings

 */

function updateSlider_s1(pixW, pixH) {

    var maxScale1 = ((dgeo_x * dgeo_c) / pixW) / (0.0254 / 96);

    var maxScale2 = ((dgeo_y * dgeo_c) / pixH) / (0.0254 / 96);

    s1 = Math.max(maxScale1, maxScale2);

}







function pMap () {

    //this.setMapProperties = pMap_setMapProperties;

    //this.getMapProperties = pMap_getMapProperties;

    this.getMapScale = pMap_setMapScale;

    this.RefMapW = pMap_getRefMapW;

    this.RefMapH = pMap_getRefMapH; 

}



function pMap_setMapProperties(width, height, scale) {

    var mapimgLayer = $('mapimgLayer');

    

    mapimgLayer.style.width = width;

    mapimgLayer.style.height = height;

    

    this.width  = width;

    this.height = height;

    this.scale  = scale;

}





function pMap_getMapProperties() {

    var width = this.width;

    var height = this.height; 

}





function pMap_setMapScale(scale) {

    //alert (scale);

    this.scale = scale;

}



function pMap_getMapScale() {

    //alert(this.scale);

    return this.scale;

}





function pMap_setRefMapW(refW) {

    this.RefMapW = refW;

}



function pMap_setRefMapH(refH) {

    this.RefMapH = refH;

}



function pMap_getRefMapW() {

    return this.RefMapW;

}



function pMap_getRefMapH() {

    return this.RefMapH;

}













/*****************************************************************************

 * SWAP FUNCTIONS FOR TOOLBAR TD -> USE ALTERNATIVELY TO IMAGE SWAP

 * Changes TD class (default.css -> .TOOLBARTD...) in toolbar

 ********************************************************************/

// Function for state buttons (CLICKED TOOLS: zoomin, pan, identify, select, measure)

function setTbTDButton(button) {

    //var tdarr = document.getElementsByName('tbtd');  //!!!!! DOES NOT WORK WITH CRAPPY IE !!!!

    var tdarr = document.getElementsByTagName('td');

    for (var i = 0; i < tdarr.length; i++) {

        var tdid = tdarr[i].id;

        //alert(tdid);

        //if (tdid != 'tsep') {

        

        //!!!!! WORKAROUND FOR CRAPPY IE !!!!

        if (tdid == 'zoomin' || tdid == 'zoomout' || tdid == 'pan' || tdid == 'identify' || tdid == 'select' || tdid == 'measure' || tdid == 'poi' || tdid == 'auto_identify') {

            if (tdid != button) {

                rmHighlTD(tdid)

            } else {

                highlTD(tdid);

            }

        }

    }

}



function highlTD(elId) {

    var but = $(elId);

    if (but) but.className='TOOLBARTD_ON';

}



function rmHighlTD(elId) {

    //alert(elId);

    var but = $(elId);

    if (but) but.className='TOOLBARTD';

}





// Functions for MouseOver/Out

function TbOverOut(elId, status){

    var but = $(elId);

    if (but) {

        if (but.className != 'TOOLBARTD_ON') {

            if (status == 'on') {

                but.className='TOOLBARTD_OVER';

            } else {

                but.className='TOOLBARTD';

            }

        }

    }

}



// MouseDown/Up, only set for stateless buttons

function TbDownUp(elId, status){

    var but = $(elId);

    if (status == 'd') {

        but.className='TOOLBARTD_ON';

    } else {

        but.className='TOOLBARTD';

    }

}



function changeButtonClr(myObj, myAction) {

    switch (myAction) {

        case 'over':

            myObj.className = 'button_on';

            break;

            alert(myAction);

            

        case 'out':

            myObj.className = 'button_off';

            break;

    }

}









/*****************************************************************************

 * IMAGE SWAP FUNCTIONS FOR TOOLBAR

 * swaps images from imgname_on.gif to imgname_off.gif and vice versa

 *********************************************************************/

// SWITCH IMAGE OF CLICKED TOOL TO 'ON', ALL OTHERS TO 'OFF'

function setButton(button) {

    var imgarr = document.getElementsByTagName('img');

    for (var i = 0; i < imgarr.length; i++) {

        var butid = imgarr[i].id;

        if (butid != 'sep') {

            if (butid != button) {

                setImg(butid, 'off');

            } else {

                setImg(butid, 'on');

            }

        }

    }

}



// set image to ON or OFF

function setImg(obj, status){

    var source = 'images/buttons/' + obj + '_' + status + '.gif';

    imgobj = $(obj);

    imgobj.src = source;

}









/**************************************************

 * Set cursor symbol according to tool selection

 *************************************************/

// return root path of application

function getRootPath() {

	var theLoc = document.location.href;

	var theLastPos = theLoc.lastIndexOf('/');

	var RootPath = theLoc.substr(0,theLastPos) + '/';

	

	return RootPath;

}



/** 

 * set the cursor to standard internal cursors

 * or special *.cur url (IE6+ only)

 */

function setCursor(rmc) {	

    if (!rmc) {

    	var varform = $("varform");

        if (varform) {

            var toolType = varform.tool.value;

        } else {

            var toolType = 'zoomin';

        }

    } else {

        toolType = 'pan';

    }



    

    /* Define settings for cursor to be used for tools

       set to true if you want to use the same cursors for all browsers (incl. IE) */

    var internalCursor = ((navigator.version < 6) || (navigator.appName == 'Netscape'));

    //var internalCursor = true; 

    

    var rootPath = getRootPath();

    var usedCursor = (internalCursor) ? toolType : "url(" +rootPath + "images/cursors/zoomin.cur)";

    

    $('mapimgLayer').style.cursor = usedCursor;

    

    switch (toolType) {

		case "zoomin" :

			var usedCursor = (internalCursor) ? 'crosshair' : 'url("' +rootPath + 'images/cursors/zoomin.ani")';	

			break;

        

        case "zoomout" :

			var usedCursor = (internalCursor) ? 'e-resize' : 'url(' +rootPath + 'images/cursors/zoomout.cur)';	

			break;

        

        case "identify" :

			//var usedCursor = (internalCursor) ? 'help' : 'url(' +rootPath + 'images/cursors/identify.cur)';	

			var usedCursor = 'help';	

            break;

        

        case "auto_identify" :	

			var usedCursor = 'pointer';	

            break;



        case "pan" :

			//var usedCursor = (internalCursor) ? 'move' : 'url(' +rootPath + 'images/cursors/pan.cur)';	

            var usedCursor = 'move';

			break;

            

        case "select" :

			//var usedCursor = (internalCursor) ? 'help' : 'url(' +rootPath + 'images/cursors/select.cur)';

            var usedCursor = (internalCursor) ? 'help' : 'help';	            

			break;

            

        case "measure" :

			var usedCursor = (internalCursor) ? 'crosshair' : 'url(' +rootPath + 'images/cursors/measure.cur)';	

			break;

            

        case "digitize" :

			var usedCursor =  'crosshair';	

			break;

            

        default:

            var usedCursor = 'default';

    }

    

    $('mapimgLayer').style.cursor = usedCursor;

    

}









/*

 * OPEN RESULT WINDOW FOR IDEBNTIFY AND SEARCH

 ***********************************************/

function openResultwin(winurl) {

    try {

        if (queryResultLayout == 'tree') {

            var winw = 300;

            var winh = 450;

        } else {

            var winw = 500;

            var winh = 200;

        }

    } catch(e) {

        var winw = 500;

        var winh = 200;

    }

    

    var w = window.open(winurl, 'resultwin', 'width=' + winw + ',height=' + winh + ',status=yes,resizable=yes,scrollbars=yes');

    //alert (winurl+' winurl pmapper.js');

		return w;

}





/*****************************************************************************

 * SEARCH

 **********/

// CLOSE SEARCH PAGE

function searchClose() {

    parent.searchFrame.location = "blank.html";

}



// OPEN SEARCH PAGE

//function searchOpen() {

//    parent.searchFrame.location = "search.phtml?"+SID;

//}







/******************************************************************************
 * PRINT FUNCTIONS
 *******************/
function openPrintDlg() {
    var pwin = window.open("printdlg.phtml?"+SID,"printdlg","width=350,height=600,status=no,resizable=yes,scrolling=yes");
}

function openPrintHighDlg() {
    var pwin = window.open("printdlg.neu.phtml?"+SID,"printdlg","width=350,height=600,status=no,resizable=yes,scrolling=yes");
}

/*function openPrintSoDlg() {
    var pwin = window.open("printdlg.orig.phtml?"+SID,"printdlg","width=600,height=400,status=no,resizable=yes");
}*/

function printMap() {
    var w = window.open("printmap.phtml?"+SID,"printdlg","width=300,height=180,status=no,resizable=no");
}







/*

 * OPEN HELP WINDOW 

 *************************************/

function openHelp() {

    window.open("help.phtml", "help","width=400,height=500,status=no,resizable=yes");

}







/************************************************************************************

 * DOWNLOAD FUNCTIONS

 * get image with higher resolution for paste in othet programs

 ****************************************************************/

function openDownloadDlg() {

    window.open("downloaddlg.phtml?"+SID, "dldlg","width=300,height=180,status=no,resizable=no");

}



function openDownload() {

    window.open("download.phtml?"+SID, "download");

}









function openPoiDlg(imgxy) {

    //alert(imgxy);

    var coordsList = imgxy.split('+');

    var mpoint = getCoords(coordsList[0], coordsList[1], false);

    

    // Round values (function 'roundN()' in 'measure.js')

    var rfactor = 4;

    var px = isNaN(mpoint.x) ? '' : roundN(mpoint.x, rfactor);

    var py = isNaN(mpoint.y) ? '' : roundN(mpoint.y, rfactor);

    

    var inserttxt = prompt(localeList['addLocation'], '');

    

    if (inserttxt) {

        //alert(inserttxt + ' --- ' + px + ' -- ' + py);

        //var ul = + px + ',' + py + ',' + inserttxt;

        var digitizeurl = PM_XAJAX_LOCATION + 'x_poi.php?' +SID + '&up=' + px + '@@' + py + '@@' + escape(inserttxt);

        //alert(digitizeurl);

        addPOI(digitizeurl, '');

    }

}

















/**

 * DOM window functions using dragresize.js

 *

 */



function initDOMWin(resizeable, wid) {    

    var dragresize = new DragResize('dragresize',

        { handles: ['tm', 'br'], minWidth: 50, minHeight: 50, minLeft: 2, minTop: 2, elmW: 20 });

    

    dragresize.isElement = function(elm)

    {

        if (elm.className && elm.className.indexOf('drsElement') > -1) return true;

    };

    

    dragresize.isHandle = function(elm)

    {

        if (elm.className && elm.className.indexOf('drsMoveHandle') > -1) return true;

    };



    dragresize.apply(document);

}





function createDOMWin(wid, W, H, T, L, oCl ) {

    var win_div = document.createElement('div');

    win_div.id = wid + '_domwin';

    win_div.className = 'drsElement';

    win_div.style.width  = W + 'px';

    win_div.style.height = H + 'px';

    win_div.style.top    = T + 'px';

    win_div.style.left   = L + 'px';

    win_div.style.visibility = 'hidden';

    

    var mh_div = document.createElement('div');

    mh_div.id = wid + '_mhandle';

    mh_div.className = 'drsMoveHandle';

    win_div.appendChild(mh_div);

    

    var con_div = document.createElement('div');

    con_div.id = wid + '_content';

    con_div.className = 'drsContent';

    win_div.appendChild(con_div);

    

    var clb_div = document.createElement('div');

    clb_div.id = wid + '_closebutton';

    clb_div.className = 'drsClosebutton';

    clb_div.onclick = oCl;

    con_div.appendChild(clb_div);

       

    document.body.appendChild(win_div);

    

    initDOMWin();

}





function createAllDOMWindows() {

    createDOMWin('query', 600, 200, 100, 100, domWinClose ) ;

    createDOMWin('print',  200, 150, 100, 100, domWinClose ) ;

    createDOMWin('download',  200, 150, 100, 100, domWinClose ) ;

}





function domWinClose() {

    var winId = this.id.split('_')[0] + '_domwin';

    document.getElementById(winId).style.visibility = 'hidden';

}





function slider_mover() {

    $('sliderArea').className = "sliderAreaOver";

}



function slider_mout() {

    $('sliderArea').className = "sliderAreaOut";

}



function runPeriodically() {

    window.setTimeout('runPeriodically()', 3600000 );

    reloadMap(false);

}



