var map, manager;
var centerLatitude = 36.7778, centerLongitude = -76.1291, startZoom = 11;
//36.85464
var MAP_HEIGHT = 600;

var courseData = new Array();


var courseIcon = new GIcon();
courseIcon.image = "/images/course_icon.gif";
courseIcon.iconSize = new GSize(20, 34);
courseIcon.shadow = "/images/shadow-course_icon.png";
courseIcon.iconSize = new GSize(24.0, 34.0);
courseIcon.shadowSize = new GSize(42.0, 34.0);
courseIcon.iconAnchor = new GPoint(12.0, 17.0);
courseIcon.infoWindowAnchor = new GPoint(12.0, 17.0);

var hotelIcon = new GIcon();
hotelIcon.image = "/images/hotel_icon.gif";
hotelIcon.shadow = "/images/shadow-hotel_icon.png";
hotelIcon.iconSize = new GSize(30.0, 23.0);
hotelIcon.shadowSize = new GSize(42.0, 23.0);
hotelIcon.iconAnchor = new GPoint(15.0, 11.0);
hotelIcon.infoWindowAnchor = new GPoint(15.0, 11.0);

var foodIcon = new GIcon();
foodIcon.image = "/images/food_icon.gif";
foodIcon.shadow = "/images/shadow-food_icon.png";
foodIcon.iconSize = new GSize(35.0, 24.0);
foodIcon.shadowSize = new GSize(48.0, 24.0);
foodIcon.iconAnchor = new GPoint(17.0, 12.0);
foodIcon.infoWindowAnchor = new GPoint(17.0, 12.0);



function createMarkerClickHandler(marker, text, link, lat, lon) {
  return function() {
	  map.panTo(new GLatLng(lat, lon));
	  marker.openInfoWindowHtml('<h3>' + text + '</h3>' + '<p><a href="../../scripts/' + link + '">Website</a></p>');
    return false;
  };
}



function createMarker(pointData) {
  	
	var latlng = new GLatLng(pointData.latitude, pointData.longitude);
	var theUrl;
	if(pointData.propertyType == 'course') {
		theUrl = "../showAccount.shtml?type=course&sname=" + pointData.sname;
		  opts = {
			"icon": courseIcon,
			"clickable": true
		  };
	} else if(pointData.propertyType == 'lodging') {
		theUrl = "../showAccount.shtml?type=lodging&sname=" + pointData.sname;
		  opts = {
			"icon": hotelIcon,
			"clickable": true
		  };
	} else if(pointData.propertyType == 'restaurant') {
		  opts = {
			"icon": foodIcon,
			"clickable": true
		  };
	} 
	
	
	  var marker = new LabeledMarker(latlng, opts);
	  //var handler = createMarkerClickHandler(marker, pointData.fname, pointData.sname, pointData.latitude, pointData.longitude);
	  var handler = createMarkerClickHandler(marker, pointData.fname, theUrl, pointData.latitude, pointData.longitude);
		
	  GEvent.addListener(marker, "click", handler);

	if(pointData.propertyType == 'course') {
	  var listItem = document.createElement('li');
	//  listItem.innerHTML = '<div class="label">'+pointData.abbr+'</div><a href="../../scripts/' + pointData.wp + '">' + pointData.name + '</a>';
	  listItem.innerHTML = '<a href="#">' + pointData.fname + '</a>';
	  listItem.getElementsByTagName('a')[0].onclick = handler;
		document.getElementById('sidebar-list').appendChild(listItem);
	} else if(pointData.propertyType == 'lodging') {
		//hotels
	  var listItem4 = document.createElement('li');
	  listItem4.innerHTML = '<a href="#">' + pointData.fname + '</a>';
	  listItem4.getElementsByTagName('a')[0].onclick = handler;  
	  document.getElementById('sidebar-hotels').appendChild(listItem4);
	} else if(pointData.propertyType == 'restaurant') {
		//food
	  var listItem5 = document.createElement('li');
	  listItem5.innerHTML = '<a href="#">' + pointData.fname + '</a>';
	  listItem5.getElementsByTagName('a')[0].onclick = handler;  
	  document.getElementById('sidebar-food').appendChild(listItem5);
	}

  return marker;
}

function windowHeight() {
  // Standard browsers (Mozilla, Safari, etc.)
  if (self.innerHeight) {
    return self.innerHeight;
  }
  // IE 6
  if (document.documentElement && document.documentElement.clientHeight) {
   return document.documentElement.clientHeight;
  }
  // IE 5
  if (document.body) {
    return document.body.clientHeight;
  }
  // Just in case. 
  return 0;
}

function handleResize() {
  var height = windowHeight() - document.getElementById('toolbar').offsetHeight - 30;
  var height2 = MAP_HEIGHT;
  document.getElementById('map').style.height = height2 + 'px';
  document.getElementById('sidebar').style.height = height + 'px';
}

function setupMapPoints(request) {
	
  handleResize();
  
  courseData = request.responseJSON;
  
  map = new GMap2(document.getElementById('map'));
  map.addControl(new GSmallMapControl());
  map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
  manager = new MarkerManager(map);

  var stage = 0;
  
  batch = [];
  /*
  for( i=0; i<5; i++) {
	 for(id in markers[i]) {
		batch.push(createMarker(markers[i][id], i));
	}
  }
  */
  for(j=0; j<courseData.length; j++) {
		batch.push(createMarker(courseData[j]));
	}
	
	
  //batch.push(createMarker(courses[1], 0));
  manager.addMarkers(batch, 11);
  manager.refresh();
  
}

window.onresize = handleResize;
//window.onload = init;
window.onunload = GUnload;
