
function linkSwitch(switchedLink, fieldArray, linkText, altLinkText) {
	link = $(switchedLink);
	if (link.text() == altLinkText) {
		$(fieldArray[0]).show();$(fieldArray[1]).hide();link.text(linkText);
	} else {
		$(fieldArray[0]).hide();$(fieldArray[1]).show();link.text(altLinkText);
	};
};


var ProviderMap = {
		bounds: null,
		map: null,
		initialized: null,
		markersShown: null,
		infoWindow: null
};

ProviderMap.init = function(lat, lng) {
    if ( ProviderMap.initialized == null ) {	
		var latLng = new google.maps.LatLng(lat, lng);
		
		var myOptions = {
		    zoom:12,
		    center: latLng,
		    mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		this.map = new google.maps.Map(document.getElementById('map'), myOptions);
		this.bounds = new google.maps.LatLngBounds();
		
		ProviderMap.infoWindow = new google.maps.InfoWindow();
		
		var zoomChangeBoundsListener = google.maps.event.addListener(ProviderMap.map, 'bounds_changed', function(event) {
			if (this.getZoom() > 12){ 
				this.setZoom(12);
			}
	               
	        google.maps.event.removeListener(zoomChangeBoundsListener);
        });

		ProviderMap.initialized = true;
		ProviderMap.markersShown = false;
    };
};

ProviderMap.attachInfo = function(marker, html) {
	google.maps.event.addListener(marker, 'click', function() {
		ProviderMap.infoWindow.setContent(html);
		ProviderMap.infoWindow.open(ProviderMap.map, marker);
    });
};

ProviderMap.placeMarkers = function(dataArray) {
	if (ProviderMap.initialized == true && ProviderMap.markersShown == false) {
		var _data = jQuery.parseJSON(dataArray);
		if ((typeof _data != 'undefined') && (_data != null)) {
			for (var i = 0; i < _data.length; i++) {      
				// create a new LatLng point for the marker
				var lat = _data[i].lat;
				var lng = _data[i].lng;
				var point = new google.maps.LatLng(parseFloat(lat),parseFloat(lng));
		      
				// extend the bounds to include the new point
				ProviderMap.bounds.extend(point);
	
				// add the marker itself
				var marker = new google.maps.Marker({
					position: point,
					map: ProviderMap.map,
					title:_data[i].name
			    });
	
				var html='<a href="'+ _data[i].url+'">'+ _data[i].name+'</a><br />'+ _data[i].address;
		
				ProviderMap.attachInfo(marker, html);
			};
			    
			// Fit the map around the markers we added:
			ProviderMap.map.fitBounds(ProviderMap.bounds);
			ProviderMap.markersShown = true;
		}
	}
};

