/**
 * Sample script for custom actions/modes
 * called from mapserver.js/zoombox_apply()
 * must be named '*_start(imgxy)'
 */
function coords_start(imgxy) {
    openCoordsDlg(imgxy);
    //alert(imgxy);
}

/**
 * custom sample script for extending tool functions
 * called from mapserver.js/domouseclick()
 * must be named '*_click()'
 */
function coords_click() {
    var varform = _$("varform");
    varform.mode.value = 'coords';
    varform.maction.value = 'click';
    varform.tool.value = 'coords'; 
    
    // define the cursor
    if (useCustomCursor) {
		var usedCursor = (iC) ? 'crosshair' : 'url(' +rootPath + 'images/cursors/pencil.cur), default';
        setCursor(true, usedCursor);
    }
}

/**
 * Custom function what to do with mouse click pixel coordinates
 */
function openCoordsDlg(imgxy) {
    var pixccoords = imgxy.split('+');
    var pixX = pixccoords[0];
    var pixY = pixccoords[1];
    
	var DejaOuvert = false;

    var mpoint = getGeoCoords(pixX, pixY, false);
    //ouvrir('../index.php?page=nvo_refuge&x=' + mpoint.x + '&y=' + mpoint.y + '');
    $.ajax({
        url: PM_PLUGIN_LOCATION + '/coordinates/x_coords.php?' + SID + '&x=' + mpoint.x + '&y=' + mpoint.y,
        dataType: "json",
        success: function(response){
            var res = response.prjJson;
            //alert(prjJson);
            var rStr = "";
            $.each(res, function(i, n)
			{	
				//alert(n.prjName);
				if (n.prjName == "latlon" && DejaOuvert == false)
				{
					//et cest là quil faut ouvrir le formulaire d'ajout d'un refuge
					//jongler avec les fenetres
					ouvrir_xy('../index.php?page=nvo_refuge&x=' + n.x + '&y=' + n.y + '');
					DejaOuvert = true;
				}
            });
        } 
    });  

}



function ouvrir_xy(url) 
{ 
	hauteur=Math.round((screen.availHeight));
	largeur=Math.round((screen.availWidth));

	var mere = window.opener;
	
	if(mere.closed == true)
	{
		nvlle = window.open(url, "site", "toolbar=1,location=1,directories=1,status=1, scrollbars=1,resizable=1,menubar=1,top="+0+",left="+0+",width="+largeur+",height="+hauteur);
		nvlle.focus();
	}
	else
	{
		mere.location='' + url + '';
		mere.focus();
	}

}
