var map = null;
var opListCurrent = null;
var index = -1;

function createOperation (type, data)
{
	var newObject = new Object ();

	newObject.type = type;
	newObject.data = data;

	return newObject;
}

function populateListToParis (lat, lng)
{

	opListToParis = new Array ();
	//opListToParis.push (createOperation ("zoomout", 2));
	opListToParis.push (createOperation ("pan", new GLatLng(parseFloat(lat), parseFloat(lng))));
	opListToParis.push (createOperation ("zoomin", 8));
}

function populateListToShanghai ()
{
	opListToShanghai = new Array ();
	opListToShanghai.push (createOperation ("zoomout", 2));
	opListToShanghai.push (createOperation ("pan", new GLatLng(48, -74)));
	opListToShanghai.push (createOperation ("zoomin", 10));
}

var panTimeout = -1;
var zoomInTimeout = -1;
var zoomOutTimeout = -1;

function handlePan (i)
{
	map.panTo (opListCurrent[i].data);
	panTimeout = -1;
	setTimeout ("handleZoomEnd()", 1); // incase we don't get the event
}

function handleZoomOut (i)
{
	map.zoomOut (map.getCenter (), true);
	zoomOutTimeout = -1;
	setTimeout ("handleZoomEnd()", 1); // incase we don't get the event
}

function handleZoomIn (i)
{
	map.zoomIn (map.getCenter (), true);
	zoomInTimeout = -1;
	setTimeout ("handleZoomEnd()", 1); // incase we don't get the event
}

function handleOp ()
{
	if (opListCurrent == null || index < 0 || index >= opListCurrent.length)
	{
		index = -1;
		return;
	}

	switch (opListCurrent[index].type)
	{
		case "zoomout":
			if (map.getZoom () > opListCurrent[index].data) 
			{
				if (zoomOutTimeout == -1) // no callbacks in flight
					zoomOutTimeout = setTimeout ("handleZoomOut (" + index + ")", 300);
				return;
			}
			break;
		case "zoomin":
			if (map.getZoom () < opListCurrent[index].data) 
			{
				if (zoomInTimeout == -1) // no callbacks in flight
					zoomInTimeout = setTimeout ("handleZoomIn (" + index + ")", 300);
				return;
			}
			break;
		case "pan":
			if (map.getCenter ().distanceFrom (opListCurrent[index].data) > 30000)
			{
				if (panTimeout == -1) // no callbacks in flight
					panTimeout = setTimeout ("handlePan (" + index + ")", 300);
				return;
			}
			break;
		default:
			break;
	}

	//document.getElementById("log").innerHTML += "<p>step " + index + "</p>";
	index = index + 1;
}

function handleToParis ()
{
	index = 0;
	opListCurrent = opListToParis;
	handleZoomEnd ();
}



function initialize(lat, lng) {

;

	if (GBrowserIsCompatible()) 
	{
		map = new GMap2(document.getElementById("map_canvas"));
		map.enableContinuousZoom ();
		map.setCenter(new GLatLng(52.9399159, -73.5491361), 4);
		map.setUIToDefault();

		populateListToShanghai ();
		populateListToParis (lat, lng);
		
		GEvent.addListener(map, "zoomend", handleZoomEnd);
		GEvent.addListener(map, "moveend", handleMoveEnd); // Triggers marker swap, Esa
	}
}

function handleZoomEnd ()
{
	//document.getElementById("log").innerHTML += "<p>handleZoomEnd " + index + "</p>";
	if (index >= 0)
		handleOp ();
}

function handleMoveEnd ()
{
	//document.getElementById("log").innerHTML += "<p>handleMoveEnd " + index + "</p>";
	if (index >= 0)
		handleOp ();
}


function handleToZoom() {


}

function createMarker(officeArray) {

	var office = new GLatLng(officeArray['lat'], officeArray['lng']); 
	var offmarker = new GMarker(office);
	map.addOverlay(offmarker);

	if(officeArray['siteurl'] != "") {
		var offInfo = "<Div id='officeInfo' style='height: 180px; overflow: auto'><a href='http://" + officeArray['siteurl'] + "' target='_blank' style='color: #000099; font-size: 12pt;'>" + 
				officeArray['name'] + "</a><br><br><br> " +
				officeArray['address1'] + "<br>" +
				officeArray['address2'] + "<br>";
	} else {
	
		var offInfo = "<Div id='officeInfo' style='height: 180px; overflow: auto'>" + 
					"<p style='color: #000099; font-size: 12pt;'>" + officeArray['name'] + "</p><br><br> " +
					officeArray['address1'] + "<br>" +
				officeArray['address2'] + "<br>";
	}
	
	if(officeArray['telephone']) {
		offInfo = offInfo + "<b>Tél : </b>" + officeArray['telephone'] + "<br>";
	}
	
	if(officeArray['courriel']) {
		offInfo = offInfo + "<br>" + "<b>Courriel : </b> <a href='mailto:" + officeArray['courriel'] + "'>" + officeArray['courriel'] + "</a><br>";
	}
			
	if(officeArray['secteur']) {
		offInfo = offInfo + "<br>" +"<b>Secteur : </b> " + officeArray['secteur'] + "<br>";
	}
			
	if(officeArray['offtype']) {
		offInfo =  offInfo + "<br>" +"<b>Type : </b> " + officeArray['offtype'] + "<br>";
	}
			
	if(officeArray['produits']) {
		offInfo = offInfo + "<br>" +"<b>Produits : </b> " + officeArray['produits'] + "<br>";
	}
		
	if(officeArray['service']) {
		offInfo = offInfo + "<br>" + "<b>Service : </b> " + officeArray['service'] + "<br>";
	}
			
	if(officeArray['mission']) {
		offInfo = offInfo + "<br>" + "<b>Mission : </b> " + officeArray['mission'] ;
	}
			
			
			
			
			
			
	GEvent.addListener(offmarker, "mouseover", function() {
		offmarker.openInfoWindowTabsHtml(offInfo);
	});

}
