var map;
var mapi;
function load() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		//map.setMapType(G_SATELLITE_MAP);
		
		if(defaultLatitude) {
			var center = new GLatLng(defaultLatitude, defaultLongitude);
		} else {
			var center = new GLatLng(52.072462, 5.120627);
		}
		map.setCenter(center, 17);
		map.enableContinuousZoom();
		map.enableScrollWheelZoom();
		
		var blueIcon = new GIcon();
		blueIcon.iconSize = new GSize(28, 28);
		blueIcon.iconAnchor = new GPoint(13, 18);
		blueIcon.infoWindowAnchor = new GPoint(0, 0);
		markerOptions = { icon:blueIcon };
		for(i = 0 ; i < info.length ; i++) {
			blueIcon.image = info[i][5];
			var point = new GLatLng(info[i][0], info[i][1]);
			map.addOverlay(createMarker(point, info[i][2], i));
		}
		
	}
	
	function createMarker(point, msg, num) {
		var marker = new GMarker(point, markerOptions);
		GEvent.addListener(marker, "click", function() {
			if(map.getZoom() < 14) {
			map.setZoom(14);
		}
		
			a = new Array();
			a["maxWidth"] = 450;
			var myHtml = msg;
			map.openInfoWindowHtml(point, myHtml, a);
			
		});
		return marker;
	}
	gdir = new GDirections(map, document.getElementById("directions"));
	
}
function zoomMap() {
	if(map.getZoom() > 1) {
		map.setZoom(1);
		map.closeInfoWindow();
		
		var c = new GLatLng(30, 30);
		map.panTo(c);
	}
	
}
function moveMap(who) {
	var c = new GLatLng(info[who][0], info[who][1]);
	if(map.getZoom() < 14) {
		map.setZoom(14);
	}
	map.panTo(c);
	a = new Array();
	a["maxWidth"] = 450;
	msg = info[who][2];
	map.openInfoWindowHtml(c, msg, a);

}

function setDirections(fromAddress1, fromAddress2, toAddress, locale) {
	map.clearOverlays();
	if(fromAddress1 && fromAddress2) {
		gdir.load("from: " + fromAddress1 + ", " + fromAddress2	+ " to: " + map.getCenter(), { "locale": locale });
	} else {
		gdir.load("from: " + fromAddress1 + " " + fromAddress2	+ " to: " + map.getCenter(), { "locale": locale });
	}
}
function showInfo(num) {
	
	
}

var opListCurrent = null;
var index = -1;

function createOperation (type, data)
{
	alert( "2");
	var newObject = new Object ();

	newObject.type = type;
	newObject.data = data;

	return newObject;
}

function populateListToParis ()
{
	opListToParis = new Array ();
	opListToParis.push (createOperation ("zoomout", 2));
	opListToParis.push (createOperation ("pan", new GLatLng(48.856667, 2.350833)));
	opListToParis.push (createOperation ("zoomin", 10));
}

function populateListToShanghai ()
{
	alert(1);
	opListToShanghai = new Array ();
	opListToShanghai.push (createOperation ("zoomout", 2));
	opListToShanghai.push (createOperation ("pan", new GLatLng(31.2, 121.5)));
	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 + ")", 500);
				return;
			}
			break;
		case "zoomin":
			if (map.getZoom () < opListCurrent[index].data) 
			{
				if (zoomInTimeout == -1) // no callbacks in flight
					zoomInTimeout = setTimeout ("handleZoomIn (" + index + ")", 500);
				return;
			}
			break;
		case "pan":
			if (map.getCenter ().distanceFrom (opListCurrent[index].data) > 30000)
			{
				if (panTimeout == -1) // no callbacks in flight
					panTimeout = setTimeout ("handlePan (" + index + ")", 500);
				return;
			}
			break;
		default:
			break;
	}

	//document.getElementById("log").innerHTML += "<p>step " + index + "</p>";
	index = index + 1;
}

function handleToParis ()
{
	index = 0;
	opListCurrent = opListToParis;
	handleZoomEnd ();
}

function handleToShanghai ()
{
	index = 0;
	opListCurrent = opListToShanghai;
	handleZoomEnd ();
}

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 ();
}

