/* ------- DEPENDENCIES ------- */

var cheerio = require("cheerio"),
    fs      = require("fs"),
    HTTP    = require("q-io/http"),
    jsdom   = require("jsdom"),
    Q       = require("q"),
    url     = require("url");

var jquery = fs.readFileSync("./jquery-2.0.0.js").toString();

/* ------- EXPORTS ------- */

function huaGetJsdom(url, jsdomCallback) {
    var deferred = Q.defer();
    HTTP.read(url).then(
        function succ(body) {
            jsdom.env({
                html: body,
                src: [jquery],
                url: url,
                done: function (errors, window) {
                    if (errors) {
                        var e = new Error("jsdom errors");
                        e.errors = errors;
                        deferred.reject(e);
                    } else {
                        deferred.resolve(jsdomCallback(window, window.$));
                    }
                }
            });            
        },
        function fail(response) {
            var e = new Error("HTTP error");
            e.response = response;
            deferred.reject(e);
        }
    );
    return deferred.promise;
}

exports.huaGetJsdom = huaGetJsdom;

function huaGet(url, cheerioCallback) {
    var deferred = Q.defer();
    HTTP.read(url).then(
        function succ(body) {
            var $ = cheerio.load(body);
            deferred.resolve(cheerioCallback(url, $));
        },
        function fail(response) {
            var e = new Error("HTTP error");
            e.response = response;
            deferred.reject(e);
        }
    );
    return deferred.promise;
}

exports.huaGet = huaGet;

function resolveURL(windowORurl, toURL) {
    if (typeof windowORurl === "object") {
        return url.resolve(windowORurl.location.href, toURL);
    } else {
        return url.resolve(windowORurl, toURL);
    }
}

exports.resolveURL = resolveURL;

function logCallback(stepNum) {
    console.log("\n*** callback #" + stepNum + " ***\n");
}

exports.logCallback = logCallback;

function logError(err) {
    console.log("ERROR!!!  message: ", err.message);
}

exports.logError = logError;

@@ -1209,16 +1209,23 @@
 n huaGet
+Cheerio
 (url, ch
@@ -1644,22 +1644,53 @@
 uaGet = 
-huaGet
+exports.huaGetCheerio = huaGetCheerio
 ;%0A%0Afunct
@@ -1722,24 +1722,41 @@
 l, toURL) %7B%0A
+    var fromURL;%0A
     if (type
@@ -1794,35 +1794,26 @@
         
-return url.resolve(
+fromURL = 
 windowOR
@@ -1829,24 +1829,16 @@
 ion.href
-, toURL)
 ;%0A    %7D 
@@ -1844,24 +1844,57 @@
  else %7B%0A    
+    fromURL = windowORurl;%0A    %7D%0A
     return u
@@ -1904,27 +1904,23 @@
 resolve(
-windowORurl
+fromURL
 , toURL)
@@ -1921,22 +1921,16 @@
 toURL);%0A
-    %7D%0A
 %7D%0A%0Aexpor

clean up resolveURL