// js for place listing

// updates the place list for the given parseid
function updatePlaceList(parseid) {
    placeList = $('ol#place-list')
    $.get('/itinerary/' + parseid,
        function(data) {
            console.log("received list of places:");
            console.log(data);
            var places = data['itinerary'];
            for(i in places) {
                console.log(places[i].name);
                placeList.append("<li data-json='"+JSON.stringify(places[i])+"' class='place-item'>" + places[i].name + '<button class="delete-button" onclick="removePlace(' + parseInt(i) + ')">X</button></li>');
                addPlaceToMap(places[i], (parseInt(i)+1), true);
            }
        });
}

// remove a place from the itinerary
function removePlace(num) {
    // find itinerary id
    var url = document.URL;
    if (url.indexOf("/id/") > -1) {
        url = url.split('/');
        var daId = url[4];
    }
    // build itinerary
    var itin = {"itinerary" : []};
    $("ol#place-list").each(function( index ) {
        $(this).find("li").each(function(){
            console.log($(this).data("json"));
            itin.itinerary.push($(this).data("json"));
        });
    });
    // remove place from itinerary
    console.log(itin);
    itin['itinerary'].splice(num, 1);
    console.log(itin);

}


$(document).ready(function() {

    var url = document.URL;
    if (url.indexOf("/id/") > -1) {
        url = url.split('/');
        var daId = url[4];
        updatePlaceList(daId);
    }
    $("ol#place-list").sortable({
        opacity: 0.6,
        cursor: 'move',
        // save itinerary when user reorders list
        update: function( event, ui ) {
            var itin = {"itinerary" : []};
            $("ol#place-list").each(function( index ) {
                $(this).find("li").each(function(){
                    console.log($(this).data("json"));
                    itin.itinerary.push($(this).data("json"));
                });
            });
            console.log(itin);
            if (daId !== undefined) {
                $.post("/itinerary/" + daId, JSON.stringify(itin), function(data) {
                    console.log("Saved new itinerary order");
                    console.log(data);
                }, "json");
                // redraw markers
                console.log("refreshed placesLayer...")
                placesLayer.clearLayers();
                var places = itin['itinerary'];
                for (i in places) {
                  addPlaceToMap(places[i], (parseInt(i)+1), false);
                }
                //map.fitBounds(placesLayer.getBounds());
            }
        }
    });
});

@@ -1023,32 +1023,62 @@
 tion( index ) %7B%0A
+        //console.log(index);%0A
         $(this).
@@ -1094,35 +1094,66 @@
 ).each(function(
+i
 )%7B%0A
+            //console.log(i);%0A
             cons
@@ -1230,32 +1230,330 @@
 .data(%22json%22));%0A
+            if (i === num) %7B%0A                $(this).remove()%0A            %7D %0A            else if (i %3E num) %7B%0A                //$(this).click(removePlace(parseInt(i)-1));%0A                $('ol#place-list').on(%22click%22, %22button.delete-button%22, function()%7BremovePlace(parseInt(i)-1);%7D);%0A            %7D %0A
         %7D);%0A    
@@ -1591,39 +1591,16 @@
 inerary%0A
-    console.log(itin);%0A
     itin
@@ -1629,16 +1629,16 @@
 um, 1);%0A
-
     cons
@@ -1651,16 +1651,530 @@
 g(itin);
+%0A    if (daId !== undefined) %7B%0A        $.post(%22/itinerary/%22 + daId, JSON.stringify(itin), function(data) %7B%0A            console.log(%22Saved new itinerary order%22);%0A            console.log(data);%0A        %7D, %22json%22);%0A        // redraw markers%0A        console.log(%22refreshed placesLayer...%22)%0A        placesLayer.clearLayers();%0A        var places = itin%5B'itinerary'%5D;%0A        for (i in places) %7B%0A          addPlaceToMap(places%5Bi%5D, (parseInt(i)+1), false);%0A        %7D%0A        //map.fitBounds(placesLayer.getBounds());%0A    %7D
 %0A%0A%7D%0A%0A%0A$(

Add functional delete buttons