﻿GLOBE = {
	coords: null,
	markers: [],
	infowindow: null,
	map: null,
	loader: null,
	init: function() 
	{
		this.loader = new YAHOO.util.YUILoader();
		this.loader.insert({
			require: ['json', 'connection', 'container'],
			onFailure: function() { alert('Failed to Initialize'); },
			scope: this,
			onSuccess: this.build
		});
	}, // End of Init
	build: function()
	{
		GLOBE.coords = YAHOO.lang.JSON.parse($('markers').innerHTML);
		var lat = (GLOBE.coords[0].us === true) ? 38.531779939899586 : 8.734089553024074;
		var lng = (GLOBE.coords[0].us === true) ? -87.0561953125 : 17.709429687500005;
		
		myLatlng = new google.maps.LatLng(lat, lng) ;

		myOptions = {
			zoom: (GLOBE.coords !== null && GLOBE.coords[0].us === true) ? 3 : 1,
			center: myLatlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};

		GLOBE.map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
		
		if (GLOBE.coords !== null) 
		{
			for (var x = 0; x < GLOBE.coords.length; x++) 
			{
				myLatlng = new google.maps.LatLng(GLOBE.coords[x].lat, GLOBE.coords[x].lng);
				
				// console.log(myLatlng, myLatlng.lat(), myLatlng.lng(), GLOBE.coords[x].lat, GLOBE.coords[x].lng);
				
				GLOBE.coords[x].marker = new google.maps.Marker({
					position: myLatlng,
					//icon: '/Content/Globe/images/red-dot.png',
					map: GLOBE.map,
					//title: GLOBE.coords[x].patients + ' ' + (GLOBE.coords[x].patients > 1 ? 'Patients' : 'Patient') + ' Seen from ' + GLOBE.coords[x].location
					title: GLOBE.coords[x].location
				});

				google.maps.event.addListener(GLOBE.coords[x].marker, 'click', function() {
					//console.log(this, arguments);

					for (var x = 0; x < GLOBE.coords.length; x++) {
						if (GLOBE.coords[x].marker === this) {
							GLOBE.infowindow = new google.maps.InfoWindow({
								//content: '<strong>' + GLOBE.coords[x].patients + ' ' + (GLOBE.coords[x].patients > 1 ? 'Patients' : 'Patient') + ' Seen from ' + GLOBE.coords[x].location + '</strong>'
								content: '<strong>' + GLOBE.coords[x].location + '</strong>'
								
							});

							GLOBE.infowindow.open(GLOBE.map, this);
							break;
						}
					}
				});

			}
		}
	},
	createMarker: function(point, pos)
	{
		//console.log(GLOBE.coords, pos);
		
		var marker = new GMarker( point );
		// var info = '<strong>' + GLOBE.coords[pos].patients + ' ' + (GLOBE.coords[pos].patients > 1 ? 'Patients' : 'Patient') + ' Seen from ' + GLOBE.coords[pos].location + '</strong>';
		var info = '<strong>' + GLOBE.coords[pos].location + '</strong>';

		GEvent.addListener( marker, "click", function() {
			GLOBE.map.openInfoWindowHtml(point, info);
		});
		
		return marker;
	} // End of createMarker
};

Event.throwErrors = true;
Event.onDOMReady(GLOBE.init, GLOBE, true );
