Refactoring of async query handles
diff --git a/asterix-app/src/main/resources/sdk/static/cherry/js/cherry.js b/asterix-app/src/main/resources/sdk/static/cherry/js/cherry.js
index 2f57e26..18f5925 100755
--- a/asterix-app/src/main/resources/sdk/static/cherry/js/cherry.js
+++ b/asterix-app/src/main/resources/sdk/static/cherry/js/cherry.js
@@ -232,10 +232,9 @@
 
 		var build_cherry_mode = "synchronous";
 		
-		// FIXME
-		//if ($('#asbox').is(":checked")) {
-		//    build_cherry_mode = "asynchronous";
-		//}
+		if ($('#asbox').is(":checked")) {
+		    build_cherry_mode = "asynchronous";
+		}
 	
         var f = new FLWOGRExpression()
             .bind(new ForClause("$t", new AExpression().set("dataset TweetMessages")))
@@ -258,7 +257,11 @@
         param_placeholder["payload"] = formData;
         param_placeholder["query_string"] = "use dataverse twitter;\n" + f.val();
         
-        A.query(f.val(), cherryQuerySyncCallback);
+        if (build_cherry_mode == "synchronous") {
+            A.query(f.val(), cherryQuerySyncCallback, build_cherry_mode);
+        } else {
+            A.query(f.val(), cherryQueryAsyncCallback, build_cherry_mode);
+        }
     
 		APIqueryTracker = {
 		    "query" : "use dataverse twitter;\n" + f.val(),
@@ -267,12 +270,11 @@
 		
 		$('#dialog').html(APIqueryTracker["query"]);
 
-        // FIXME
-        //if (!$('#asbox').is(":checked")) {
-		//    $('#show-query-button').attr("disabled", false);
-        //} else {
-        //    $('#show-query-button').attr("disabled", true);
-        //}
+        if (!$('#asbox').is(":checked")) {
+		    $('#show-query-button').attr("disabled", false);
+        } else {
+            $('#show-query-button').attr("disabled", true);
+        }
         
         // FIXME disable click behavior for tabs while running sync query?
     });
@@ -293,6 +295,7 @@
 
 /** Asynchronous Query Management - Handles & Such **/
 
+
 /**
 * Checks through each asynchronous query to see if they are ready yet
 */
@@ -304,6 +307,7 @@
     }
 }
 
+
 /**
 * Returns current time interval to check for asynchronous query readiness
 * @returns  {number}    milliseconds between asychronous query checks
@@ -313,24 +317,6 @@
     return seconds * 1000;
 }
 
-/**
-* Updates UI when an API Query's status is marked ready
-* @param    {Object}    res, a result object from the Asterix API
-* @param    {object}    extra_info, containing the asynchronous handle's id
-*/
-function asynchronousQueryAPIStatusReceived (res, extra_info) {
-
-    var handle_id = extra_info["handle_id"];
-    if (res["status"] == "SUCCESS") {
-    
-        // We don't need to check if this one is ready again, it's not going anywhere...
-        // Unless the life cycle of handles has changed drastically
-        asyncQueryManager[handle_id]["ready"] = true;
-        
-        // Make this handle's result look retrievable
-        $('#handle_' + handle_id).addClass("label-success");
-    }    
-}
 
 /**
 * Retrieves status of an asynchronous query, using an opaque result handle from API
@@ -339,30 +325,34 @@
 */
 function asynchronousQueryGetAPIQueryStatus (handle, handle_id) {
 
-    var a = new AExpression();
-    a.run(
-        "http://localhost:19002/query/status",
+    // FIXME query status call should disable other functions while it 
+    // loads...for simplicity, really...
+    A.query_status( 
         {
             "handle" : JSON.stringify(handle)
         },
-        {
-            "sync" : asynchronousQueryAPIStatusReceived
-        },
-        {
-            "handle_id" : handle_id
-        }         
-     )
+        function (res) {
+            if (res["status"] == "SUCCESS") {
+                // We don't need to check if this one is ready again, it's not going anywhere...
+                // Unless the life cycle of handles has changed drastically
+                asyncQueryManager[handle_id]["ready"] = true;
+            
+                // Indicate success. 
+                $('#handle_' + handle_id).addClass("label-success");
+            }
+        }    
+     );
 }
 
+
 /**
 * On-success callback after async API query
 * @param    {object}    res, a result object containing an opaque result handle to Asterix
-* @param    {object}    extra, a result object containing a query string and query parameters
 */
-function cherryQueryAsyncCallback(res, extra) {
+function cherryQueryAsyncCallback(res) {
     
     // Parse handle, handle id and query from async call result
-    var handle_query = extra["query_string"];
+    var handle_query = param_placeholder["query_string"];
     var handle = res;
     var handle_id = res["handle"].toString().split(',')[0]; 
     
@@ -370,8 +360,7 @@
     asyncQueryManager[handle_id] = {
         "handle" : handle,
         "query" : handle_query,
-        "data" : extra["payload"],
-        //"ready" : true
+        "data" : param_placeholder["payload"],
     };
     
     $('#review-handles-dropdown').append('<a href="#" class="holdmenu"><span class="label" id="handle_' + handle_id + '">Handle ' + handle_id + '</span></a><br/>');
@@ -400,19 +389,10 @@
             $('#dialog').html(APIqueryTracker["query"]);
         
             // Generate new Asterix Core API Query
-            var ah = new AExpression();
-            ah.run(
-                "http://localhost:19002/query/result",
-                { "handle" : JSON.stringify(asyncQueryManager[handle_id]["handle"])},
-                {
-                    "sync"  : cherryQuerySyncCallback,
-                },
-                {
-                    "payload"       : asyncQueryManager[handle_id]["data"],
-                    "query_string"  : asyncQueryManager[handle_id]["query"]
-                }
+            A.query_result(
+                { "handle" : JSON.stringify(asyncQueryManager[handle_id]["handle"]) },
+                cherryQuerySyncCallback
             );
-            
         }
     });
 }
@@ -867,12 +847,13 @@
         // Attach a message showing minimum bounds     
         $('#legend-label').html('Regions with at least ' + breakpoints[0] + ' tweets');
         $('#legend-label').css({
+            "margin-top" : 0,
             "color" : "black"
         });
     }
     
     // Add legend to map
-    map.controls[google.maps.ControlPosition.RIGHT_BOTTOM].push(document.getElementById('legend-holder'));
+    map.controls[google.maps.ControlPosition.LEFT_BOTTOM].push(document.getElementById('legend-holder'));
     $('#map_canvas_legend').show(); 
 }
 
@@ -895,7 +876,11 @@
     map_tweet_markers = [];
     
     // Remove legend from map
-    map.controls[google.maps.ControlPosition.RIGHT_BOTTOM].clear();
+    map.controls[google.maps.ControlPosition.LEFT_BOTTOM].clear();
+    
+    // Reset map center and zoom
+    map.setCenter(new google.maps.LatLng(38.89, 77.03));
+    map.setZoom(4);
 }
 
 /**
diff --git a/asterix-app/src/main/resources/sdk/static/js/asterix-sdk-stable.js b/asterix-app/src/main/resources/sdk/static/js/asterix-sdk-stable.js
index e4bd564..dd03e9c 100644
--- a/asterix-app/src/main/resources/sdk/static/js/asterix-sdk-stable.js
+++ b/asterix-app/src/main/resources/sdk/static/js/asterix-sdk-stable.js
@@ -20,19 +20,19 @@
 };
 
 
-AsterixDBConnection.prototype.query = function(statements, successFn) {
+AsterixDBConnection.prototype.query = function(statements, successFn, mode) {
  
     if ( typeof statements === 'string') {
         statements = [ statements ];
     }
     
+    var m = mode;
     var query = "use dataverse " + this._properties["dataverse"] + ";\n" + statements.join("\n");
-    var mode = this._properties["mode"];
     
     this._api(
         {
             "query" : query,
-            "mode"  : mode
+            "mode"  : m
         },
         successFn, 
         "http://localhost:19002/query"
@@ -42,6 +42,28 @@
 };
 
 
+AsterixDBConnection.prototype.query_status = function(data, successFn) {
+
+    this._api(
+        data,
+        successFn,
+        "http://localhost:19002/query/status"
+    );
+
+    return this;
+};
+
+
+AsterixDBConnection.prototype.query_result = function(data, successFn) {
+    this._api(
+        data,
+        successFn,
+        "http://localhost:19002/query/result"
+    ); 
+
+    return this;
+};
+
 AsterixDBConnection.prototype._api = function(json, onSuccess, endpoint) {
     var success_fn = onSuccess;
     
@@ -83,55 +105,6 @@
 
 
 AExpression.prototype.run = function(successFn) {
-    var success_fn = successFn;
-
-    $.ajax({
-        type : 'GET',
-        url : "http://localhost:19002/query",
-        data : {"query" : "use dataverse TinySocial;\n" + this.val()},
-        dataType : "json",
-        success : function(data) {     
-            success_fn(data);
-        },
-        error: function(r) {
-            //alert(JSON.stringify(r));
-        }
-    });
-
-    /*$.ajax({
-        "type" : 'GET',
-        "url" : endpoint,
-        "data" : payload,
-        "dataType" : "json",
-        "success" : function(response) {     
-        
-            alert("DEBUG: Run Response: " + JSON.stringify(response));
-        
-            if (response && response["error-code"]) {
-           
-                alert("Error [Code" + response["error-code"][0] + "]: " + response["error-code"][1]);
-            
-            } else if (response && response["results"]) {
-            
-                var fn_callback = myThis._callbacks["sync"];
-                fn_callback(response, myThis._extras);
-            
-            } else if (response["handle"]) {
-            
-                var fn_callback = myThis._callbacks["async"];
-                fn_callback(response, myThis._extras);
-            
-            } else if (response["status"]) {
-                
-                var fn_callback = myThis._callbacks["sync"];
-                fn_callback(response, myThis._extras);
-            }
-        },
-        "error": function (xhr, ajaxOptions, thrownError) {
-            alert("AJAX ERROR " + thrownError);
-        }
-    });*/
-
     return this;
 };