global_markers = new Array; // global
var map_point; 
var bounds;
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(4); 
		
		 
		bounds = map.getBounds();
		
		search_stores(url);
 
	} 
}

function init2(property_id){

	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(4); 
		
		 
		
		
		search_stores(url);
 
	} 
}

function init_search(query_string){
  
	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(4); 
		
		 
		
		var url  = "getproperties.php"+"?"+query_string;
		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("Property").length);
		if(xml.getElementsByTagName("Property").length > 0)
		{			
		 
		 // Set the markers
			var markers = xml.documentElement.getElementsByTagName("Property");
			var bounds = new GLatLngBounds;
			
			for (var i = 0; i < xml.getElementsByTagName("Property").length; i++) {
				
				//GLog.write(markers[i].getAttribute("Latitude")+'\n');
				
				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 marker = new GMarker(point);
  	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><a title="Click here to view details." href="details.php?property_id='+marker_xml.getAttribute("store_id")+'" style="color: #05674E">'+marker_xml.getAttribute("store_name") + "</a></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/>"+'</font>';
	  //GLog.write(marker.tabContent.location+'\n');

    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();
	marker.openInfoWindow(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, bounds) {
	this.html_   = html;
	this.marker_ = marker;
	this.bounds_ = bounds;
}

InfoWindow.prototype = new GOverlay();

InfoWindow.prototype.initialize = function(map) {
	
	var div   =document.createElement("div");
	div.setAttribute("class","info_window"); 
	div.style.position = "absolute"; 
	div.style.backgroundColor = "#fff";
	div.style.width = "200px";
	div.style.padding="5px";
	div.style.border="1px solid #cccccc";
	
	Event.observe (div,'click', function(){this.remove()}); 
	
	map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
	this.map_ = map; 
  this.div_ = div; 
		
}



InfoWindow.prototype.remove = function() {
	 this.div_.parentNode.removeChild(this.div_);
}


InfoWindow.prototype.copy = function() {
	return new InfoWindow(this.marker_, this.html_, this.bounds_);
}


InfoWindow.prototype.redraw = function(force) {
// We only need to redraw if the coordinate system has changed
  if (!force) return;

  var pixelLocation = this.map_.fromLatLngToDivPixel(this.marker_.getPoint()); 
  var horiz_loc = parseInt(pixelLocation.x) - 0; 
  var vert_loc = parseInt(pixelLocation.y) - 125;



  // Now position our DIV based on the DIV coordinates of our bounds 
  this.div_.style.left = horiz_loc + "px";
  this.div_.style.top = vert_loc + "px";

 

this.div_.innerHTML = this.html_; 


}

GMarker.prototype.InfoWindowInstance = null;
GMarker.prototype.openInfoWindow = function(content) {


if(this.InfoWindowInstance == null) {
	this.InfoWindowInstance = new InfoWindow(this,content, bounds)
	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;


