$(document).ready(function() {
	$("#trip_destinations .trip_destination:last,#previous_trips tr.trip:last").css("border-bottom", "medium none");

	$("#feature_trip").click(function(e) {
		e.preventDefault();
		$("#FeaturedTripAddForm").submit();
	});

	$("#trip_picker").change(function() {

		$.get(
			'/trips/ajaxGetTripFeed',
			{
				trip: $(this).val()
			},
			function(plots, success) {
				$("#trip_route .content .feed_wrap").slideUp(1000, function() {
					map.clearOverlays();
					$("#tabs").html("");

					var bounds = new GLatLngBounds();

					var latlngs = new Array();
					var subtrip_latlngs = new Array();
					var objDepartureLatLng = null;
					var objFirstLatLng = null;
					for(id in plots) {
						var plot = plots[id];

						if(__getClass(plot) == 'Object') {
							var latlng = new GLatLng(plot['lat'], plot['lng']);

							if(objDepartureLatLng != null) {
								bounds.extend(latlng);
							}

							if(plot.parent != null) {
								if(__getClass(subtrip_latlngs[plot.parent]) != 'Array') {
									subtrip_latlngs[plot.parent] = new Array();
								}

								subtrip_latlngs[plot.parent].push(latlng);
							} else {
								if(objDepartureLatLng == null) {
									objDepartureLatLng = latlng;
								} else {
									if(objFirstLatLng == null) {
										objFirstLatLng = latlng;
									}

									latlngs.push(latlng);
								}
							}

							var scoopIcon = new ScoopIcon(latlng, plot['trip_destination_id'], plot['hasBlog'], plot['hasPhotoAlbum'], plot['hasVideo']);

							$("#tabs").append(plot['tabs']['destination']);
							$("#tabs").append(plot['tabs']['blog']);
							$("#tabs").append(plot['tabs']['photo']);
							$("#tabs").append(plot['tabs']['video']);

							map.addOverlay(scoopIcon);
						}
					}

					// plot departure destination latlng in a different colour
					map.addOverlay(new GPolyline([objDepartureLatLng, objFirstLatLng], '#222250'));

					// round sub-trips to include parent destination at start and end, and display
					for(i in subtrip_latlngs) {
						if(typeof(subtrip_latlngs[i]) != 'undefined') {
							var subtrip = subtrip_latlngs[i];
							var rounded_subtrip = new Array();
							rounded_subtrip.push(new GLatLng(plots[i]['lat'], plots[i]['lng']));
							for(j in subtrip) {
								if(__getClass(subtrip[j]) == 'Object') {
									rounded_subtrip.push(subtrip[j]);
								}
							}
							rounded_subtrip.push(new GLatLng(plots[i]['lat'], plots[i]['lng']));
							map.addOverlay(new GPolyline(rounded_subtrip, '#6b94b6'));
						}
					}

					// plot other destinations
					map.addOverlay(new GPolyline(latlngs));
					map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));

					$("#trip_route .content .feed_wrap").remove();
					$("#trip_route .content").append(plots['feed']);
					$("#trip_route .header .title").html(plots['link'])
				});
			},
			'json'
		);
	});
});
