Index: Dev/branches/jQueryUI/js/api.js
===================================================================
--- Dev/branches/jQueryUI/js/api.js	(revision 246)
+++ Dev/branches/jQueryUI/js/api.js	(revision 247)
@@ -1,41 +1,194 @@
 $(function() {
+
+    jQuery.extend({
+        getScript: function(url, callback) {
+            var d = $.Deferred();
+            if (callback)
+                d.done(callback);
+            var head = document.getElementsByTagName("head")[0];
+            var script = document.createElement("script");
+            script.src = url;
+
+            // Handle Script loading
+            {
+                var done = false;
+
+                // Attach handlers for all browsers
+                script.onload = script.onreadystatechange = function(){
+                    if ( !done && (!this.readyState ||
+                        this.readyState == "loaded" || this.readyState == "complete") ) {
+                        done = true;
+                        d.resolve();
+
+                        // Handle memory leak in IE
+                        script.onload = script.onreadystatechange = null;
+                    }
+                };
+            }
+
+            head.appendChild(script);
+
+            // We handle everything using the script element injection
+            return d;
+        }
+    });
 
     rft = {
         api: {
-            post: function(action,args,success,error) {
-                $.ajax({
+            post: function(action,args) {
+                return $.ajax({
                     type: "POST",
                     url: "api.php",
                     contentType: "application/json",
-                    data: JSON.stringify({ action:action, args:args }),
-                    success: success,
-                    error: error
+                    data: JSON.stringify({
+                        uid:rft.auth.getUid(),
+                        action:action, 
+                        args:args
+                    })
+                });
+            },
+            createObject: function(type, args) {
+                var d = $.Deferred();
+                rft.api.post("createObject",
+                {
+                    type: type,
+                    values: args
+                })
+                .done(function(data, textStatus, jqXHR) {
+                    d.resolve(data.uid);
+                })
+                .fail(function(jqXHR,textStatus,errorThrown){
+                    d.reject(); 
+                });
+                return d.promise();
+            },
+            getObject: function(type, uid, success) {
+                var d = $.Deferred();
+                rft.api.post("getObject",
+                {
+                    type: type,
+                    uid: uid
+                })
+                .done(function(data, textStatus, jqXHR) {
+                    d.resolve(data.object);
+                })
+                .fail(function(jqXHR,textStatus,errorThrown){
+                    d.reject(); 
+                });
+                return d.promise();
+            },
+            getObjects: function(type, predicates, success) {
+                var d = $.Deferred();
+                rft.api.post("getObjects",
+                {
+                    type: type,
+                    predicates: predicates
+                })
+                .done(function(data, textStatus, jqXHR) {
+                    d.resolve(data.objects);
+                })
+                .fail(function(jqXHR,textStatus,errorThrown){
+                    d.reject(); 
+                });
+                return d.promise();
+            },
+            deleteObject: function(type, uid, success) {
+                var d = $.Deferred();
+                d.reject();
+                return d.promise();
+
+            }
+        },
+        auth: new (function() {
+            var self = this;
+            var currentUid = null;
+            
+            self.login = function(username,password,success,error) {
+                var d = $.Deferred();
+                rft.api.post("login",
+                {
+                    username:username,
+                    password:password
+                })
+                .done(function(data, textStatus, jqXHR) {
+                    currentUid = data.uid;
+                    d.resolve(data.uid);
+                })
+                .fail(function(jqXHR,textStatus,errorThrown){
+                    d.reject(); 
+                });
+                return d.promise();
+            };
+            
+            self.register = function(username,password,success,error) {
+                var d = $.Deferred();
+                rft.api.post("register",
+                {
+                    username:username,
+                    password:password
+                })
+                .done(function(data, textStatus, jqXHR) {
+                    currentUid = data.uid;
+                    d.resolve(data.uid);
+                })
+                .fail(function(jqXHR,textStatus,errorThrown){
+                    d.reject(); 
+                });
+                return d.promise();
+            };
+            
+            self.getUid = function(){
+                return currentUid;
+            };
+        })(),
+        content: new (function() {
+            var self = this;
+            var current = "";
+            
+            function init() {
+                $(window).bind( 'hashchange', function(e){
+                    var url = $.bbq.getState("content");
+                    self.goTo(url);
                 });
             }
-        },
-        auth: {
-            login: function(username,password,success,error) {
-                rft.api.post("login",
-                    {username:username,password:password},
-                    function(data,textStatus,jqXHR) {
-                        success && success()
-                    },
-                    function(jqXHR,textStatus,errorThrown) {
-                        error && error()
-                    }
-                );
-            },
-            register: function(username,password,success,error) {
-                rft.api.post("register",
-                    {username:username,password:password},
-                    function(data,textStatus,jqXHR) {
-                        success && success()
-                    },
-                    function(jqXHR,textStatus,errorThrown) {
-                        error && error()
-                    }
-                );
+            
+            self.initial = function(location) {
+                if ( current ) {
+                    self.goTo(location);
+                }
+                var url = $.bbq.getState("content");
+                self.goTo(url || location);
             }
-        }
+            
+            self.goTo = function(location) {
+                if ( current == location ) {
+                    return $.when();
+                }
+                current = location;
+                var d = $.Deferred();
+                $.get("pages/"+location+"/"+location+".html")
+                .done(function(html){
+                    $("#content").html(html);
+                    $.getScript("pages/"+location+"/"+location+".js")
+                    .done(function(){
+                        d.resolve(); 
+                    })
+                    .fail(function(){
+                        d.reject(); 
+                    })
+                })
+                .fail(function(){
+                    $("#content").html("Page "+location+" not found.");
+                    d.reject();
+                }).then(function(){
+                    $.bbq.pushState({
+                        "content": location
+                    });
+                })
+                return d;
+            }
+            
+            init();
+        })()
     };
 
