var map;
function initialize() {
  var myLatlng = new google.maps.LatLng(33.702207,-78.872824);
  var myOptions = {
    zoom: 12,
    center: myLatlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    mapTypeControl: true,
    mapTypeControlOptions: google.maps.NavigationControlStyle.DROPDOWN_MENU
  }
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  
  jQuery.ajax({
  	type: "GET",
  	url: "/index.cfm?event=page.resorts&requestFormat=json",
  	dataType: "json",
  	success: function(data) {
  		setMarkers(map, data['DATA']);
  	}
  });
}

function directions() {
	map = new GMap2(document.getElementById('directionsMap'));
	//map = new google.maps.Map(document.getElementById("directionsMap"));
	directionsPanel = document.getElementById('directionsTurns');
	//map.setCenter(new GLatLng());
	directions = new GDirections(map, directionsPanel);
	var start = document.getElementById('start').value;
	var end = document.getElementById('end').value;
	directions.load("from: " + start + " to: " + end);
}

var setMarkers = function(map, markers) {
	//add markers to the map
	for (var i = 0; i < markers.length; i++) {
		createMarker(map, markers[i]);
	}
}

var createMarker = function(map, point) {
	var image = new google.maps.MarkerImage('http://www.intellistrand.com/maps/images/hotel_icon.png', new google.maps.Size(12, 20), new google.maps.Point(0,0), new google.maps.Point(6,20));
	var myLatLng = new google.maps.LatLng(point[12], point[13]);
	var marker = new google.maps.Marker({
		position: myLatLng,
		map: map,
		icon: image,
		title: point[1]
	});
	var infowindow = new google.maps.InfoWindow({
		content: '<p><strong><a href="/' + point[2] + '/">' + point[1] + '</a></strong><br />' + point[7] + '<br />' + point[8] + ', ' + point[9] + ' ' + point[10] + '<br />' + point[11] + '<br /><a href="/' + point[2] + '/">more information</a></p><p><strong>Get Directions</strong> to here from</p><form name="directions" id="directions" method="post" action="/maps/directions/"><input type="hidden" name="end" value="' + point[12] + ',' + point[13] + ' (' + point[1] + ')" /><input type="text" name="start" id="start" value="" />&nbsp;<input type="submit" name="submit" value="go" /></form>',
		size: new google.maps.Size(50,50)
	});
	google.maps.event.addListener(marker, 'click', function() {
		infowindow.open(map, marker);
	});
	if (jQuery('#resortToShow')[0]) {
		if (point[5] == jQuery('#resortToShow').html()) {
			infowindow.open(map, marker);
		}
	}
}

jQuery().ready(function(){
	if (jQuery('#map_canvas')[0])
		initialize();
	if (jQuery('#directionsMap')[0])
		directions();
});