
var pagename; // extracts the page name from the content_main class name, on Flash pages, so that we know what backgrounds to use
var flash_loaded = false; // this is for the timer loop checking that flash has loaded (or not)

// hide the main heading until the images are done, but only on pages where a pageheading is to be replaced
/*
var headID = document.getElementsByTagName("head")[0];         
var cssNode = document.createElement('link');
cssNode.type = 'text/css';
cssNode.rel = 'stylesheet';
cssNode.href = 'css/replaceheadings.css';
cssNode.media = 'screen';
headID.appendChild(cssNode);
*/

// various bits & pieces on page load
function loadPage () {

	// wait a few seconds, then check if Flash has loaded because there is a bug in Firefox which will leave the content area covered in white if the Flash is stuck
	//setTimeout ( "checkFlash()", 500 );
	
	// if the side panel contains only two insets boxes, shorten the second one by 1px to make it fit neatly
	adjustSidebox();
	
	// create the heading image(s) to show a nice font display
	//ReplaceHeading();
	
	// load the google map on contact pages
	if (document.getElementById('googlemap')) {
		LoadGooglemap("googlemap");
	}
	
	// make the image animation on the homepage
	if (document.getElementById('slideshow')) {
		$(function() {
			$('#slideshow').cycle({ 
				timeout: 6000,
				delay: 1000
			});
		});
	}
	
}

// on loading the search form, hide the "people ->" link in the search form which is for non-JS users only, show the radio button instead
function showSitesearch() {

	document.getElementById('tdsearchsiteradio1').className = "visibleinline";
	document.getElementById('tdsearchsiteradio2').className = "visibleinline";
	document.getElementById('tdsearchsitelink').className = "hidden";

}

// on selecting "property" or "people" in the search form at the top of the page, display different input fields
function switchSearchform(mode) {
	
	var inversemode;
	if (mode == "property") {
		inversemode = "site";
	} else {
		inversemode = "property";
	}
	document.getElementById('formrow_' + inversemode + '1').className = "hidden";
	document.getElementById('formrow_' + inversemode + '2').className = "hidden";
	document.getElementById('formrow_' + mode + '1').className = "visible";
	document.getElementById('formrow_' + mode + '2').className = "visible";
	
}

// hide the content for js users at the start (on flash pages only)
function hideContent() {
	
	// hide the non-js users' background images
	if (document.getElementById('noscript_developer')) {
		document.getElementById('noscript_developer').className = "hidden";
		pagename = "developer";
	}
	if (document.getElementById('noscript_investor')) {
		document.getElementById('noscript_investor').className = "hidden";
		pagename = "investor";
	}
	if (document.getElementById('noscript_occupier')) {
		document.getElementById('noscript_occupier').className = "hidden";
		pagename = "occupier";
	}
	if (document.getElementById('noscript_publicsector')) {
		document.getElementById('noscript_publicsector').className = "hidden";
		pagename = "publicsector";
	}
	if (document.getElementById('noscript_companyethos')) {
		document.getElementById('noscript_companyethos').className = "hidden";
		pagename = "companyethos";
	}
	// hide the main content until the flash has loaded
	document.getElementById('main_right').className = "hidden";
	
}

// this is triggered by the flash movie on a flash page, it makes the initially hidden main content visible underneath the flash so that it fades in nicely later
function showContent() {
	if (document.getElementById('main_right')) {
		document.getElementById('main_right').className = "visible";
	}
}

// this is also triggered from the flash movie, on loading, and tells javascript that Flash has loaded
function flashLoaded() {
	flash_loaded = true;
}

// wait a few seconds, then if flash has not loaded, dump it so the content becomes visible (Firefox problem)
function checkFlash() {
	if (!(flash_loaded)) {
		if (document.getElementById('flashcontent')) {
			document.getElementById('flashcontent').innerHTML = '<div id="noscript_' + pagename + '"></div>';
			showContent();
		}
	}
}

// if the side panel contains only two insets boxes, shorten the second one by 1px to make it fit neatly
function adjustSidebox() {
	if (document.getElementById('content_side')) {
		var arrElms = document.getElementById('content_side').childNodes;
		var boxcounter = 0;
		var heightsum = 0;
		var arrBoxes = new Array();
		var arrHeights = new Array();
		if ((arrElms) && (arrElms.length > 0)) {
			for (var i = 0; i < arrElms.length; i++) {
				if ((arrElms[i].className) && (arrElms[i].className.match(/sidebox_(red|grey) h[0-9]+/))) {
					var arrBits = arrElms[i].className.toString().split(" ");
					var boxheight = arrBits[1].replace(/h/, "");
					boxheight = parseInt(boxheight);
					heightsum += boxheight;
					arrBoxes[boxcounter] = arrElms[i];
					arrHeights[boxcounter] = boxheight;
					boxcounter ++;
				}
			}
			if (boxcounter == 2) {
				arrBoxes[boxcounter-1].style.height = arrHeights[boxcounter-1] - 1 + "px";
			}
		}
	}
}

// google maps
var map;
var icon;
var newpoints = new Array();
var coordinates_array = new Array();
coordinates_array["london"] = new Array(51.515714, -0.131484);
coordinates_array["bath"] = new Array(51.381444, -2.348681);
coordinates_array["glasgow"] = new Array(55.86069,-4.255194);
coordinates_array["manchester"] = new Array(53.480774, -2.245646);
coordinates_array["southampton"] = new Array(50.909149, -1.417537);
coordinates_array["sunderland"] = new Array(54.9093, -1.373025);
var locationname;
var address_array = new Array();
address_array["london"] = new Array('<b>London Office</b><br />20 Soho Square<br />London<br />W1D 3QW');
address_array["bath"] = new Array('<b>Bath Office</b><br />The Malt House<br />Sydney Buildings<br />Bath<br />BA2 6BZ');
address_array["glasgow"] = new Array('<b>Glasgow Office</b><br />16 Gordon Street<br />Glasgow<br />G1 3PT');
address_array["manchester"] = new Array('<b>Manchester Office</b><br />1 St James\'s Square<br />Manchester<br />M2 6DN');
address_array["southampton"] = new Array('<b>Southampton Office</b><br />Anglo City House<br />2-6 Shirley Road<br />Southampton<br />SO15 3EU');
address_array["sunderland"] = new Array('<b>Sunderland Office</b><br />Quayside House, Wylam Wharf<br />Low Street<br />Sunderland<br />SR1 2AD');

function LoadGooglemap(mapcontainer) {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById(mapcontainer));

		//map.addControl(new GLargeMapControl());
		//map.addControl(new GMapTypeControl());
		
		// find out which contact page we're on and assign location	
		locationname = document.getElementById(mapcontainer).className;
		
		map.setCenter(new GLatLng(coordinates_array[locationname][0], coordinates_array[locationname][1]), 15);
		 
		icon = new GIcon();
		icon.image = "http://www.google.com/mapfiles/marker.png";
		icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
		icon.iconSize = new GSize(20, 34);
		icon.shadowSize = new GSize(37, 34);
		icon.iconAnchor = new GPoint(9, 34);
		icon.infoWindowAnchor = new GPoint(9, 2);
		icon.infoShadowAnchor = new GPoint(18, 25);
		
		addPoints();
	}
}

function addPoints() {
	newpoints[0] = new Array(coordinates_array[locationname][0], coordinates_array[locationname][1], icon, 'office', '<div id="popup">' + address_array[locationname] + '</div>'); 
	for(var i = 0; i < newpoints.length; i++) {
 
		var point = new GLatLng(newpoints[i][0], newpoints[i][1]);
 
		//Add overlay to map 
		var popuphtml = newpoints[i][4] ;
		var marker = createMarker(point,newpoints[i][2],popuphtml);
		map.addOverlay(marker);
	}
}

function createMarker(point, icon, popuphtml) {
	var marker = new GMarker(point, icon);
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(popuphtml);
	});
	return marker;
}

// remove a submit button which is only meant for non-JS users
function ClearSubmit(idstring) {
	var id_array = new Array();
	if (idstring.match(/::/)) {
		id_array = idstring.split("::");
	} else {
		id_array[0] = idstring;
	}
	for (var i = 0; i < id_array.length; i++) {
		if (document.getElementById(id_array[i])) {
			document.getElementById(id_array[i]).className = "hidden";
		}
	}
}
