/* global LD2h, rdf */

$(function () {
    var prefix = "http://example.org/";
    var graphname = "http://zazuko.com/projects";
    var store = new rdf.SparqlStore({
        endpointUrl: $("#sparqlQuery").val(),
        updateUrl: $("#sparqlUpdate").val() //shouldn't be neede
    });

    LD2h.getDataGraph = function (callback) {
        store.graph(graphname, callback); //Caveat: with newer rdf-ext version the callback takes (uri,g)
    };
    $("#setEndpoints").click(function (e) {
        $("#endpointsForm").hide();
        $("#endpointsLink").show();
    });
    $("#showEndpointsConfig").click(function (e) {
        $("#endpointsLink").hide();
        $("#endpointsForm").show();
    });
    var sparqlUpdate = function(update) {
        jQuery.post($("#sparqlUpdate").val(), {update: update})
                .done(function () {
                    console.log("successfully updated");
                    window.location.reload();
                })
                .fail(function (e) {
                    console.log("error with query " + update, e);
                    alert("error (see console for details)");
                });
    }
    $("#createProject").on("click", function () {
        var projectTitle = $("#projectTitle").val();
        if (projectTitle.length < 4) {
            alert("Project title too short.");
            return;
        }
        var shortName = $("#projectName").val();
        if (shortName.length < 3) {
            alert("Project short name must not be shorter than 3 characters.");
            return;
        }
        if (shortName.indexOf(' ') > -1) {
            alert("Project short name must not contain spaces.");
            return;
        }
        var projectHours = $("#projectHours").val();
        if (!projectHours) {
            projectHours = 0;
        }
        var projectIRI = prefix + "project/"+shortName;
        var update = "PREFIX dct: <http://purl.org/dc/terms/>\n" +
                "PREFIX pro: <http://schema.zazukoians.org/projects/>\n" +
                "PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
                "INSERT DATA\n" +
                "{ GRAPH <" + graphname + ">\n" +
                "{ <" + projectIRI + ">  rdf:type  pro:Project." +
                "<" + projectIRI + ">  pro:plannedTime  " + projectHours + "." +
                "<" + projectIRI + "> dct:title  \"" + projectTitle + "\" } \n" +
                "}";
        sparqlUpdate(update);
    });
    $("#reportHours").on("click", function () {
        function makeid() {
            var text = "";
            var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

            for( var i=0; i < 7; i++ )
                text += possible.charAt(Math.floor(Math.random() * possible.length));

            return text;
        };
        var entryHours = $("#entryHours").val();
        if (!entryHours) {
            alert("Must set entry hours");
            return;
        }
        var entryIri = prefix+"entry/"+makeid();
        var update = "PREFIX dct: <http://purl.org/dc/terms/>\n" +
                "PREFIX pro: <http://schema.zazukoians.org/projects/>\n" +
                "PREFIX rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n" +
                "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n" +
                "INSERT DATA\n" +
                "{ GRAPH <" + graphname + ">\n" +
                "{ <" + entryIri + ">  rdf:type  pro:Entry.\n" +
                "<" + entryIri + ">  pro:spentTime  " + entryHours + ".\n" +
                "<" + entryIri + ">  dct:description  \"" + $("#entryDescription").val() + "\" .\n" +
                "<" + entryIri + ">  pro:project  <" + $("#entryProject").val() + "> .\n" +
                "<" + entryIri + "> pro:entryDate  \"" + $("#entryDate").val() + "\"^^xsd:date } \n" +
                "}";
        sparqlUpdate(update);
    });
    LD2h.expand();
});
@@ -3905,24 +3905,768 @@
 e);%0A    %7D);%0A
+    $(%22#createPerson%22).on(%22click%22, function () %7B%0A          var update = %22PREFIX schema: %3Chttp://schema.org/%3E%5C%0A                PREFIX foaf: %3Chttp://xmlns.com/foaf/0.1/%3E%5C%0A                INSERT DATA%5C%0A                %7B GRAPH %3Chttp://zazuko.com/projects%3E%5C%0A                 %7B%5C%0A                    %3Chttp://data.zazuko.com/employees/%22+ $(%22#nick%22).val() + %22%3E a schema:Person, foaf:Person;%5C%0A                      foaf:nick %5C%22%22+ $(%22#nick%22).val() + %22%5C%22 ;%5C%0A                      schema:givenName %5C%22%22+ $(%22#givenName%22).val() + %22%5C%22 ;%5C%0A                      schema:familyName %5C%22%22+ $(%22#familyName%22).val() + %22%5C%22 ;%5C%0A                      schema:email %5C%22%22+ $(%22#email%22).val() + %22%5C%22 .%5C%0A                  %7D %5C%0A                %7D%22;%0A        sparqlUpdate(update);%0A    %7D);%0A
     LD2h.exp

INSERT done for Person