absoluteUrl = "/";
map = "";
icons = new Array();
allpoints = new Array();
active = new Array();

document.getElementsByClassName = function(class_name) {
    var docList = this.all || this.getElementsByTagName('*');
    var matchArray = new Array();
    /*Create a regular expression object for class*/
    var re1 = new RegExp("\\b"+class_name+"\\b");
    /*Create regular expression to handle dashes in class name*/
    var re2 = new RegExp("[\S]"+class_name+"|"+class_name+"[\S]");
    for (var i = 0; i < docList.length; i++) {
        if (re1.test(docList[i].className) && !re2.test(docList[i].className) ) {
            matchArray[matchArray.length] = docList[i];
        }
    }
    return matchArray;
}//eof annonymous function

function loadmap() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("largeMap"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(33.70320652139349, -78.90552520751953), 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(/largeMapNew\.js(\?.*)?$/)) {
			id = scripts[i].src.match(/id=([0-9,]*)/);
			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(',');
		active = active.filter(function(item, index){
			return item != ',';
		});
	}
	else if (id == null && category != null) { /*console.log('category');*/ }
	
	var xmlpath;
	xmlpath = absoluteUrl + "components/mapdata.cfm";
	
	cachePoints(map, xmlpath, icons, active);
	
	var links = document.getElementsByClassName('myLittlePony');
	var linksActive = document.getElementsByClassName('myLittlePony-selected');
	var filter = document.getElementById('filter-submit');
	var directionsLink = document.getElementById('direction-submit');
	for (var i = 0; i < linksActive.length; i++) {
		active.push(linksActive[i].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);
			buildDropDowns(allpoints[obj].containerId, allpoints[obj].idkey);
		});
	}, 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);
		});
		/*console.log(filterElements);*/
		showCategoryPoints(map, categories);
	});
	directionsLink.addEvent('click', function(e) {
		new Event(e).stop();
		var idstring = "http://www.golfholiday.com/travel/directions/?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>";
}
	
function openDirections(dropDown,index){
	var stringToSplit = dropDown[index].value;
	if (stringToSplit != ''){
		var temp = new Array;
		temp = stringToSplit.split('|');
		var w = window.open('/travel/directions.cfm?id='+ temp[0] + ',' + temp[1]);
		//var w = window.open('http://maps.intellistrand.com/directions.cfm?origaddress=' + temp[0] + '&origcity=' + temp[1] + '&origstateprovince=' + temp[2] + '&destaddress=' + temp[3] + '&destcity=' + temp[4] + '&deststateprovince=' + temp[5] + '&callingpage=<cfoutput>#GetFileFromPath(GetBaseTemplatePath())#</cfoutput>','directionsWindow','toolbar=no,scrollbars=yes,location=no,resizable=yes,directories=no,status=no,menubar=no,width=630,height=500,screenX=0,screenY=0,left=0,top=0');
		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 = document.getElementsByClassName('myLittlePony');
	links.each(function(el, index) {
		if (el.getProperty('jscontent') == reference) { 
			if (this.className == "myLittlePony-selected") this.className = "myLittlePony";
			else this.className = "myLittlePony-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); 
				buildDropDowns(this.containerId, this.idkey);
				//buildDropDowns(this.containerId,this.address,this.city,this.state); 
			});
			if (id > 0) { 
				allpoints[i].marker.openInfoWindowHtml(allpoints[i].gdetail); 
				buildDropDowns(allpoints[i].containerId, allpoints[i].idkey);
				//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); 
				buildDropDowns(this.containerId, this.idkey);
				//buildDropDowns(this.containerId,this.address,this.city,this.state); 
			});
			if (id > 0) { 
				allpoints[i].marker.openInfoWindowHtml(allpoints[i].gdetail); 
				buildDropDowns(allpoints[i].containerId, allpoints[i].idkey);
				//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) { }
}