<!-- 'Layer dependencies modal' -->
    <div id="dependencies_modal" class="modal hide fade" tabindex="-1" role="dialog" aria-hidden="true">
        <form id="dependencies_modal_form">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
            <h3><span id="title"></span> dependencies</h3>
        </div>
        <div class="modal-body">
            <p id="body-text"> <strong id="layer-name"></strong> depends on some layers that are not added to your project. Select the ones you want to add:</p>
            <ul class="unstyled" id="dependencies_list">
            </ul>
        </div>
        <div class="modal-footer">
            <button class="btn btn-primary" type="submit">Add layers</button>
            <button class="btn" type="reset" data-dismiss="modal">Cancel</button>
        </div>
        </form>
    </div>

<script>
  /* projectId: current project
   * layer: Object representing the parent layer { id: .. name: ... url }
   * dependencies: array of dependency layer objects { id: .. name: ..}
   * title: optional override for title
   * body: optional override for body
   * addToProject: Whether to add layers to project on accept
   * successAdd: function to run on success
   */
function show_layer_deps_modal(projectId, layer, dependencies, title, body, addToProject, successAdd) {

    // update layer name
    if (title) {
      $('#dependencies_modal #title').text(title);
    } else {
      $('#dependencies_modal #title').text(layer.name);
    }

    if (body) {
      $("#dependencies_modal #body-text").html(body);
    } else {
      $("#dependencies_modal #layer-name").text(layer.name);
    }

    var deplistHtml = "";
    for (var i = 0; i < dependencies.length; i++) {
      deplistHtml += "<li><label class=\"checkbox\"><input name=\"dependencies\" value=\"";
        deplistHtml += dependencies[i].id;
        deplistHtml +="\" type=\"checkbox\" checked=\"checked\"/>";
        deplistHtml += dependencies[i].name;
        deplistHtml += "</label></li>";
    }
    $('#dependencies_list').html(deplistHtml);

    var selected = [];
    /* -1 is a special dummy Id which we use when the layer isn't yet in the
     * system, normally we would add the current layer to the selection.
     */
    if (layer.id != -1)
      selected.push(layer.id);

    var layer_link_list = "<a href='"+layer.url+"'>"+layer.name+"</a>";

    $("#dependencies_modal_form").submit(function (e) {
        e.preventDefault();
        $("input[name='dependencies']:checked").map(function () { selected.push(parseInt($(this).val()))});
        if (selected.length > 1) {
            tooltipUpdateText = "" + selected.length + " layers added";
        } else {
            tooltipUpdateText = "1 layer added";
        }

        for (var i = 0; i < selected.length; i++) {
            for (var j = 0; j < dependencies.length; j++) {
                if (dependencies[j].id == selected[i]) {
                    layer_link_list+= ", <a href='"+dependencies[j].layerdetailurl+"'>"+dependencies[j].name+"</a>"
                    break;
                }
            }
        }

        $('#dependencies_modal').modal('hide');

        if (addToProject) {
          var editProjectUrl  = "{% url 'xhr_projectedit' project.id %}";
          libtoaster.editProject(editProjectUrl, projectId, { 'layerAdd': selected.join(",") }, function () {
            if (successAdd) {
              successAdd(selected);
            }
          }, function () {
            console.log ("Adding layers to project failed");
          });
        } else {
          successAdd(selected);
        }
    });

    $('#dependencies_modal').modal('show');
}
</script>
