	var geocoder = new GClientGeocoder();
	var map;
	var blnDebug  = false;	
	var gMapCanvasID = 'gMap';
    var bounds;	

	   
	var MapItem = function()
	{
			this.itemid;
			this.latY;
			this.longX;
			this.address;
			this.addressSuccess;
			this.title; 
			this.isPivot;
			this.markerAdded;
			this.hasData;
			this.searchAddress;
			this.point;
			this.distanceFromDestination;
			this.viewdetailsurl;
			this.iNearestToPivotOrder;
			this.requiresGeoCoding;
	} 	// fn.MapItem
	var ArrMapItems =  new Array();			
	// Global map item definition contents defined server side.
	var mi = new MapItem();	
	
	
	var ArrGeoCodedMapItems = new Array(); 
	
	function alertTrace(sVar)
	{
		 if (blnDebug == true)
		 {
			alert(sVar);
		 } 
	}// fn.alertTrace 			
		
 
		
				
	function MapItem_Describe(mpitm)
	{
			var describe = "";
			describe += " itm.itemid		: " + mpitm.itemid + "\n";
			describe += " itm.latY		 	: " + mpitm.latY + "\n";
			describe += " itm.longX		 	: " + mpitm.longX + "\n";
			describe += " itm.address		: " + mpitm.address + "\n";
			describe += " itm.addressSuccess: " + mpitm.addressSuccess + "\n";
			describe += " itm.title			: " + mpitm.title + "\n";
			describe += " itm.isPivot		: " + mpitm.isPivot + "\n";
			describe += " itm.markerAdded	: " + mpitm.markerAdded + "\n";
			describe += " itm.hasData		: " + mpitm.hasData + "\n";			
			describe += " itm.distanceFromDestination: " + mpitm.distanceFromDestination + "\n";														
			describe += " itm.viewdetailsurl: " + mpitm.viewdetailsurl + "\n";	
			describe += " itm.iNearestToPivotOrder: " + mpitm.iNearestToPivotOrder + "\n";	
			describe += " itm.requiresGeoCoding	: " + mpitm.requiresGeoCoding + "\n";
			return describe;
	} // fn.MapItem_Describe
		
	function addToArray(itm) 
	{

	try {
		ArrMapItems[ArrMapItems.length] = itm;
		
		}catch(ex){
			alertTrace('fn.addToArray Error');
		}			
	} //fn.addToArray
	
	
	
	

	function viewArrayContents()
	{	
	try {
		for (var i in ArrMapItems )
		{
			alertTrace(MapItem_Describe(ArrMapItems[i]));
		}	
		} catch(ex){
			alertTrace('fn.viewArrayContents Error');
		}						
	} // fn.viewArrayContents
	


	function loadMarkerItems_Custom()
	{
	try {	

	var firstValidItemSet = false;
		
			// load the primary marker
	for (var i in ArrMapItems )
		{
			if (ArrMapItems[i].requiresGeoCoding == false)
			{			
				addLetteredMarkerToMap_Custom(ArrMapItems[i]);
			    ArrMapItems[i].markerAdded = true;	
						
			}	// if no geo coding required		
		} // for

	
		} catch(ex){
	
				alertTrace('fn.loadMarkerItems_Custom Error ' + ex.description);	
		}		
	}	// fn.loadMarkersForGeoCoding
		
		
	
		

	function openItem(itemID){
	try {
			var itmMi = null;
			itmMi = getItemByItemID(itemID);
				if (itmMi != null)
				{
				alertTrace(MapItem_Describe(itmMi));
				}
		} catch(ex){
			alertTrace('fn.openItem Error');	
		}
	
	}// openItem 
		
		
	function getItemByItemID(itemID){
	try {	
			for ( var i in ArrMapItems )
				{
				if (ArrMapItems[i].itemid == itemID) 
				{ 
					return ArrMapItems[i];
				}										 								
				}	
			 return null;		
		} catch(ex){
			alertTrace('fn.getItemByItemID Error');	
		}			 		 
	}// getItemByItemID
	
	


     function createLetteredMarker (index, point) {       
        try
        {
                var baseIcon = new GIcon(G_DEFAULT_ICON);
                baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
                baseIcon.iconSize = new GSize(20, 34);
                baseIcon.shadowSize = new GSize(37, 34);
                baseIcon.iconAnchor = new GPoint(9, 34);
                baseIcon.infoWindowAnchor = new GPoint(9, 2);
                
                // lettered marker which starts at "A" and wraps at "Z"
                var range       = "Z".charCodeAt(0) - "A".charCodeAt(0) + 1;
                var letter      = String.fromCharCode("A".charCodeAt(0) + (index % range));
                var iconType    = new GIcon(baseIcon);
                iconType.image  = "http://www.google.com/mapfiles/marker" + letter + ".png";
                
                var markerOptions = { 
                    icon: iconType,
                    bouncy: true
                };
                
		         var marker = new GMarker(point, markerOptions);
        
        
                return marker;
        } catch(ex) {
        	alertTrace('fn.createLetteredMarker Error');	
        }
        
     }  // fn.createLetteredMarker  
	
	
	
	function addLetteredMarkerToMap_Custom(objMapItem)
	{
	try	{	
			if (!(map))
			{
			
			if (GBrowserIsCompatible()) {
				try
				{
				map = new GMap2(document.getElementById(gMapCanvasID));
				map.addControl(new GSmallMapControl());
				map.addControl(new GMapTypeControl());					
						
			     } catch(ex) {
        			alertTrace('fn.addLetteredMarkerToMap_Custom map create failed');	
				 }	
				
			}
			
			
			}
	
	
	
			if (map) 
			{
			
				if (objMapItem.markerAdded == false)
				{	
					map.setCenter(new GLatLng(objMapItem.latY, objMapItem.longX),7);
				}
				
				var point = new GLatLng(objMapItem.latY, objMapItem.longX);
				var marker = null;			
													
				marker = createLetteredMarker(objMapItem.iNearestToPivotOrder - 1 ,point);	
				GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(formatAddressForInfoBubble(objMapItem)); map.setCenter(marker.getLatLng(),12);});
				
				
				try
				{
				var link = null;
				link = document.getElementById("maplink" + objMapItem.itemid);		 							
				GEvent.addDomListener(link, "click", function() {marker.openInfoWindowHtml(formatAddressForInfoBubble(objMapItem)); map.setCenter(marker.getLatLng(),12);}); 
			
			
				} catch(ex){
					alertTrace('Issue with map link');	
				}
         
         //       map.setCenter(marker.getLatLng(),limitZoomLevel( map.getBoundsZoomLevel(bounds) + 1 ));
				
				
				
				map.addOverlay(marker);	
			}
		
		} catch(ex){
			alertTrace('fn.addLetteredMarkerToMap_Custom Error');	
		}				
	}	 // fn.addLetteredMarkerToMap_Custom  
	
	
	function reApplyMapPinsForMapLinkEvent(iStartIndex, iEndIndex)
	{
		for (var i in ArrMapItems )
		{
		
			if (ArrMapItems[i].iNearestToPivotOrder > iStartIndex && ArrMapItems[i].iNearestToPivotOrder <= iEndIndex)
			{
			if (ArrMapItems[i].requiresGeoCoding == false)
			{			
				alertTrace('Re-adding ' + ArrMapItems[i].title);
				addLetteredMarkerToMap_Custom(ArrMapItems[i]);
									
			}	// if no geo coding required		
			}
		
		} // for

	
	}

	
	function reAlignDomListeners(iPageIndex)
	{
	
		alertTrace('calling reAlignDomListeners ' + iPageIndex);
		//var iPageIndex = document.getElementById("tbCurrentPageIndex").value;
		var iStartIndex = 0;
		var iEndIndex = 0;
		
		if (iPageIndex == 0)
		{
			iStartIndex = 0;
			iEndIndex= 6;
			reApplyMapPinsForMapLinkEvent(iStartIndex,iEndIndex);		
		
		}else {
		
			iStartIndex =radGridPageSize * iPageIndex;
			iEndIndex= iStartIndex + radGridPageSize;
			reApplyMapPinsForMapLinkEvent(iStartIndex,iEndIndex);
					
		}
					
		onResponseEnd();
				
			
	}
		
	


	function handleResponse(response)
	{
	
	if (!response || response.Status.code != 200) {
		alertTrace("handleResponse unable to geocode that address");
		} else {
			place = response.Placemark[0];
			gMi.latY  = place.Point.coordinates[1];
			gMi.longX = place.Point.coordinates[0];
			alertTrace('Success');
			alertTrace(openItem(gMi.ItemID));
		}
	}


	function geoCodeForCoordinatesMapItem(addressVal) {
	try
		{	
			geocoder.getLocations(addressVal, handleResponse);
			} catch(ex){
				alertTrace('fn.geoCodeForCoordinatesMapItem Error ' + ex.description);	
			}				
		} // fn.geoCodeForCoordinatesMapItem  
			
		
	function geoCodeForCoordinatesMapItemX(objMapItem, addressVal) {
	try
		{	 
		
		
			
			geocoder.getLatLng( 
				addressVal,
				function(point) {
				 objMapItem.point= point;
			//	setTimeout("", 1000);
				if (!point)
				 {	
					 
						
				 } else {
						
					
						objMapItem.requiresGeoCoding = false;
						objMapItem.hasData = true;	
						objMapItem.addressSuccess =addressVal;
						objMapItem.latY = point.y;
						objMapItem.longX = point.x;		
						addToGArray(itm);	
				} 
				} // callback function
			);  //geocoder
				return objMapItem;
			} catch(ex){
				alertTrace('fn.geoCodeForCoordinatesMapItem Error ' + ex.description);	
			}				
		} // fn.geoCodeForCoordinatesMapItem  
		

		
	function formatAddressForInfoBubble(objMapItem)
	{
	try
		{	
			var sRetVal = "";		
			var orgTitleHtml = "<strong>" + objMapItem.title + "</strong><br />";		
			var mySplitVal = objMapItem.address.split(",");
			var sReformattedAddress = "";
			var intOffSet = 0;
			var sCoordinateData = "";
							
			if (blnDebug)
			{
				sCoordinateData = "<br />Long = " + objMapItem.longX + "<br />" + "Lat =" +  objMapItem.latY + " Distance From Destination = " +  objMapItem.distanceFromDestination;
			}

			// preserve the last comma elements		
			// eg. NSW, 2020
	
			if (mySplitVal.length >= 2)
			{
				intOffSet = 2;
			} else {
				intOffSet = mySplitVal.length;
			}	

			for(i = 0; i < mySplitVal.length; i++)
			{	if (i <= intOffSet)
				{	sReformattedAddress += 	mySplitVal[i] + "<br />";						
					} else {
					sReformattedAddress += 	mySplitVal[i] + ", ";		
				}
			}
			if (sReformattedAddress != "undefined")
			{ sReformattedAddress = sReformattedAddress.slice(0, -2);}						
			sRetVal = "<div style='font-family : Tahoma; color : Black; font-size : 12px; text-align:center;'>" + orgTitleHtml + sReformattedAddress +  "<br /></div>";										
			return sRetVal;
		} catch(ex){
			alertTrace('fn.formatAddressForInfoBubble Error');	
		}				
	}	// fn.formatAddressForInfoBubble end





function PersistFormUrl()
{	try {
			document.Form1.action = window.location;
	} catch(ex)
	{
		alertTrace('fn.PersistFormUrl Error');
	} 
	
} // fn.PersistFormUrl




	