// JavaScript Document
var map;
var theMarker, theLocation;
var directionsPanel;
var directions;

var erin = new Location("Trinity Hospital Cardiology, 353 E Main St, , Erin, TN, 37061, 615-342-7790",36.3202,-87.6897,13,new Array("J. Gregory Sikes, M.D."));
var ashlandCity = new Location("Centennial Medical Center, 313 N Main St, , Ashland City, TN, 37015, 615-792-0318",36.2774,-87.0652,13,new Array("Byron Haitas, M.D."));
var nashville = new Location("Frist Cardiology, 2400 Patterson St., Suite 304, Nashville, TN, 37203, 615-342-7790",36.1531,-86.8091,12,new Array("Byron Haitas, M.D.","Thomas J. Killian, M.D.","J. Gregory Sikes, M.D.","Jeffrey C. Webber, M.D."));
var smithville = new Location("De Kalb Hospital, 520 W Main St, , Smithville, TN, 37166, 615-597-4395",35.9611,-85.8294,13,new Array(""));
var woodbury = new Location("Stones River Specialty Clinic, 370 Doolittle Rd, , Woodbury, TN, 37190, 615-563-5675",35.8325,-86.0722,13,new Array("Jeffrey C. Webber, M.D."));
var columbia = new Location("Core Physicians, 1114 West 7th St, , Columbia, TN, 38401, 931-388-9706",35.6183,-87.0618,13,new Array("Thomas J. Killian, M.D."));
var manchester = new Location("Medical Center of Manchester, 481 Interstate Dr, , Manchester, TN, 37355, 931-728-3553",35.4972,-86.0784,13,new Array("Byron Haitas, M.D.","Thomas J. Killian, M.D.","Jeffrey C. Webber, M.D."));
var shelbyville = new Location("Heritage Medical Plaza, 2839 Highway 231 N, , Shelbyville, TN, 37160, 615-342-7790",35.56493,-86.445968,13,new Array("J. Gregory Sikes, M.D."));
var southernHills = new Location("Southern Hills Medical Center, 397 Wallace Road, Suite C-216, Nashville, TN, 37211, 615-832-8731",36.077174,-86.725092,12,new Array("Thomas J. Killian, M.D."));
var springHill = new Location("Campbell Station Cardiology, 4847 Main St, , Spring Hill, TN, 37174, 615-599-9908",35.7790503,-86.9145504,12,new Array("Thomas J. Killian, M.D."));

var erinMarker, ashlandCityMarker, nashvilleMarker, smithvilleMarker, woodburyMarker, columbiaMarker, manchesterMarker, shelbyvilleMarker, southernHillsMarker, springHillMarker;

function Location(location_string, lat, lng, zoomLevel, physicians) {
	this.point = new GLatLng(lat,lng);
	var scaleFactor = .0036 * (13 - zoomLevel) + .0031;
	this.zoomCenter = new GLatLng(lat+scaleFactor,lng);
	var temp = new Array();
	location_split = location_string.split(', ');
	this.buildingName = location_split[0];
	this.address1 = location_split[1];
	this.address2 = location_split[2];
	this.city = location_split[3];
	this.state = location_split[4];
	this.zipcode = location_split[5];
	this.phoneNumber = location_split[6];
	this.queryString = this.address1 + ', ' + this.city + ', ' + this.state + ' ' + this.zipcode;
	this.zoomLevel = zoomLevel;
	this.physicians = physicians;
}

function mapReset() {
	directions.clear();
	map.clearOverlays();
}

function defaultView() {
	mapReset();
	map.setCenter(nashville.point,8);
	map.disableDragging();
	
	erinMarker = new GMarker(erin.point);
	ashlandCityMarker = new GMarker(ashlandCity.point);		
	nashvilleMarker = new GMarker(nashville.point);
	smithvilleMarker = new GMarker(smithville.point);
	woodburyMarker = new GMarker(woodbury.point);
	columbiaMarker = new GMarker(columbia.point);
	manchesterMarker = new GMarker(manchester.point);
	shelbyvilleMarker = new GMarker(shelbyville.point);
	southernHillsMarker = new GMarker(southernHills.point);
	springHillMarker = new GMarker(springHill.point);
	
	map.addOverlay(erinMarker);
	map.addOverlay(ashlandCityMarker);
	map.addOverlay(nashvilleMarker);
	map.addOverlay(smithvilleMarker);
	map.addOverlay(woodburyMarker);
	map.addOverlay(columbiaMarker);
	map.addOverlay(manchesterMarker);
	map.addOverlay(shelbyvilleMarker);
	map.addOverlay(southernHillsMarker);
	map.addOverlay(springHillMarker);
	
	GEvent.addListener(erinMarker, "click", function() { zoomView(erin);});
	GEvent.addListener(ashlandCityMarker, "click", function() { zoomView(ashlandCity); });
	GEvent.addListener(nashvilleMarker, "click", function() { zoomView(nashville); });
	GEvent.addListener(smithvilleMarker, "click", function() { zoomView(smithville ); });
	GEvent.addListener(woodburyMarker, "click", function() { zoomView(woodbury); });
	GEvent.addListener(columbiaMarker, "click", function() { zoomView(columbia); });
	GEvent.addListener(manchesterMarker, "click", function() { zoomView(manchester); });
	GEvent.addListener(shelbyvilleMarker, "click", function() { zoomView(shelbyville); });
	GEvent.addListener(southernHillsMarker, "click", function() { zoomView(southernHills); });
	GEvent.addListener(springHillMarker, "click", function() { zoomView(springHill); });
}

function zoomView(thisLocation) { 
	mapReset();
	thisMarker = new GMarker(thisLocation.point);
	map.addOverlay(thisMarker);
	theLocation = thisLocation;
	theMarker = thisMarker;
	map.closeInfoWindow();
	map.setCenter(thisLocation.zoomCenter,thisLocation.zoomLevel);
	map.disableDragging();
	var htmlDirections = '<strong>' + thisLocation.buildingName + '</strong><br />' + thisLocation.address1;
	if(thisLocation.address2) { htmlDirections += '<br />' + thisLocation.address2; }
	htmlDirections += '<br />' + thisLocation.city + ', ' + thisLocation.state + ' ' + thisLocation.zipcode;
	htmlDirections += '<br /><br /><em>To make appointments call:</em><br />' + thisLocation.phoneNumber;
//	htmlDirections += '<br /><br /><div id="query_box"><a style="{font-size:20px;}" id="get_directions" href="javascript:startDirections()">Get directions</a><br /><br /></div>'
	var htmlPhysicians = "";
	for(var i = 0; i < thisLocation.physicians.length; i++) {
		htmlPhysicians += ('<a href="/physicians/' + lastName(thisLocation.physicians[i]) + '" class="physician_link">' + thisLocation.physicians[i] + '</a><br />');
	}
	var labelPhysicians = "Physician";
	if (thisLocation.physicians.length > 1)
		labelPhysicians += "s";
	tabs = new Array(2);
	tabs[0] = new GInfoWindowTab(thisLocation.city, htmlDirections);
	tabs[1] = new GInfoWindowTab(labelPhysicians, htmlPhysicians);
	thisMarker.openInfoWindowTabsHtml(tabs);
	map.disableDragging();
	GEvent.addListener(thisMarker, "click", function() { thisMarker.openInfoWindowTabsHtml(tabs);});
}
	
function startDirections() {
	document.getElementById("query_box").innerHTML = '<form>start address: <br /><input id="start" name="start" type="text" /><input id="end" name="end" value="' +theLocation.queryString + '" type="hidden" /><input type="submit" value="Go" /></form>';
	document.getElementById("start").focus();
}

function dynamicDirections() {
	var query = document.getElementById("start").value + ' to ' + document.getElementById("end").value;
	map.clearOverlays();
	//	directionsPanel.innerHTML = "";
	directions.load(query);
}

function lastName(md_name_string) {
	var split_str = md_name_string.split(/\,/);
	split_str = split_str[0].split(/\s/);
	var last_name = split_str[split_str.length - 1].toLowerCase();
	return last_name
}
