map = "";
icons = new Array();
allpoints = new Array();
active = new Array();

loadmap = function() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("largeMap"));
    	map.addControl(new GSmallMapControl());
	    map.addControl(new GMapTypeControl());
	    map.setCenter(new GLatLng(37.270697, -76.707410), 10);

		//var icons = new Array();
		//Hotel Icon = category 1
		icons[0] = new GIcon();
		icons[0].image = "/media/map-icons/hotel_icon.png";
		icons[0].iconSize = new GSize(25, 20);
		icons[0].shadowSize = new GSize(1, 1);
		icons[0].iconAnchor = new GPoint(15, 15);
		icons[0].infoWindowAnchor = new GPoint(5, 1);
		//Course Icon = category 2
		icons[1] = new GIcon();
		icons[1].image = "/media/map-icons/golf_icon.png";
		icons[1].iconSize = new GSize(25, 20);
		icons[1].shadowSize = new GSize(1, 1);
		icons[1].iconAnchor = new GPoint(6, 31);
		icons[1].infoWindowAnchor = new GPoint(5, 1);
		//School Icon = category 3
		icons[2] = new GIcon();
		icons[2].image = "/media/map-icons/school_icon.png";
		icons[2].iconSize = new GSize(25, 25);
		icons[2].shadowSize = new GSize(1, 1);
		icons[2].iconAnchor = new GPoint(6, 20);
		icons[2].infoWindowAnchor = new GPoint(5, 1);
		//Food Icon = category 4
		icons[3] = new GIcon();
		icons[3].image = "/media/map-icons/food_icon.png";
		icons[3].iconSize = new GSize(25, 25);
		icons[3].shadowSize = new GSize(1, 1);
		icons[3].iconAnchor = new GPoint(6, 20);
		icons[3].infoWindowAnchor = new GPoint(5, 1);
		//Car Rentals = category 5
		icons[4] = new GIcon();
		icons[4].image = "/media/map-icons/car_rental.png";
		icons[4].iconSize = new GSize(20, 20);
		icons[4].shadowSize = new GSize(1, 1);
		icons[4].iconAnchor = new GPoint(6, 20);
		icons[4].infoWindowAnchor = new GPoint(5, 1);
		//Real Estate = category 4
		icons[5] = new GIcon();
		icons[5].image = "/media/map-icons/real_estate.png";
		icons[5].iconSize = new GSize(20, 20);
		icons[5].shadowSize = new GSize(1, 1);
		icons[5].iconAnchor = new GPoint(6, 20);
		icons[5].infoWindowAnchor = new GPoint(5, 1);
	
		//grab an id if it exists to only show one property
		var scripts = document.getElementsByTagName('script');
		for (i=0; i<scripts.length; i++) {
			if (scripts[i].src.match(/largeMap\.js(\?.*)?$/)) {
				id = scripts[i].src.match(/id=([0-9,|0-9.0]*)/);
				category = scripts[i].src.match(/category=([0-9,]*)/);
				id = (id != null ? id[1] : null);
				category = (category != null ? category[1] : null);
			}
		}
	
		if (id != null && category == null) {
			active = id.split(',').map(function(item, index){
				return item.toInt();
			});
		}
		else if (id == null && category != null) { /*console.log('category');*/ }
		
		var xmlpath = "/components/mapdata.cfm";
	
		cachePoints(map, xmlpath, icons, active); //Points should now be cached for easy access
	
		var filter = $('filter-submit');
		var directionsLink = $('direction-submit');
		var links = $$('.mapListing a');
		var linksActive = links.filter(function(el) { return el.hasClass('selected'); });
		linksActive.each(function(el) {
			active.push(el.getProperty('id').replace('id',''));
		});

		links.each(function(el, index) {
			el.setProperty('jscontent', index);
			el.addEvent('click', function(e) {
				new Event(e).stop();
				var obj = this.getProperty('jscontent');
				allpoints[obj].marker.openInfoWindowHtml(allpoints[obj].gdetail);
				if (allpoints[obj].category == "Accommodations") { buildDropDowns(allpoints[obj].containerId, allpoints[obj].idkey, 2); }
				else { buildDropDowns(allpoints[obj].containerId, allpoints[obj].idkey, 1); }
			});
		}, this);
		filter.addEvent('click', function(e) {
			new Event(e).stop();
			var categories = new Array();
			filterElements = filter.getParent().getElements('input[type!=submit]');
			filterElements.each(function(el,index) {
				if (el.checked == true) categories.push(index);
			});
			showCategoryPoints(map, categories);
		});
		directionsLink.addEvent('click', function(e) {
			new Event(e).stop();
			var idstring = "/directions/driving/?id=";
			objs = directionsLink.getParent().getElements('select');
			objs.each(function(el,index) {
				idstring = idstring + el.options[el.selectedIndex].value + ",";
			});
			idstring = idstring.substring(0,[idstring.length-1]);
			var w = window.open(idstring);
		});
	}
}

function clickedhtml(imgsrc, name, address, id, description, propertyURL, siteURL) {
	if (propertyURL == "") { propertyURL = siteURL; }
	return "<div class='infoWindow'><h3><a href='" + siteURL + "'>" + name + "</a></h3><p>" + address + "</p><p><a href='" + propertyURL + "' rel='external'>Visit Site</a></p><p>" + description + "</p><p><br /><b>Get Directions From The Above Address To:</b><br /><div id='dropDownContainer" + id + "'></div></p></div>";
}
	
openDirections = function(dropDown,index) {
	var stringToSplit = dropDown[index].value;
	if (stringToSplit != ''){
		temp = stringToSplit.split(';');
		var w = window.open('/directions/driving/?id='+ temp[0] + ',' + temp[1]);
		w.focus(); 
	}
}

function cachePoints(map, xmlpath, icons) {
	GDownloadUrl(xmlpath, function(data, responseCode) {
		var xml = GXml.parse(data);
	  	var markers = xml.documentElement.getElementsByTagName("marker");
	  	for (var i = 0; i < markers.length; i++) {
	    	allpoints[i] = new Object();
			allpoints[i].idkey = markers[i].getAttribute("idkey");
			allpoints[i].category = markers[i].getAttribute("category");
	    	allpoints[i].point = new GLatLng(parseFloat(markers[i].getAttribute("lat")),parseFloat(markers[i].getAttribute("lng")));
			allpoints[i].gdetail = clickedhtml(markers[i].getAttribute("src"), markers[i].getAttribute("name"), markers[i].getAttribute("address"), markers[i].getAttribute("idkey"), markers[i].getAttribute("description"), markers[i].getAttribute("propertyURL"), markers[i].getAttribute("siteURL"));
			allpoints[i].containerId = 'dropDownContainer' + markers[i].getAttribute("idkey");
			allpoints[i].address = markers[i].getAttribute("address");
			allpoints[i].city = markers[i].getAttribute("city");
			allpoints[i].state = markers[i].getAttribute("state");
			allpoints[i].marker = new GMarker(allpoints[i].point, icons[markers[i].getAttribute("category")-1]);
	  	}
		addArrayPoints(map, active);
	});
}

function listModify(reference) {
	var links = $$('.mapListing a');
	links.each(function(el, index) {
		if (el.getProperty('jscontent') == reference) { this.toggleClass('selected'); }
	});
}


function addArrayPoints(map, array) {
	array.sort(function(a,b){return a - b}); //This improves the speed of adding the elements in the while loops by causing earlier breaks.
	map.clearOverlays();
  	for (var i = 0; i < allpoints.length; i++) {
		var usethispoint = false;
		for (var j = 0; j < array.length; j++) { if (allpoints[i].idkey == array[j]) { usethispoint = true; break; } }
		if (usethispoint) {
    		map.addOverlay(allpoints[i].marker);
			GEvent.bind(allpoints[i].marker, "click", allpoints[i], function(){ 
				this.marker.openInfoWindowHtml(this.gdetail); 
				if (allpoints[i].category == "Accommodations") { buildDropDowns(allpoints[i].containerId, allpoints[i].idkey, 2); }
				else { buildDropDowns(allpoints[i].containerId, allpoints[i].idkey, 1); }
				//buildDropDowns(this.containerId,this.address,this.city,this.state); 
			});
			if (id > 0) { 
				allpoints[i].marker.openInfoWindowHtml(allpoints[i].gdetail); 
				if (allpoints[i].category == "Accommodations") { buildDropDowns(allpoints[i].containerId, allpoints[i].idkey, 2); }
				else { buildDropDowns(allpoints[i].containerId, allpoints[i].idkey, 1); }
				//buildDropDowns(allpoints[i].containerId, allpoints[i].address, allpoints[i].city, allpoints[i].state); 
			}
		}
  	}
}

function showCategoryPoints(map, category) {
	map.clearOverlays();
	for (var i = 0; i < allpoints.length; i++) {
		var usethispoint = false;
		for (var j = 0; j < category.length; j++) { if (allpoints[i].category == (category[j]+1)) { usethispoint = true; break; } }
		if (usethispoint) {
    		map.addOverlay(allpoints[i].marker);
			GEvent.bind(allpoints[i].marker, "click", allpoints[i], function(){ 
				this.marker.openInfoWindowHtml(this.gdetail); 
				if (this.category == "Accommodations") { buildDropDowns(this.containerId, this.idkey, 2); }
				else { buildDropDowns(this.containerId, this.idkey, 1); }
				//buildDropDowns(this.containerId,this.address,this.city,this.state); 
			});
			if (id > 0) { 
				allpoints[i].marker.openInfoWindowHtml(allpoints[i].gdetail);
				if (allpoints[i].category == "Accommodations") { buildDropDowns(allpoints[i].containerId, allpoints[i].idkey, 2); }
				else { buildDropDowns(allpoints[i].containerId, allpoints[i].idkey, 1); } 
				//buildDropDowns(allpoints[i].containerId, allpoints[i].address, allpoints[i].city, allpoints[i].state); 
			}
		}
  	}
}
	
window.onload = function() {
	try { loadmap(); }
	catch(err) { console.log(err); }
}

window.onunload = function() {
	try { GUnload(); }
	catch(err) { }
}