
/******************************************************************************
 *
 * 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
 *
 ******************************************************************************/

/** 
 * LOAD MAP IMAGE INTO PARENT WINDOW MAP DIV
 */
function loadMapImg(mapImgSrc) {
    // SWAP MAP IMG
    var theMapImg = _$("mapImg");

    theMapImg.src = mapImgSrc;

	//si la couche est visible par défaut
	var laChaineDesCouches;

	laChaineDesCouches = getLayers();
	if (laChaineDesCouches.length==0)
	{
		laChaineDesCouches = 'Refuge';
	}
	loadDivToolTips(laChaineDesCouches);
}
//*******************************************************************

//ajout par ben

function loadDivToolTips(laChaineDesCouches){

	var fini = false;
	var cmpt = 0;
	var visi = '';
	var no_visi = ''; 


	//alert(navigator.appName);

//recherch si refuge est affiché

	var resuTest = '';
	resuTest = laChaineDesCouches.indexOf('Refuge',0);

	var laCoucheVisible = '';

	if (resuTest != '-1')
	{	
		laCoucheVisible =  'Refuge';
	}
	else
	{
		laCoucheVisible =  'Aucun';
	}

	//choper tout les div 'tooltip' et modifier leur style (coord en particulier)
	while(fini == false)
	{
		//test si fin de bcle
		var id_tooltip = "Tooltip" + cmpt;
		var id_hidden = "hid" + cmpt;

		var divTooltip = _$(id_tooltip);
		var hidToolTip = _$(id_hidden);

		

		if (divTooltip == null || hidToolTip == null )
		{
			fini = true;
		}
		else
		{
				var coordXpx = hidToolTip.style.left;
				var coordYpx = hidToolTip.style.top;

				var coordX = coordXpx.substring(0, coordXpx.length - 2);
				var coordY = coordYpx.substring(0, coordYpx.length - 2);
			
			//calculer la nouvelle position...
			var nouvelX = mapgeo2mapx(coordX, minx_geo, mapW, xdelta_geo)-10;
			var nouvelY = mapgeo2mapx(coordY, maxy_geo, mapH, -ydelta_geo)-3;

			//et l'attribuer à l'ancienne:
			var x = parseInt(nouvelX);
			var y = parseInt(nouvelY);


				divTooltip.style.left = x + 'px';
				divTooltip.style.top = y + 'px';

				visi = "visible";
				no_visi = "hidden";

			//test si la couche est visible...
			if (laCoucheVisible != 'Refuge')
			{
				divTooltip.style.visibility = "hidden";
			}
			else
			{
				divTooltip.style.visibility = "visible";
			}
		}

		cmpt = cmpt + 1;
	}

}
//*******************************************************************
/**
 * Reset parameters of some DIV's
 */
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').hidev();
    $('#loading').hidev();
    
    maploading = false;
    
    var varformMode = _$("varform").mode.value;
    if (varformMode == 'measure') {
        resetMeasure();
        polyline = toPxPolygon(geoPolyline);
        if (polyline.getPointsNumber()>0) {
            drawPolyline(jg,polyline);
        }
    }
}


/**
 * Update s1 value for slider settings
 */
function updateSlider_s1(pixW, pixH) {
    var maxScale1 = ((PMap.dgeo_x * PMap.dgeo_c) / pixW) / (0.0254 / 96);
    var maxScale2 = ((PMap.dgeo_y * PMap.dgeo_c) / pixH) / (0.0254 / 96);
    PMap.s1 = Math.max(maxScale1, maxScale2);
}



/*****************************************************************************
 * 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)
 * set class for active tool button
 */
function setTbTDButton(button) {
    if (PMap.tbImgSwap != 1) {
        $("#mapZone .TOOLBARTD").addClass('TOOLBARTD_OFF').removeClass('TOOLBARTD_ON');
        $('#tb_' + button).removeClass('TOOLBARTD_OFF').addClass('TOOLBARTD_ON').removeClass('TOOLBARTD_OVER');
    } else {
        $("#mapZone .TOOLBARTD").each(function() {
            //$(this).addClass('TOOLBARTD_OFF').removeClass('TOOLBARTD_ON');
            $(this).find('>img').imgSwap('_on', '_off');
        });
        $('#tb_' + button).find('>img').imgSwap('_off', '_on').imgSwap('_over', '_on');
    }
}

/**
 * MouseDown/Up, only set for stateless buttons
 */
function TbDownUp(elId, status){
    var but = $('#tb_' + elId);
    if (status == 'd') {
        if (PMap.tbImgSwap != 1) {
            but.addClass('TOOLBARTD_ON').removeClass('TOOLBARTD_OFF').removeClass('TOOLBARTD_OVER');
        } else {
            but.find('>img').imgSwap('_off', '_on').imgSwap('_over', '_on');
        }
    } else {
        if (PMap.tbImgSwap != 1) {
            but.addClass('TOOLBARTD_OFF').removeClass('TOOLBARTD_ON').addClass('TOOLBARTD_OVER');
        } else {
            if (PMap.tbImgSwap == 1) but.find('>img').imgSwap('_on', '_off');
        }
    }
}


function changeButtonClr(myObj, myAction) {
    switch (myAction) {
        case 'over':
            myObj.className = 'button_on';
            break;
            
        case 'out':
            myObj.className = 'button_off';
            break;
    }
}



/**************************************************
 * 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, ctype) {	
    if (!rmc) {
    	var varform = _$("varform");
        if (varform) {
            var toolType = varform.tool.value;
        } else {
            var toolType = 'zoomin';
        }
    } else {
        toolType = 'pan';
    }

    // take definition from ja_config.php 
    try {
        var iC = pmUseInternalCursors;
    } catch(e) {
        var iC = true;
    } 
    
    var rootPath = getRootPath();
    var usedCursor = (iC) ? toolType : 'url("' +rootPath + 'images/cursors/zoomin.cur"), default';
    
    _$('mapimgLayer').style.cursor = usedCursor;
    
    switch (toolType) {
		case "zoomin" :
			var usedCursor = (iC) ? 'crosshair' : 'url("' +rootPath + 'images/cursors/zoomin.cur"), default';	
            break;
        
        case "zoomout" :
			var usedCursor = (iC) ? 'e-resize' : 'url(' +rootPath + 'images/cursors/zoomout.cur), default';	
			break;
        
        case "identify" :
			//var usedCursor = (iC) ? 'help' : 'url(' +rootPath + 'images/cursors/identify.cur), default';	
			var usedCursor = 'help';	
            break;
        
        case "auto_identify" :	
			var usedCursor = 'pointer';	
            break;

        case "pan" :
			//var usedCursor = (iC) ? 'move' : 'url(' +rootPath + 'images/cursors/pan.cur), default';	
            var usedCursor = 'move';
			break;
            
        case "select" :
			//var usedCursor = (iC) ? 'help' : 'url(' +rootPath + 'images/cursors/select.cur), default';
            var usedCursor = (iC) ? 'help' : 'help';	            
			break;
            
        case "measure" :
			var usedCursor = (iC) ? 'crosshair' : 'url(' +rootPath + 'images/cursors/measure.cur), default';	
			break;
            
        case "digitize" :
			var usedCursor =  'crosshair';	
			break;
            
        default:
            var usedCursor = 'default';
    }

    if (ctype) usedCursor = ctype;
    _$('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 = 550;
        }
    } catch(e) {
        var winw = 500;
        var winh = 200;
    }
    
    var w = window.open(winurl, 'resultwin', 'width=' + winw + ',height=' + winh + ',status=yes,resizable=yes,scrollbars=yes');
    w.focus();
    return w;
}



/**
 * PRINT FUNCTIONS
 */
function openPrintDlg() {
   createDnRDlg({w:240, h:200, l:200, t:200}, {resizeable:true, newsize:true}, 'pmDlgContainer', localeList['Print_Settings'], 'printdlg.phtml?'+SID);
}


/**
 * OPEN HELP WINDOW 
 */
function openHelp() {
    createDnRDlg({w:350, h:500, l:100, t:50}, {resizeable:true, newsize:true}, 'pmDlgContainer', localeList['Help'], 'help.phtml?'+SID);
}



function getMapTL() {
    var mapTL = new Object();
    var oMap = $('#map');
    mapTL.L = oMap.offset()['left'];
    mapTL.T = oMap.offset()['top'];
    return mapTL;
}


/************************************************************************************
 * DOWNLOAD FUNCTIONS
 * get image with higher resolution for paste in othet programs
 ****************************************************************/
function openDownloadDlg() {
    createDnRDlg({w:260, h:220, l:200, t:200}, {resizeable:false, newsize:true}, 'pmDlgContainer', localeList['Download'], 'downloaddlg.phtml?'+SID );
}

function openDownload() {
    window.open("download.phtml?"+SID, "download");
}


/**
 * Open popup dialaog for adding POI 
 */
function openPoiDlg(imgxy) {
    var coordsList = imgxy.split('+');
    var mpoint = getGeoCoords(coordsList[0], coordsList[1], false);
    
    // Round values (function 'roundN()' in 'measure.js')
    var rfactor = 5;
    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) {
        var digitizeurl = PM_XAJAX_LOCATION + 'x_poi.php?' +SID + '&up=' + px + '@@' + py + '@@' + escape(inserttxt);
        //alert(digitizeurl);
        addPOI(digitizeurl);
    }
}

//-------------------------------------------------------------------------
// recupere les coordonnees de la souris
// les affecte au style de la div infobulle
function get_mouse(e){
	if(ie){
	//alert("ie");
		x = event.x;
		y = event.y;
		window.status = x;
	}else{
	//alert("pas ie");
		x = e.pageX;
		y = e.pageY;
	}	
	
	//alert(x + ' ' + y);
	bubble = document.getElementById("infobulle");

	y = y - 60;
	bubble.style.left = x+"px";
	bubble.style.top = y+"px";
	
}

//----------------------------------------------------------------------------
function see_bubble(text){
	bubble = document.getElementById("infobulle");
	bubble.style.visibility = "visible";
	bubble.style.display = "block";
	
	if (ns) {
		var lyr = document.getElementById("infobulle");
		lyr.write(text);
		lyr.close();	
	}
	else bubble = document.getElementById("infobulle").innerHTML = text;;
	//document.all["infobulle"].innerHTML = text;

	//-----------------------------------
	//il faut aussi desactiver le mode de click,

	outil = document.varform.tool.value;
	action = document.varform.maction.value;
	mode = document.varform.mode.value;
	zoom_type = document.varform.zoom_type.value;
	zoom_fact = document.varform.zoom_factor.value;

    document.varform.mode.value = '';
    document.varform.zoom_type.value = '';
    document.varform.zoom_factor.value = '';
    document.varform.maction.value = '';
    document.varform.tool.value = '';


}
//----------------------------------------------------------------------------
// cache la bubble
function kill_bubble(){
	bubble = document.getElementById("infobulle");
	bubble.style.visibility = "hidden";
	bubble.style.display = "none";

        document.varform.mode.value = mode;
        document.varform.zoom_type.value = zoom_type;
        document.varform.zoom_factor.value = zoom_fact;
        document.varform.maction.value = action;
        document.varform.tool.value = outil;

}
//-----------------------------------------------------------------------------
// Create a DHTML layer
function createLayer(name, x, y, num, inleft, intop, width, height, visible, content) {
	  var msg = '';	
	  var layer;


		if (isNaN(inleft))
			inleft=0;
		if (isNaN(intop))
			intop=0;

		document.writeln('<div id="' + name + '' + num + '" style="position:absolute; overflow:hidden; left:' + inleft + 'px; top:' + intop + 'px; width:' + width + 'px; height:' + height + 'px; z-index:1; visibility:' + (visible ? 'visible;' : 'hidden;') +  '">');
		
		document.writeln(content);
		document.writeln('</div>');
		document.writeln('<div id="hid' + num + '" style="position:absolute; overflow:hidden; left:' + x + 'px; top:' + y + 'px; width:1px; height:1px;' + '; visibility:hidden">ici, contenu non-visible</div>');
}
//----------------------------------------------------------------------------

// Create a DHTML layer
function createContent(content,desc,id) {

	info='<table  CELLSPACING=0><tr class=bleu align=center><td><span class=texteblanc_tp_gras >'+desc+'</span></td></tr><tr><td class=blanc_bordbleu align=left><span class=textebleu_xp> '+content+' </span></td></tr></table>';

	chemin = '../fr/affiche.php?numenr=';

	url = chemin + id;
	
	result = '<a href="#" onclick="javascript:ouvrir(\'../fr/affiche.php?numenr=' + id + '\');"  onmouseover="javascript:see_bubble(\''+info+'\');" onmouseout="javascript:kill_bubble();"><img src="images/transparent.gif" border=1></a>'; 


	return result;
}
//--------------------------------------------------
function no_accent($str_accent) {

   return $str_accent;
} 

//--------------------------------------------------
function ouvrir(url) 
{ 
	s = '1';
	l = '800';
	h = '600';
	hauteur = 10;
	largeur = 10;

	win = window.open(url, "fiche", "toolbar=1,location=1,directories=0,status=1, scrollbars="+s+",resizable=1,menubar=0,top="+hauteur+",left="+largeur+",width="+l+",height="+h);
	win.focus();
}
//----------------------------------------------------


