/*
	per map js
*/

function setActive(elem) {
	mapMessage.style.display = 'none';
	map.enableDoubleClickZoom();
	map.enableScrollWheelZoom();

	$('selector_blogs').removeClassName('active');
	$('selector_photos').removeClassName('active');
	$('selector_videos').removeClassName('active');
	$('selector_' + elem).addClassName('active');
	$('selector_' + elem).blur();
	return true;
}

function importanceOrder (marker,b) {
	return GOverlay.getZIndex(marker.getPoint().lat()) + marker.importance*1000000;
}

function createMarker(point,icon,label,html,importance) {
	var marker = new GMarker(point, {icon:icon, title:label, zIndexProcess:importanceOrder});
	marker.importance = importance;
	GEvent.addListener(marker, "click", function() {
		try {
			marker.openInfoWindowHtml(html);
		}

		catch(e) {}
	});

	return marker;
}

function mediaTypeControl() {
}

mediaTypeControl.prototype = new GControl();

mediaTypeControl.prototype.initialize = function(map) {
	var container = $('control');
	map.getContainer().appendChild(container);
	container.style.display='block';
	return container;
}

mediaTypeControl.prototype.getDefaultPosition = function() {
	return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(167,7));
}

var numberedIcon = new GIcon();
numberedIcon.shadow = "/journal/includes/maps/images/shadow50.png";
numberedIcon.iconSize = new GSize(20, 34);
numberedIcon.shadowSize = new GSize(37, 34);
numberedIcon.iconAnchor = new GPoint(10, 34);
numberedIcon.infoWindowAnchor = new GPoint(20, 10);

function parseComplete(markers, extra) {

	try {
		$('amtc_option_0').firstDescendant().update('Map');
		$('amtc_option_1').firstDescendant().update('Aerial');
	}

	catch(e){}

	map.setCenter(new GLatLng('-46.36', '-4.17'), 7);

	setActive(extra);

	if (extra == 'blogs') {
		var drawLines = true;
	}

	map.clearOverlays();
    var bounds 		= new GLatLngBounds();
	markerArray = new Array();
	var linePoints = [];
	itineraryPoints = [];

	for (var i = 0; i < markers.length; i++) {

		markerArray[i] 	= new Array();

		markerArray[i]['html']		= markers[i].getAttribute("html");
		markerArray[i]['label']		= markers[i].getAttribute("label");
		markerArray[i]['lat']		= parseFloat(markers[i].getAttribute("lat"));
		markerArray[i]['lng']		= parseFloat(markers[i].getAttribute("lng"));
		markerArray[i]['point']		= new GLatLng(markerArray[i]['lat'], markerArray[i]['lng']);
		markerArray[i]['isExact']	= markers[i].getAttribute("exact");
		markerArray[i]['isFuture']	= parseInt(markers[i].getAttribute("future"));
		markerArray[i]['isCurrent']	= parseInt(markers[i].getAttribute("current"));
		markerArray[i]['isCenter']	= markers[i].getAttribute("isCenter");
		markerArray[i]['zoom']		= parseInt(markers[i].getAttribute("zoom"));
		markerArray[i]['markerImg'] = '/journal/includes/phpThumb/phpThumb.php?src=' + markers[i].getAttribute("img") + '&w=30&h=30&zc=1&fltr[]=usm&fltr[]=bord|1|0|0|FFFFFF';

		icon = new GIcon();
		icon.image = markerArray[i]['markerImg'];
		icon.iconSize	= new GSize(30,30);
		icon.iconAnchor = new GPoint(15,15);
		icon.infoWindowAnchor = new GPoint(30,0);
		icon.shadow = '';

		markerArray[i]['icon'] = icon;

		bounds.extend(markerArray[i]['point']);

		if (drawLines) {

			markerArray[i]['icon'] = numberedIcon;

			linePoints = new Array();

			if (prevLat && prevLng) {
				linePoints.push(markerArray[i]['point']);
				linePoints.push(new GLatLng(prevLat,prevLng));

                if (markerArray[i-1]['isFuture'] == 1) {
					itineraryPoints.push(markerArray[i]['point']);
				}

				else {
		 			map.addOverlay(new GPolyline(linePoints, null, 3, '0.5', {geodesic:true,clickable:false}));
				}
			}

			var prevLat = markerArray[i]['lat'];
			var prevLng = markerArray[i]['lng'];



         	var markerNo = markers.length - i;

			if (markerArray[i]['isExact'] == 0 && markerArray[i]['isCurrent']) {
				numberedIcon.image = "/journal/includes/maps/images/nos/curr_" + markerNo + ".png";
				markerArray[i]['importance'] = 100;
				markerArray[i]['label'] = 'Current location: \n' + markerArray[i]['label'];
			}

			else if (markerArray[i]['isExact'] == 1 && markerArray[i]['isCurrent']) {
				numberedIcon.image = "/journal/includes/maps/images/nos/curr_" + markerNo + ".png";
				markerArray[i]['importance'] = 100;
				markerArray[i]['label'] = 'Current location: \n' + markerArray[i]['label'];
			}

			else if (markerArray[i]['isExact'] == 0) {
				numberedIcon.image = "/journal/includes/maps/images/nos/visited_" + markerNo + ".png";
				markerArray[i]['importance'] = 50;
			}

			else if (markerArray[i]['isFuture'] == 1) {
				numberedIcon.image = "/journal/includes/maps/images/nos/fut_" + markerNo + ".png";
				markerArray[i]['importance'] = 25;
			}

			else {
				markerArray[i]['importance'] = 50;
				numberedIcon.image = "/journal/includes/maps/images/nos/visited_" + markerNo + ".png";
			}
		}

		marker = createMarker(markerArray[i]['point'], markerArray[i]['icon'], markerArray[i]['label'], markerArray[i]['html'], markerArray[i]['importance']);
		map.addOverlay(marker);

		if (markerArray[i]['isCurrent']) {
			currentMarker = marker;
		}
	}

	if (markerArray[0] && markerArray[0]['isFuture'] == 1) {
		itineraryPoints.reverse();
		itineraryPoints.push(markerArray[0]['point']);
		itineraryPoints.reverse();
		itineraryLine = new BDCCPolyline(itineraryPoints,'#000000',2,0.2,null,"dash",{geodesic:true,clickable:false});
		showItinerary = true;
		checkItinerary = setInterval('drawItinerary()',100);
	}

	//set centre & zoom according to bounds
	var centreLat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) / 2;
	var centreLng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) / 2;
	map.setCenter(new GLatLng(centreLat, centreLng));

	if (markers.length > 1) {
		map.setZoom(map.getBoundsZoomLevel(bounds));
	}

	else if (markers.length == 0) {
		map.setCenter(new GLatLng(0, 0), 2);
		map.setMapType(G_PHYSICAL_MAP);
		mapMessage.innerHTML = mapMessageStr[extra];
		mapMessage.style.display = 'block';
		map.disableDoubleClickZoom();
		map.disableScrollWheelZoom();
	}

	else {
		map.setZoom(5);
	}

}

if (GBrowserIsCompatible()) {
	var map = new GMap2(document.getElementById("map"));

	setGUnload();
}