Index: /main/trunk/model-sites-dev/von-sparql/js/von-sparql-script.js
===================================================================
--- /main/trunk/model-sites-dev/von-sparql/js/von-sparql-script.js	(revision 29430)
+++ /main/trunk/model-sites-dev/von-sparql/js/von-sparql-script.js	(revision 29431)
@@ -1,9 +1,15 @@
+//Sparql overview script
+//Query generation code is fairly messy, watch out!
+
+//Creates a new query
+//containerID: 	ID of div to draw results in
+//hardLimit: 	Returned results limit
 function newQuery(containerID, GraphType,hardLimit){
-		var Q = new sgvizler.Query();
-		Q.query(generateQuery(hardLimit))
-			.endpointURL(sparqlEndpoint)
-			.endpointOutputFormat("json")
-			.chartFunction($('#sel_ChartType').val())
-			.draw(containerID);
+	var Q = new sgvizler.Query();
+	Q.query(generateQuery(hardLimit))
+		.endpointURL(sparqlEndpoint)
+		.endpointOutputFormat("json")
+		.chartFunction($('#sel_ChartType').val())
+		.draw(containerID);
 }
 
@@ -16,15 +22,16 @@
 }
 
+//Generates a query based on the predicates etc selected by the user
 function generateQuery(hardLimit){
-     var predicateURL = $('#sel_Predicate').val();
-     var sparqlQuery = "";
+	var predicateURL = $('#sel_Predicate').val();
+	var sparqlQuery = "";
 
 	 //Fix to make queries work when object starts with a '<' or '>'
-	 var speechMarks = "\"";
-	 if($('#sel_RefineObject').val().substring(0,1)=="<") 
+	var speechMarks = "\"";
+	if($('#sel_RefineObject').val().substring(0,1)=="<") 
 		speechMarks="";
     
 	//Refine to specific graph if chosen
-    var refineDataset = "GRAPH gsdl:" + collectionName + " {\n";
+	var refineDataset = "GRAPH gsdl:" + collectionName + " {\n";
 	var select;
 	var extra = "";
@@ -56,31 +63,31 @@
 	}
 
-    sparqlQuery = "PREFIX gsdl: <http://localhost:3030/greenstone/data/> \n";
-    sparqlQuery += "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n";
+	sparqlQuery = "PREFIX gsdl: <http://localhost:3030/greenstone/data/> \n";
+	sparqlQuery += "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>\n";
 	  
-    if(isRefinedQuery())
-       sparqlQuery += select +
-                     "  " + refineDataset +
-                     "    ?s <"+ predicateURL + "> "+ speechMarks + $('#sel_RefineObject').val() + speechMarks +";\n" +
-                     "      <"+$('#sel_RefinePredicate').val() +"> ?o\n"
+	if(isRefinedQuery())
+	sparqlQuery += select +
+		     "  " + refineDataset +
+		     "    ?s <"+ predicateURL + "> "+ speechMarks + $('#sel_RefineObject').val() + speechMarks +";\n" +
+		     "      <"+$('#sel_RefinePredicate').val() +"> ?o\n"
 					 + extra +
 					 "}\n" ;
-    else
-       sparqlQuery += select +
-                     "  " + refineDataset +
-                     "    ?s <" + predicateURL + "> ?o\n  "  
+	else
+	sparqlQuery += select +
+		     "  " + refineDataset +
+		     "    ?s <" + predicateURL + "> ?o\n  "  
 					 + extra + 
 					 "}\n";
         
 	//add extra curly brace for end of GRAPH block
-    sparqlQuery+= "}\n";
-
-    sparqlQuery +=  group;
-
-    //If a limit was selected, apply it
-    if($('#sel_Limit').val()!="None")
-        sparqlQuery += " LIMIT " + $('#sel_Limit').val();
-	else
-        sparqlQuery += " LIMIT " + hardLimit;
+	sparqlQuery+= "}\n";
+
+	sparqlQuery +=  group;
+
+	//If a limit was selected, apply it
+	if($('#sel_Limit').val()!="None")
+		sparqlQuery += " LIMIT " + $('#sel_Limit').val();
+	else
+		sparqlQuery += " LIMIT " + hardLimit;
 		
 
@@ -90,128 +97,129 @@
 	query = query.replace(/\n/g, '<br/>');
 
-    $('#span_sparqlQuery').empty() ;
-    $('#span_sparqlQuery').html(query);
-    return sparqlQuery;
-}
-
-//Updates possiblel values for predicate selector
+	$('#span_sparqlQuery').empty() ;
+	$('#span_sparqlQuery').html(query);
+	return sparqlQuery;
+}
+
+//Updates possible values for predicate selector by sending a query to the 
+//triple-store requesting a list of all predicates.
 function updatePredicateSelector(){
-    var ajaxSparqlQuery = sparqlEndpoint + "?query=" + encodeURIComponent(overviewSparqlQuery);
+	var ajaxSparqlQuery = sparqlEndpoint + "?query=" + encodeURIComponent(overviewSparqlQuery);
         $.ajax({
-            url: ajaxSparqlQuery,
-            type:"GET",
-            data:"output=json",
+		url: ajaxSparqlQuery,
+		type:"GET",
+		data:"output=json",
         })
         .done(function( json ) {
-            var parsedJSON = ((typeof json) == "string") ? $.parseJSON(json) : json;
-            $.each(parsedJSON.results.bindings,function(index,item){
-                var pred  = item.Predicate.value;
-                var name = tidyPredicate(pred);
-                $('#sel_Predicate')
-                    .append("<option value=\""+pred+"\">" + name + "</option>");
-                $('#sel_RefinePredicate')
-                    .append("<option value=\""+pred+"\">" + name + "</option>");
-            });
+		var parsedJSON = ((typeof json) == "string") ? $.parseJSON(json) : json;
+		$.each(parsedJSON.results.bindings,function(index,item){
+			var pred  = item.Predicate.value;
+			var name = tidyPredicate(pred);
+			$('#sel_Predicate')
+			    .append("<option value=\""+pred+"\">" + name + "</option>");
+			$('#sel_RefinePredicate')
+			    .append("<option value=\""+pred+"\">" + name + "</option>");
+		});
         });
 }
 
-//Fill in possible values for predicate
+//Fill in possible values for 'refine' object  selector, based on what predicate the user
+//has selected before, only existing objects will be available.
 function updateRefineObjectSelector(predicate){
-     var refineDataset = "GRAPH gsdl:" + collectionName + " {\n";
-
-    var sparqlQuery = "PREFIX gsdl: <http://localhost:3030/greenstone/data/> \n";
-    sparqlQuery += "SELECT ?obj WHERE{"+
+	var refineDataset = "GRAPH gsdl:" + collectionName + " {\n";
+
+	var sparqlQuery = "PREFIX gsdl: <http://localhost:3030/greenstone/data/> \n";
+	sparqlQuery += "SELECT ?obj WHERE{"+
 					  refineDataset +  
-                      "   ?s <"+predicate+"> ?obj }}"+
-                      "GROUP BY ?obj ORDER BY (?obj)";
-    var ajaxSparqlQuery = sparqlEndpoint + "?query=" + encodeURIComponent(sparqlQuery);
-
-    $.ajax({
-        url: ajaxSparqlQuery,
-        type:"GET",
-        data:"output=json",
-    })
-    .done(function( json ) {
-        var parsedJSON = ((typeof json) == "string") ? $.parseJSON(json) : json;
+		      "   ?s <"+predicate+"> ?obj }}"+
+		      "GROUP BY ?obj ORDER BY (?obj)";
+	var ajaxSparqlQuery = sparqlEndpoint + "?query=" + encodeURIComponent(sparqlQuery);
+
+	$.ajax({
+		url: ajaxSparqlQuery,
+		type:"GET",
+		data:"output=json",
+	})
+	.done(function( json ) {
+		var parsedJSON = ((typeof json) == "string") ? $.parseJSON(json) : json;
 
 		//Clear selector. Add empty option to start.
-        $('#sel_RefineObject').empty();
-		$('#sel_RefineObject')
-		.append("<option value=\"None\">  </option>");
-
-        $.each(parsedJSON.results.bindings,function(index,item){
-            var obj;
+		$('#sel_RefineObject').empty();
+			$('#sel_RefineObject')
+			.append("<option value=\"None\">  </option>");
+
+		$.each(parsedJSON.results.bindings,function(index,item){
+			var obj;
 			if(item.obj.type == "uri"){
 				obj = "&lt;" + item.obj.value + "&gt;";
-            	$('#sel_RefineObject')
-                .append("<option value="+ obj + ">" + obj.substring(0,75) + "</option>");
+				$('#sel_RefineObject')
+				.append("<option value="+ obj + ">" + obj.substring(0,75) + "</option>");
 			}
 			else{
 				obj = item.obj.value;
-            	$('#sel_RefineObject')
-                .append("<option value=\""+ obj +"\">" + obj.substring(0,75) + "</option>");
+				$('#sel_RefineObject')
+				.append("<option value=\""+ obj +"\">" + obj.substring(0,75) + "</option>");
 			}
-        });
+		});
 		$('#sel_RefineObject').next().find('.custom-combobox-input').val("");
-    });
-}
+	});
+}
+
+//When the user has selected a predicate and an object to restrict it to, this predicate selector will update 
+//showing the predicates that are available for selection based on what triples will be returned from
+//the restriction chosen.
 
 function updateRefinePredicateSelector(obj){
-    //Perform ajax query to return possible predicate values, given object and predicate values already chosen
-    var predicate = $('#sel_Predicate').val();
+	//Perform ajax query to return possible predicate values, given object and predicate values already chosen
+	var predicate = $('#sel_Predicate').val();
 	var sparqlQuery;
 	if(obj.substring(0,1) == "<") 
-    sparqlQuery = "SELECT ?predicate WHERE{"+
-                      "   ?s <" + predicate + ">  "+ obj + ";"+
-                      "      ?predicate ?o}"+
-                      "GROUP BY ?predicate ORDER BY DESC(?predicate)";
-	else
-    sparqlQuery = "SELECT ?predicate WHERE{"+
-                      "   ?s <" + predicate + ">  \""+ obj + "\";"+
-                      "      ?predicate ?o}"+
-                      "GROUP BY ?predicate ORDER BY DESC(?predicate)";
-    var ajaxSparqlQuery = sparqlEndpoint + "?query=" + encodeURIComponent(sparqlQuery);
-
-    $.ajax({
-        url: ajaxSparqlQuery,
-        type:"GET",
-        data:"output=json",
-    })
-    .done(function( json ) {
-        var parsedJSON = ((typeof json) == "string") ? $.parseJSON(json) : json;
-        $('#sel_RefinePredicate').empty();
-		$('#sel_RefinePredicate')
-		.append("<option value=\"None\">  </option>");
-        $.each(parsedJSON.results.bindings,function(index,item){
-            var pred = item.predicate.value;
-            var name = tidyPredicate(pred);
-            $('#sel_RefinePredicate')
-                .append("<option value=\""+pred+"\">" + name + "</option>");
-        });
+	sparqlQuery = "SELECT ?predicate WHERE{"+
+		      "   ?s <" + predicate + ">  "+ obj + ";"+
+		      "      ?predicate ?o}"+
+		      "GROUP BY ?predicate ORDER BY DESC(?predicate)";
+	else
+	sparqlQuery = "SELECT ?predicate WHERE{"+
+		      "   ?s <" + predicate + ">  \""+ obj + "\";"+
+		      "      ?predicate ?o}"+
+		      "GROUP BY ?predicate ORDER BY DESC(?predicate)";
+	var ajaxSparqlQuery = sparqlEndpoint + "?query=" + encodeURIComponent(sparqlQuery);
+
+	$.ajax({
+		url: ajaxSparqlQuery,
+		type:"GET",
+		data:"output=json",
+	})
+	.done(function( json ) {
+		var parsedJSON = ((typeof json) == "string") ? $.parseJSON(json) : json;
+		$('#sel_RefinePredicate').empty();
+			$('#sel_RefinePredicate')
+			.append("<option value=\"None\">  </option>");
+		$.each(parsedJSON.results.bindings,function(index,item){
+			var pred = item.predicate.value;
+			var name = tidyPredicate(pred);
+			$('#sel_RefinePredicate')
+			.append("<option value=\""+pred+"\">" + name + "</option>");
+		});
 		$('#sel_RefinePredicate').next().find('.custom-combobox-input').val("");
-    });
-}
+	});
+}
+
+
+//Hides search-refining elements of page when not needed
 function toggle_RefineSearch(enable){
-    if(enable)
-    {
-		
+	if(enable)
 		$('#refineControls').toggle("slide", { direction: "up" });
-        //$('#sel_RefinePredicate').removeAttr("disabled");
-        //$('#sel_RefineObject').removeAttr("disabled");
-    }
-    else
-    {
+	else
 		$('#refineControls').toggle("slide", { direction: "up" });
-        //$('#sel_RefinePredicate').attr("disabled","disabled");
-        //$('#sel_RefineObject').attr("disabled","disabled");
-    }
-}
-
+}
+
+//Tidys up a predicate for easy readability in dropdown lists.
 function tidyPredicate(name)
 {
-            name = name.replace("http://purl.org/dc/elements/1.1/","dc.");
-            name = name.replace("http://greenstone.org/gsembedded#","gsembedded.");
-            name = name.replace("http://greenstone.org/gsextracted#","gsextracted.");
-            return name;
-}
-
+	name = name.replace("http://purl.org/dc/elements/1.1/","dc.");
+	name = name.replace("http://greenstone.org/gsembedded#","gsembedded.");
+	name = name.replace("http://greenstone.org/gsextracted#","gsextracted.");
+	return name;
+}
+
