Event.observe(window, 'load', function() {
	initmap(); 
});

function showHideDealer() {
	if ($('dealer-locator').getStyle('display') == 'none') {
		$('dealer-locator').style.display = "block";	
		$('product-right').style.display = "none";	
	} else {
		$('dealer-locator').style.display = "none";	
		$('product-right').style.display = "block";	
	}
}

// Google maps
function initmap() {
	
	if (GBrowserIsCompatible()) {
		
		// Array to hold markers
		// Attach it to the window object to make it global
		window.gmarkers = [];
		
		
		// First off setup the custom icon
		var icon = new GIcon();
		icon.image = "http://www.holmesonhomes.com/makeitright/Holmes_Products/_images/holmes_marker.png";
		icon.shadow = "http://www.holmesonhomes.com/makeitright/Holmes_Products/_images/holmes_marker_shadow.png";
		icon.iconSize = new GSize(23, 36);
		icon.shadowSize = new GSize(36, 36);
		icon.iconAnchor = new GPoint(11, 34);
		icon.infoWindowAnchor = new GPoint(5, 15);
		
		// Attach it to the window object to make it global
		window.icon = icon;
		
		// Create the map
		window.map = new GMap2(document.getElementById("map"));
		
		// Set the center to show the world
		window.map.setCenter(new GLatLng(41.244772343082076, -97.3828125), 2);	 
		
		// Once the map is created, set the map div to display none
		showHideDealer();
		
	} else {
		$('map').innerHTML = "Your browser is incompatible with Google Maps";
	}
	
}

// Google maps function to create markers
function createMarker(point, icon, sid) {
	
	var marker = new GMarker(point, {icon:icon});
	
	/*GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(html);
	});*/
	
	marker.sid = sid;
	marker.point = point;
	
	/*GEvent.addListener(marker, "mouseover", function() {
		marker.setImage("http://www.mixedmarket.com/bodytools/images/layout/marker_orange.png");
	});
	
	GEvent.addListener(marker, "mouseout", function() {
		marker.setImage("http://www.mixedmarket.com/bodytools/images/layout/marker_green.png");
	});*/
	
	window.gmarkers.push(marker);
	
	return marker;
	
}


function doSearch() {
				
	if ($('dealer-locator').getStyle('display') == 'none') {
		$('dealer-locator').style.visibility = "visible";	
		$('dealer-locator').style.display = "block";	
		$('product-right').style.display = "none";
		$('zip').value = $F('fzip');
	}
	
	$('dealer-results').update('');
				
	pars = "zip="+$('zip').value+"&products_id="+pid;
					
	var url = 'ajax.dealer_results.php';
	
	new Ajax.Updater('dealer-results', url, { method: 'get', parameters: pars });
		
		
	if (GBrowserIsCompatible()) {
		updateMap();
	}
		
}



function updateMap() {
	
	//hideMarkers();
	
	map.clearOverlays();
	//delete (window.gmarkers);

	window.gmarkers = [];
	
	var bounds = new GLatLngBounds();

	pars = "?zip="+$('zip').value+"&products_id="+pid;
	
	GDownloadUrl("ajax.dealer_markers.php"+pars, function(doc) {
		var xmlDoc = GXml.parse(doc);
		
		var markers = xmlDoc.documentElement.getElementsByTagName("marker");
	
		for (var i = 0; i < markers.length; i++) {
			
			// obtain the attribues of each marker
			var lat = parseFloat(markers[i].getAttribute("lat"));
			var lng = parseFloat(markers[i].getAttribute("lng"));
			var sid = parseFloat(markers[i].getAttribute("sid"));

			var point = new GLatLng(lat,lng);
			
			bounds.extend(point);
			
			// create the marker
			var marker = createMarker(point, window.icon, sid);
			window.map.addOverlay(marker);
		}
		
		if (markers.length < 1) {
			window.map.setCenter(new GLatLng(41.244772343082076, -97.3828125), 2);	 
		} else {
		
			map.addControl(new GSmallMapControl());
			
			map.setCenter(new GLatLng(0,0), 0);
			
			// work out the zoom from the bounds
			map.setZoom(7);
	
			// work out the center from the bounds
			map.setCenter(bounds.getCenter());
			
		}
			
	}); 	
	
	map.checkResize();
	
}

function showLargerMap() {
	Lightview.show({
    href: 'dealer_large_map.php?zip='+$F('zip')+'&products_id='+pid,
    rel: 'iframe',
    options: {
			width: 700,
			height: 570,
      topclose: true
    }
  });
}
