var map = null;
var geocoder = null;
var tooltip = null;
var side_bar_html = "";
var gmarkers = [];
var htmls = [];
var i = 0;
var debug = false;

//This is page manipulation
var mapdiv = null;
var body = null;
var loaderDiv = null;
var sidebar = null;



function createMapDiv() {

//	mapdiv = document.getElementById("map");
mapdiv = document.createElement("div"); 
	//mapdiv.style.visibility = "visible";
	mapdiv.id = "map";

	mapdiv.style.width = "750px";
	mapdiv.style.height = "450px";

//	mapdiv.style.left = "50%";
//	mapdiv.style.top="50%";
//	mapdiv.style.position = 'absolute';
//	mapdiv.style.background = "#FFF";
//	mapdiv.style.lineHeight = "100px";
//	mapdiv.style.margin = "-50px 0 0 - 150px";
//	mapdiv.style.textalign = "centeer";
//	mapdiv.style.border = "1px solid #870108";
	
}

function createSidebarDiv() {
	
//	sidebarDiv = document.getElementById("map");
sidebarDiv = document.createElement("div"); 
	//sidebarDiv.style.visibility = "visible";
	sidebarDiv.id = "side_bar";

	sidebarDiv.style.width = "350px";
	sidebarDiv.style.height = "450px";

//	sidebarDiv.style.left = "50%";
//	sidebarDiv.style.top="50%";
//	sidebarDiv.style.position = 'absolute';
//	sidebarDiv.style.background = "#FFFFFF";
//	sidebarDiv.style.lineHeight = "100px";
//	sidebarDiv.style.margin = "-50px 0 0 - 150px";
//	sidebarDiv.style.textalign = "center";
//	sidebarDiv.style.border = "1px solid #870108";	
}
function createLoaderDiv() {
	
//	loadingDiv = document.getElementById("map");
loadingDiv = document.createElement("div"); 
	//loadingDiv.style.visibility = "visible";
	loadingDiv.id = "load_bar";

	loadingDiv.style.width = "350px";
	//loadingDiv.style.height = "450px";

	loadingDiv.style.left = "50%";
	loadingDiv.style.top="50%";
	loadingDiv.style.position = 'absolute';
	loadingDiv.style.background = "#FFF";
	loadingDiv.style.lineHeight = "100px";
//	loadingDiv.style.margin = "-50px 0 0 - 150px";
	loadingDiv.style.textalign = "center";
	loadingDiv.style.border = "1px solid #870108";	
	
	loadingDiv.appendChild(document.createTextNode("Loading, Please Wait\u2026"));
}
function addSidebar() {
if (debug) 	
alert("sidebar added");
//var sidebarTable = document.getElementById("sidebarTable");
var sidebarTable = document.createElement("table");
var tbody = document.createElement("tbody");
var row = document.createElement("tr");
var data1 = document.createElement("td");
data1.appendChild(sidebarDiv);
//data1.appendChild(document.createTextNode("Bobers"));
//data1.innerHTML = sidebarDiv.innerHTML
row.appendChild(data1);
tbody.appendChild(row);
sidebarTable.appendChild(tbody);
//sidebarTable.appendChild(document.createTextNode("bob"));
//alert(sidebarTable.innerHTML);
body.appendChild(sidebarTable);
}

function addLoader() {
	if (debug) 	
	alert("loader added");
body.appendChild(loadingDiv);		
}

function removeLoader() {
	if (debug) 	
	alert("loader removed");
var loader = document.getElementById("load_bar");
loader.parentNode.removeChild(loader); 	

}

function addMap() {
	if (debug) 	
	alert("map added");
	var mapTableData = document.getElementById("mapTableData");
mapTableData.appendChild(mapdiv);	
	
}
function removeMap() {
	if (debug) 	
	alert("map removed");
var mapElem = document.getElementById("map");
mapElem.parentNode.removeChild(mapElem); 	
}


function removeForm() {
	if (debug) 	
	alert("form removed");
var firstForm = document.getElementById("firstForm");
firstForm.parentNode.removeChild(firstForm); 	
}

function initPageCode() {
createMapDiv();	
createSidebarDiv();
createLoaderDiv();
body = document.getElementsByTagName("body")[0];
//var mapData = document.getElementById("map_data");


}

    function load() {
      if (GBrowserIsCompatible()) {
        
        
        
initPageCode();   
 removeForm();
 
 addLoader();   
 if (debug) 	
 alert("hi");
 
  mapdiv.style.visibility = "hidden";
  addMap();
 
 
map = new GMap2(document.getElementById("map"));
//map = new GMap2(mapdiv);


 removeMap();
mapdiv.style.visibility = "visible";
if (debug) 	
alert("bye");

geocoder=new GClientGeocoder();
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
map.setCenter(new GLatLng(42.24122708941077, -83.6220932006836), 13);
       tooltip = document.createElement("div");
     mapdiv.appendChild(tooltip);
      tooltip.style.visibility="hidden";
      }
      
     
    }
if (debug) 	
alert("after gmap stuff");
//<!--  <div id="map" style="width: 750px; height: 450px"></div> -->
//      // ====== set up marker mouseover tooltip div ======

//
//
//      // Read the data from example.xml
//      var request = GXmlHttp.create();
//      request.open("GET", "example.xml", true);
//      request.onreadystatechange = function() {
//        if (request.readyState == 4) {
//          var xmlDoc = request.responseXML;
//          // obtain the array of markers and loop through it
//          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 point = new GLatLng(lat,lng);
//            var html = markers[i].getAttribute("html");
//            var label = markers[i].getAttribute("label");
//            // create the marker
//            var marker = createMarker(point,label,html);
//          }
//          // put the assembled side_bar_html contents into the side_bar div
//          document.getElementById("side_bar").innerHTML = side_bar_html;
//        }
//      }
//      request.send(null);
//      }
//    } 



      // A function to create the marker and set up the event window
      function createMarker(point,name,html) {
        var marker = new GMarker(point);
        // === store the name so that the tooltip function can use it ===
        marker.tooltip = '<div class="tooltip">'+name+'</div>';
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowHtml(html);
        });
        gmarkers[i] = marker;
        htmls[i] = html;
        
        i++;
        //alert(f);
        map.addOverlay(marker);

        //  ======  The new marker "mouseover" and "mouseout" listeners  ======
        GEvent.addListener(marker,"mouseover", function() {
          showTooltip(marker);
        });        
        GEvent.addListener(marker,"mouseout", function() {
		tooltip.style.visibility="hidden"
        });        
      }

      // ====== This function displays the tooltip ======
      // it can be called from an icon mousover or a side_bar mouseover
      function showTooltip(marker) {
      	tooltip.innerHTML = marker.tooltip;
	var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.getBounds().getSouthWest(),map.getZoom());
	var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
	var anchor=marker.getIcon().iconAnchor;
	var width=marker.getIcon().iconSize.width;
	var pos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(offset.x - point.x - anchor.x + width,- offset.y + point.y +anchor.y)); 
	pos.apply(tooltip);
	tooltip.style.visibility="visible";
      }

      // ===== This function is invoked when the mouse goes over an entry in the side_bar =====
      // It launches the tooltip on the icon      
      function mymouseover(i) {
        showTooltip(gmarkers[i])
      }
      // ===== This function is invoked when the mouse leaves an entry in the side_bar =====
      // It hides the tooltip      
      function mymouseout() {
	tooltip.style.visibility="hidden";
      }

      // This function picks up the side_bar click and opens the corresponding info window
      function myclick(i) {
        gmarkers[i].openInfoWindowHtml(htmls[i]);
      }

function getSideBar() {
	return side_bar_html;
}
      
    

   


function showAddress(address, hotel) {
  try {
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
      	
        alert(address + " can't find!");
      } else {
      	
            var html = buildMarkerHtml(hotel);
            var label = buildMarkerLabel(hotel);
            // create the marker
            var marker = createMarker(point,label,html);


        map.setCenter(point, 13);
//        var marker = new GMarker(point);
//        map.addOverlay(marker);
//        marker.openInfoWindowHtml(buildMarker(hotel));
        
        
      }
    }
  );
}
catch ( e) {
	
}
}
function buildMarkerHtml(hotel) {
	var markerStr = hotel.name 
//	+hotel.address + "&lt;br&gt;"
	+"<p>" + hotel.address  + "</p>"
	+"<p>" + hotel.phone + "\n" + "</p>";
	
/*    	if (starsChecked == true) {
    	markerStr += "<p>" + "Number of Stars: " +hotel.stars + "\n" + "</p>";
    	
    	}
    	if (priceChecked == true) {
    	markerStr += "<p>" + "Price: " +hotel.price + "\n" + "</p>";	
    	}
    	*/
	return markerStr;

}

function buildMarkerLabel (hotel) {
	return hotel.name;
}

var request = null; 

 
  function getFullAddress()  {
    return getAddress()+", "+getCity()+", "+getState();
  }

function updateMap()  {
  showAddress(getFullAddress());
  }



 function createRequest()  {
    try {
      request = new XMLHttpRequest();
    } catch (trymicrosoft)   {
      try  {
        request = new ActiveXObject("Msxm12.XMLHTTP");
      } catch (othermicrosoft)  {
        try  {
         request = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (failed)  {
          request = null;
        }
      }
     }

if (request == null)
  alert("Error creating request object!");
 }
