$(document).ready(function(){
	initializeMap(data);
	if(data.length < 0){
		$('#km').val('');
		$('#mileage').val('');
	}
	$('a.map-tool').click(function(e){
		e.preventDefault();
	});
	$('a#map-undo').click(function(e){
		removeLastLeg();
	});
	$('a#map-redo').click(function(e){
		redoLastLeg();
	});
	$('a#start-over').click(function(e){
		$('a#snap-road span').html('SNAP to Road ON');
		clearLinkHandler();
	});
	$('a#close-loop').click(function(e){
		completeThereAndBackCourse();
	});
	$('a#snap-road').click(function(e){
		if(bRecordPoints){
			$('a#snap-road span').html('SNAP to Road OFF');
			bRecordPoints=false;
			snap_to_road = false;
		}
		else{
			$('a#snap-road span').html('SNAP to Road ON');
			bRecordPoints=true;
			snap_to_road = true;
		}
	});
	$('#saveLink').click(function(e){
		$('#route-detail').stop();
		createPermalink();
	});
	
	$('#map-preview').click(function(e){
		if(routeExist && jQuery.trim($('#name').val()).length > 0 && jQuery.trim($('#area').val()).length) 		{
			$('#route-detail').stop();
			createPermalink();
			$("#controlPanel").attr("action", "/route/preview");
			$("#controlPanel").submit();
		}
		else {
			var msg = '';
			if(jQuery.trim($('#area').val()).length <= 0)
				msg = 'Please enter area of the route!';
			if(jQuery.trim($('#name').val()).length <= 0)
				msg = 'Please enter name of the route!';
			if(!routeExist)
				msg = 'Please draw the route!';
			alert(msg);
		}
	});
	
});

