var kidsfest = {};
kidsfest.map = {};

kidsfest.prepareHomepageSlideshow = function() {
	var container	= $$('#slideshow ul')[0];
	var slides		= $$('#slideshow ul li');
	var buttons		= slides;
	var options		= {
		'slideInterval':		4000,
		'transitionDuration':	700
	};
	
	if (container && slides.length>1) {
		new SimpleCarousel(container, slides, buttons, options);
	}
};

kidsfest.prepareHomepagePromos = function() {
	var container	= $$('#promos ul')[0];
	var slides		= $$('#promos ul li');
	var buttons		= slides;
	var options		= {
		'slideInterval':		4000,
		'transitionDuration':	700
	};
	
	if (container && slides.length>1) {
		new SimpleCarousel(container, slides, buttons, options);
	}
};

kidsfest.prepareMap = function() {
	
	var map_div = $('googlemap');
	
	if (map_div && typeof GBrowserIsCompatible === 'function') {
		
		map_div.getChildren('').dispose();
		
		/**
		* Toggles Street View on and off.
		*/
		kidsfest.map.toggleStreetView = function() {
		  if (typeof(kidsfest.map.street_view_overlay)==='undefined' || kidsfest.map.street_view_overlay===null) {
			  
			kidsfest.map.street_view_button.toggleOnOff($('streetviewtogglebutton'));
			
			kidsfest.map.street_view_marker = new GMarker(kidsfest.map.map.getCenter(), {icon: kidsfest.map.street_view_marker_icon, draggable: true});
			kidsfest.map.map.addOverlay(kidsfest.map.street_view_marker);
			
			kidsfest.map.street_view_marker.openInfoWindow("Drag me onto a blue-outlined street!");
			kidsfest.map.onDragEnd();
		
			kidsfest.map.street_view_overlay = new GStreetviewOverlay();
			kidsfest.map.map.addOverlay(kidsfest.map.street_view_overlay);
			
			kidsfest.map.last_marker_location = this.map.getCenter();
			GEvent.addListener(kidsfest.map.street_view_marker,'dragstart', kidsfest.map.onDragStart);
			GEvent.addListener(kidsfest.map.street_view_marker,'dragend', kidsfest.map.onDragEnd);
			GEvent.addListener(kidsfest.map.street_view_marker,'click', kidsfest.map.openStreetViewInfoWindow);
			/*GEvent.addListener(map,'click', function(overlay,latlng) {
				this.street_view_marker.setLatLng(latlng);
				this.onDragEnd();
				this.openStreetViewInfoWindow();
			});*/
			kidsfest.map.street_view_current_pov = null;
			if (kidsfest.map.street_view_object) {
				kidsfest.map.street_view_object.setPOV({yaw:0});
			}
			
		  } else {
			kidsfest.map.street_view_button.toggleOnOff($('streetviewtogglebutton'));
			kidsfest.map.map.removeOverlay(kidsfest.map.street_view_marker);
			kidsfest.map.map.removeOverlay(kidsfest.map.street_view_overlay);
			kidsfest.map.street_view_overlay = null;
		  }
		};
		
		/**
		* Opens the Street View info window / bubble.
		*/
		kidsfest.map.openStreetViewInfoWindow = function() {
			kidsfest.map.street_view_container = new Element('div',{
				'id': 'streetviewcontainer',
				'styles': {
					'width': '460px',
					'height': '275px'
				}
			});
			kidsfest.map.street_view_container.adopt(
				new Element('div',{'id': 'streetviewaddress'}),
				new Element('div',{
					'id': 'streetview',
					'styles': {
						'width': '460px',
						'height': '250px'
					}
				})
			);
			
			kidsfest.map.street_view_object = new GStreetviewPanorama(kidsfest.map.street_view_container.childNodes[1]);
			kidsfest.map.street_view_object.setLocationAndPOV(kidsfest.map.street_view_marker.getLatLng(), kidsfest.map.street_view_current_pov);
			
			kidsfest.map.street_view_marker.openInfoWindow(kidsfest.map.street_view_container);
			
			GEvent.addListener(kidsfest.map.street_view_object, "initialized", kidsfest.map.onPanoLocationChange);
			GEvent.addListener(kidsfest.map.street_view_object, "yawchanged", kidsfest.map.onYawChange);
		
			kidsfest.map.onPanoLocationChange({latlng: kidsfest.map.street_view_marker.getLatLng()});
		};
		
		/**
		* Updates the Street View marker icon depending on the direction street view is facing.
		*/
		kidsfest.map.onYawChange = function(newYaw) {
			var GUY_NUM_ICONS = 16;
			var GUY_ANGULAR_RES = 360/GUY_NUM_ICONS;
			if (newYaw < 0) {
				newYaw += 360;
			}
			var guyImageNum = Math.round(newYaw/GUY_ANGULAR_RES) % GUY_NUM_ICONS;
			var guyImageUrl = "http://maps.google.com/intl/en_us/mapfiles/cb/man_arrow-" + guyImageNum + ".png";
			kidsfest.map.street_view_marker.setImage(guyImageUrl);
			kidsfest.map.street_view_current_pov = kidsfest.map.street_view_object.getPOV();
		};
		
		/**
		* Updates the address shown at the top of the Street View window, using the geocoder.
		*/
		kidsfest.map.onPanoLocationChange = function(loc) {
			
			kidsfest.map.street_view_marker.setLatLng(loc.latlng);
			
			/* reset() function causing errors
			var info_window = mapview.map.getInfoWindow();
			if (info_window) {
				glatlng = new GLatLng(loc.latlng.x,loc.latlng.y);
				info_window.reset(glatlng,null,null,null,null);
			}
			*/
			
			kidsfest.map.geocoder.getLocations(loc.latlng, function(addresses) {
				if (addresses) {
					if (addresses.Status.code !== 200) {
						if ($('streetviewaddress')) {
							$('streetviewaddress').set('html','');
						}
					} else {
						if (addresses && addresses.Placemark[0] && addresses.Placemark[0].AddressDetails && addresses.Placemark[0].AddressDetails.Country && addresses.Placemark[0].AddressDetails.Country.AdministrativeArea && addresses.Placemark[0].AddressDetails.Country.AdministrativeArea.Locality && addresses.Placemark[0].AddressDetails.Country.AdministrativeArea.Locality.Thoroughfare && addresses.Placemark[0].AddressDetails.Country.AdministrativeArea.Locality.Thoroughfare.ThoroughfareName) {
							var streetviewaddress = addresses.Placemark[0].AddressDetails.Country.AdministrativeArea.Locality.Thoroughfare.ThoroughfareName;
							if ($('streetviewaddress')) {
								$('streetviewaddress').set('html',streetviewaddress+'<span class="approx">(approx.)</span>');
							}
						}
					}
				}
			});
		
		};
		
		/**
		* Closes the Street View info window when the user begins to drag the marker.
		*/
		kidsfest.map.onDragStart = function() {
			kidsfest.map.map.closeInfoWindow();
		};
		
		/**
		* When the user drops the street view marker, look for a valid street view location.
		*/
		kidsfest.map.onDragEnd = function() {
			if (typeof kidsfest.map.street_view_marker !== 'undefined' && kidsfest.map.street_view_marker !== null) {
				var latlng = kidsfest.map.street_view_marker.getLatLng();
				kidsfest.map.street_view_client.getNearestPanorama(latlng, kidsfest.map.onResponse);
			}
			if (kidsfest.map.street_view_object) {
				kidsfest.map.street_view_current_pov = kidsfest.map.street_view_object.getPOV();
			}
		};
		
		/**
		* Called by onDragEnd. If the street view client found a street view location, open the street view window there,
		* otherwise move the marker back to where it was.
		* @param {Object} The response object from Google's getNearestPanorama function
		*/
		kidsfest.map.onResponse = function(response) {
			if (response.code !== 200) {
				kidsfest.map.street_view_marker.setLatLng(kidsfest.map.last_marker_location);
			} else {
				var latlng = new GLatLng(parseFloat(response.Location.lat), parseFloat(response.Location.lng));
				kidsfest.map.street_view_marker.setLatLng(latlng);
				kidsfest.map.last_marker_location = latlng;
				kidsfest.map.openStreetViewInfoWindow();
			}
		};
		
		/**
		* Create a Street View button class. We use it in Google's addControl, so it needs to have
		* specific methods for Google to hook in to.
		*/
		kidsfest.map.StreetViewButton = function() {};
		kidsfest.map.StreetViewButton.prototype = new GControl();
		
		kidsfest.map.StreetViewButton.prototype.initialize = function(map) {
		  var container = document.createElement("div");
		  var ButtonOutter = document.createElement("div");
		  this.setButtonStyleOutter_(ButtonOutter);
		  container.appendChild(ButtonOutter);
		  
		  var ButtonInner = document.createElement("div");
		  ButtonInner.setAttribute('id','streetviewtogglebutton');
		  this.setButtonStyleInner_(ButtonInner);
		  ButtonOutter.appendChild(ButtonInner);
		  
		  ButtonInner.appendChild(document.createTextNode("Street View"));
		
		GEvent.addDomListener(ButtonInner, "click", function() {
			kidsfest.map.toggleStreetView();
		  });
		  map.getContainer().appendChild(container);
		  return container;
		};
		
		kidsfest.map.StreetViewButton.prototype.getDefaultPosition = function() {
		  return new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(300, 7));
		};
		
		kidsfest.map.StreetViewButton.prototype.setButtonStyleOutter_ = function(button) {
		  button.style.border = "1px solid black";
		  button.style.textAlign = "center";
		  button.style.width = "6em";
		};
		
		kidsfest.map.StreetViewButton.prototype.setButtonStyleInner_ = function(button) {
		  button.style.backgroundColor = "white";
		  button.style.font = "12px/1.5em Arial";
		  button.style.border = "1px solid";
		  button.style.borderColor = "white rgb(176,176,176) rgb(176,176,176) white";
		  button.style.padding = "2px";
		  button.style.textAlign = "center";
		  button.style.cursor = "pointer";
		};
		
		kidsfest.map.StreetViewButton.prototype.toggleOnOff = function(button) {
			if (button.style.fontWeight!=='bold') {
				button.style.fontWeight='bold';
				button.style.borderColor = "rgb(52,86,132) rgb(108,157,223) rgb(108,157,223) rgb(52,86,132)";
			} else {
				button.style.fontWeight='';
				button.style.borderColor = "white rgb(176,176,176) rgb(176,176,176) white";
			}
		};
		
		/**
		* Street View has a bug with one of its ajax calls, which, after the request is received, tries to
		* do something with the street view flash object without first checking whether it exists. If the
		* street view window has been closed, IE throws an error because it can't find the element.
		* 
		* This function creates a bunch of elements with the ids that street view normally uses, so that
		* instead of street view not finding anything and throwing an error, it will just call the empty
		* funtion of these fake ones.
		* 
		* This isn't really an IE error, it's a bug logged with Google, but IE seems to be the only one
		* throwing an error.
		*/
		kidsfest.map.fixStreetViewErrors = function() {
			var i;
			for (i=1;i<=10;i++) {
				var fake_pano_flash = new Element('div',{
					'id':	'panoflash'+i
				});
				fake_pano_flash.inject($$('body')[0],'bottom');
				fake_pano_flash.SetReturnValue = function(value) {};
			}	
		}
		
		if (GBrowserIsCompatible()) {
			
			map_div.setStyle('height','600px');
			
			this.map.street_view_overlay = null;
			this.map.street_view_client = new GStreetviewClient();
			this.map.street_view_object = null;
			this.map.street_view_container = null;
			this.map.street_view_current_pov = null;
			this.map.geocoder = null;
			
			// This breaks stuff now, Google must have changed their code
			//this.map.fixStreetViewErrors();
			
			var kidstown_coords = new GLatLng(-36.3867,145.38105);
			
			this.map.map = new GMap2(map_div);
			this.map.map.setCenter(kidstown_coords, 14);
			this.map.map.setUIToDefault();
			
			this.map.map.addOverlay(new GMarker(kidstown_coords));
			
			this.map.street_view_button = new kidsfest.map.StreetViewButton();
			this.map.map.addControl(kidsfest.map.street_view_button);
			
			this.map.geocoder = new GClientGeocoder();
			
			this.map.street_view_marker_icon = new GIcon(G_DEFAULT_ICON);
			this.map.street_view_marker_icon.shadow = "";
			this.map.street_view_marker_icon.image = "http://maps.google.com/intl/en_us/mapfiles/cb/man_arrow-0.png";
			this.map.street_view_marker_icon.transparent = "http://maps.google.com/intl/en_us/mapfiles/cb/man-pick.png";
			this.map.street_view_marker_icon.imageMap = [
				26,13, 30,14, 32,28, 27,28, 28,36, 18,35, 18,27, 16,26,
				16,20, 16,14, 19,13, 22,8
			];
			this.map.street_view_marker_icon.iconSize = new GSize(49, 52);
			this.map.street_view_marker_icon.iconAnchor = new GPoint(25, 35);  // near base of guy's feet
			this.map.street_view_marker_icon.infoWindowAnchor = new GPoint(25, 5);  // top of guy's head
			
		}
		
	}
	
};

// -------------------------------------------------------------------------------------------------------------

window.addEvent('domready',function() {
	kidsfest.prepareHomepagePromos();
	kidsfest.prepareHomepageSlideshow();
	kidsfest.prepareMap();
});
