
// Live  ABQIAAAAbl0AX6tQAtTm3OTYVu98ORTAfqWOh0Vqbk88vcSyq1IxvKe_UxQBUHLrmH1WoqZKYsJvH-K5LnJubA
// Local ABQIAAAAbl0AX6tQAtTm3OTYVu98ORS2NPAWjOf3kO8FeJ6Kcmvki-ZnGhRVAW2Wvcwq8t8fdHB1cvTEjF_NRg

google.load("maps", "2");
google.load("jquery", "1.3.2");

var map, sanFrancisco, london, singapore, t;
var count = 0;
var views = [];
var zoomLevel = 6;
var cycleSpeed = 10000;

var continents = new Array();
continents['sanFrancisco'] = "offices-us";
continents['london'] = "offices-european";
continents['singapore'] = "offices-asia";


function initialize() {
	map = new google.maps.Map2(document.getElementById("map"));
	sanFrancisco	= new google.maps.LatLng(37.791023,-122.392867);
	london			= new google.maps.LatLng(51.508429,-0.140612);
	singapore		= new google.maps.LatLng(1.292726,103.860433);
	var center		= new google.maps.LatLng(51.13067673775053, 0.27431488037109375);

	//map.addControl(new GSmallMapControl()); // Zoom etc.
	map.addControl(new GMapTypeControl());  // Map/Satellite/Hybrid
	map.setCenter(singapore, zoomLevel);	// Initial map view

	var logo = '<img src="../images/wbr_logo.gif" alt="" />';

	// Markers
	var markerUS = new GMarker(sanFrancisco);
	var USdetails = logo + "<br><small>San Francisco</small>";
	map.addOverlay(markerUS);
	// markerUS.openInfoWindowHtml(USdetails);
	
	var markerUK = new GMarker(london);
	var UKdetails = logo + "<br><small>London</small>";
	map.addOverlay(markerUK);
	// markerUK.openInfoWindowHtml(UKdetails);
	
	var markerAsia = new GMarker(singapore);
	var Asiadetails = logo + "<br><small>Singapore</small>";
	map.addOverlay(markerAsia);
	// markerAsia.openInfoWindowHtml(Asiadetails);

	// Make the marker clickable
	/*GEvent.addListener(markerUS, "click", function() {
		markerUS.openInfoWindowHtml(USdetails);
	});
	GEvent.addListener(markerUK, "click", function() {
		markerUK.openInfoWindowHtml(UKdetails);
	});
	GEvent.addListener(markerAsia, "click", function() {
		markerAsia.openInfoWindowHtml(Asiadetails);
	});*/


	GEvent.addListener(map, "click", function() {
	  clearInterval(t);
	});
	
	
	/* Movable markers 
	GEvent.addListener(map, "dragstart", function() {
		document.getElementById("message").innerHTML = map.getCenter();
	});*/
	
	/*var marker = new GMarker(center, {draggable: true});

	GEvent.addListener(marker, "dragstart", function() {
	  map.closeInfoWindow();
	});

	GEvent.addListener(marker, "dragend", function() {
		//marker.openInfoWindowHtml("Just bouncing along..." + marker.getLatLng());
		document.getElementById("message").innerHTML = marker.getLatLng();
	});

	map.addOverlay(marker);*/
	

	$(document).ready(function(){
		
		$(".view a").click(function(){
			clearInterval(t);
			
			//var office = $(this).parents(".vcard").attr("id"); // This fails on IE & Safari because the DOM is broken by Menu Machine which prevents traversing (e.g. parent() or parents())
			var office = $(this).attr("href");
			var continent = $(this).attr("class");
			
			selectOffice(office.substr(1));
			return false;
		});	
		
		$(".view").each(function(){
			views.push( $(this).parents(".vcard").attr("id") );
		});
	
		$("#offices-asia").addClass("continentOn");
		
		t = setInterval("cycleOffices()", cycleSpeed);
	
	});

}


var selectOffice = function(strOffice) {
	
	var strContinent = continents[strOffice];
	
	$(".continent").removeAttr("style").removeClass("continentOn");

	$("#"+strContinent).addClass("continentOn").jFade({
		property: 'background',
		start: 'ffffff',
		end: 'f1f1f1',
		steps: 25,
		duration: 30
		}, function(){
			$(this).addClass("continentOn");
		}
	);
	
	// Centre Google map on chosen office
	eval("map.setCenter("+strOffice+", zoomLevel)");
}


var cycleOffices = function() {
	if (count==views.length) count = 0;
	selectOffice(views[count]);
	count++;
}


google.setOnLoadCallback(initialize);
