/* mapping */
$(document).ready(function() {
	initialiseMap();
});
// setup globals
var marker = new Array(), pubMarkers = new Array();

// define markers
function initialiseMap() {
	if ($('#map').length != 0 && typeof GBrowserIsCompatible == 'function' && GBrowserIsCompatible()) {
		
		for (i in pubMarkers) {
			marker[i] = new GIcon(G_DEFAULT_ICON);
			//marker[i].image = '/static/images/markers/alpha/'+pubMarkers[i].letter+'.png';
			//marker[i].image = '/images/09/map_tick.png';
			//marker[i].shadow = '/images/09/map_shadow.png';
			marker[i].iconSize = new GSize(24, 29);
			marker[i].shadowSize = new GSize(37, 31);
			marker[i].iconAnchor = new GPoint(12, 32);
			marker[i].imageMap = [23,0,23,1,23,2,23,3,23,4,23,5,23,6,23,7,23,8,23,9,23,10,23,11,23,12,23,13,23,14,23,15,23,16,23,17,23,18,23,19,23,20,23,21,23,22,23,23,16,24,15,25,15,26,14,27,14,28,13,29,13,30,12,31,11,31,10,30,10,29,9,28,9,27,8,26,8,25,7,24,0,23,0,22,0,21,0,20,0,19,0,18,0,17,0,16,0,15,0,14,0,13,0,12,0,11,0,10,0,9,0,8,0,7,0,6,0,5,0,4,0,3,0,2,0,1,0,0];
		}
		
		youAreHereMarker = new GIcon(G_DEFAULT_ICON);
		youAreHereMarker.image = '/images/09/map_tick.png';
		youAreHereMarker.shadow = '/images/09/map_shadow.png';
		youAreHereMarker.iconSize = new GSize(24, 29);
		youAreHereMarker.shadowSize = new GSize(37, 31);
		youAreHereMarker.iconAnchor = new GPoint(12, 32);
		
		the_map = new GMap2(document.getElementById("map"));
		
		the_map.setUIToDefault();
		
		plotMarkers(pubMarkers);
		
		search = new GlocalSearch();
		
		//link = $('#map').prev().createElement('a');
		//$(link).attr('href', '/home/pubs/'+data.name_slug);
		//alert(youAreHereMarker.lattitude);
	}
}

// draw markers onto the map and apply click callbacks
function plotMarkers(data) {
	var bounds = new GLatLngBounds();

	the_map.clearOverlays();
	
	var bounds = new GLatLngBounds();
	/*
	$.each(pubMarkers, function(i, data) {
		var point = new GLatLng(data.latitude, data.longitude);
		gmarker = new GMarker(point, marker[i]);
		
		GEvent.addListener(gmarker, 'click', function() {
			popup = document.createElement('div');
			$(popup).addClass('popup');
			
			name_div = document.createElement('div');
			$(name_div).addClass('name');
			$(name_div).text(data.name);
			
			address = document.createElement('div');
			$(address).addClass('address');
			
			address_line_1 = document.createElement('div');
			$(address_line_1).addClass('address_line_1');
			$(address_line_1).text(data.address_line_1);
			
			address_line_2 = document.createElement('div');
			$(address_line_2).addClass('address_line_2');
			$(address_line_2).text(data.address_line_2);
			
			town = document.createElement('div');
			$(town).addClass('town');
			$(town).text(data.town);
			
			county = document.createElement('div');
			$(county).addClass('county');
			$(county).text(data.county);
			
			post_code = document.createElement('div');
			$(post_code).addClass('post_code');
			$(post_code).text(data.post_code);
			
			$(address).append(address_line_1, address_line_2, town, county, post_code);
			
			image = document.createElement('div');
			$(image).addClass('image');
			$(image).css('background-image', 'url('+data.image+')');
			
			break_div = document.createElement('div');
			$(break_div).addClass('break');
			
			distance = document.createElement('div');
			$(distance).addClass('distance');
			$(distance).text(data.distance);
			
			link = document.createElement('a');
			$(link).addClass('view-pub');
			$(link).attr('href', '/home/pubs/'+data.name_slug);
			
			hide_text = document.createElement('span');
			$(hide_text).addClass('hide_text');
			$(hide_text).text('View Pub');
			
			$(link).append(hide_text);
			
			spacer = document.createElement('img');
			$(spacer).addClass('spacer');
			$(spacer).attr('src', '/static/images/spacer.gif');
			$(spacer).attr('width', '100');
			$(spacer).attr('height', '10');
			
			footer_div = document.createElement('div');
			$(footer_div).addClass('footer');
			$(footer_div).html('&nbsp;');
			
			$(popup).append(name_div, address, image, break_div, distance, link, break_div, spacer, footer_div);
			
			the_map.setCenter(new GLatLng(data.latitude, data.longitude));
		
			the_map.openInfoWindow(the_map.getCenter(), popup);
		});
		
		the_map.addOverlay(gmarker);
		
		bounds.extend(point);
	});
	*/
	// add search marker
	var point = new GLatLng(starting_latitude, starting_longitude);
		
	gmarker = new GMarker(point, youAreHereMarker);
	
	the_map.addOverlay(gmarker);
	
	bounds.extend(point);
		
	var SW = bounds.getSouthWest();
	var NE = bounds.getNorthEast();
	var height = 0.01;
	var width = 0.04;
	// Set a 5% margin for left, right, and bottom, but 10% margin for top as markers are tall and need extra space
	var marginBounds = new GLatLngBounds(new GLatLng(SW.lat() - 0.05*height, SW.lng() - 0.05*width), new GLatLng(NE.lat() + 0.10*height, NE.lng() + 0.05*width) );

	the_map.setCenter(marginBounds.getCenter(), the_map.getBoundsZoomLevel(marginBounds));
	
	link = document.createElement('a');
	$(link).attr('href', '/venues/map?latitude='+starting_latitude+'&longitude='+starting_longitude);
	$(link).addClass('replace').addClass('view-larger');
	$(link).text('Zoom in');
	
	//$('#map').insertBefore($(link));
	$(link).insertBefore($('#map'));
}
