/* Used to display a map */

google.load('maps', '2');
/*google.load('jquery', '1.3.2');*/ // Now included with jQuery Tools
google.load('search', '1');
google.setOnLoadCallback(initialise);

function initialise()
{
	
	if (google.maps.BrowserIsCompatible()) 
	{
		var centre = new google.maps.LatLng(50.81918789808769, -0.13655662536621094);
        var map = new google.maps.Map2($('#map')[0]);
        map.setCenter(centre, 1);
        
        var uiOptions = new GMapUIOptions(map.getSize());
        
        uiOptions.controls.maptypecontrol = false;
        uiOptions.zoom.scrollwheel = false;
        
        map.setUI(uiOptions);
        
		var marker = new google.maps.Marker(centre, {draggable: true, icon: Util.getCustomIcon()});
		
		var bubbleField = $('<textarea id="bubble_field" rows="4" value="">Type your note here</textarea>');
		
		$('h1').click(function() { location.href = '/'; });
		$('h2').click(function() { location.href = '/'; });
		
		$('#search_button').click(function()
		{
			var button = $(this);
			var field = $('#search_field');
			
			var text = field.val();
			
			if (text == '') 
			{
				return false;
			}
			
			Util.insertSpinner($('#display'));
			
			var search = new google.search.LocalSearch();
			
			search.setSearchCompleteCallback(null, function()
			{
				if (!(search.results) || search.results.length == 0)
				{
					var error='<div class="error_message"><h3>Sorry</h3><p>We could not find your location. Please try again.</p></div>';
					$('#display').html(error);
				}
				else
				{
					var result = search.results[0];
					var point = new google.maps.LatLng(result.lat, result.lng);
					map.setCenter(point, getZoomLevel(result.accuracy));
					marker.setLatLng(point);
					map.addOverlay(marker);
					
					$('#display').load('./create_placemark_form.ajax');
				}
				
			});
			
			search.execute(text);
			return false;
			
		});
		
		$('#create_button').live('click', function()
		{
			$('#display').append($(Util.getSpinnerHtml()));
			
			var point = marker.getLatLng();
			
			var bubbleValue = Util.isCheckboxChecked('bubble_checkbox') ? $('#bubble_field').val() : '';
			var slugValue = $('#slug_field').val();
			var emailValue = $('#email_field').val();
			
			var placemark = 
			{
				'latitude' : point.lat(),
				'longitude' : point.lng(),
				'zoom' : map.getZoom(),
				'bubble' : bubbleValue,
				'slug' : slugValue,
				'email' : emailValue
			};
			
			$.ajax(
			{
				type: 'POST',
				url: '.',
				data: placemark,
				success: function(html)
				{
					$('#display').html(html);
				},
				error: function(xmlhttprequest)
				{
					$('#display').html(xmlhttprequest.responseText);
				}
			});
						
			return false;
		});
		
		$('#more_info_button, #beta_badge').click(function()
		{
			$('#display').html(Util.getSpinnerHtml());
			$('#display').load('./more_info.ajax');
			return false;
		});
		
		$('#search_field')
		.one('focus', function()
		{
			$(this).val('');
		})
		.keypress(function(e)
		{
			var code = (e.keyCode ? e.keyCode : e.which);
			if(code == 13) //Enter keycode
			{ 
				$('#search_button').click();
			}
		});
		
		bubbleField.one('focus', function()
		{
			$(this).val('');
		});
		
		$('#bubble_checkbox').live('click', function()
		{
			var checked = Util.isCheckboxChecked('bubble_checkbox');
			
			if (checked)
			{
				marker.openInfoWindow(bubbleField[0], {'noCloseOnClick':true});		
				
				// Pan to make sure the bubble is visible. Needs fixing.
				map.panBy(new GSize(0, 50));		
			}
			else
			{
				marker.closeInfoWindow();
			}
		});	
		
		$('#show_advanced').live('click', function()
		{
			$('#advanced_options').slideToggle('fast');
			return false;
		});
    }
}

function getZoomLevel(accuracy)
{
	var tabAccuracy = [2,4,6,10,12,13,16,16,17];
	return tabAccuracy[accuracy];
}

