//var map;
var geocoder;
var cityZoom = 11;
var startZoom = 13;
var defaultZoom = 10;
var defaultAddress = "United States";

var map = null;
var polys = [];
var lines = [];
var tooltip = document.createElement("div");

function init()
{
	if (GBrowserIsCompatible())
	{
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		geocoder = new GClientGeocoder();
		//showDefault(defaultAddress);
	}
}

function showCity(address,city,state) {
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " not found");
      } else {
        map.setCenter(point, cityZoom);
		/*
		var listItem = document.createElement('li');
		var listItemLink = listItem.appendChild(document.createElement('p'));
		listItemLink.innerHTML = "<a href=''>Start A New Search</a>";
				
		document.getElementById('citylocation-list').appendChild(listItem);
		*/
		var listItem = document.createElement('li');
		var listItemLink = listItem.appendChild(document.createElement('a'));
		listItemLink.innerHTML = "Start A New Search";
		listItemLink.href = "";
		
		document.getElementById('citylocation-list').appendChild(listItem);
		//var goBack = document.getElementById("citylocation");
		//goBack.firstChild.nodeValue = "Start A New Search";
      }
    }
  );
}

/*function showAddress(addr,name,id,type) {
	var address = addr;
  new google.maps.getLatLng(
    address,
    function(point) {
      if (!point) {
        //alert(address + " not found");
      } else {
		
		if(type == 'hotel'){
			var Icon = new GIcon();
			Icon.image = "/icons/hotel.png";
			Icon.shadow = "/icons/shadow.png";
			//Icon.iconSize = new GSize(12, 20);
			//Icon.shadowSize = new GSize(22, 20);
			Icon.iconAnchor = new GPoint(6, 20);
			Icon.infoWindowAnchor = new GPoint(6, 1);
			Icon.infoShadowAnchor = new GPoint(13, 13);
			
			var infoTabs = [
			new GInfoWindowTab("Details", name + "<br><a href='/details/-hotels_/" + id + "/'>View Hotel</a> - <a href='http://travel.ian.com/index.jsp?pageName=hotInfo&cid=139972&hotelID=" + id + "&stateProvince=&country=US&hotel=1' target='_blank'>Reserve a Room</a>"),
			new GInfoWindowTab("Information", addr)
			];
			var marker = new GMarker(point, Icon);
			GEvent.addListener(marker, "click", function() {
			 marker.openInfoWindowTabsHtml(infoTabs);
			 });
		} else if(type == 'restaurant'){
			var Icon = new GIcon();
			Icon.image = "/icons/food.png";
			Icon.shadow = "/icons/shadow.png";
			//Icon.iconSize = new GSize(12, 20);
			//Icon.shadowSize = new GSize(22, 20);
			Icon.iconAnchor = new GPoint(6, 20);
			Icon.infoWindowAnchor = new GPoint(6, 1);
			Icon.infoShadowAnchor = new GPoint(13, 13); 
		
			var infoTabs = [
			new GInfoWindowTab("Details","<b>"+ name + "</b><br>" + addr)
			];
			var marker = new GMarker(point, Icon);
			GEvent.addListener(marker, "click", function() {
			 marker.openInfoWindowTabsHtml(infoTabs);
			 });
		} else if(type == 'entertainment'){
			var Icon = new GIcon();
			Icon.image = "/icons/entertain.png";
			Icon.shadow = "/icons/shadow.png";
			//Icon.iconSize = new GSize(12, 20);
			//Icon.shadowSize = new GSize(22, 20);
			Icon.iconAnchor = new GPoint(6, 20);
			Icon.infoWindowAnchor = new GPoint(6, 1);
			Icon.infoShadowAnchor = new GPoint(13, 13); 
		
			var infoTabs = [
			new GInfoWindowTab("Details","<b>"+ name + "</b><br>" + addr + "<br>" + city + ", " + state + "<br><a href='/entertainment/" + encodeUrlWithDash(city) + "-entertainment_" + encodeUrlWithDash(name) + "/" + id + "/'>View Listing</a>")
			];
			var marker = new GMarker(point, Icon);
			GEvent.addListener(marker, "click", function() {
			 marker.openInfoWindowTabsHtml(infoTabs);
			 });
		} else if(type == 'nightlife'){
			var Icon = new GIcon();
			Icon.image = "/icons/entertain.png";
			Icon.shadow = "/icons/shadow.png";
			//Icon.iconSize = new GSize(12, 20);
			//Icon.shadowSize = new GSize(22, 20);
			Icon.iconAnchor = new GPoint(6, 20);
			Icon.infoWindowAnchor = new GPoint(6, 1);
			Icon.infoShadowAnchor = new GPoint(13, 13); 
		
			var infoTabs = [
			new GInfoWindowTab("Details","<b>"+ name + "</b><br>" + addr + "<br>" + city + ", " + state + "<br><a href='/nightlife/" + encodeUrlWithDash(city) + "-nightlife_" + encodeUrlWithDash(name) + "/" + id + "/'>View Listing</a>")
			];
			var marker = new GMarker(point, Icon);
			GEvent.addListener(marker, "click", function() {
			 marker.openInfoWindowTabsHtml(infoTabs);
			 });
		} else if(type == 'general'){
			var Icon = new GIcon();
			Icon.image = "/icons/scenic.png";
			Icon.shadow = "/icons/shadow.png";
			//Icon.iconSize = new GSize(12, 20);
			//Icon.shadowSize = new GSize(22, 20);
			Icon.iconAnchor = new GPoint(6, 20);
			Icon.infoWindowAnchor = new GPoint(6, 1);
			Icon.infoShadowAnchor = new GPoint(13, 13); 
		
			var infoTabs = [
			new GInfoWindowTab("Details","<b>"+ name + "</b><br>" + addr + "<br>" + city + ", " + state + "<br><a href='/general/" + encodeUrlWithDash(city) + "-general-interest_" + encodeUrlWithDash(name) + "/" + id + "/'>View Listing</a>")
			];
			var marker = new GMarker(point, Icon);
			GEvent.addListener(marker, "click", function() {
			 marker.openInfoWindowTabsHtml(infoTabs);
			 });
		} else if(type == 'retail'){
			var Icon = new GIcon();
			Icon.image = "/icons/building.png";
			Icon.shadow = "/icons/shadow.png";
			//Icon.iconSize = new GSize(12, 20);
			//Icon.shadowSize = new GSize(22, 20);
			Icon.iconAnchor = new GPoint(6, 20);
			Icon.infoWindowAnchor = new GPoint(6, 1);
			Icon.infoShadowAnchor = new GPoint(13, 13); 
		
			var infoTabs = [
			new GInfoWindowTab("Details","<b>"+ name + "</b><br>" + addr + "<br>" + city + ", " + state + "<br><a href='/retail/" + encodeUrlWithDash(city) + "-retail_" + encodeUrlWithDash(name) + "/" + id + "/'>View Listing</a>")
			];
			var marker = new GMarker(point, Icon);
			GEvent.addListener(marker, "click", function() {
			 marker.openInfoWindowTabsHtml(infoTabs);
			 });
		} else if(type == 'golf'){
			var Icon = new GIcon();
			Icon.image = "/icons/golf.png";
			Icon.shadow = "/icons/shadow.png";
			//Icon.iconSize = new GSize(12, 20);
			//Icon.shadowSize = new GSize(22, 20);
			Icon.iconAnchor = new GPoint(6, 20);
			Icon.infoWindowAnchor = new GPoint(6, 1);
			Icon.infoShadowAnchor = new GPoint(13, 13); 
		
			var infoTabs = [
			new GInfoWindowTab("Details","<b>"+ name + "</b><br>" + addr + "<br>" + city + ", " + state + "<br><a href='/golf/" + encodeUrlWithDash(city) + "-golf_" + encodeUrlWithDash(name) + "/" + id + "/'>View Listing</a>")
			];
			var marker = new GMarker(point, Icon);
			GEvent.addListener(marker, "click", function() {
			 marker.openInfoWindowTabsHtml(infoTabs);
			 });
		} else if(type == 'banks'){
			var Icon = new GIcon();
			Icon.image = "/icons/bank.png";
			Icon.shadow = "/icons/shadow.png";
			//Icon.iconSize = new GSize(12, 20);
			//Icon.shadowSize = new GSize(22, 20);
			Icon.iconAnchor = new GPoint(6, 20);
			Icon.infoWindowAnchor = new GPoint(6, 1);
			Icon.infoShadowAnchor = new GPoint(13, 13); 
		
			var infoTabs = [
			new GInfoWindowTab("Details","<b>"+ name + "</b><br>" + addr + "<br>" + city + ", " + state + "<br><a href='/banks/" + encodeUrlWithDash(city) + "-banks_" + encodeUrlWithDash(name) + "/" + id + "/'>View Listing</a>")
			];
			var marker = new GMarker(point, Icon);
			GEvent.addListener(marker, "click", function() {
			 marker.openInfoWindowTabsHtml(infoTabs);
			 });
		}
		
		//var focusPoint = function() {
			//marker.openInfoWindowHtml(name);
			//return false;
		//}
		
        map.addOverlay(marker);
		//GEvent.addListener(marker, 'click', focusPoint);
		//GEvent.addListener(arrHotels[num].marker, 'click', focusPoint);
      }
    }
  );
}*/

function showWaypoint(name,addressShort,city,state) {
	var address = addressShort + ", " + city + ", " + state;
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " not found");
      } else {
		
			var Icon = new GIcon();
			Icon.image = "/new/icons/info.png";
			Icon.shadow = "/new/icons/shadow.png";
			//Icon.iconSize = new GSize(12, 20);
			//Icon.shadowSize = new GSize(22, 20);
			Icon.iconAnchor = new GPoint(6, 20);
			Icon.infoWindowAnchor = new GPoint(6, 1);
			Icon.infoShadowAnchor = new GPoint(13, 13);
			
			var infoTabs = [
			new GInfoWindowTab("Details", name),
			new GInfoWindowTab("Information", address)
			];
			var marker = new GMarker(point, Icon);
			GEvent.addListener(marker, "click", function() {
			 marker.openInfoWindowTabsHtml(infoTabs);
			 });
		
		//var focusPoint = function() {
			//marker.openInfoWindowHtml(name);
			//return false;
		//}
		
        map.addOverlay(marker);
		//GEvent.addListener(marker, 'click', focusPoint);
		//GEvent.addListener(arrHotels[num].marker, 'click', focusPoint);
      }
    }
  );
}

function showDefault(address,zoom,flag) {
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " not found");
      } else {
        map.setCenter(point, zoom);
      }
    }
  );
}

function encodeUrlWithDash(target){
	var clean = target.replace(" ","-");
	var clean = clean.replace("'","");
	
	return clean.toLowerCase();
}

function show(target,address,zoom,flag){
	document.getElementById(target).style.display='block';
	init();
	showDefault(address,zoom,flag);
}

function hide(target){
	document.getElementById(target).style.display='none';
}

function showAddress(address,name,id,type){
	geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        //alert(address + " not found");
      } else {
		var hoodIcon = new google.maps.Icon();
		hoodIcon.image = "/icons/food.png";
		hoodIcon.shadow = "/icons/shadow.png";
		hoodIcon.iconSize = new google.maps.Size(23, 30);
		hoodIcon.iconAnchor = new google.maps.Point(6, 20);
		hoodIcon.infoWindowAnchor = new google.maps.Point(6, 1);
		hoodIcon.infoShadowAnchor = new google.maps.Point(13, 13);
		
		// obtain the attribues of each marker
		var pid = id;
		var link = "/restaurant/"+id+"/"+name.replace(" ","-")+".html";
		var label = name;
		if(i == 1000) {
		  var marker = createLinkMarker(point,link,youarehereIcon,label,pid,2);
		  youareherepid = pid;
		} else {
		  var marker = createLinkMarker(point,link,hoodIcon,label,pid,2);
		}
	  }
	}
	);
}

function plotHotels(names,addresses,id,type){
	for (i=0;i<names.length;i++){
		showAddress(addresses[i],names[i],id[i],type);
	}
}

function plotAttractions(names,addresses,cities,states,zips,id,type){
	for (i=0;i<names.length;i++){
		showAddress(addresses[i],cities[i],states[i],zips[i],names[i],id[i],type[i]);
	}
}

function createLinkMarker(point,link,markerIcon,label,pid,importance) {
    var marker = new google.maps.Marker(point,{zIndexProcess:importanceOrder,icon:markerIcon});
    marker.importance = importance;
    marker.pid = pid;
    marker.tooltip = '<div id="tTip"><nobr>'+label+'</nobr></div>';

    map.addOverlay(marker);

    google.maps.Event.addListener(marker, "click", function() {
			location.href = link;
    });

    //  ======  The new marker "mouseover" and "mouseout" listeners  ======
    google.maps.Event.addListener(marker,"mouseover", function() {
      showTooltip(marker);
    });

   google.maps.Event.addListener(marker,"mouseout", function() {
	tooltip.style.visibility="hidden";
  });

    return marker;
  }
  
  function importanceOrder (marker,b) {
    return google.maps.Overlay.getZIndex(marker.getPoint().lat()) + marker.importance*1000000;
  }
  
  function showTooltip(marker) {
    tooltip.innerHTML = marker.tooltip;
    var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(0,0),true),map.getZoom());
    var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
    var anchor=marker.getIcon().iconAnchor;
    var width=marker.getIcon().iconSize.width;
    var height=tooltip.clientHeight;
    var pos = new google.maps.ControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - 10, offset.y - point.y + 15));
    pos.apply(tooltip);
    tooltip.style.visibility="visible";
  }
  
function test(){
	
	  map = new google.maps.Map2(document.getElementById("bbMap"));

      map.addControl(new google.maps.SmallMapControl());
      var point = new google.maps.LatLng(42.3491257811,-71.1);
      map.setCenter(point, 11);
      var bounds = new google.maps.LatLngBounds();
	  
	  // ====== set up marker mouseover tooltip div ======
      map.getPane(G_MAP_FLOAT_PANE).appendChild(tooltip);
      tooltip.style.visibility="hidden";
	  
	  // Create a base icon for all of our markers that specifies the
      // shadow, icon dimensions, etc.
      /*var hoodIcon = new google.maps.Icon();
      hoodIcon.image = "http://static.csimg.com/img/gmap/pin-neighborhood.png";
      hoodIcon.iconSize = new google.maps.Size(33, 30)
      hoodIcon.iconAnchor = new google.maps.Point(10, 27);
      hoodIcon.infoWindowAnchor = new google.maps.Point(10, 1);
      hoodIcon.imageMap = [10,1,20,10,10,27,1,10];*/
	  
	  

      // Read the data from example.xml
      var request = google.maps.XmlHttp.create();
      request.open("GET", "/data_v2.xml", true);
      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          var xmlDoc = google.maps.Xml.parse(request.responseText);


          // ========== Now process the markers ============
          // obtain the array of markers and loop through it
          var markers = xmlDoc.documentElement.getElementsByTagName("marker");

          for (var i = 0; i < markers.length; i++) {
			  var iconArray = new Array("/icons/red-pushpin.png","/icons/blue-pushpin.png","/icons/ylw-pushpin.png","/icons/purple-pushpin.png","/icons/grn-pushpin.png");
			  var randomNumber=Math.floor(Math.random()*5);
			  
			  var imageLocation = iconArray[randomNumber];
			  
			  var hoodIcon = new google.maps.Icon();
			hoodIcon.image = "/icons/food.png";
			hoodIcon.shadow = "/icons/shadow.png";
			hoodIcon.iconSize = new google.maps.Size(23, 30);
			hoodIcon.iconAnchor = new google.maps.Point(6, 20);
			hoodIcon.infoWindowAnchor = new google.maps.Point(6, 1);
			hoodIcon.infoShadowAnchor = new google.maps.Point(13, 13);
			
            // obtain the attribues of each marker
            var pid = parseFloat(markers[i].getAttribute("pid"));
            var lat = parseFloat(markers[i].getAttribute("lat"));
            var lng = parseFloat(markers[i].getAttribute("lng"));
            var point = new google.maps.LatLng(lat,lng);

            var link = markers[i].firstChild.firstChild.nodeValue;
            var label = markers[i].getAttribute("label");
            if(i == 1000) {
              var marker = createLinkMarker(point,link,youarehereIcon,label,pid,2);
              youareherepid = pid;
            } else {
              var marker = createLinkMarker(point,link,hoodIcon,label,pid,2);
            }

            // ==== Each time a point is found, extent the bounds ato include it =====
            bounds.extend(point);
          }
          // ================================================

        }

      }
      request.send(null);	
}

function showV2(){
	
	  map = new google.maps.Map2(document.getElementById("bbMap"));

      map.addControl(new google.maps.SmallMapControl());
      var point = new google.maps.LatLng(42.3491257811,-71.1);
      map.setCenter(point, 11);
      var bounds = new google.maps.LatLngBounds();
	  
	  // ====== set up marker mouseover tooltip div ======
      map.getPane(G_MAP_FLOAT_PANE).appendChild(tooltip);
      tooltip.style.visibility="hidden";
	  geocoder = new GClientGeocoder();
}