var customIcon = new GIcon(); 
customIcon.image = 'http://labs.google.com/ridefinder/images/mm_20_blue.png';
customIcon.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
customIcon.iconSize = new GSize(12, 20);
customIcon.shadowSize = new GSize(22, 20);
customIcon.iconAnchor = new GPoint(6, 20);
customIcon.infoWindowAnchor = new GPoint(5, 1);
var map;
var directionsPanel;
var directions;
var items = [
{id: 1,name: "Daytona Beach Office",rawaddress: "545 Health Boulevard, Daytona Beach, FL 32114",
	address: "545 Health Boulevard<br/>Daytona Beach, FL 32114",lat: "29.208023",lng: "-81.060292"},
{id: 2,name: "DeLand Office",rawaddress: "685 Peachwood Drive, DeLand, FL 32720",
	address: "685 Peachwood Drive<br/>DeLand, FL 32720",lat: "29.045027",lng: "-81.316155"},
{id: 3,name: "New Smyrna Beach",rawaddress: "506 Palmetto Street, New Smyrna Beach, FL 32168",
	address: "506 Palmetto Street<br/>New Smyrna Beach, FL 32168",lat: "29.020643",lng: "-80.9227"},

{id: 4,name: "Orange City",rawaddress: "1061 Medical Center Drive, Orange City, FL 32763",
	address: "1061 Medical Center Drive, Suite 300<br/>Orange City, FL 32763",lat: "28.914419",lng: "-81.285095"},
{id: 5,name: "Palm Coast",rawaddress: "21 Hospital Drive, Suite 140, Palm Coast, Fl 32164",
	address: "21 Hospital Drive, Suite 140<br/>Palm Coast, Fl 32164", lat: "29.47045",lng: "-81.25333"},
{id: 6,name: "Port Orange",rawaddress: "633 Dunlawton Ave, Port Orange, FL 32127",
	address: "633 Dunlawton Ave, Suite 2<br/>Port Orange, FL 32127",lat: "29.140196",lng: "-80.995676"},

{id: 7,name: "Southeast Regional Prostate<br/>Cancer Treatment Center",rawaddress: "1620 Mason Avenue, Daytona Beach, Fl 32114",
	address: "1620 Mason Avenue, Suite E<br/>Daytona Beach, Fl 32114", lat: "29.211295",lng: "-81.067504"},
{id: 8,name: "St Augustine",rawaddress: "2460 Old Moultrie Rd, St. Augustine, FL 32086",
	address: "2460 Old Moultrie Rd, Suite 5<br/>St. Augustine, FL 32086",lat: "29.851915",lng: "-81.326927"}
];
function load() {
	if (GBrowserIsCompatible()) {
map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(29.25, -81.1), 10);
		directionsPanel = document.getElementById("route");
		directions = new GDirections(map, directionsPanel);

		for (var i = 0; i < items.length; i++) {
			var name = items[i].name;
			var address = items[i].address;
			items[i].i = i;
			items[i].point = new GLatLng(parseFloat(items[i].lat),parseFloat(items[i].lng));
			items[i].marker = createMarker(items[i]);
			map.addOverlay(items[i].marker);
			if( typeof location_visible_id != "undefined") if (items[i].id == location_visible_id) {
				openMarker(items[i].id);
			}
		}
	}
}
String.prototype.ucwords = function(){ //v1.0
    return this.replace(/\w+/g, function(a){
        return a.charAt(0).toUpperCase() + a.substr(1).toLowerCase();
    });
};




function createMarker(item) {
	var marker = new GMarker(item.point, customIcon);
	GEvent.addListener(marker, 'click', function() { openMarker(item.id); });
	return marker;
}
function openMarker(id) {
	new Ajax.Request("locations_details.php?id="+id, {
		onSuccess: function (tr){
			resp = tr.headerJSON;
			var htmltoadd = "<br><table  cellpadding='0' cellspacing='0' width='100%'><tr><Td class='location_details_header'><table cellpadding='10' cellspacing='0' width='100%'><tr><td  valign='top'><b>"+resp.name+"<br/><hr width=95%><br/>";
			if (typeof resp.files != 'undefined') {
				if (typeof resp.files.length != 'undefined') {
					var opendir = "";
					htmltoadd += "<b>Forms, Files, and Information</b><blockquote style='margin-top:0;'>";
					for (var i=0;resp.files.length>i;i++) {
						filename = resp.files[i];
						if (-1 != (filehasdir = filename.indexOf('/'))) {
							if (opendir != (newopendir = filename.substring(0, filehasdir))) {
								if (opendir != "") htmltoadd += "</blockquote>";
								opendir = newopendir;
								htmltoadd += "<b>"+opendir.capitalize()+"</b><blockquote style='margin-top:0;'>";
							}
						}
						htmltoadd += "<a href='"+resp.folder+filename+"' target='_blank'>"+ filename.substring(0, filename.lastIndexOf('.')).substring(filename.indexOf('/')+1).ucwords()+"</a><br/>";
					}
					if (opendir != "") htmltoadd += "</blockquote>";
					htmltoadd += "</blockquote>";
				}
			}
			htmltoadd += "</td><td style='width:"+(resp.imgwidth+10)+"px;' valign='top'><div style='padding-top:5px;'><img src='"+resp.image+"' style='border: 1px solid #000000;height:"+resp.imgheight+"px;width:"+resp.imgwidth+"px;' align='center' /></div></td></tr></table></td></tr></table>";
			document.getElementById('details').innerHTML = htmltoadd;
		}
	});
	for (var i = 0; i < items.length; i++) {
		if (items[i].id == id) {
			//<img align='left' src='images/locations/testing.gif' style='width:60px;height:60px;padding-right:2px;' onclick='Element.absolutize(this);this.style.width = \"250px\";this.style.height = \"auto\";this.style.zIndex=\"9999\"' onmouseout='Element.relativize(this);this.style.width = \"60px\";this.style.height = \"60px\"' />
			var html = "<div style='width:250px;'><b>" + items[i].name + "</b> <br/>" + items[i].address + "<br/><br/><span id='direct_"+ i +"'><br/><br/><a href='#moreinfo'>More Info</a> | <a href='javascript:void(0);' onclick='toInput(this.parentNode,"+ i +");return false;'>Get Directions</span><br/></div>";
			items[i].marker.openInfoWindowHtml(html);
			break;
		}
	}
}
function toInput(e,id) {
	var submitid = id+"sub";
	e.innerHTML = "<form method='get' onsubmit='getDirections("+id+",$(\""+id+"sub\"));return false;' style='display:inline;'><span id='dir_shown'><br/>Enter your address accurately:</span><br/><input name='text' value='' id='dsearch_"+id+"' /> <input type='submit' id='"+id+"sub' value='Go!' onclick='$(\"dir_shown\").innerHTML = \"Scroll down for turn-by-turn directions.\"' /></form>";
}
function getDirections(id,button) {
  button.style.disabled = true;
  directions.load("from: "+document.getElementById('dsearch_'+id).value+" to: "+items[id].rawaddress);
}
window.onload = function() { load(); };