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

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,city,state,zip,name,id,type) {
	var address = addr + ", " + city + " " + state + " " + zip;
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        //alert(address + " not found");
      } else {
		
		if(type == 'hotel'){
			var Icon = new GIcon();
			Icon.image = "/new/icons/hotel.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 + "<br><a href='/details/" + encodeUrlWithDash(city) + "-hotels_" + encodeUrlWithDash(name) + "/" + id + "/'>View Hotel</a> - <a href='http://travel.ian.com/index.jsp?pageName=hotInfo&cid=139972&hotelID=" + id + "&city=" + city + "&stateProvince=" + state + "&country=US&hotel=1' target='_blank'>Reserve a Room</a>"),
			new GInfoWindowTab("Information", addr + "<br>" + city + ", " + state)
			];
			var marker = new GMarker(point, Icon);
			GEvent.addListener(marker, "click", function() {
			 marker.openInfoWindowTabsHtml(infoTabs);
			 });
		} else if(type == 'restaurant'){
			var Icon = new GIcon();
			Icon.image = "/new/icons/food.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","<b>"+ name + "</b><br>" + addr + "<br>" + city + ", " + state + "<br><a href='/restaurant/" + encodeUrlWithDash(city) + "-restaurants_" + encodeUrlWithDash(name) + "/" + id + "/'>View Restaurant</a>")
			];
			var marker = new GMarker(point, Icon);
			GEvent.addListener(marker, "click", function() {
			 marker.openInfoWindowTabsHtml(infoTabs);
			 });
		} else if(type == 'entertainment'){
			var Icon = new GIcon();
			Icon.image = "/new/icons/entertain.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","<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 = "/new/icons/entertain.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","<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 = "/new/icons/scenic.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","<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 = "/new/icons/building.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","<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 = "/new/icons/golf.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","<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 = "/new/icons/bank.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","<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) {
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        alert(address + " not found");
      } else {
        map.setCenter(point, defaultZoom);
      }
    }
  );
}

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

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

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

function plotHotels(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);
	}
}

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]);
	}
}