
    
    //<![CDATA[






// geocoder variables and constants
	var places = [ ];
	var geo = new GClientGeocoder();
	var reasons = [ ] ;
		reasons[G_GEO_SUCCESS] = "Success" ;
		reasons[G_GEO_MISSING_ADDRESS] = "Missing Address: The address was either missing or had no value." ;
		reasons[G_GEO_UNKNOWN_ADDRESS] = "Unknown Address:  No corresponding geographic location could be found for the specified address." ;
		reasons[G_GEO_UNAVAILABLE_ADDRESS] = "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons." ;
		reasons[G_GEO_BAD_KEY] = "Bad Key: The API key is either invalid or does not match the domain for which it was given" ;
		reasons[G_GEO_TOO_MANY_QUERIES] = "Too Many Queries: The daily geocoding quota for this site has been exceeded." ;
		reasons[G_GEO_SERVER_ERROR] = "Server error: The geocoding request could not be successfully processed." ;
		reasons[G_GEO_BAD_REQUEST] = "A directions request could not be successfully parsed.";
		reasons[G_GEO_MISSING_QUERY] = "No query was specified in the input.";
		reasons[G_GEO_UNKNOWN_DIRECTIONS] = "The GDirections object could not compute directions between the points.";
	var accuracy = [ ] ;
		accuracy[0] = 'Unknown' ;
		accuracy[1] = 'Country' ;
		accuracy[2] = 'Region' ;
		accuracy[3] = 'Sub-region' ;
		accuracy[4] = 'Town' ;
		accuracy[5] = 'Post code' ;
		accuracy[6] = 'Street' ;
		accuracy[7] = 'Intersection' ;
		accuracy[8] = 'Address' ;
		accuracy[9] = 'Premise' ;   	
// fix precision of reported Lat/Lng 
    function fix6ToString(n) {
		return n.toFixed(6).toString();
    }
// geocoder variables and constants




    var IMAGES = [ "craftDlr_top", "mapIcon_guitar" ];
    var ICONS = [];
    var map = null;
    var mgr = null;
    var doc = null;
    
<!--<%=strDlrs %>//-->

     
    function setupMap() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        map.addControl(new GLargeMapControl());
        //map.setCenter(new GLatLng(52.17, -1.82), 4); //ENGLAND
        //map.setCenter(new GLatLng(37.4419, -122.1419), 13); //USA
        //map.setCenter(new GLatLng( 43.907787,-79.359741), 9); //USA
        //map.setCenter(new GLatLng(41.5, -98), 3); //USA
        map.setCenter(new GLatLng(38.4, -97), 3); //USA
        map.setZoom(4) 
        map.enableDoubleClickZoom();
        //window.setTimeout(setupWeatherMarkers, 0);
        map.setUIToDefault();
        //GDownloadUrl("example2.txt", setupMarkers);
      
      /*
        GEvent.addListener(map, "moveend", function() {
          myMapHasChanged('moved');
        });
      */
        
        // Add a move listener to restrict the bounds range
        GEvent.addListener(map, "move", function() {
         checkBounds();
        });

        //setup & populate marker manager
        mgr = new MarkerManager(map);
        //setupMarkers_top();
        mgr.addMarkers(setupMarkers_top(), 5, 7);  //6, 15); //add to minZoom, maxZoom
        //setupMarkers_zoomed();
        mgr.addMarkers(setupMarkers_zoomed(), 8, 17);  //6, 15); //add to minZoom, maxZoom
        //refresh mgr
        mgr.refresh();
        
        checkBounds();
      }
    }
    
    
      // The allowed region which the whole map must be within
      //var allowedBounds = new GLatLngBounds(new GLatLng(23.5,-126.4), new GLatLng(50.9,-66.2));
      var allowedBounds = new GLatLngBounds(new GLatLng(19.6,-130), new GLatLng(55,-62));
      var allowedZoom = 4; //sets max zoom allowed
      
      // If the map position is out of range, move it back
      function checkBounds() {
        // Perform the check and return if OK
        
        if (map.getZoom() < allowedZoom) {
            //set to max zoom
            map.setZoom(4);
        }
        else{

            // get current positions
            var currBounds = map.getBounds();
            var currSW = currBounds.getSouthWest();
            var currNE = currBounds.getNorthEast();
            
            //alert("currBounds: "+currBounds+ " mapCenter: " +mapCenter);
            
            if (allowedBounds.contains(currSW) && allowedBounds.contains(currNE)) {
                return; //OK
            }
            else{
                
                var currMaxLng = currNE.lng();
                var currMaxLat = currNE.lat();
                var currMinLng = currSW.lng();
                var currMinLat = currSW.lat();

                var currLngSpan = currNE.lng() - currSW.lng();
                var currLatSpan = currNE.lat() - currSW.lat();

                var mapCenter = map.getCenter();
                var currCtrLng = mapCenter.lng();
                var currCtrLat = mapCenter.lat();
                
                // get bounds limits
                var maxLng = allowedBounds.getNorthEast().lng();
                var maxLat = allowedBounds.getNorthEast().lat();
                var minLng = allowedBounds.getSouthWest().lng();
                var minLat = allowedBounds.getSouthWest().lat();
            
            
                // set new center coords to existing by default
                var newCtrLng = currCtrLng;
                var newCtrLat = currCtrLat;

                //determine which dimension is out of bounds
                if (currMaxLng > maxLng){
                    newCtrLng = maxLng-(currLngSpan/2)-2  //the final -1 is just top make sure
                    }
                else if(currMinLng < minLng){
                    newCtrLng = minLng+(currLngSpan/2)+2 //the final +1 is just top make sure
                    }
                    
                if(currMaxLat > maxLat){ 
                    newCtrLat = maxLat-(currLatSpan/2)-2  //the final -1 is just top make sure
                    }
                else if(currMinLat < minLat){
                    newCtrLat = minLat+(currLatSpan/2)+2 //the final +1 is just top make sure 
                    }
                map.setCenter(new GLatLng(newCtrLat,newCtrLng));

            }

        }
      }

    

/*
    function myMapHasChanged(strEvent){
        
        //set boundary
        var maxLat = 49.09545216253422;
        var minLng = 
        //1 get current positions
        var currBounds = map.getBounds();
        var currSW = currBounds.getSouthWest();
        var currNE = currBounds.getNorthEast();
        var currLngSpan = currNE.lng() - currSW.lng();
        var currLatSpan = currNE.lat() - currSW.lat();

        var mapCenter = map.getCenter();
        var currCtrLng = mapCenter.lng();
        var currCtrLat = mapCenter.lat();
        
        //2 check if any bounds are outside the maximum
        
        
        
        //2 determine new center point
        str = southWest.lat() + "," + southWest.lng() + " --- " + northEast.lat() + "," + northEast.lng()


        //need to grab new data and rebuild markers here ****************************************

        alert("window has been " + strEvent + "!\n\n"+str + "\n" + bounds);
        
        var mapCenter = map.getCenter();
        alert("mapCenter: "+mapCenter);
        //document.write("bounds... " + bounds);
    }
//opening bounds of usa
//                          SW(lat,long)                  NE(lat,long)          
//bounds... ((27.059125784374068, -126.5625), (53.33087298301704, -69.43359375))
        
        
*/

    function getIcon_ORIG_UNUSED(iconNum) {
        
        var icon = new GIcon();
        icon.image = "images/" + IMAGES[iconNum] + ".png";
        icon.iconAnchor = new GPoint(16, 16);
        icon.infoWindowAnchor = new GPoint(16, 0);
        icon.iconSize = new GSize(32, 32);
        icon.shadow = "images/" + IMAGES[iconNum] + "-shadow.png";
        icon.shadowSize = new GSize(59, 32);
        ICONS[iconNum] = icon;

      return ICONS[iconNum];
    }

    function getLargeIcon(iconNum) {
        
        var icon = new GIcon();
        icon.image = "images/mapIcon_guitar.png";
        icon.iconAnchor = new GPoint(16, 16);
        icon.infoWindowAnchor = new GPoint(16, 0);
        icon.iconSize = new GSize(32, 32);
        icon.shadow = "images/mapIcon_guitar-shadow.png";
        icon.shadowSize = new GSize(59, 32);
        ICONS[iconNum] = icon;

      return ICONS[iconNum];
    }
    
    function getSmallIcon(iconNum) {
        
        var icon = new GIcon();
        icon.image = "images/mapIcon_guitar_16.png";
        icon.iconAnchor = new GPoint(8, 8);
        icon.infoWindowAnchor = new GPoint(8, 0);
        icon.iconSize = new GSize(16, 16);
        icon.shadow = "images/mapIcon_guitar_16-shadow.png";
        icon.shadowSize = new GSize(30, 16);
        ICONS[iconNum] = icon;

      return ICONS[iconNum];
    }
    


    function createMarker_zoomed(posn, title, html) 
    {
      var marker = new GMarker(posn, {title: title, icon: getLargeIcon(1), draggable:false}); // was icon: getIcon(1)
        GEvent.addListener(marker, 'click', function()
            {  marker.openInfoWindowHtml(html); } );
        //GEvent.addListener(marker, "click", function() {
        //      marker.closeInfoWindow();
        //    });
      return marker;
    } 
    

    function setupMarkers_zoomed(doc) {
    //alert("setupWeatherMarkers: " + doc);
    
      //mgr = new MarkerManager(map);

      var batch = []; //holds the GMarker's to add with addMarkers
        
        //alert("dlrDat.length: " + dlrDat.length);
        
        for (var i=0; i<dlrDat.length; i++) {
          if (dlrDat[i].length > 1) {

            var currDealer = dlrDat[i];
            
            var dlrlat = parseFloat(currDealer[0]);
            var dlrlng = parseFloat(currDealer[1]);
            var dlrName = currDealer[2];
            var dlrPhWrk = currDealer[3];
            var dlrPhMob = currDealer[4];
            var dlrWeb = currDealer[5];
            var dlrEml = currDealer[6];
            var dlrStks = currDealer[7];

            var html = "";
            html = html + "<div id='dlrInfoDiv'>";
            
            if(dlrName!="")
                {html = html + "<span class='dlrName'>" + dlrName + "</span><br/>";}
            if(dlrPhWrk!="")
                {html = html + "shop no: " + dlrPhWrk + "<br/>";}
            if(dlrPhMob!="")
                {html = html + "mobile no: " + dlrPhMob + "<br/>";}
            if(dlrWeb!="")
                {html = html + "<a href='http://" + dlrWeb + "' target='_blank' class='linksDlr'>website</a><br/>";}
            if(dlrEml!="")
                {html = html + "<a href='mailto:" + dlrEml + "' target='_blank' class='linksDlr'>email</a><br/>";} 
            if(dlrStks!="")
                {html = html + "" + dlrStks + "<br/>";}

            html = html + "</div>";


            //alert("html: " + html);
            var label = dlrName;
            var point = new GLatLng(dlrlat,dlrlng);
            // create the marker
             batch.push( createMarker_zoomed(point, label, html));  
           

          }
        }
      return batch;
      //mgr.addMarkers(batch, 8, 18);  //6, 15); //add to minZoom, maxZoom
      //mgr.refresh();
    }

    function createMarker_top(posn, title, html) 
    {
      var marker = new GMarker(posn, {title: 'crafter dealer', icon: getSmallIcon(0), draggable:false}); // was icon: getIcon(0)
        GEvent.addListener(marker, 'click', function()
            {  marker.openInfoWindowHtml(html); } );
      return marker;
    } 
     
    function setupMarkers_top(doc) {
    //alert("setupWeatherMarkers: " + doc);
    
      //mgr = new MarkerManager(map);

      var batch = []; //holds the GMarker's to add with addMarkers
        
        //alert("dlrDat.length: " + dlrDat.length);
        
        for (var i=0; i<dlrDat.length; i++) {
          if (dlrDat[i].length > 1) {

            var currDealer = dlrDat[i];
            
            var dlrlat = parseFloat(currDealer[0]);
            var dlrlng = parseFloat(currDealer[1]);


            var html = "";
            html = html + "<div id='dlrInfoDiv'>";
            
            html = html + "<span class='dlrName'>This is a Crafter Dealer</span><br/>";
            html = html + "For more detailed dealer information,<br/>you will need to zoom further in to the<br/> map, and then click on one of the<br/> larger dealer icons.";
       
            html = html + "</div>";
            
            var point = new GLatLng(dlrlat,dlrlng);
            
            // create the marker
             batch.push( createMarker_top(point,'',html));  
          }
        }
      return batch;
      //mgr.addMarkers(batch, 5, 7);  //6, 15); //add to minZoom, maxZoom
      //mgr.refresh();
    }
    



// Find an Address...
	function findaddress(){
	
	    var strResultsHTML = "";
	    
		if (document.addressform.address.value) {
		    var numFound = 0;
			var address = document.addressform.address.value;
			address = address.replace(" ","+");

			geo.getLocations(address, function (result) { 
				if (result.Status.code == G_GEO_SUCCESS) {
      				strResultsHTML = "<b>Found " + result.Placemark.length +" result(s)...</b>";
      				for (var i=0; i<result.Placemark.length; i++){
						places[i] = result.Placemark[i];
      					var p = places[i].Point.coordinates;
      					var currPoint = new GLatLng(p[1],p[0])
                        if (allowedBounds.contains(currPoint)) {
                            //OK
       					    strResultsHTML += "<br /> <input type='button' value='Go To' onclick='finder_setMapPos(" + i + ")' name='goto" + i + "' /> <b>"+(i+1)+": "+ places[i].address + "</b> : " + p[1] + " , " + p[0] + "<br /> Accuracy: " + accuracy[places[i].AddressDetails.Accuracy] + "<hr/>"; //+ ", Code: " + reasons[result.Status.code];
       					    numFound++;
       					}
     				}
 				    if(numFound==0){
 				        displayResults('ERROR: Could not find "' + address + '" in the USA!',0);
 				    }
 				    else{
 				        displayResults(strResultsHTML,numFound);
 				        finder_setMapPos(0);
 				    }
				    
     			} else {
      				var reason="Code "+result.Status.code;
      				if (reasons[result.Status.code]) {
      					reason = reasons[result.Status.code]
      				} 
      				displayResults('ERROR: Could not find "' + address + '" in the USA! ' + reason,0);
     			}
    		} ) ;
   		} else {
			displayResults('ERROR: Must have something in the address!',0)
		}
	}
	
// Set new map pos for find address
	function finder_setMapPos(n) {
      	var p = places[n].Point.coordinates;
      	var currPoint = new GLatLng(p[1],p[0])
        if (allowedBounds.contains(currPoint)) {
      	    map.setCenter(new GLatLng(p[1],p[0]), 8 + places[n].AddressDetails.Accuracy);
		    //marker.setPoint(new GLatLng(p[1],p[0]));
		    //marker.redraw();
		    //GEvent.trigger(marker,'click'); 
		}					
	}


    //]]>