var trips = new Object;

function showHideTrack(id)
{
	if($('#trackShowHide'+id).prop('checked')) {
		showTrack(id)
	} else {
		hideTrack(id)
	}
}

function showTrack(id)
{
	if (typeof (trips[id]) == 'undefined') {
		loadTrack(id)
	} else {
		if (trips[id].status != 'show') {
			trips[id].addTo(myMap)
			trips[id].status = 'show';
		}

		myMap.fitBounds(trips[id].getBounds());
		return true;
	}
}

function hideTrack(id)
{
	if (typeof (trips[id]) == 'undefined') {
		return true;
	} else {
		if (trips[id].status == 'show') {
			myMap.removeLayer(trips[id]);
		}

		trips[id].status = 'hide';
		return true;
	}
}

function loadTrack(id)
{
	colors = ['#ff0000', '#ff8e00', '#ff0', '#0f0', '#003cff', '#ff00ad'];
	colorIndex = 0;
	tracks = new Array();
	markers = new Array();
	$.ajax({
		url: "/json/track/id/" + id,
		success: function (data) {
			trips[id] = L.featureGroup();

			if (typeof (data.tracks) != 'undefined') {
				for (var i = 0; i < data.tracks.length; i++) {
					color = colors[colorIndex];
					colorIndex = (colorIndex == colors.length-1) ? 0 : colorIndex+1;

					tracks[i] = L.polyline(data.tracks[i].latlngs, {color: color, width: 4, opacity: 0.8});
					tracks[i].bindPopup('<b>' + data.tracks[i].name + '</b><br/>' + data.tracks[i].description);
					trips[id].addLayer(tracks[i])
				}
			}

			if (typeof (data.wayPoints) != 'undefined') {
				for (var i = 0; i < data.wayPoints.length; i++) {
					markers[i] = L.marker([data.wayPoints[i]['lat'], data.wayPoints[i]['lng']], {icon: categories.marker, title: data.wayPoints[i]['name']})
					markers[i].bindPopup('<b>' + data.wayPoints[i]['name'] + '</b><br/>' + data.wayPoints[i]['description']);
					trips[id].addLayer(markers[i])
				}
			}

			showTrack(id)
		},
		error: function (data) {
			alert('Не удалось загрузить трек. Возможно это приватный трек и вы не его владелец.')
		}
	});
}

function loadSimpleTrack(id)
{
	colors = ['red', 'green', 'blue', 'orange'];
	colors = ['red'];
	colorIndex = 0;
	tracks = new Array();
	markers = new Array();
	$.ajax({
		url: "/json/simpletrack/id/" + id,
		success: function (data) {
			trips[id] = L.featureGroup();
			console.log(data.totCnt);

			if (typeof (data.tracks) != 'undefined') {
				for (var i = 0; i < data.tracks.length; i++) {
					color = colors[colorIndex];
					colorIndex = (colorIndex == colors.length-1) ? 0 : colorIndex+1;

					tracks[i] = L.polyline(data.tracks[i].latlngs, {color: color, width: 4, opacity: 0.8});
					tracks[i].bindPopup(data.tracks[i].name);
					trips[id].addLayer(tracks[i])
				}
			}

			if (typeof (data.wayPoints) != 'undefined') {
				for (var i = 0; i < data.wayPoints.length; i++) {
					markers[i] = L.marker([data.wayPoints[i]['lat'], data.wayPoints[i]['lng']], {icon: categories.marker, title: data.wayPoints[i]['name']})
					markers[i].bindPopup('<b>' + data.wayPoints[i]['name'] + '</b><br/>' + data.wayPoints[i]['description']);
					trips[id].addLayer(markers[i])
				}
			}

			showTrack(id)
		},
		error: function (data) {
			alert('Не удалось загрузить трек. Возможно это приватный трек и вы не его владелец.')
		}
	});
}


