if (typeof HSP == "undefined") HSP = new Object();
if (typeof HSP.index == "undefined") HSP.index = new Object();

HSP.init_index = function ()
{
	console.log("HSP.init_index()");
	
	HSP.map.paddingX = 80;
	HSP.map.paddingY = 20;

	HSP.map.init("place_marker", HSP.map.MAP_1875, 17);
//	HSP.map.init("place_marker", HSP.map.NORMAL, 17);
	
	var center = HSP.map.the_map.getCenter();
	var bounds = HSP.map.the_map.getBounds();
	
	HSP.index.center = new Object;
	HSP.index.center.lat = center.lat();
	HSP.index.center.lng = center.lng();
	
	HSP.index.bounds = new Object();
	HSP.index.bounds.sw = new Object();
	HSP.index.bounds.ne = new Object();

	HSP.index.bounds.sw.lat = bounds.getSouthWest().lat();
	HSP.index.bounds.sw.lng = bounds.getSouthWest().lng();

	HSP.index.bounds.ne.lat = bounds.getNorthEast().lat();
	HSP.index.bounds.ne.lng = bounds.getNorthEast().lng();
	
	// fudge 1 is effectively degrees per pixel at the zoom level, ignoring projection
	// fudge 2 is effectively half the hieght of the map in degrees, ignoring projection

	HSP.index.fudge1 = (HSP.index.bounds.ne.lat - HSP.index.bounds.sw.lat) / $("#map").height();
	HSP.index.fudge2 = (HSP.index.center.lat - HSP.index.bounds.sw.lat);

	HSP.index.start_animation();
}

HSP.index.tid = -1;
HSP.index.i = -1;

HSP.index.t1 = 500;
HSP.index.t2 = 9000;
HSP.index.t3 = 250;

HSP.index.start_animation = function ()
{
	var i;
	
	console.log("HSP.index.start_animation()");
	
	HSP.index.nanimation = HSP.index.animation.length;

	if (HSP.index.nanimation == 0) {
		HSP.index.animation.push(Math.floor(Math.random() * HSP.map.places.length));
	}

	HSP.index.panto_marker();
	
	GEvent.addListener(HSP.map.the_map, "click", HSP.index.stop_animation);
	
	$("#hsp-marker-popup").live("click", HSP.index.stop_animation);
}

HSP.index.stop_animation = function ()
{
	console.log("HSP.index.stop_animation()");
	
	clearTimeout(HSP.index.tid);
	
	HSP.index.tid = -1;
}

HSP.index.panto_marker = function ()
{
	var i = HSP.index.i;
	if (++i >= HSP.index.nanimation) i = 0;
	var j = HSP.index.animation[i];
	
	console.log("HSP.index.panto_marker()", i, j);
	
	var latitude = HSP.map.places[j].latitude + HSP.index.fudge2 - HSP.index.fudge1 * 20;
	var longitude = HSP.map.places[j].longitude;
	
	HSP.map.closeMarkers();
	HSP.map.the_map.panTo(new GLatLng(latitude, longitude));
	
	HSP.index.i = i;
	
	HSP.index.tid = setTimeout(HSP.index.open_marker, HSP.index.t1);
}

HSP.index.open_marker = function ()
{
	var i = HSP.index.i;
	var j = HSP.index.animation[i];

	console.log("HSP.index.open_marker()", i, j);

	if (HSP.map.markers.all[j]) GEvent.trigger(HSP.map.markers.all[j], "click");
	
	if (HSP.index.nanimation > 1) {
		HSP.index.tid = setTimeout(HSP.index.close_marker, HSP.index.t2);
	}
}

HSP.index.close_marker = function ()
{
	var i = HSP.index.i;
	var j = HSP.index.animation[i];
	
	console.log("HSP.index.close_marker()", i, j);

	HSP.map.closeMarkers();
	
	HSP.index.tid = setTimeout(HSP.index.panto_marker, HSP.index.t3);
}