// copyright 2008 Damien Watson, damien.watson@gmail.com

var map;
var gmarkers = [];
var idx = 0;
var speed = 0;
var polylines = [];
var poppy_markers = [];
var yellow_markers = [];
var geocoder = new GClientGeocoder();
var base = "http://www.ibistrilogy.com";

// points of interest
var map_start = {"x": "1.0546279422758869", "y": "83.14453125", "zoom": 3}
var route_start = {"x": "-36.17335693522159", "y": "19.86328125", "zoom": 4}

// icons
var ibis_marker = new GIcon();
ibis_marker.image = base + "/skins/main/images/boat-right-24px-32bit.png";
ibis_marker.size = new GSize(24,24);
ibis_marker.iconAnchor = new GPoint(12,12);
ibis_marker.infoWindowAnchor = new GPoint(10,10);

var opt
opt = {}
opt.icon = ibis_marker;
opt.draggable = false;
opt.clickable = false;
opt.dragCrossMove = true;

var poppy_icon = new GIcon();
poppy_icon.image = base + "/skins/main/images/poppy-16.png";
poppy_icon.iconSize = new GSize(16, 16);
poppy_icon.iconAnchor = new GPoint(8, 8);
poppy_icon.infoWindowAnchor = new GPoint(8, 8);

var yellow_icon = new GIcon();
yellow_icon.image = base + "/skins/main/images/poppy-16-yellow.png";
yellow_icon.iconSize = new GSize(16, 16);
yellow_icon.iconAnchor = new GPoint(8, 8);
yellow_icon.infoWindowAnchor = new GPoint(8, 8);

// functions
function report(msg){

	if(xGetElementById('messages')){
		xGetElementById('messages').innerHTML = '<span>' + msg + '</span>';
	}else{
		alert(msg);
	}

}

function showAddress(address) {
	geocoder.getLatLng(
		address,
		function(point) {

			if(point){

				var EventIcon = new GIcon(yellow_icon);
				markerOptions = { icon:EventIcon, draggable:true };
				var marker = new GMarker(point, markerOptions);
				GEvent.addListener(marker, "click", function() {
						marker.openInfoWindowHtml('<div class="infoContainer">Please drag this marker to adjust your location, or enter your details and message in the form provided.</div>');
					});

				GEvent.addListener(marker, "dragstart", function() {
					map.closeInfoWindow();
				});

				GEvent.addListener(marker, "dragend", function() {
					document.getElementById('latitude').value = point.y;
					document.getElementById('longitude').value = point.x;
					document.getElementById('messages').innerHTML = 'Location updated.';
				});
				map.setCenter(point, 13);
				map.addOverlay(marker);

				document.getElementById('latitude').value = point.y;
				document.getElementById('longitude').value = point.x;

				xRemoveClass(document.getElementById('locationStatus'), 'error');
				document.getElementById('locationStatus').innerHTML = ' - Found';


			}else{
				document.getElementById('latitude').value = '';
				document.getElementById('longitude').value = '';

				xAddClass(document.getElementById('locationStatus'), 'error');
				document.getElementById('locationStatus').innerHTML = ' - Not Found';

			}
		}
	);
}

function createMarker(point, title, message, marker_type){

	if(marker_type == 'poppy_icon'){
		var EventIcon = new GIcon(poppy_icon);
	}else{
		var EventIcon = new GIcon(yellow_icon);
	}
	var msg = '<div class="infoContainer"><div class="title">' + title + '</div><div class="content">' + message + '</div><div>';

	markerOptions = { icon:EventIcon };
	var marker = new GMarker(point, markerOptions);
	GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml(msg);
		});
	return marker;

/*
	var tabs_array = [
				new GInfoWindowTab('Location', 'Location Name would go here'),
				new GInfoWindowTab('Info', '<div class="infoContainer">' + message + '<div>'),
				new GInfoWindowTab('Story', 'Storey Snippet would go here')
			];

	GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowTabsHtml(tabs_array);
		});
	return marker;*/

}

function clear_route(){

	try{

		for(i = 0; i < polylines.length; i++){
			map.removeOverlay(polylines[i]);
		}

	}catch(e){ }

}

function doStep(longitude, latitude, olongitude, olatitude, time, mode) {

	try{

		setTimeout( function() {

			var polyline = new GPolyline([
					new GLatLng( olatitude, olongitude),
					new GLatLng(latitude, longitude)
				], "#ff0000", 5);

			polylines[idx] = polyline;
			map.addOverlay(polylines[idx]);

			idx = idx + 1;

			if(mode == 'follow'){
				var marker = new GMarker(new GPoint(longitude, latitude), opt);
				map.addOverlay(marker);
				gmarkers[idx] = marker ;
				if ( idx > 1) map.removeOverlay(gmarkers[idx-1]);
				map.panTo(new GLatLng(latitude, longitude));
			}

		}, 1500 * time); // change this to change speed

	}catch(e){ }
}

function follow_route(){

	try{

		clear_route();

		map.setCenter(new GLatLng(route_start.x, route_start.y), route_start.zoom, G_HYBRID_MAP);

		//initialize variables
		olongitude = points[0].longitude;
		olatitude = points[0].latitude;

		// This is where we loop through the points.
		for (i in points){
			speed += 1;
			longitude = points[i].longitude;
			latitude = points[i].latitude;
			doStep(longitude, latitude, olongitude, olatitude, speed, 'follow');
			olongitude = longitude;
			olatitude = latitude;
		}

	}catch(e){ }

}

function toggle_markers(marker_type){

	if(marker_type == 'route'){

		try{

			for (i in poppy_markers) {

				if (poppy_markers[i].isHidden()) {
					poppy_markers[i].show();
				}else{
					poppy_markers[i].hide();
				}
			}

		}catch(e){ }

	}else{

		try{

			for (i in yellow_markers) {

				if (yellow_markers[i].isHidden()) {
					yellow_markers[i].show();
				}else{
					yellow_markers[i].hide();
				}
			}

		}catch(e){ }
	}
}

function show_route(){

	try{

		clear_route();
		map.setCenter(new GLatLng(route_start.x, route_start.y), route_start.zoom, G_HYBRID_MAP);

		olongitude = points[0].longitude;
		olatitude = points[0].latitude;

		for (i in points){
			speed = 1;
			longitude = points[i].longitude;
			latitude = points[i].latitude;
			doStep(longitude, latitude, olongitude, olatitude, speed, 'show');
			olongitude = longitude;
			olatitude = latitude;
		}

	}catch(e){ }
}