var centerLatitude = 34.187578;
var centerLongitude = -82.159499;
var startZoom = 10;

var map;

function init() {
	map = new GMap2(document.getElementById("map"));
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);

}

//window.onload = init;

var iconBlue = new GIcon(); 
    iconBlue.image = 'http://greenwoodpartnershipalliance.com/site/wp-content/themes/golden_essence_261/images/gmaps/buildings.png';
    iconBlue.shadow = 'http://greenwoodpartnershipalliance.com/site/wp-content/themes/golden_essence_261/images/gmaps/mm_20_shadow.png';
    iconBlue.iconSize = new GSize(16, 23);
    iconBlue.shadowSize = new GSize(22, 20);
    iconBlue.iconAnchor = new GPoint(6, 20);
    iconBlue.infoWindowAnchor = new GPoint(5, 1);

    var iconRed = new GIcon(); 
    iconRed.image = 'http://greenwoodpartnershipalliance.com/site/wp-content/themes/golden_essence_261/images/gmaps/sites.png';
    iconRed.shadow = 'http://greenwoodpartnershipalliance.com/site/wp-content/themes/golden_essence_261/images/gmaps/mm_20_shadow.png';
    iconRed.iconSize = new GSize(16, 23);
    iconRed.shadowSize = new GSize(22, 20);
    iconRed.iconAnchor = new GPoint(6, 20);
    iconRed.infoWindowAnchor = new GPoint(5, 1);
	
	var iconPlane = new GIcon(); 
    iconPlane.image = 'http://greenwoodpartnershipalliance.com/site/wp-content/themes/golden_essence_261/images/gmaps/plane.png';
    iconPlane.shadow = 'http://greenwoodpartnershipalliance.com/site/wp-content/themes/golden_essence_261/images/gmaps/mm_20_shadow.png';
    iconPlane.iconSize = new GSize(16, 23);
    iconPlane.shadowSize = new GSize(22, 20);
    iconPlane.iconAnchor = new GPoint(6, 20);
    iconPlane.infoWindowAnchor = new GPoint(5, 1);
	
	var iconKschool = new GIcon(); 
    iconKschool.image = 'http://greenwoodpartnershipalliance.com/site/wp-content/themes/golden_essence_261/images/gmaps/kschool.png';
    iconKschool.shadow = 'http://greenwoodpartnershipalliance.com/site/wp-content/themes/golden_essence_261/images/gmaps/mm_20_shadow.png';
    iconKschool.iconSize = new GSize(16, 23);
    iconKschool.shadowSize = new GSize(22, 20);
    iconKschool.iconAnchor = new GPoint(6, 20);
    iconKschool.infoWindowAnchor = new GPoint(5, 1);
	
	var iconHospitals = new GIcon(); 
    iconHospitals.image = 'http://greenwoodpartnershipalliance.com/site/wp-content/themes/golden_essence_261/images/gmaps/hospitals.png';
    iconHospitals.shadow = 'http://greenwoodpartnershipalliance.com/site/wp-content/themes/golden_essence_261/images/gmaps/mm_20_shadow.png';
    iconHospitals.iconSize = new GSize(16, 23);
    iconHospitals.shadowSize = new GSize(22, 20);
    iconHospitals.iconAnchor = new GPoint(6, 20);
    iconHospitals.infoWindowAnchor = new GPoint(5, 1);

    var customIcons = [];
    customIcons["industrial_buildings"] = iconBlue;
    customIcons["industrial_sites"] = iconRed;
	customIcons["airports"] = iconPlane;
	customIcons["kschools"] = iconKschool;
	customIcons["hospitals"] = iconHospitals;

//function storeMarker(){
//	var lng = document.getElementById("longitude").value;
//	var lat = document.getElementById("latitude").value;
//
//	var getVars =  "?found=" + document.getElementById("found").value
//	+ "&left=" + document.getElementById("left").value
//	+ "&lng=" + lng
//	+ "&lat=" + lat ;
//
//	var request = GXmlHttp.create();
//
//	//open the request to storeMakres.php on your server
//	//request.open('GET', 'storeMarker.php' + getVars, true);
//	request.onreadystatechange = function() {
//		if (request.readyState == 4) {
//			//the request is complete
//
//			var xmlDoc = request.responseXML;
//
//			//retrieve the root document element (response)
//			var responseNode = xmlDoc.documentElement;
//
//			//retrieve the type attribute of the node
//			var type = responseNode.getAttribute("type");
//
//			//retrieve the content of the responseNode
//			var content = responseNode.firstChild.nodeValue;
//
//			//check to see if it was an error or success
//			if(type!='success') {
//				alert(content);
//			} else {
//				//Create a new marker and add it's info window.
//				var latlng = new GLatLng(parseFloat(lat),parseFloat(lng));
//
//				var marker = createMarker(latlng, content);
//
//				map.addOverlay(marker);
//				map.closeInfoWindow();
//			}
//		}
//	}
//	request.send(null);
//	return false;
//}

function createMarker(latlng, html, type) {
	var marker = new GMarker(latlng, customIcons[type]);
	GEvent.addListener(marker, 'click', function() {
		var markerHTML = html;
		marker.openInfoWindowHtml(markerHTML);
	});
	return marker;
}

function retrieveMarkers(str) {
	
	//alert(str);
	
	// clear what was already there
	map.clearOverlays();
	
	// var from form
	
	//var prop_type = document.getElementById("prop_type").value;
	//var buildings = document.getElementById("buildings_type").value;
	//var sites = document.getElementById("sites_type").value;
	var railroad = "n";
	var airports = "n";
	var kschools = "n";
	var hospitals = "n";
	
	//alert('buildings= ' + buildings);
	//alert('sites= ' + sites);
	
	if (str == "industrial_buildings") {
			var prop_type = "industrial_buildings";
		} else {
			var prop_type = "industrial_sites";
			}
	
	// check to see if the checkboxes are checked
	//if (document.queryform.railroad.checked) {
			//var railroad = "y";
		//}
	
	//if (document.queryform.airports.checked) {
			//var airports = "y";
		//}
		
	//if (document.queryform.kschools.checked) {
			//var kschools = "y";
		//}
		
	//if (document.queryform.hospitals.checked) {
			//var hospitals = "y";
		//}
	
	//var prop_area = document.getElementById("prop_area").value;
	//alert(prop_type);
	//alert("check point 1");
	// attach this to our php page to filter results
	var set_query = "?prop_type=" + prop_type;
	//
	//alert(set_query);
	//alert("check point 2");
	
	var request = GXmlHttp.create();

	//tell the request where to retrieve data from.
	request.open('GET', 'http://greenwoodpartnershipalliance.com/site/wp-content/themes/golden_essence_261/js/phpsqlajax_genxml.php' + set_query, true);

	//tell the request what to do when the state changes.
	request.onreadystatechange = function() {
		if (request.readyState == 4) {
			
			var xmlDoc = request.responseXML;
			
			var locations_number = document.getElementById('locations_number');
			
			var locations_div = document.getElementById('locations_div');
			locations_div.innerHTML = '';
			
			var markers = xmlDoc.documentElement.getElementsByTagName("marker");
			for (var i = 0; i < markers.length; i++) {
				//var mls_num = markers[i].getAttribute("mls_num");
				var price = markers[i].getAttribute("price");
				var site_name = markers[i].getAttribute("name");
				var lat = markers[i].getAttribute("lat");
				var lng = markers[i].getAttribute("lng");
				var address = markers[i].getAttribute("address");
				
				// Building Size, Ceiling Height, etc.
				var feature1 = markers[i].getAttribute("feature1");
				var feature2 = markers[i].getAttribute("feature2");
				
				var certified = markers[i].getAttribute("certified");
				if (certified == "1") {
					var certified = "http://greenwoodpartnershipalliance.com/site/wp-content/themes/golden_essence_261/images/CertifiedSite.gif";
					} else {
						var certified = "";
						}
				//var sqft = markers[i].getAttribute("sqft");
				var type = markers[i].getAttribute("type");
				
				locations_number.innerHTML = '<h2>Search Results: ' + markers.length + '</h2>';
				// list the locations that have been found.
				
				
				
				//locations_div.innerHTML += '<p style="border-bottom:1px dashed #000000;"><b>'+ site_name + '</b><br/>' + address + '</p>';
				//locations_div.innerHTML +='<table width="400" border="1" cellspacing="0" cellpadding="5"'
				if (i % 2) {
					locations_div.innerHTML +='<table width="400" border="0" cellspacing="0" cellpadding="5" bgcolor="#ECEBD5">'
											+'<tr>'
											+ '<td width="100" rowspan="2" align="left" valign="top"><img src="http://greenwoodpartnershipalliance.com/site/wp-content/uploads/primary '+site_name+'.jpg" width="100" /></td>'
											+ '<td align="left" valign="top" style="border-bottom:1px #0F4B7D solid"><span style="float:left"><a href="http://greenwoodpartnershipalliance.com/site/index.php/properties/property-details/?prop_type=' + type + '&prop_name=' + site_name + '"><strong style="color:#0F4B7D">'+ site_name + '</strong></a></span><span style="float:right"><a href="http://greenwoodpartnershipalliance.com/site/wp-content/uploads/' + site_name + '.pdf" target="_blank"><img border="0" width="18" height="18" style="padding-left: 10px" src="http://greenwoodpartnershipalliance.com/site/wp-content/themes/golden_essence_261/images/pdf_document.gif" /></a></span></td>'
										  + '</tr>'
										  + '<tr>'
											+ '<td align="left" valign="top" height="100px" style="background:url('+ certified +') no-repeat right top;">'+ feature1 +'<br />'+ feature2 +'</td>'
										  + '</tr>'
										+ '</table>';
					} else {
					locations_div.innerHTML +='<table width="400" border="0" cellspacing="0" cellpadding="5" bgcolor="#E1E0BE">'
												+'<tr>'
											+ '<td width="100" rowspan="2" align="left" valign="top"><img src="http://greenwoodpartnershipalliance.com/site/wp-content/uploads/primary '+site_name+'.jpg" width="100" /></td>'
											+ '<td align="left" valign="top" style="border-bottom:1px #0F4B7D solid"><span style="float:left"><a href="http://greenwoodpartnershipalliance.com/site/index.php/properties/property-details/?prop_type=' + type + '&prop_name=' + site_name + '"><strong style="color:#0F4B7D">'+ site_name + '</strong></a></span><span style="float:right"><a href="http://greenwoodpartnershipalliance.com/site/wp-content/uploads/' + site_name + '.pdf" target="_blank"><img border="0" width="18" height="18" style="padding-left: 10px" src="http://greenwoodpartnershipalliance.com/site/wp-content/themes/golden_essence_261/images/pdf_document.gif" /></a></span></td>'
										  + '</tr>'
										  + '<tr>'
											+ '<td align="left" valign="top" height="100px" style="background:url('+ certified +') no-repeat right top;">'+ feature1 +'<br />'+ feature2 +'</td>'
										  + '</tr>'
										+ '</table>';
						}
				
				// var type = markers[i].getAttribute("type");
				// check for lng and lat so MSIE does not error
				// on parseFloat of a null value
				if(lng && lat) {
					var latlng = new GLatLng(parseFloat(lat),parseFloat(lng));
					
					// the basic info for all markers: name and address
					var html = '<span class="iwstyle"><strong>' + markers[i].getAttribute("name") + '</strong><br />'
					+ '<strong>Address: </strong>' + markers[i].getAttribute("address") + '</span>';
					
					// if this is a building or site we'll add some extra info
					if (type == "industrial_sites" || type == "industrial_buildings") {
					html += '<span class="iwstyle"><br /><strong>Price: </strong>$' + markers[i].getAttribute("price")
					//+ '<br /><strong>Size: </strong> ' + markers[i].getAttribute("sqft")
					+ '<br /><a href="http://greenwoodpartnershipalliance.com/site/index.php/properties/property-details/?prop_type=' + type + '&prop_name=' + site_name + '">More Info</a></span>';
					}
					
					var marker = createMarker(latlng, html, type);
					map.addOverlay(marker);
				}
			} //for
		} //if
	} //function

	request.send(null);
}
