// Added by suggesion of SwansonRussell
var requireIcon = false;
// End Add


var map = null;
var geocoder = null;
var distance = 5;

var markerScript = document.getElementById('GeoCodeScript').src.replace('geocode.js','dealer_markers.aspx')

var Markers = new Array()
var Ma = -1

/* Setup the map properties */
function initialize() {
	map = new GMap2(document.getElementById("map_canvas"));
	// Add controls for moving and map type
	map.setUIToDefault();
	geocoder = new GClientGeocoder();
	geocoder.getLatLng('Mali',
		function(point){
			map.setCenter(point,2)
		})
}

/* Displays map of the area typed in the search textbox */
function showAddress(address,g_dist) {
	distance = g_dist
	geocoder.getLatLng(address,
		function(point) {
			if (!point) 
				alert(address + " not found.  Please try your search using a different description.")
			else {
				map.setCenter(point, GetZoom(distance))
				AddMarkers(point['y'],point['x'])
			}
		}
	)
}

/* Returns the proper Google Zoom level based on distance of search */
function GetZoom(dist) {
	switch(dist) {
		case '5':
			return 12
			break
		case '10':
			return 11
			break
		case '25':
			return 10
			break
		case '50':
			return 9
			break
		case '100':
			return 8	
			break
		case '250':
			return 7
			break
		default:
			return 11
	}
}

/* Function to add dealer markers to the map */
function AddMarker(map, Lat, Long, IconURL, IconSize, ShadowSize, Desc) {

	// Added by suggestion of SwansonRussel
	if (requireIcon && IconURL == '') return;
	// End Add

	// Build a point object with the Lat/Long of the dealer
	var point = new GLatLng(Lat,Long);
	
	// Create a new icon object based on the default icon
	var TriIcon = new GIcon(G_DEFAULT_ICON);
	if(IconURL.length)  
		TriIcon.image = IconURL;
	else
		if (typeof BaseIconURL != 'undefined') TriIcon.image = BaseIconURL
		
	// Create a marker object based on the point data, use the appropriate icon
	var marker = new GMarker(point,{icon:TriIcon});
	Ma++
	Markers[Ma] = marker


	// Store the description text in an attribute
	marker.alt = Desc;
	// Add the marker to the map
	map.addOverlay(marker);
	// Add a "click" event to the maker to show the details bubble
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(Desc); SetClass2(this.getPoint())
	});
	
	// Build the dealer list if the div exists	
	DealerList = document.getElementById('dealer_list') 
	if(DealerList != null){
		DealerList.style.display = 'block';
		var NewDealer = document.createElement('div')
		NewDealer.className = 'DealerListing'
		// Add an onclick event to trigger the popup in the map
		NewDealer.onclick = function() {SetClass(this); DoPopup(eval('new GLatLng' + point))}
		NewDealer.innerHTML = Desc
		NewDealer.id = "Listing_" + Ma
		NewDealer.title = point
		DealerList.appendChild(NewDealer)
	}
}

/* Finds a map marker by Point and opens the InfoWindow */
function DoPopup(marker){
	for(i=0;i<Markers.length;i++)
		if (Markers[i].getPoint().equals(marker))
			Markers[i].openInfoWindowHtml(Markers[i].alt)
}

/* Changes the class of the Dealer listing that is currently selected */
function SetClass(SelectedDiv){
	var DealerList = SelectedDiv.parentNode
	for(i=0;i<DealerList.childNodes.length;i++){
		DealerList.childNodes[i].className = 'DealerListing'
	}
	SelectedDiv.className = 'DealerListingSelected'
}

/* Changes the class of the Dealer listing associated with a clicked InfoWindow */
function SetClass2(newPoint){
	
	if(document.getElementById('dealer_list') == null) return false
	var DealerList = document.getElementById('dealer_list')
	
	for(i=0;i<DealerList.childNodes.length;i++){
		DealerList.childNodes[i].className = (newPoint == DealerList.childNodes[i].title) ? 'DealerListingSelected' : 'DealerListing'
		if (newPoint == DealerList.childNodes[i].title) DealerList.childNodes[i].scrollIntoView()
	}
}



/*	This function embeds the JO dealer locator javascript into the document 
		and loads the dealers within the region.  */
function AddMarkers(myLat,myLong){
	map.clearOverlays()
	DealerList = document.getElementById('dealer_list') 
	if(DealerList != null){
		DealerList.innerHTML = ''		
	}
	var html_doc = document.getElementsByTagName('head').item(0);
	var js = document.createElement('script');
	js.setAttribute('language', 'javascript');
	js.setAttribute('type', 'text/javascript');
	js.setAttribute('src', markerScript + '?lat=' + myLat + '&long=' + myLong + '&distance=' + distance + '&group=' + Group);
	html_doc.appendChild(js);
}




