window.onload=function(){
		
	if($('toggleDiv')){		
		$('toggleDiv').slide('hide');
		
		$('toggleSwitch').addEvent('click', function(event){
			event.stop();
			$('toggleDiv').slide('toggle');
			//help.getElement('.lca-help-toggle a').toggleClass('expanded');
		});
	};
	
	
	if(document.getElement('#map')){
		doMap();
	}
	
	/*if(document.getElement('#gallery')){
		var bottomGalleryPopout=document.getElement('#gallery .bottom .popout');
		bottomGalleryPopout.getElement('.arrow').style.top=bottomGalleryPopout.measure(function(){return this.getSize().y})-24+"px";
		
		//prevent css hover states from being used on the gallery
		document.getElementById("gallery").removeClass('gallery');
			
		var presentation;
		window.addEvent('domready', function(){
			new CrossFadingPresentation('gallery', {
				transitionDuration: 300,
				transitionInterval: 5000,
				transitionType: Fx.Transitions.Quad.easeInOut
			});
		});
	}
	
	//give all gallery slides position absolute
	var slides=document.getElements('.gallerySlide');
	for(var i=0;i<slides.length;i++){
		slides[i].style.position="absolute";
	}
	
	new SlidingPresentation('fullPartners', {
			transitionDuration: 250,
            transitionInterval: 5000,
            transitionType: Fx.Transitions.Quad.easeInOut
	});
	*/
		
	function doMap(){
		//Get the map div
		var mapDiv=document.getElementById("map");
		var mapChildren=mapDiv.childNodes;
		//Remove Child elements
		for(var i=mapChildren.length-1;i>-1;i--){
			mapDiv.removeChild(mapChildren[i]);
		}
		
		var mapWrapper=document.createElement("div");
		mapWrapper.setAttribute("id","mapWrapper");
		mapDiv.appendChild(mapWrapper);
		
		//Some useful variables
		var map;
		var directionsService = new google.maps.DirectionsService();
	
		//Where is the location
		var coords = new google.maps.LatLng(51.511604,-0.094883);
		//Set up some map options
		var mapOptions={
			zoom: 15,
			center: coords,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		}
		
		//Put the map on the page
		map = new google.maps.Map(mapWrapper, mapOptions);
		
		//Define a marker for the map
		var pointerImg = new google.maps.MarkerImage('js/images/painters-map-icon.png',
			// This marker size.
			new google.maps.Size(118, 73),
			// The origin
			new google.maps.Point(0,0),
			// The anchor
			new google.maps.Point(59, 73)
		);
	
		//Give the marker a shadow
		var pointerShadow = new google.maps.MarkerImage('js/images/shadow-cerebMapPointer.png',
			// The shadow image is larger in the horizontal dimension
			// while the position and offset are the same as for the main image.
			new google.maps.Size(86, 21),
			new google.maps.Point(0,0),
			new google.maps.Point(27.0, 21.0)
		);
	
		//Put the marker on the map
		var marker = new google.maps.Marker({
			position: coords,
			map: map,
			shadow: pointerShadow,
			icon: pointerImg,
			title: "CEREB"
		});
		
		//If the marker gets clicked, centre on it and zoom
		google.maps.event.addListener(marker, 'click', function() {
			map.setZoom(15);
			map.setCenter(coords);
		});
		
		//Declare form elements
		var directionsForm=document.createElement("form");
		var directionsLabel=document.createElement("label");
		var directionsInput=document.createElement("input");
		var directionsSubmit=document.createElement("input");
		//Set attributes on form and form elements
		//The form itself:
		directionsForm.setAttribute("id", "directionsForm");
		//The label:
		directionsLabel.setAttribute("for", "startLocation");
		directionsLabel.appendChild(document.createTextNode("For directions, enter an address below"));
		//The text field:
		directionsInput.setAttribute("type", "text");
		directionsInput.setAttribute("name","startLocation");
		directionsInput.setAttribute("id","startLocation");
		//The submt button:
		directionsSubmit.setAttribute("type", "submit");
		directionsSubmit.setAttribute("name","submit");
		directionsSubmit.setAttribute("class","submit");
		directionsSubmit.setAttribute("value","Go");
		//IE 6 and 7 don't render CSS for this form element so it's declared inline
		directionsSubmit.style.width="auto";
		directionsSubmit.style.marginTop="0em";
		directionsSubmit.style.marginBottom="0em";
		directionsSubmit.style.backgroundColor="rgb(215, 132, 46)";
		directionsSubmit.style.color="#000";
		
		//Put the form elements inside the form
		directionsForm.appendChild(directionsLabel);
		directionsForm.appendChild(directionsInput);
		directionsForm.appendChild(directionsSubmit);
		//Put the form on the page
		//document.getElementById("contactForm").parentNode.insertBefore(directionsForm,document.getElementById("contactForm"));
		mapDiv.appendChild(directionsForm);
		
		var directionsDisplay = new google.maps.DirectionsRenderer();
		//directionsDisplay.setMap(map);
	    //A variable for error checking
		var displayingErrors=false;
		directionsSubmit.onclick=function(){
			var startValue=document.getElementById("startLocation").value;
			var geocoder = new google.maps.Geocoder();
			if (geocoder) {
				geocoder.geocode( { 'address': startValue}, function(results, status) {
					if (status == google.maps.GeocoderStatus.OK) {
						/*Get directions*/
						var startPoint = results[0].geometry.location;
						var endPoint = coords;
	
						var request = {
							origin:startPoint, 
							destination:endPoint,
							travelMode: google.maps.DirectionsTravelMode.WALKING
						};
						
						directionsService.route(request, function(result, status){
							if (status == google.maps.DirectionsStatus.OK) {
								if(displayingErrors==true){
									var errors=document.getElementById("errors");
									errors.parentNode.removeChild(errors);
									displayingErrors=false;
								}
								directionsDisplay.setMap(map);
								directionsDisplay.setDirections(result);
								changeHeight();
							}else{
								if(displayingErrors==true){
									directionsDisplay.setMap(null);
									var errors=document.getElementById("errors");
									var errorChildren=errors.childNodes;
									for(var i=errorChildren.length-1;i>-1;i--){
										errors.removeChild(errorChildren[i]);
									}
									var errorMessages=new Array("Google wasn't able to pinpoint your starting point.","The most common reason for this is that somewhere in the world shares the same name as the starting point which you entered.","Try using a more specific address or the address of somewhere nearby as a starting point for your journey.", "For example, if you are leaving from London Waterloo train station, try entering 'London Waterloo', rather than 'Waterloo' alone.");
									for(var i=0;i<errorMessages.length;i++){
										var error=document.createElement("p");
										error.appendChild(document.createTextNode(errorMessages[i]));
										errors.appendChild(error);
									}
									mapDiv.appendChild(errors);
									map.setZoom(12);
									map.setCenter(coords);
									changeHeight();
								}else{
									directionsDisplay.setMap(null);
									displayingErrors=true;
									var errors=document.createElement("div");
									errors.setAttribute("id","errors");
									var errorMessages=new Array("Google wasn't able to pinpoint your starting point.","The most common reason for this is that somewhere in the world shares the same name as the starting point which you entered.","Try using a more specific address or the address of somewhere nearby as a starting point for your journey.", "For example, if you are leaving from London Waterloo train station, try entering 'London Waterloo', rather than 'Waterloo' alone.");
									for(var i=0;i<errorMessages.length;i++){
										var error=document.createElement("p");
										error.appendChild(document.createTextNode(errorMessages[i]));
										errors.appendChild(error);
									}
									mapDiv.appendChild(errors);
									map.setZoom(12);
									map.setCenter(coords);
									changeHeight();
								}
							}
						});
					}else{
						if(displayingErrors==true){
							directionsDisplay.setMap(null);
							var errors=document.getElementById("errors");
							var errorChildren=errors.childNodes;
							for(var i=errorChildren.length-1;i>-1;i--){
								errors.removeChild(errorChildren[i]);
							}
							var errorMessages=new Array("Sorry, we couldn't find the place you searched for.","Check your spelling and try again.","Alternatively, try the name of a different, nearby location.","Remember, you don't need to enter the full address. You can save time by entering just a postcode or town name.");
							for(var i=0;i<errorMessages.length;i++){
								var error=document.createElement("p");
								error.appendChild(document.createTextNode(errorMessages[i]));
								errors.appendChild(error);
							}
							mapDiv.appendChild(errors);
							map.setZoom(12);
							map.setCenter(coords);
							changeHeight();
						}else{
							directionsDisplay.setMap(null);
							displayingErrors=true;
							var errors=document.createElement("div");
							errors.setAttribute("id","errors");
							var errorMessages=new Array("Sorry, we couldn't find the place you searched for.","Check your spelling and try again.","Alternatively, try the name of a different, nearby location.","Remember, you don't need to enter the full address. You can save time by entering just a postcode or town name.");
							
							for(var i=0;i<errorMessages.length;i++){
								var error=document.createElement("p");
								error.appendChild(document.createTextNode(errorMessages[i]));
								errors.appendChild(error);
							}
							mapDiv.appendChild(errors);
							map.setZoom(12);
							map.setCenter(coords);
							changeHeight();
						}
					}
				});
			}
			return false;
		}
	
	}
	
}