/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU AFFERO General Public License as published by
the Free Software Foundation; either version 3 of the License, or
any later version.

This program 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 Affero General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
or see http://www.gnu.org/licenses/agpl.txt.
*/

// OSRM RoutingGUI
// [handles all GUI events that interact with routing]


OSRM.RoutingGUI = {

// click: button "reset"
resetRouting: function() {
	document.getElementById('gui-input-source').value = "";
	document.getElementById('gui-input-target').value = "";
	
	OSRM.G.route.hideAll();
	OSRM.G.markers.removeAll();
	OSRM.G.markers.highlight.hide();
	
	document.getElementById('information-box').innerHTML = "";
	document.getElementById('information-box-header').innerHTML = "";
	
	OSRM.JSONP.reset();	
},

// click: button "reverse"
reverseRouting: function() {
	// invert input boxes
	var tmp = document.getElementById("gui-input-source").value;
	document.getElementById("gui-input-source").value = document.getElementById("gui-input-target").value;
	document.getElementById("gui-input-target").value = tmp;
	
	// recompute route if needed
	if( OSRM.G.route.isShown() ) {
		OSRM.G.markers.route.reverse();
		OSRM.Routing.getRoute();				// temporary route reversal for query, actual reversal done after receiving response
		OSRM.G.markers.route.reverse();
		OSRM.G.markers.highlight.hide();
		OSRM.RoutingDescription.showSimple( OSRM.G.response );
	
	// simply reverse markers		
	} else {
		OSRM.G.markers.reverseMarkers();		
	}
},

// click: button "show"
showMarker: function(marker_id) {
	if( OSRM.JSONP.fences["geocoder_source"] || OSRM.JSONP.fences["geocoder_target"] )
		return;
	
	if( marker_id == OSRM.C.SOURCE_LABEL && OSRM.G.markers.hasSource() )
		OSRM.G.markers.route[0].centerView();
	else if( marker_id == OSRM.C.TARGET_LABEL && OSRM.G.markers.hasTarget() )
		OSRM.G.markers.route[OSRM.G.markers.route.length-1].centerView();
},

// changed: any inputbox (is called when enter is pressed [after] or focus is lost [before])
inputChanged: function(marker_id) {
	if( marker_id == OSRM.C.SOURCE_LABEL)	
		OSRM.Geocoder.call(OSRM.C.SOURCE_LABEL, document.getElementById('gui-input-source').value);
	else if( marker_id == OSRM.C.TARGET_LABEL)
		OSRM.Geocoder.call(OSRM.C.TARGET_LABEL, document.getElementById('gui-input-target').value);
},

// click: button "open JOSM"
openJOSM: function() {
	var center = OSRM.G.map.getCenterUI();
	var bounds = OSRM.G.map.getBoundsUI();
	
	var xdelta = Math.min(0.02, Math.abs(bounds.getSouthWest().lng - center.lng) );
	var ydelta = Math.min(0.01, Math.abs(bounds.getSouthWest().lat - center.lat) );
	
	var p = [ 'left='  + (center.lng - xdelta).toFixed(6), 'bottom=' + (center.lat - ydelta).toFixed(6), 'right=' + (center.lng + xdelta).toFixed(6), 'top=' + (center.lat + ydelta).toFixed(6)];
	var url = 'http://127.0.0.1:8111/load_and_zoom?' + p.join('&');
 
	var frame = document.getElementById('josm-frame');
	if(!frame) {
		frame = L.DomUtil.create('iframe', null, document.body);
		frame.style.display = "none";
		frame.id = 'josm-frame';
	}
	frame.src = url;
},

//click: button "open OSM Bugs"
openOSMBugs: function() {
	var position = OSRM.G.map.getCenterUI();
	window.open( "http://osmbugs.org/?lat="+position.lat.toFixed(6)+"&lon="+position.lng.toFixed(6)+"&zoom="+OSRM.G.map.getZoom() );
},

//click: button "delete marker"
deleteMarker: function(marker_id) {
	var id = null;
	if(marker_id == 'source' && OSRM.G.markers.hasSource() )
		id = 0;
	else if(marker_id == 'target' && OSRM.G.markers.hasTarget() )
		id = OSRM.G.markers.route.length-1;
	if( id == null)
		return;
	
	OSRM.G.markers.removeMarker( id );
	OSRM.Routing.getRoute();
	OSRM.G.markers.highlight.hide();	
}

};