var tooltip,map;
var marks = new Array();
var loading = false;

google.load("maps", "2"); 
// Call this function when the page has been loaded
   
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 initialize() {
	var sPath = 'http://www.nology.co.uk/images/';   
	if ($('map') != null){

		map = new google.maps.Map2($('map'));
	    map.addControl(new google.maps.SmallMapControl());
    
    	tooltip = document.createElement("div");
		map.getPane(G_MAP_FLOAT_PANE).appendChild(tooltip);
      
		tooltip.style.visibility='hidden';
    
		setBounds();
    
		map.setMapType(G_PHYSICAL_MAP);
	
		var tinyIcon = new google.maps.Icon();
		tinyIcon.image = sPath+"mapicon.png";
		tinyIcon.shadow = sPath+"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 google.maps.Icon();
		fsIcon.image = sPath+"mapicon2.png";
		fsIcon.shadow = sPath+"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 google.maps.LatLng(sPoints[i][0],sPoints[i][1]);
			//alert(sPoints[i][0]+' - '+sPoints[i][1]+' - '+sPoints[i][2]+' - '+sPoints[i][3]+' - '+sPoints[i][4]);
			marks[i] = new google.maps.Marker(ll,sPoints[i][4]==1?markerOptions:altMarkerOptions);
			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];
			mark.id = (sPoints[i][4]==1?"":"")+sPoints[i][3];
			google.maps.Event.addListener(mark,'click',function(){
				map.setZoom(15);
				map.setCenter(this.getLatLng());
				map.setMapType(G_NORMAL_MAP);
				map.openInfoWindow(map.getCenter(),$('nology' + this.id).clone(), {onCloseFn: function(){ setBounds();  map.setMapType(G_PHYSICAL_MAP);} });
			});
			google.maps.Event.addListener(mark,'mouseover',function(){showTooltip(this)});
			google.maps.Event.addListener(mark,'mouseout',function(){tooltip.style.visibility='hidden'});
		}
	}
	
	if ($('shopmap')!=null) {
		map = new google.maps.Map2($('shopmap'));
		map.addControl(new google.maps.SmallMapControl());
		
		setBounds();
		map.setZoom(15);
		map.setMapType(G_NORMAL_MAP);
		
		var tinyIcon = new google.maps.Icon();
		tinyIcon.image = sPath+"mapicon.png";
		tinyIcon.shadow = sPath+"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);
		
		markerOptions = {icon:tinyIcon};
		
		for (var i=0; i<sPoints.length; i++) {	
			var ll = new google.maps.LatLng(sPoints[i][0],sPoints[i][1]);
			var mark = new google.maps.Marker(ll,sPoints[i][4]==1?markerOptions:altMarkerOptions);
			map.addOverlay(mark);
		}
	}
}
  
  
function showFS(id) {
  
	loading = true;
	map.setZoom(15);

	for(var i=0;i<marks.length;i++)
		if (marks[i].id == "f" + id)
		map.setCenter(marks[i].getLatLng());
		
	map.setMapType(G_NORMAL_MAP);
	map.openInfoWindow(map.getCenter(),$('nology' + id).clone(), {onCloseFn: function(){ if (!loading) {setBounds();  map.setMapType(G_PHYSICAL_MAP);} }});
	loading = false;
}
  
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';
}
  
  
window.addEvent('unload',function(){google.maps.Unload()});
window.addEvent('domready',function(){initialize();});
  

