jQuery(document).ready(function($) {
	$('a[rel="external"]').live("click", function(){
			window.open( $(this).attr('href') );
			return false;
		});

	
	function createMarker(point, address, i){
	
	//alert ('marker');
		var marker = new GMarker(point);
		$("#gc_"+i).prepend('<a href="#" id="contact_anchor_'+i+'" name="listing_anchor_'+i+'"></a>');
		var html = '<div class="gmap_infobox">'+address+'<a href="#contact_anchor_'+i+'" class="maplink_to_contact" id="map_link_to_contact'+i+'">Go to listing</a></div>';
		
			$('#map_link_to_contact'+i).live("click", function(e){
				
				$("div#gc_"+i).hide().fadeIn("slow");
			});	

		
		GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(html);});
		
		$("#gc_"+i).append(" <a class='map_anchor' href='#map_anchor'>show on map</a>");
			$("#gc_"+i+" a.map_anchor").click(function () { 
				marker.openInfoWindowHtml(html);
				map.setCenter(point,16);
			});		
		return marker;
		
	};
	
	
	
	var map = new GMap2(document.getElementById('geocon_map'));
        map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(0,0),0);
		var bounds = new GLatLngBounds();

	
		
	//I suppose an alternative to this would be to parse the doc looking for a hidden input field
	var pins_lon = geolist_settings.pins_lon;
	var pins_lat = geolist_settings.pins_lat;
	var pins_index = geolist_settings.pins_index;
	
	pins_lon = pins_lon.split(",");
	pins_lat = pins_lat.split(",");
	pins_index = pins_index.split(",");
	
	var i=0;
	var points = [];
	for (i=0;i<pins_index.length;i++)
	{
		contact_index = (pins_index[i]);
		if(contact_index >0){
			if(pins_lat[i] > 0){
				var contact_info = $('#gc_'+contact_index).html();
				var point = new GLatLng(pins_lat[i],pins_lon[i]);
				bounds.extend(point);
				//points.push(point);
				var marker = createMarker(point, contact_info, contact_index);
				map.addOverlay(marker);
				//var organisation = $('#gc_'+contact_index+' .organisation').html();
			};
			//alert('point');
			
		};
	}
	
	map.setZoom(map.getBoundsZoomLevel(bounds));
	map.setCenter(bounds.getCenter());
});