var map, dvMap, dvList;
var g_aMarkers = [ ];
var customIcons = {};
var aPages = [];
var bounds = {};

function initMap( sMapElID, sName, fLat, fLng, sPostcode, bShowPopup )
  				{
  					if( !sMapElID )
  					{
  						sMapElID = 'map';
  					}
					var iconPub = new GIcon();
					iconPub.image = "/extension/gpgdesign/design/ezflow/images/pub-all.png";	                
					iconPub.iconSize = new GSize(28, 28);	    
					iconPub.iconAnchor = new GPoint(14, 14);
					iconPub.infoWindowAnchor = new GPoint(14, 1);
			
					var iconGPGPub = new GIcon();
					iconGPGPub.image = "/extension/gpgdesign/design/ezflow/images/gpg-pub.png";	                
					iconGPGPub.iconSize = new GSize(28, 28);	    
					iconGPGPub.iconAnchor = new GPoint(14, 14);
					iconGPGPub.infoWindowAnchor = new GPoint(14, 1);
					
					var iconLuckyPub = new GIcon();
					iconLuckyPub.image = "/extension/gpgdesign/design/ezflow/images/gpg-luckydip-standard.png";	                
					iconLuckyPub.iconSize = new GSize(28, 28);	    
					iconLuckyPub.iconAnchor = new GPoint(14, 14);
					iconLuckyPub.infoWindowAnchor = new GPoint(14, 1);
					
					var iconGPGPubMatch = new GIcon();
					iconGPGPubMatch.image = "/extension/gpgdesign/design/ezflow/images/gpg-pub-match.png";	                
					iconGPGPubMatch.iconSize = new GSize(28, 28);	    
					iconGPGPubMatch.iconAnchor = new GPoint(14, 14);
					iconGPGPubMatch.infoWindowAnchor = new GPoint(14, 1);
					
					var iconLuckyPubMatch = new GIcon();
					iconLuckyPubMatch.image = "/extension/gpgdesign/design/ezflow/images/gpg-luckydip-exactmatch.png";	                
					iconLuckyPubMatch.iconSize = new GSize(28, 28);	    
					iconLuckyPubMatch.iconAnchor = new GPoint(14, 14);
					iconLuckyPubMatch.infoWindowAnchor = new GPoint(14, 1);

					var iconPubMatch = new GIcon();
					iconPubMatch.image = "/extension/gpgdesign/design/ezflow/images/pub-match.png";	                
					iconPubMatch.iconSize = new GSize(28, 28);	    
					iconPubMatch.iconAnchor = new GPoint(14, 14);
					iconPubMatch.infoWindowAnchor = new GPoint(14, 1);
					
					var iconArrow = new GIcon(); 
					iconArrow.image = "/extension/gpgdesign/design/ezflow/images/marker-arrow.png";
					iconArrow.iconSize = new GSize(28, 28);
					iconArrow.iconAnchor = new GPoint(14, 28);
					iconArrow.infoWindowAnchor = new GPoint(14, 1);
			
					customIcons["location"] = iconArrow;
					
					customIcons["item"] = iconPub;
					customIcons["item-match"] = iconPubMatch;
										
					customIcons["gpg-item"] = iconGPGPub;	
					customIcons["gpg-item-match"] = iconGPGPubMatch;	
					
					customIcons["lucky-item"] = iconLuckyPub;	
					customIcons["lucky-item-match"] = iconLuckyPubMatch;			
					
					dvMap = document.getElementById( sMapElID );
					var dvTab2 = document.getElementById( 'tab2' );
					if( dvTab2 && typeof( dvTab2 ) == 'object' )
					{
						// commented out due to change in the way tabview works
						//var sDisplay = dvTab2.style.display == 'block' ? 'block' : 'none';
						//dvTab2.style.display = 'block';
						map = new GMap2( dvMap );
						//dvTab2.style.display = sDisplay;
					}
					else
					{
						map = new GMap2( dvMap );
					}
					                
					  if( !sPostcode )
					  {
						sPostcode = 'uk';
					  }
					  var geocoder = new GClientGeocoder();
					  geocoder.setBaseCountryCode( 'uk' );
					map.addControl(new GSmallMapControl());
					map.addControl(new GMapTypeControl());  
					if( fLat !== undefined && 
						fLng !== undefined )
					{	             
				
								if( fLat === false && fLng === false )
								{
				
									  geocoder.getLatLng(
										sPostcode,
										setLocation );
								}
								else
								{
								
									var point = new GLatLng( fLat, fLng );
									setLocation( point, sName, bShowPopup ); 
								}
						 
					}
					else
					{
						 geocoder.getLatLng(
								sPostcode,
								setLocation,
								bShowPopup );
					}	               
  		}
  		
  		function setLocation( point, sName, bShowPopup )
		{
			map.setCenter( point, 16 );
			if( sName === undefined )
			{
				sName = 'It\'s located here';
			}
			var marker = createMarker( point, sName, 'item' );
                        map.addOverlay( marker );
			if( bShowPopup ) {
				marker.openInfoWindowHtml(sName);
			}
		}

		 function createMarker(point, content, type) {
                        var marker = new GMarker(point, customIcons[type]);
                        marker.hEvent = GEvent.addListener(marker, "click", function() {
                                        marker.openInfoWindowHtml(content);
                                        });
                        return marker;
                }
		function destroyMarkers( )
		{
			/*
			for( var i = 0; i < g_aMarkers.length; i++ )
			{
				GEvent.removeListener( g_aMarkers[i].hEvent );
				map.removeOverlay( g_aMarkers[i] );
				delete g_aMarkers[i];
			}
			g_aMarkers = [];	
			*/
			map.clearOverlays( );
		}
		
		
