$(document).ready(function(){
	
	if(window.mxn && window.NewMind && window.NewMind.mapping) {
		
		if(window.NewMind.tourism && window.NewMind.tourism.settings) {
			window.NewMind.tourism.settings['mappingDefaultPolyOn'] = false;
		}

		
		/*
		$(document.body).append('<div id="debug"><h2>Debug</h2></div>');
		
		NewMind.mapping.configure({
			DEBUG: true,
			DEBUG_OUTPUT_DIV: $('#debug').get(0)
		});
		*/
		
	}
	
	/*
	 * This doesn't actually do anything with the map it manipulates link and 
	 * forms and things to allow mapping stuff to happen
	 */	
	
	// hide the Search By Map link
	$('#SearchByMap').hide(0);
	
	// add a "show results on map" checkbox to refine
	$('#refineByDefList')
		.append('<dd><div class="row"><span class="field"><input type="checkbox" id="autoshowmap" name="autoshowmap" value="1" class="checkbox" /><label for="autoshowmap" class="text" id="autoshowmapLabel">Tick here to show results on a map.</label></span></div></dd>')
		.find('#autoshowmap').each(function(){
			if(window.location.search.indexOf('autoshowmap') > -1) {
				this.checked = true;
			}
		});
		
	// now add tick box to availability search functionality too	
	$('#availabilitySearchFields').each(function() {
		// check to make sure the availability search hasn't already been performed (checks for <input name="stage" value="4"/>)
		var stage = $('input[@name=stage]').val();
		
		if (stage !== "4") {		
			$('#availabilitySearchFields')
				.append('<div class="row"><span class="field"><input type="checkbox" id="autoshowmap" name="autoshowmap" value="1" class="checkbox" /><label for="autoshowmap" class="text" id="autoshowmapLabel">Tick here to show results on a map.</label></span></div>')
				.find('#autoshowmap').each(function(){
					if(window.location.search.indexOf('autoshowmap') > -1) {
						this.checked = true;
					}
				}
			)
		}
		
	});
	
	//shove it on the advanced search too
	$('.srcAdvFields')
		.append('<div class="row"><span class="labelwide"><input type="checkbox" id="autoshowmap" name="autoshowmap" value="1" class="checkbox" /><label for="autoshowmap" class="text" id="autoshowmapLabel">Tick here to show results on a map.</label></span></div>')
		.find('#autoshowmap').each(function(){
			if(window.location.search.indexOf('autoshowmap') > -1) {
				this.checked = true;
			}
		});
	
	// what's nearby should auto show the map by default
	$('#whatsNearby form, #refineBy-whatsNearby form').append('<input type="hidden" id="autoshowmap" name="autoshowmap" value="1" />');
		
});