/*********************************************************************************
 Copyright (c) 2002-2005 Armin Burger
 
 Permission is hereby granted, free of charge, to any person obtaining 
 a copy of this software and associated documentation files (the "Software"), 
 to deal in the Software without restriction, including without limitation 
 the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 and/or sell copies of the Software, and to permit persons to whom the Software 
 is furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included 
 in all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 
 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR OR 
 COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 
 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**********************************************************************************/



/**************************************************** 
 * LOAD MAP IMAGE INTO PARENT WINDOW MAP DIV
 * Reset parameters of some DIV's
 ****************************************************/
function loadMapImg(mapImgSrc) {
    var theMapImgL = window.parent.document.getElementById("mapimgLayer");
    var theMapImg = window.parent.document.getElementById("mapImg");
    
    // SWAP MAP IMG
    theMapImg.style.width = mapW;
    theMapImg.style.height = mapH;
    theMapImg.src = mapImgSrc;
    
    // RESET MAP DIV TO DEFAULT POSITION AND SIZE
    theMapImgL.style.top  = 0;  
    theMapImgL.style.left = 0;
    theMapImgL.style.width = mapW;
    theMapImgL.style.height = mapH;
    theMapImgL.style.clip = 'rect(auto auto auto auto)';  // NEEDED TO RESET DIV TO NON-CLIPPED AND ORIGINAL SIZE
   
    window.parent.document.getElementById("zoombox").style.visibility = 'hidden';
    //alert(theMapImgL.style.top);    
}


function init() {
    resizeDiv();
    createSearchOptions();
    domouseclick('zoomin');
    setTbTDButton('zoomin');
}



function resizeDiv() {
    var winix = document.body.clientWidth;
    var winiy = document.body.clientHeight;
    
    document.getElementById('top').style.width = winix;
    document.getElementById('bottom').style.width = winix;
    
}



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 = document.getElementById('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; 
    //alert(width + ' - ' + height);
    //loadMapImg0();
}


/**
 * SCALE OF CURRENT MAP
 */
function pMap_setMapScale(scale) {
    //alert (scale);
    this.scale = scale;
}

function pMap_getMapScale() {
    //alert(this.scale);
    return this.scale;
}


/**
 * REFERENCE MAP WIDTH/HEIGHT
 */
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 == 'recenter' || tdid == 'coords' || tdid == 'insert') {
            if (tdid != button) {
                rmHighlTD(tdid)
            } else {
                highlTD(tdid);
            }
        }
    }
}

function highlTD(elId) {
    var but = document.getElementById(elId);
    if (but) but.className='TOOLBARTD_ON';
}

function rmHighlTD(elId) {
    //alert(elId);
    var but = document.getElementById(elId);
    if (but) but.className='TOOLBARTD';
}


// Functions for MouseOver/Out
function TbOverOut(elId, status){
    var but = document.getElementById(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 = document.getElementById(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 = document.getElementById(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() {
    if (document.varform) {
        var toolType = document.varform.tool.value;
    } else {
        var toolType = 'zoomin';
    }

    
    /* 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)";
    
    document.getElementById('mapimgLayer').style.cursor = usedCursor;
    
    switch (toolType) {
		case "zoomin" :
			var usedCursor = (internalCursor) ? 'crosshair' : 'url(' +rootPath + 'images/cursors/zoomin.cur)';	
			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 = (internalCursor) ? 'help' : 'pointer';	
            break;

        case "pan" :
			//var usedCursor = (internalCursor) ? 'move' : 'url(' +rootPath + 'images/cursors/pan.cur)';	
            var usedCursor = 'move';
			break;
            
        case "recenter" :
			var usedCursor = (internalCursor) ? 'move' : 'url(' +rootPath + 'images/cursors/center.cur)';	
			break;
            
        case "select" :
			var usedCursor = (internalCursor) ? 'help' : 'url(' +rootPath + 'images/cursors/select.cur)';	
			break;
            
        case "measure" :
			var usedCursor = (internalCursor) ? 'crosshair' : 'url(' +rootPath + 'images/cursors/measure.cur)';	
			break;
            
        case "coords" :
			var usedCursor = (internalCursor) ? 'crosshair' : 'url(' +rootPath + 'images/cursors/xy.cur)';	
			break;
		
		case "insert" :
			var usedCursor = (internalCursor) ? 'crosshair' : 'url(' +rootPath + 'images/cursors/identify.cur)';	
			break;
            
        default:
            var usedCursor = 'default';
    }
    
    document.getElementById('mapimgLayer').style.cursor = usedCursor;
    
}




/*
 * OPEN RESULT WINDOW FOR IDEBNTIFY AND SEARCH
 ***********************************************/
function openResultwin(winurl) {
    var w = window.open(winurl, "resultwin", "width=500,height=500,status=no,resizable=yes,scrollbars=yes");
}


/*****************************************************************************
 * 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=300,height=180,status=no,resizable=no");
}

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=200,status=no,resizable=yes");
}

function openDownload() {
    window.open("download.phtml?"+SID, "download");
}



/************************************************************************** 
 * DISPLAY COORDINATES IN MORE THAN ONE COORDIMNATE SYSTEM
 **************************************************************************/
function showCoords(imgxy) {
    window.open("showcoords.phtml?"+SID + "&imgxy="+imgxy, "coords","width=280,height=130,status=no,resizable=no");
}

function openProjection() {
    window.open("projection.phtml", "projection","width=320,height=180,status=no,resizable=yes");
}

/************************************************************************** 
 * Open Insert-dialog for digitizing into GRASS Usermap
 **************************************************************************/
function insertpoints(imgxy) {
    window.open("insertpoints.phtml?"+SID + "&imgxy="+imgxy, "coords","width=280,height=280,status=no,resizable=no");
}
