var tooltip,map;
var marks = new Array();
var loading = false;

	google.load("maps", "2.x"); 


	$(document).ready(function(){
							   
		var map = new GMap2($("#map").get(0));					   

		setBounds();
		
		map.addControl(new google.maps.SmallMapControl());
	    map.setMapType(G_PHYSICAL_MAP);

		tooltip = document.createElement("div");
		map.getPane(G_MAP_FLOAT_PANE).appendChild(tooltip);
      	tooltip.style.visibility='hidden';

		var tinyIcon = new GIcon();
		tinyIcon.image = "../images/mapicon.png";
		tinyIcon.shadow = "../images/mapiconshadow.png";
		tinyIcon.iconSize = new google.maps.Size(12,20);
		tinyIcon.shadowSize = new google.maps.Size(22,20);
		tinyIcon.iconAnchor = new google.maps.Point(6,20);
		tinyIcon.tooltipAnchor = new google.maps.Point(4,8);
		tinyIcon.infoWindowAnchor = new google.maps.Point(5,1);
		
		var fsIcon= new GIcon();
		fsIcon.image = "../images/mapicon2.png";
		fsIcon.shadow = "../images/mapiconshadow.png";
		fsIcon.iconSize = new google.maps.Size(12,20);
		fsIcon.shadowSize = new google.maps.Size(22,20);
		fsIcon.iconAnchor = new google.maps.Point(6,20);
		fsIcon.tooltipAnchor = new google.maps.Point(4,8);
		fsIcon.infoWindowAnchor = new google.maps.Point(5,1);	
		
		markerOptions = {icon:tinyIcon};
		altMarkerOptions = {icon:fsIcon};


		for (var i=0; i<sPoints.length; i++) {	
			var ll = new GLatLng(sPoints[i][0],sPoints[i][1]);
			
			marks[i] = new GMarker(ll,sPoints[i][4]==1?altMarkerOptions:markerOptions);
			var mark = marks[i];
			map.addOverlay(mark);
			
			mark.value = i
			mark.tooltip = '<div class="tooltip">' + sPoints[i][2] + '</div>';
			mark.id = (sPoints[i][4]==1?"f":"")+sPoints[i][3];
			GEvent.addListener(mark,'click',function(){
																
				map.setZoom(15);
				map.setCenter(this.getLatLng());
				map.setMapType(G_NORMAL_MAP);
				//alert($('#vmaxx' + this.id).html());
				map.openInfoWindowHtml(
					map.getCenter(),
					$('#vmaxx' + this.id).html(), 
					{
						onCloseFn: function(){ 
							setBounds();  
							map.setMapType(G_PHYSICAL_MAP);
					} 
							
				});
	
			}); 
		
			GEvent.addListener(mark,'mouseover',function(){showTooltip(this)});
			GEvent.addListener(mark,'mouseout',function(){tooltip.style.visibility='hidden'});
		}


		function setBounds(){
			var bounds = new google.maps.LatLngBounds;
			for (var i=0; i<sPoints.length; i++) {	
				var ll = new google.maps.LatLng(sPoints[i][0],sPoints[i][1]);
				bounds.extend(ll);
			}
			map.setCenter(bounds.getCenter());
			map.setZoom(map.getBoundsZoomLevel(bounds));
		}
								   

		function showTooltip(marker){
			tooltip.innerHTML = marker.tooltip;
		
			var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint	(0,0),true),map.getZoom());
			var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
			var anchor=marker.getIcon().tooltipAnchor;
			var width=marker.getIcon().iconSize.width;
			var height=tooltip.clientHeight;
			var pos = new google.maps.ControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - anchor.x + width, 	offset.y - point.y -anchor.y -height)); 
			pos.apply(tooltip);
			
			tooltip.style.visibility='visible';
		}

	});
