// developed by: Ennio Bozzetti
// date: 10/21/2009
// version: 0.0.1a
// description: Configuration needed for the poll. Uses ColdFusion and AJAX


//Call to the ColdFusion CFC
var pt = new practice();
var lat = "38.83";
var lng = "-93.47";
var zoom = 3;
var mapCenter = "";
var distanceDiv = document.createElement('div');
var insideLogo = document.createElement('div');
var zipc = "";
var spc = "";
var dis = "10";

//Landing page search by zipcode
function getLandingpageZipcode(zipcode, distance){
	zoom = 8;
	pt.setCallbackHandler(displayMap);
	pt.setErrorHandler(pErr);
	pt.getAllPracticebyzipcode(zipcode, distance);
}

//Error function, display error message
function pErr(code, message){
	alert('Please try again!');
}

//HELPER FUNCTION FOR DISPLAY INFORMATION
//Display practice on map
function displayMap(result){
	//PREPARE DATA TO DISPLAY
	var listData = eval("(" + result + ")");
	var colMap = new Object();
	var officeList = "";
	var offices = [];

	for (var i =0; i < listData.COLUMNS.length; i++) {
		colMap[listData.COLUMNS[i]] = i;
	}

	var zipZoom = false;
	if (zoom == 6 || zoom == 8){
		var temp = "";
		var temp2 = "";
		if (listData.DATA.length >= 1){ 
			temp = listData.DATA[0][colMap["PROVIDERBUSINESSLATITUDE"]];
			temp2 = listData.DATA[0][colMap["PROVIDERBUSINESSLONGITUDE"]];
		} else {
			zipZoom = true;
			temp = "38.83";
			temp2 = "-93.47";
			zoom = 3;
		}
		mapCenter = new google.maps.LatLng(temp,temp2);
	} else {
		mapCenter = new google.maps.LatLng(lat, lng);
	}

	//SETUP GOOGLE MAPS
	var myOptions = {
		zoom: zoom,
		center: mapCenter,
		disableDefaultUI: true,
		navigationControl: true,
		navigationControlOptions: {
			style: google.maps.NavigationControlStyle.SMALL
		},
		mapTypeId: google.maps.MapTypeId.ROADMAP
	}
	
	//CLEAR MAP
	document.getElementById("map_canvas").innerHTML = "";
	var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	
	google.maps.event.addListener(map, 'click', function() {
		infowindow.close();
	});

	for (var i = 0; i < listData.DATA.length; i++) {
		var html = '<div><p>' + listData.DATA[i][colMap["PROVIDERFIRSTNAME"]] + ' ' + listData.DATA[i][colMap["PROVIDERLASTNAME"]] + ', ' + listData.DATA[i][colMap["PROVIDERCREDENTIAL"]] + '</p><p><a href="/' + listData.DATA[i][colMap["PROVIDERURL"]] + '">View more information</a></p></div>';
		var longitude = listData.DATA[i][colMap["PROVIDERBUSINESSLONGITUDE"]];
		var latitude = listData.DATA[i][colMap["PROVIDERBUSINESSLATITUDE"]];	
		newMarkers(map, html, latitude, longitude);
	}
}

function newMarkers(map, html, latitude, longitude) {
	var myLatLng = new google.maps.LatLng(latitude, longitude);
	var marker = createMarker(map,myLatLng,html,html);
}
