﻿GLOBE = {
	loader: null,
	menu: null,
	coords: null,
	markers: [],
	infowindow: null,
	map: null,
	PageID: null,

	init: function() {
		this.PageID = document.getElementById("PageID");
		this.loader = new YAHOO.util.YUILoader();
		this.loader.insert({
			require: ['json', 'connection', 'container'],
			onFailure: function() { alert('Failed to Initialize'); },
			scope: this,
			onSuccess: this.build
		});

	},

	build: function() {
		var scope = GLOBE;
		var myLatlng = null;
		var myOptions = null;
		var locationsDiv = null;
		var googlemap = null;
		var directions = null;
		var li = null;
		var a = null;
		var info = null;
		YAHOO.util.Connect.asyncRequest('POST', '/phy/GetAddressInfoAjax.asp?PageID=' + GLOBE.PageID.value, {

			success: function(o) {
				GLOBE.coords = YAHOO.lang.JSON.parse(o.responseText);
				locationsUL = document.getElementById('Locations');
				googlemap = document.getElementById('GoogleMap');
				myLatlng = new google.maps.LatLng(33.999056, -81.073167);
				//console.log(GLOBE.coords);
				myOptions = {
					zoom: 8,
					center: myLatlng,
					mapTypeId: google.maps.MapTypeId.ROADMAP
				};
				GLOBE.map = new google.maps.Map(document.getElementById("GoogleMap"), 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);
						GLOBE.coords[x].marker = new google.maps.Marker({
							position: myLatlng,
							//icon: 'red-dot.png',
							map: GLOBE.map,
							title: GLOBE.coords[x].Name
						});
						li = document.createElement('li');
						a = document.createElement('a');
						a.innerHTML = GLOBE.coords[x].Name + '<br />' + GLOBE.coords[x].street;
						a.target = 'googlemap';
						a.id = 'marker_' + x;
						a.href =
							"http://maps.google.com/maps?f=q&q=" +
							encodeURIComponent(GLOBE.coords[x].lat) + '+' +
							encodeURIComponent(GLOBE.coords[x].lng) + '+' +
							encodeURIComponent("(" + GLOBE.coords[x].Name + ")") + "&z=7";
						Event.addListener(a, 'click', GLOBE.onLabelClick, GLOBE, true);
						li.appendChild(a);
						locationsUL.appendChild(li);

						google.maps.event.addListener(GLOBE.coords[x].marker, 'click', function() {

							for (var x = 0; x < GLOBE.coords.length; x++) {
								var address;
								if (GLOBE.coords[x].marker === this) {

									if (GLOBE.infowindow) {
										GLOBE.infowindow.close();
									}

									if (GLOBE.coords[x].Name && GLOBE.coords[x].Name.length) {
										info = '<strong>' + GLOBE.coords[x].Name + '</strong>';
									}

									if (GLOBE.coords[x].street && GLOBE.coords[x].street.length) {
										info += '<br />' + GLOBE.coords[x].street;
										address = GLOBE.coords[x].street;
									}

									if (GLOBE.coords[x].extAdd1 && GLOBE.coords[x].extAdd1.length) {
										info += '<br />' + GLOBE.coords[x].extAdd1;
										address += ' ' + GLOBE.coords[x].extAdd1;
									}

									if (GLOBE.coords[x].extAdd2 && GLOBE.coords[x].extAdd2.length) {
										info += '<br />' + GLOBE.coords[x].extAdd2;
										address += ' ' + GLOBE.coords[x].extAdd2;
									}

									if (GLOBE.coords[x].locality && GLOBE.coords[x].locality.length) {
										info += '<br />' + GLOBE.coords[x].locality;
										address += ' ' + GLOBE.coords[x].locality;
									}

									if (GLOBE.coords[x].region && GLOBE.coords[x].region.length) {
										info += ', ' + GLOBE.coords[x].region;
										address += ', ' + GLOBE.coords[x].region;
									}

									if (GLOBE.coords[x].postalCode && GLOBE.coords[x].postalCode.length) {
										info += ' ' + GLOBE.coords[x].postalCode;
										address += ' ' + GLOBE.coords[x].postalCode;
									}

									if (GLOBE.coords[x].street && GLOBE.coords[x].street.length) {
										//directions = GLOBE.coords[x].street;
										/*if (GLOBE.coords[x].lat && GLOBE.coords[x].lng) {
										directions += ' @' + (GLOBE.coords[x].lat) + ',' + (GLOBE.coords[x].lng);
										}*/
										address = encodeURIComponent(address);
										info += '<br /><a target="_blank" href=' + "http://maps.google.com/maps?f=d&q=" + address + '>Get Directions</a>'
									}

									GLOBE.infowindow = new google.maps.InfoWindow({
										content: info
									});

									GLOBE.infowindow.open(GLOBE.map, this);
									break;
								}
							}
						});

					}
				}
			},

			failure: function() {
				alert("Error occurred during Map Initialize.");
			}
		});
	},

	onLabelClick: function(e) {
		var info, address;
		Event.stopEvent(e);
		var target = Event.getTarget(e);
		target = target.id;
		target = target.split('_')[1];
		target = parseInt(target, 10);
		target = this.coords[target];
		if (this.infowindow) {
			this.infowindow.close();
		}
		if (target.Name && target.Name.length) {
			info = '<strong>' + target.Name + '</strong>';
		}

		if (target.street && target.street.length) {
			info += '<br />' + target.street;
			address = target.street;

		}

		if (target.extAdd1 && target.extAdd1.length) {
			info += '<br />' + target.extAdd1;
			address += ' ' + target.extAdd1;
		}

		if (target.extAdd2 && target.extAdd2.length) {
			info += '<br />' + target.extAdd2;
			address += ' ' + target.extAdd2;
		}

		if (target.locality && target.locality.length) {
			info += '<br />' + target.locality;
			address += ' ' + target.locality;
		}

		if (target.region && target.region.length) {
			info += ', ' + target.region;
			address += ', ' + target.region;
		}

		if (target.postalCode && target.postalCode.length) {
			info += ' ' + target.postalCode;
			address += ' ' + target.postalCode;
		}

		if (target.street && target.street.length) {
			/*if (target.lat && target.lng) {
			address += ' @' + (target.lat) + ',' + (target.lng);
			}*/
			address = encodeURIComponent(address);
			info += '<br /><a target="_blank" href=' + "http://maps.google.com/maps?f=d&q=" + address + '>Get Directions</a>'
		}
		this.infowindow = new google.maps.InfoWindow({
			content: info
		});

		this.infowindow.open(this.map, target.marker);
	}

};

	Event.throwErrors = true;
	Event.onDOMReady(GLOBE.init, GLOBE, true);