global_markers = new Array; // global
var map_point;

function init()
{

	if (GBrowserIsCompatible()) {
	
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl()); 
		
		map.setCenter(new GLatLng(34.45221847282654, -89.912109375));
		map.setZoom(1); 
		
		
	 
		
		
		var url  = "getStores.php?zip_code="+document.getElementById('map_zip_code').value+"&miles="+document.getElementById('map_miles').value+"&page="+document.getElementById('map_page').value;
		search_stores(url);
 
	} 
}


function search_stores(xml_location) {
		selected_marker = null;
		
		GDownloadUrl(xml_location, function(data, responseCode) {
	
		var xml = GXml.parse(data);
		
		//GLog.write(xml.getElementsByTagName("Store").length);
		if(xml.getElementsByTagName("Store").length > 0)
		{			
		 
		 // Set the markers
			var markers = xml.documentElement.getElementsByTagName("Store");
			var bounds = new GLatLngBounds;
			
			for (var i = 0; i < xml.getElementsByTagName("Store").length; i++) {
				
				var point = new GLatLng(parseFloat(markers[i].getAttribute("Latitude")), parseFloat(markers[i].getAttribute("Longitude")));
				map.addOverlay(addStore(point,markers[i]));
	
				//Add the point & extend the neccecary bounds
				bounds.extend(point);
			}		
	
	
			// Set the zoom level & center to encompass all		
			var center_point = new GLatLng( (bounds.getSouthWest().lat() + bounds.getNorthEast().lat())/2, (bounds.getSouthWest().lng() + bounds.getNorthEast().lng())/2);
			map.setCenter(center_point, map.getBoundsZoomLevel(bounds));
								
		}				
	
	}); // end GDownloadUrl
		
	 
}

 

function addStore(point, marker_xml){


	// Create marker image
	 
    var icon = new GIcon();
    icon.image = "images/marker.png";
    icon.iconSize = new GSize(47, 59);
    icon.iconAnchor = new GPoint(28, 28);
    icon.infoWindowAnchor = new GPoint(11, 1);
    
	
	var marker = new GMarker(point, icon);  
  	var id = marker_xml.getAttribute("store_id");
	

 // Give the marker all of it's information
    marker.tabContent = new Object();
	marker.tabContent.location = '<font size="2"><b>'+marker_xml.getAttribute("sno")+'. '+marker_xml.getAttribute("store_name") + "</b><br >"+marker_xml.getAttribute("address_1") + "<br >" + marker_xml.getAttribute("city") + ", " +  marker_xml.getAttribute("state") + " " + marker_xml.getAttribute("zip") + "<br >" + marker_xml.getAttribute("phone") + "<br/>";
	if( marker_xml.getAttribute("online") == 1 )
		marker.tabContent.location += '<a href="http://store.greatamericancookies.com?sid='+marker_xml.getAttribute("store_id")+'" style="font-weight:bold;color:#666;">Order Cookie Cakes Online</a><br/>';
	marker.tabContent.location += '</font>';

    marker.internal_id = marker_xml.getAttribute("store_id");
    marker.id = marker_xml.getAttribute("store_id");
	
	
	GEvent.addListener(marker, "click", function() {
	    show_custom_info_window(marker);	  
	});
	
	GEvent.addListener(marker, "click_action", function() {
	    show_custom_info_window(marker);
	});
     
	global_markers[marker.internal_id] = marker;
	return marker
}



function show_custom_info_window(marker){	
	marker.closeInfoWindow();
	// nsmith 10/07/2008: commented out and replaced by following line
	//marker.openInfoWindow(marker.tabContent.location);
	marker.openInfoWindowHtml(marker.tabContent.location);
	position_window(marker);
}	


function position_window(marker){
  
	var pan_to = new GLatLng( marker.getPoint().lat(), marker.getPoint().lng() )
	
	var gPoint = map.fromLatLngToDivPixel(pan_to);
	gPoint.x = gPoint.x + 55;
	gPoint.y = gPoint.y - 85;

	var new_point = map.fromDivPixelToLatLng(gPoint);
	map.panTo( new_point );
	
	
	
	
} 
		

function activate_marker(marker_id){
	GEvent.trigger(global_markers[marker_id], "click_action");	
}

function close_window(){
    if ($('overlay')) {
	    $('overlay').style.display = 'none';
	}	
	return false;
}





//create the ToolTip overlay object
function InfoWindow(marker,html) {
	this.html_ = html;
	this.marker_ = marker;
}

InfoWindow.prototype = new GOverlay();

InfoWindow.prototype.initialize = function(map) {
	
	map.getPane(G_MAP_FLOAT_PANE).appendChild($('overlay'));
	this.map_ = map;
	 
		
}



InfoWindow.prototype.remove = function() {
	 
}


InfoWindow.prototype.copy = function() {
	return new InfoWindow(this.html_);
}


InfoWindow.prototype.redraw = function(force) {
if (!force) return;

var my_window = $('overlay');

var pixelLocation = this.map_.fromLatLngToDivPixel(this.marker_.getPoint());
var horiz_loc = parseInt(pixelLocation.x) - 55;
var vert_loc = parseInt(pixelLocation.y) - 150;


my_window.innerHTML = this.html_;
my_window.style.position = 'absolute';
my_window.style.left = horiz_loc + "px";
my_window.style.top = vert_loc + "px";
my_window.style.display = 'block';


}

GMarker.prototype.InfoWindowInstance = null;
GMarker.prototype.openInfoWindow = function(content) {


if(this.InfoWindowInstance == null) {
	this.InfoWindowInstance = new InfoWindow(this,content)
	map.addOverlay(this.InfoWindowInstance);
}
}


GMarker.prototype.closeInfoWindow = function() {
if(this.InfoWindowInstance != null) {
	map.removeOverlay(this.InfoWindowInstance);
	this.InfoWindowInstance = null;
}
}












Event.observe (window,'load',init,false);
Event.observe (window,'unload',GUnload,false); // Prevent memory leaks
selected_location = false;


