//
// LW access to LWAPI
//

function LWAPI(baseurl) {
	this.base = baseurl;
	this.serviceUrl = baseurl + "/services"
	this.contentService = "community-nns";
	this.usersService = "users-nns";
	this.responseType = "application/xml";
}

LWAPI.prototype.callContentService = function(method, params, callback) {
	if (params == null) { params = ""; }
	var request = this.serviceUrl + "/" + this.contentService + "/" + method + "?" + params;
	this.callAPI(request, method, callback);
}

LWAPI.prototype.callUsersService = function(method, params, callback) {
	if (params == null) { params = ""; }
	var request = this.serviceUrl + "/" + this.usersService + "/" + method + "?" + params;
	this.callAPI(request, method, callback);
}

LWAPI.prototype.callAPI = function(request, method, callback) {
	var hashttp = request.indexOf("http:", 0);
	if (request.indexOf("http:") == 0) {
		request = request.substring(this.base.length);
	}
	// add param to prevent caching on IE6/7
	var dt = new Date();
	request += "&nocache="+dt.getTime();
	HTTP.getTextWithScript(this.base, request,
		function(responseText) {
			if (responseText != null && responseText != "") {
			    //LWAPI.debug(responseText);
				var data = eval( responseText );
				var response = data["ns:" + method + 'Response'];
				var jsondata = response['return'];
				if (jsondata) {
				    callback(jsondata);
				}
			}
		}
	);
}


//
// HTTP Utilties from Javascript, the Definitive Guide 5th Edition, from O'Reilly
// chapter 20
// David Flanagan
//

var HTTP;
if (HTTP && (typeof HTTP != "object" || HTTP.NAME))
    throw new Error("Namespace 'HTTP' already exists");

// Create our namespace, and specify some meta-information
HTTP = {};
HTTP.NAME = "HTTP";    // The name of this namespace
HTTP.VERSION = 1.0;    // The version of this namespace


HTTP.getTextWithScript = function(base, url, callback) {
    // Create a new script element and add it to the document
    var script = document.createElement("script");
    document.body.appendChild(script);

    // Get a unique function name
    var funcname = "func" + HTTP.getTextWithScript.counter++;

    // Define a function with that name, using this function as a
    // convenient namespace.  The script generated on the server
    // invokes this function
    HTTP.getTextWithScript[funcname] = function(text) {
        // Pass the text to the callback function
        callback(text);

        // Clean up the script tag and the generated function
        document.body.removeChild(script);
        delete HTTP.getTextWithScript[funcname];
    }

	//
	// LW: Updated serverside component for custom Liveworld encoder
	// 
    script.src = base + "/merge/scripts/js-liveapiwrap.jsp" +
                 "?uri=" + encodeURIComponent(url) + "&callback=" +
                 encodeURIComponent("HTTP.getTextWithScript." + funcname);
}

// We use this to generate unique function callback names in case there
// is more than one request pending at a time.
HTTP.getTextWithScript.counter = 0;



