function mapInitialize()
{
	if(typeof _urls == 'undefined') {
		return false;
	}
	(function($)
	{
		var maps = {},
			mapCount = 0,
			mapBounds = new google.maps.LatLngBounds(new google.maps.LatLng(49.5, -10), new google.maps.LatLng(59, 2.6)),
			display = null,
			slowspotdef = '2',
			currentSlowspotdef = false,
			currentDisplay = false,
			subscribeLock = false,
			localSearch;

		function tileURL(tile, zoom)
		{
			//Lets check if the tile intersects with the allowed bounds, if it does serve real tile url otherwise give blank url.
			//Firstly convert tile coords to google.maps.LatLngBounds
			var bounds = tileToBounds(tile, zoom);

			//Now we see if it intersecs
			if(bounds.intersects(mapBounds)) {
				return _urls.tiles+zoom+'/'+tile.x+'/'+tile.y+'-'+(display == null ? 'all' : encodeURIComponent(display))+'-'+slowspotdef+'.png';
			} else {
				return '';
			}
		}

		function tileToBounds(tile, zoom)
		{
			var tilesAtThisZoom = 1 << zoom;
			var lngWidth = 360.0 / tilesAtThisZoom;
			var lng = - 180 + (tile.x * lngWidth);
			var latHeightMerc = 1.0 / tilesAtThisZoom;
			var topLatMerc = tile.y * latHeightMerc;
			var bottomLatMerc = topLatMerc + latHeightMerc;
			var bottomLat = (180 / Math.PI) * ((2 * Math.atan(Math.exp(Math.PI * (1 - (2 * bottomLatMerc))))) - (Math.PI / 2));
			var topLat = (180 / Math.PI) * ((2 * Math.atan(Math.exp(Math.PI * (1 - (2 * topLatMerc))))) - (Math.PI / 2));

			return new google.maps.LatLngBounds(new google.maps.LatLng(bottomLat, lng), new google.maps.LatLng(topLat, lng+lngWidth));
		}

		function initSearch()
		{
			localSearch = new GlocalSearch();

			//Lets find some
			$('input.map-search').each(function()
				{
					var	input = this,
						$input = $(input),
						mapID = input.id.replace(/-search$/, '');

					if(maps[mapID] || mapCount == 1) {
						if(mapID == 'yourmap-map') {
							$input.siblings('.search-submit').click(function(event)
								{
									search(input);
								}
							);
							$input.keypress(function(event)
								{
									if(event.which == 13) {
										search(input);
										event.stopPropagation();
										event.preventDefault();
										return false;
									}
								}
							);
						} else {
							$input.parents('form').submit(function(event)
								{
									search(input);
									event.stopPropagation();
									event.preventDefault();
									return false;
								}
							);

							if($input.val() && $input.hasClass('auto-search')) {
								search(this)
							}
						}
					}
				}
			);
		}

		function search(input)
		{
			if(localSearch) {
				var $input = $(input)
					mapID = input.id.replace(/-search$/, '');

				if($input.val()) {
					if(!maps[mapID] && mapCount == 1) {
						mapID = null;
						for (k in maps) {
							mapID = k;
						}
					}
					if(maps[mapID]) {
						localSearch.setSearchCompleteCallback(null, function()
							{
								if (localSearch.results[0]) {
									maps[mapID].map.setCenter(new google.maps.LatLng(localSearch.results[0].lat, localSearch.results[0].lng), localSearch.results[0].accuracy > 2 ? localSearch.results[0].accuracy*3 : 11);
								}
							}
						);

						localSearch.execute($input.val() + ', UK');
						return true;
					}
				}
			}
			return false;
		}

		function yourmapLocationFieldChange()
		{
			var mapID = 'yourmap-map';
			if(!maps[mapID]) {
				return false;
			}
			var $lat = $(':input[name=yourmap\[lat\]]'),
				$lng = $(':input[name=yourmap\[lng\]]'),
				$zoom = $(':input[name=yourmap\[zoom\]]')
				lat = $lat.val(),
				lng = $lng.val(),
				zoom = $zoom.val();

			if(lat != '' && lng != '' && zoom != '') {
				maps[mapID].map.setCenter(new google.maps.LatLng(parseFloat(lat), parseFloat(lng)), parseInt(zoom));
			}
		}

		function yourmapSizeFieldChange(event, element)
		{
			var mapID = 'yourmap-map';
			if(!maps[mapID]) {
				return false;
			}
			var $width = $(':input[name=yourmap\[width\]]'),
				$height = $(':input[name=yourmap\[height\]]'),
				width = parseInt($width.val()),
				height = parseInt($height.val()),
				maptype = $(':radio[name^=yourmap\[maptype\]]:checked').val(),
				imageRatio, newWidth, newHeight;
			if(maptype == 'basic') {
				originalWidth = 817;
				originalHeight = 1691;

				if(typeof element == 'undefined') {
					element = this;
				}

				if($(element).attr('name') == 'yourmap[width]') {
					if(width) {
						if( originalWidth > width) {
							$height.val(Math.round(originalHeight * ( width / originalWidth )));
						} else {
							$width.val(originalWidth);
							$height.val(originalHeight);
						}
					}
				} else {
					if(height) {
						if(originalHeight > height) {
							$width.val(Math.round(originalWidth * ( height / originalHeight )));
						} else {
							$width.val(originalWidth);
							$height.val(originalHeight);
						}
					}
				}
			}
		}

		function initYourmapFeatures()
		{
			var mapID = 'yourmap-map';
			if(!maps[mapID]) {
				return false;
			}

			initSearch();

			var $lat = $(':input[name=yourmap\[lat\]]'),
				$lng = $(':input[name=yourmap\[lng\]]'),
				$zoom = $(':input[name=yourmap\[zoom\]]'),
				$width = $(':input[name=yourmap\[width\]]'),
				$height = $(':input[name=yourmap\[height\]]'),
				$types = $(':checkbox[name^=yourmap\[type\]]')
				$slowspotdefs = $(':radio[name^=yourmap\[slowspotdef\]]');

			if(!$lat.length || !$lng.length || !$zoom.length) {
				return false;
			}

			$lat.change(yourmapLocationFieldChange).val(maps[mapID].map.getCenter().lat());
			$lng.change(yourmapLocationFieldChange).val(maps[mapID].map.getCenter().lng());
			$zoom.change(yourmapLocationFieldChange).val(maps[mapID].map.getZoom());

			$width.change(yourmapSizeFieldChange);
			$height.change(yourmapSizeFieldChange);

			GEvent.addListener(maps[mapID].map, 'moveend', function()
				{
					updateHash(mapID);
					$lat.val(this.getCenter().lat());
					$lng.val(this.getCenter().lng());
					$zoom.val(this.getZoom());
				}
			);

			if(display != null) {
				$types.filter(':not([value='+display+'])').removeAttr('checked');
				if(display == 'notspots') {
					$('#yourmap-map-slowspotdef').hide();
				}
			}

			$types.click(function(event)
				{
					var $checked = $types.filter(':checked');
					if($checked.length == 1) {
						display = $checked.val();
						if(display == 'notspots') {
							$('#yourmap-map-slowspotdef').slideUp('normal');
						} else {
							$('#yourmap-map-slowspotdef').slideDown('normal');
						}
					} else {
						$types.attr('checked', 'checked');
						display = null;
						$('#yourmap-map-slowspotdef').slideDown('normal');
					}
					if(display != currentDisplay) {
						updateHash(mapID);
						for (var i = 0; i < maps[mapID].overlays.length; i++) {
							maps[mapID].overlays[i].refresh();
						}
						currentDisplay = display;
					}
				}
			);

			if(slowspotdef) {
				$slowspotdefs.filter('[value='+slowspotdef+']').attr('checked', 'checked');
			}

			$slowspotdefs.click(function()
				{
					slowspotdef = $(this).val();
					if(slowspotdef != currentSlowspotdef) {
						updateHash(mapID);
						for (var i = 0; i < maps[mapID].overlays.length; i++) {
							maps[mapID].overlays[i].refresh();
						}
						currentSlowspotdef = slowspotdef;
					}
				}
			);

			$maptypes = $(':radio[name=yourmap\[maptype\]]');
			if($maptypes.length) {
				$settings = $('#yourmap-map-settings'),
				$outline = $('#yourmap-map-basic');
				if($settings.length) {
					var val = $maptypes.filter(':checked').val();
					if(val != 'interactive' && val != 'static') {
						$settings.hide();
						if(val == 'basic') {
							if($width.val() != '') {
								yourmapSizeFieldChange(null, $width[0]);
							} else if($height.val() != '') {
								yourmapSizeFieldChange(null, $height[0]);
							}
						} else {
							$outline.hide();
						}
					} else {
						$outline.hide();
					}
					$maptypes.click(function(event)
						{
							var val = $(this).filter(':checked').val();
							if(val == 'interactive' || val == 'static') {
								$settings.slideDown('normal');
								$outline.slideUp('normal');
							} else {
								if(val == 'basic') {
									$outline.slideDown('normal');
									if($width.val() != '') {
										yourmapSizeFieldChange(null, $width[0]);
									} else if($height.val() != '') {
										yourmapSizeFieldChange(null, $height[0]);
									}
								}
								$settings.slideUp('normal');
							}
						}
					);
				}
			}
		}

		function initMainMapFeatures()
		{
			var mapID = 'main-map';
			if(!maps[mapID]) {
				return false;
			}

			var $link = $('<a href="#" id="link-to-map">[Link to this map]</a>'),
				$yourmaplink = $('<a href="#" id="yourmap-link">[Display this on your website]</a>'),
				$tools = $('<div id="map-tools">Display: notspots <input type="checkbox" name="notspots" value="notspots" checked="checked" /> | slow-spots <input type="checkbox" name="slowspots" value="slowspots" checked="checked" /> <select name="slowspotdef"><option value="2">2Mbps or less</option><option value="1">1Mbps or less</option><option value="500">500Kbps or less</option></select> | </div>').append($link).append($yourmaplink);
				$subscribe = $('form#map-subscribe'),
				$comments = $('#main-map-comments'),
				$output = false;

			if(typeof __enableMapOutput != 'undefined' && __enableMapOutput && _urls.output) {
				$output = $('#main-map-output');
				if($output.length) {
					$diag = $('<div class="diag" />'),
					$soutput = $('<div class="souput" />');

					$output.prepend($diag).append($soutput);

					$diag.html('Bounding: ' + maps[mapID].map.getBounds().toString() + '<br />Centre:' + maps[mapID].map.getCenter().toString() + '<br />Zoom: ' + maps[mapID].map.getZoom() + '<br />Mouse: N/A');
					GEvent.addListener(maps[mapID].map, 'moveend', function()
						{
							$diag.html('Bounding: ' + this.getBounds().toString() + '<br />Centre:' + this.getCenter().toString() + '<br />Zoom: ' + this.getZoom() + '<br />Mouse: N/A');
						}
					);
					GEvent.addListener(maps[mapID].map, 'mousemove', function(latlng)
						{
							$diag.html('Bounding: ' + this.getBounds().toString() + '<br />Centre:' + this.getCenter().toString() + '<br />Zoom: ' + this.getZoom() + '<br />Mouse: ' + latlng.toString());
						}
					);
				}
			}

			updateHash(mapID, $link, $yourmaplink)

			$('#main-map').before($tools);

			if(display != null) {
				$tools.find(':checkbox:not([name='+display+'])').removeAttr('checked');
			}

			$tools.find(':checkbox').click(function(event)
				{
					var $this = $(this),
						$checked = $this.parent().find(':checkbox:checked');
					if($checked.length == 1) {
						display = $checked.val();
					} else {
						$this.parent().find(':checkbox').attr('checked', 'checked');
						display = null;
					}
					if(display != currentDisplay) {
						updateComments($comments);
						updateHash(mapID, $link, $yourmaplink);
						for (var i = 0; i < maps[mapID].overlays.length; i++) {
							maps[mapID].overlays[i].refresh();
						}
						currentDisplay = display;

						if($output && $output.length) {
							updateMapOutput(mapID, $soutput);
						}
					}
				}
			);

			if(slowspotdef == '2' || slowspotdef == '1' || slowspotdef == '500') {
				$tools.find('select option[value="'+slowspotdef+'"]').attr('selected', 'selected');
			}

			$tools.find('select').change(function(event)
				{
					slowspotdef = $(this).val();
					if(slowspotdef != currentSlowspotdef) {
						updateComments($comments);
						updateHash(mapID, $link, $yourmaplink);
						for (var i = 0; i < maps[mapID].overlays.length; i++) {
							maps[mapID].overlays[i].refresh();
						}
						currentSlowspotdef = slowspotdef;
						if($output && $output.length) {
							updateMapOutput(mapID, $soutput);
						}
					}
				}
			);

			if($comments.length) {
				updateComments($comments);
			}

			GEvent.addListener(maps[mapID].map, 'moveend', function()
				{
					updateHash(mapID, $link, $yourmaplink);
					if($comments.length) {
						updateComments($comments);
					}
					if($output && $output.length) {
						updateMapOutput(mapID, $soutput);
					}
				}
			);

			initSearch();

			if($subscribe.length) {
				var $emailField = $subscribe.find('input[name=email]'),
					$emailResult = null,
					val = $.trim($emailField.val());

				if($emailField.length) {

					if(val == '' || val == 'e-mail address') {
						$emailField.val('e-mail address').addClass('grey-text');
					}

					$emailField.focus(function(event)
						{
							var $this = $(this),
								val = $.trim($this.val());

							if(val == 'e-mail address' || val == '') {
								$this.val('');
							}
							$this.removeClass('grey-text');
						}
					).blur(function(event)
						{
							var $this = $(this);
							if($.trim($this.val()) == '') {
								$this.val('e-mail address').addClass('grey-text');
							}
						}
					);

					$subscribe.submit(function(event)
						{
							if(subscribeLock) {
								event.stopPropagation();
								event.preventDefault();
								return false;
							}

							subscribeLock = true;

							if($emailResult != null) {
								$emailResult.remove();
								$emailResult = null;
							}
							var val = $.trim($emailField.val());
							if(val == '' || val == 'e-mail address') {
								$emailResult = $('<div class="message error">Required.</div>');
							} else if(!/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(val)) {
								$emailResult = $('<div class="message error">Invalid.</div>');
							} else if(!maps['main-map'].map.getBounds().intersects(mapBounds)) {
								$emailResult = $('<div class="message error">You are trying to subscribe to an area outside the UK!</div>');
							} else if(maps['main-map'].map.getZoom() < 8) {
								$emailResult = $('<div class="message error">Please zoom in closer to subscribe to an area.</div>');
							} else {
								var bounds = maps['main-map'].map.getBounds(),
									swLatLng = bounds.getSouthWest(),
									neLatLng = bounds.getNorthEast();

								$.ajax(
									{
										type: 'POST',
										url: _urls.keepmeupdated,
										data:
											{
												email: val,
												swLat: swLatLng.lat(),
												swLng: swLatLng.lng(),
												neLat: neLatLng.lat(),
												neLng: neLatLng.lng(),
												zoom: maps['main-map'].map.getZoom()
											},
										success: function(data, textStatus)
										{
											if($emailResult != null) {
												$emailResult.remove();
												$emailResult = null;
											}
											if(data.success) {
												$emailResult = $('<div class="message green"></div>').text(data.message);
												$emailField.val('e-mail address').addClass('grey-text');
											} else {
												$emailResult = $('<div class="message error"></div>').text(data.message);
											}
											$subscribe.append($emailResult);
											subscribeLock = false;
										},
										error: function (XMLHttpRequest, textStatus, errorThrown)
										{
											if($emailResult != null) {
												$emailResult.remove();
												$emailResult = null;
											}
											$emailResult = $('<div class="message error">We are unable to process your request at this time.</div>');
											$subscribe.append($emailResult);
											subscribeLock = false;
										},
										dataType: 'json'
									}
								);
							}

							if($emailResult != null) {
								$subscribe.append($emailResult);
								subscribeLock = false;
							}

							event.stopPropagation();
							event.preventDefault();
							return false;
						}
					);
				}
			}
		}

		function updateComments($comments)
		{
			var mapID = 'main-map';
			if(!maps[mapID]) {
				return false;
			}
			var bounds = maps[mapID].map.getBounds(),
				swLatLng = bounds.getSouthWest(),
				neLatLng = bounds.getNorthEast();

			$comments.html('<p>Loading...</p>');

			$.get(
				_urls.comments,
				{
					swLat: swLatLng.lat(),
					swLng: swLatLng.lng(),
					neLat: neLatLng.lat(),
					neLng: neLatLng.lng(),
					zoom: maps[mapID].map.getZoom(),
					type: display,
					spdef: slowspotdef
				},
				function(data, textStatus)
				{
					if(data.comments.length) {
						$comments.html(data.comments.join('<hr />') + '<hr /><div style="text-align:center">Showing latest ' + data.comments.length + ' comments</div><br clear="all" />');
					} else {
						$comments.html('<p>No comments found.</p>');
					}
				},
				'json'
			);
		}

		function updateHash(mapID, $link, $yourmaplink)
		{
			if(!mapID || !maps[mapID]) {
				return false;
			}
			var disp = display == null ? 'all' : display;
			location.hash = '#'+maps[mapID].map.getZoom()+','+maps[mapID].map.getCenter().lat()+','+maps[mapID].map.getCenter().lng()+','+disp+','+slowspotdef;
			if($link) {
				$link.attr('href', document.location.href);
			}
			if($yourmaplink) {
				$yourmaplink.attr('href', _urls.yourmap + location.hash);
			}
		}

		function updateMapOutput(mapID, $soutput, page)
		{
			if(!mapID || !maps[mapID]) {
				return false;
			}
			var bounds = maps[mapID].map.getBounds(),
				swLatLng = bounds.getSouthWest(),
				neLatLng = bounds.getNorthEast();

			$soutput.html('<p>Loading...</p>');

			$.get(
				_urls.output,
				{
					swLat: swLatLng.lat(),
					swLng: swLatLng.lng(),
					neLat: neLatLng.lat(),
					neLng: neLatLng.lng(),
					zoom: maps[mapID].map.getZoom(),
					type: display,
					spdef: slowspotdef,
					page: page
				},
				function(data, textStatus)
				{
					$soutput.html(data);
					$soutput.find('.pager a').click(function(event)
						{
							updateMapOutput(mapID, $soutput, $(this).attr('href').substr(1));
							console.log($(this).attr('href'));
							event.stopPropagation();
							event.preventDefault();
							return false;
						}
					);
				},
				'html'
			);
		}

		function parseHash()
		{
			if(document.location.hash) {
				var matches = /^#(\d+),(-{0,1}\d+\.{0,1}\d*),(-{0,1}\d+\.{0,1}\d*),(notspots|slowspots|all|)(,(2|1|500)){0,1}/.exec(document.location.hash),
					disp = null,
					spdef = 2;
				if(matches != null) {
					var latlng = google.maps.LatLng.fromUrlValue(matches[2]+','+matches[3]), zoom;
					if(latlng.toString() != '(NaN, NaN)') {
						matches[1] = parseInt(matches[1]);
						if(matches[1] < 5) {
							zoom = 5;
						} else if(matches[1] > 15) {
							zoom = 15;
						} else {
							zoom = matches[1];
						}
						if(matches[4] == 'notspots' || matches[4] == 'slowspots') {
							disp = matches[4];
						}
						if(matches[6]) {
							spdef = matches[6];
						}
						return [latlng, zoom, disp, spdef];
					}
				}
			}
			return false;
		}

		function addOverlay(mapID)
		{
			if(!maps[mapID]) {
				return false;
			}

			//Create the tile overlay
			tileLayer = new GTileLayer(null, null, null, { isPng: true, opacity: 0.35 });
			tileLayer.getTileUrl = tileURL;

			tileLayerOverlay = new GTileLayerOverlay(tileLayer);

			maps[mapID].overlays.push(tileLayerOverlay);

			//Add the overlay to the map
			maps[mapID].map.addOverlay(tileLayerOverlay);
		}

		function removeOverlays(mapID)
		{
			if(!maps[mapID]) {
				return false;
			}
			var overlays = maps[mapID].overlays.length,
				i = 0;
			if(overlays) {
				for(; i < overlays; i++) {
					maps[mapID].map.removeOverlay(maps[mapID].overlays[i]);
				}
				maps[mapID].overlays = [];
			}
		}

		$(document).ready(function()
			{
				//Start the maps.
				$('.map').each(function()
					{
						mapCount++;
						if(!this.id) {
							this.id = '_map_auto_id_'+mapCount;
						}

						//Check we don't already have a map with this id in storage
						if(maps[this.id]) {
							return false;
						}

						var $this = $(this),
							iframe = $this.hasClass('iframe'), //Is this is a iframe map
							mapTypes = [G_PHYSICAL_MAP],
							centre = false,
							mapID = this.id,
							map, mapTypes, hash;

						//Restrict the zoom levels.
						for (var i = 0; i < mapTypes.length; i++) {
							mapTypes[i].getMinimumResolution = function() {return 5;}
							mapTypes[i].getMaximumResolution = function() {return 15;}
						}

						map = new google.maps.Map2(this, { mapTypes: mapTypes });

						map.setMapType(G_PHYSICAL_MAP);

						maps[mapID] = { map: map, overlays: [], markerClusterer : false, timers: {} }; //Add the map to the maps storage object

						if(mapID == 'main-map' || mapID == 'yourmap-map') {
							hash = parseHash();
						}

						if(hash) {
							map.setCenter(hash[0], hash[1]);
							display = hash[2];
							currentDisplay = display;
							slowspotdef = hash[3]
							currentSlowspotdef = slowspotdef;
							$input = $('input#main-map-search');
							if($input.length) {
								$input.val('');
							}
						} else {
							//Lets centre the map
							if(typeof mapCentres != 'undefined' && mapCentres[mapID]) {
								map.setCenter(new google.maps.LatLng(mapCentres[mapID].lat, mapCentres[mapID].lng), mapCentres[mapID].zoom);
								if(typeof mapCentres[mapID].display != 'undefined') {
									display = mapCentres[mapID].display;
								}
								if(typeof mapCentres[mapID].slowspotdef != 'undefined') {
									slowspotdef = mapCentres[mapID].slowspotdef;
								}
							} else {
								map.setCenter(new google.maps.LatLng(55.354, -4.8), 5);
							}
						}

						if(iframe || mapID == 'yourmap-map') {
							map.addControl(new GSmallZoomControl3D());
						} else {
							map.addControl(new GLargeMapControl3D());
						}

						//Enable zoon via double click
						map.enableDoubleClickZoom();

						//Enable scroll wheel
						//map.enableScrollWheelZoom();

						//Add the tile overlay
						addOverlay(mapID);

						if(mapID == 'main-map') {
							initMainMapFeatures();
						} else if(mapID == 'yourmap-map') {
							initYourmapFeatures();
						}
					}
				);
			}
		);
	})(jQuery);
}

(function($)
{
	$(document).ready(function()
		{
			$types = $(':radio[name=report\[type\]]');
			if($types.length) {
				$reasons = $('#failed_reason_fieldset');
				$speeds = $('#current_speed_fieldset');
				if($reasons.length) {
					var val = $types.filter(':checked').val();
					if(val == 'slowspot') {
						$reasons.hide();
					} else if(val == 'notspot') {
						$speeds.hide();
					} else {
						$speeds.hide();
						$reasons.hide();
					}
					$types.click(function(event)
						{
							var val = $(this).filter(':checked').val();
							if(val == 'slowspot') {
								$speeds.slideDown('normal');
								$reasons.slideUp('normal');
							} else {
								$speeds.slideUp('normal');
								$reasons.slideDown('normal');
							}
						}
					);
				}
			}
		}
	);
})(jQuery);

$.getScript = function(url, callback, cache)
	{
		$.ajax(
			{
				type: 'GET',
				url: url,
				success: callback,
				dataType: 'script',
				cache: cache
			}
		);
	};
