﻿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,
			myLatlng = null,
			myOptions = null,
			locationsDiv = null,
			locationsUL = null,
			googlemap = null,
			directions = null,
			li = null,
			a = null,
			x = null,
			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 (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');
						/* This is where the links are formated.  
						* Do not add any additional tags (<b>, <span>, etc).  DON'T DO IT.
						* It will break, because YAHOO.util.Event.getTarget looks 
						* for EXACTLY what is clicked on.  -Anna, 2-4-2011
						*/
						a.innerHTML = GLOBE.coords[x].Name +
							' - ' + 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 (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) {
		Event.stopEvent(e);
		var info,
			address,
			target = Event.getTarget(e);  //Problem is here.  Not passing the DOM properly.

		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);
