//
// LW access to LWAPI
//

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

//
// blogs
//

LWAPI.prototype.getBlogsCategory = function(callback) {
	this.getCategoryContent("getBlogsCategory", "", callback);
}

LWAPI.prototype.getBlogs = function(categoryID, start, numResults, sort, sortOrder, callback) {
	if (sortOrder == "revChron") {
		sort = "d";
		sortOrder = "d";
	}
	if (!sort) {
		sort = "d"
	}
	if (!sortOrder) {
		sortOrder = "d";
	}
	this.getForumListContent("getBlogs",
		"categoryID=" + categoryID + "&start=" + start + "&numResults=" + numResults +
		"&sort=" + sort + "&sortOrder=" + sortOrder,
		callback);
}

LWAPI.prototype.getBlogEntries = function(forumID, start, numResults, callback) {
	this.getThreadListContent("getBlogEntries",
		"forumID=" + forumID + "&start=" + start + "&numResults=" + numResults,
		callback);
}

LWAPI.prototype.getBlogEntryComments = function(threadID, start, numResults, callback) {
	this.getMessageListContent("getBlogEntryComments",
		"threadID=" + threadID + "&start=" + start + "&numResults=" + numResults,
		callback);
}

LWAPI.prototype.getBlogEntryTrackbacks = function(threadID, callback) {
	LWAPI.NA('LWAPI.prototype.getBlogEntryTrackbacks');
}

LWAPI.prototype.getFeaturedBlogEntries = function(categoryID, start, numResults, callback) {	
	this.getMessageListContent("getFeaturedBlogEntries",
		"categoryID=" + categoryID + "&start=" + start + "&numResults=" + numResults,
		callback);
}

LWAPI.prototype.getBlogroll = function(forumID, callback) {	
	this.getNameValueContent("getBlogroll",
		"forumID=" + forumID, callback);
}


//
// videos
//

LWAPI.prototype.getVideosCategory = function(callback) {
	this.getCategoryContent("getVideosCategory", "", callback);
}

LWAPI.prototype.getVideo = function(threadID, callback) {
	this.getContent("getVideo", 
		"threadID=" + threadID, 
		callback);
}

LWAPI.prototype.getCategoryVideos = function(categoryID, start, numResults, callback) {
	this.getListContent("getCategoryVideos",
		"categoryID=" + categoryID + "&start=" + start + "&numResults=" + numResults,
		callback);
}
LWAPI.prototype.getFeaturedVideos = function(categoryID, start, numResults, callback) {
	this.getMessageListContent("getFeaturedVideos",
		"categoryID=" + categoryID + "&start=" + start + "&numResults=" + numResults,
		callback);
}
LWAPI.prototype.getVideoComments = function(threadID, start, numResults, callback) {
	this.getMessageListContent("getVideoComments",
		"threadID=" + threadID + "&start=" + start + "&numResults=" + numResults,
		callback);
}
LWAPI.prototype.getVideoTrackbacks = function(threadID, callback) {
	this.getListContent("getVideoTrackbacks",
		"threadID=" + threadID,
		callback);
}


//
// bookmarks
//

LWAPI.prototype.getBookmarksCategory = function(callback) {
	this.getCategoryContent("getBookmarksCategory", "", callback);
}
LWAPI.prototype.getFeaturedBookmarks = function(categoryID, start, numResults, callback) {
	LWAPI.NA('LWAPI.prototype.getFeaturedBookmarks');
}
LWAPI.prototype.getPopularCategoryBookmarks = function(categoryID, start, numResults, callback) {
	LWAPI.NA('LWAPI.prototype.getPopularCategoryBookmarks');
}
LWAPI.prototype.getRecentCategoryBookmarks = function(categoryID, start, numResults, callback) {
	LWAPI.NA('LWAPI.prototype.getRecentCategoryBookmarks');
}
LWAPI.prototype.getUserBookmarks = function(userID, start, numResults, callback) {
	LWAPI.NA('LWAPI.prototype.getUserBookmarks');
}

//
// discussions (forums)
//

LWAPI.prototype.getForumsCategory = function(callback) {
	this.getCategoryContent("getForumsCategory", "", callback);
}

LWAPI.prototype.getThreadMessages = function(threadID, start, numResults, sortOrder, callback) {
	this.getMessageListContent("getThreadMessages", 
		"threadID=" + threadID + "&start=" + start + "&numResults="+numResults + "&sortOrder=" + sortOrder, 
		callback);
}

LWAPI.prototype.getForums = function(categoryID, callback) {
	this.getForumListContent("getForums",
		"categoryID=" + categoryID,
		callback);
}

LWAPI.prototype.getPopularForums = function(categoryID, numResults, callback) {
	this.getForumListContent("getPopularForums",
		"categoryID=" + categoryID + "&numResults=" + numResults,
		callback);
}

LWAPI.prototype.getRecentForumThreads = function(forumID, start, numResults, callback) {
	this.getThreadListContent("getRecentForumThreads",
		"forumID=" + forumID + "&start=" + start + "&numResults=" + numResults,
		callback);
}

LWAPI.prototype.getFeaturedBoardMessages = function(categoryID, start, numResults, callback) {
	LWAPI.NA('LWAPI.prototype.getFeaturedBoardMessages');
}

LWAPI.prototype.getExternalMessages = function(itemID, start, numResults, sortOrder, callback) {
	this.getMessageListContent("getExternalMessages", 
		"itemID=" + itemID + "&start=" + start + "&numResults="+numResults + "&sortOrder=" + sortOrder, 
		callback);
}

LWAPI.prototype.createExternalMessage = function(itemID, body, callback) {
	this.getMessageContent("createExternalMessage", 
		 "itemID=" + escape(itemID) + "&body="+ escape(body), 
		callback);
}

LWAPI.prototype.getExternalRating = function(itemID, callback) {
	this.callContentService("getExternalRating", 
				"itemID=" + escape(itemID),
		function(data) {
			callback(LWAPI.makeObject(data));
		}
	);
}

LWAPI.prototype.getExternalUpDownRating = function(itemID, callback) {
	this.callContentService("getExternalUpDownRating", 
				"itemID=" + escape(itemID),
		function(data) {
			callback(LWAPI.makeObject(data));
		}
	);
}

LWAPI.prototype.createExternalRating = function(itemID, rating, callback) {
	this.callContentService("createExternalRating", 
				"itemID=" + itemID + "&rating=" + rating,
		function(data) {
			callback(LWAPI.makeObject(data));
		}
	);
}

LWAPI.prototype.rateMessage = function(messageID, rating, callback) {
	this.callContentService("rateMessage", 
				"messageID=" + messageID + "&rating=" + rating,
		function(data) {
			callback(LWAPI.makeObject(data));
		}
	);
}

LWAPI.prototype.rateThread = function(threadID, rating, callback) {
	this.callContentService("rateThread", 
				"threadID=" + threadID + "&rating=" + rating,
		function(data) {
			callback(LWAPI.makeObject(data));
		}
	);
}

//
// gallery
//

LWAPI.prototype.getMainGalleryCategory = function(callback) {
	this.getCategoryContent("getMainGalleryCategory", "", callback);
}

//
// photo albums
//

LWAPI.prototype.getPhotoAlbumsCategory = function(callback) {
	this.getCategoryContent("getPhotoAlbumsCategory", "", callback);
}

LWAPI.prototype.getAlbumPhotos = function(albumID, start, numResults, callback) {
	this.getListContent("getAlbumPhotos", 
		"threadID=" + albumID + "&start=" + start + "&numResults=" + numResults,
		 callback);
}

LWAPI.prototype.getFeaturedPhotos = function(categoryID, start, numResults, callback) {
	this.getListContent("getFeaturedPhotos", 
		"categoryID=" + categoryID + "&start=" + start + "&numResults=" + numResults,
		 callback);
}
LWAPI.prototype.getPhotoWithComments = function(messageID, start, numResults, callback) {
	LWAPI.NA('LWAPI.prototype.getPhotoWithComments');
}
LWAPI.prototype.getCategoryPhotos = function(categoryID, start, numResults, sort, sortOrder, callback) {
	this.getListContent("getCategoryPhotos", 
		"categoryID=" + categoryID + "&start=" + start + "&numResults=" + numResults + "&sort=" + sort + "&sortOrder="+ sortOrder,
		 callback);
}

//
// storyboards
//

LWAPI.prototype.getStoryboardsCategory = function(callback) {
	this.getCategoryContent("getStoryboardsCategory", "", callback);
}

LWAPI.prototype.getCategoryStoryboards = function(categoryID, start, numResults, callback) {
	LWAPI.NA('LWAPI.prototype.getCategoryStoryboards');
}
LWAPI.prototype.getFeaturedStoryboards = function(categoryID, start, numResults, callback) {
	LWAPI.NA('LWAPI.prototype.getFeaturedStoryboards');
}
LWAPI.prototype.getForumStoryboards = function(forumID, start, numResults, callback) {
	LWAPI.NA('LWAPI.prototype.getForumStoryboards');
}
LWAPI.prototype.getStoryboard = function(threadID, callback) {
	LWAPI.NA('LWAPI.prototype.getStoryboard');
}
LWAPI.prototype.getStoryboardComments = function(threadID, start, numResults, callback) {
	LWAPI.NA('LWAPI.prototype.getStoryboardComments');
}

//
// calendar
//

LWAPI.prototype.getCalendarEvents = function(forumID, start, numResults, dateRange, callback) {
	LWAPI.NA('LWAPI.prototype.getCalendarEvents');
}
LWAPI.prototype.getEventDetails = function(threadID, start, numResults, sortOrder, callback) {
	LWAPI.NA('LWAPI.prototype.getEventDetails');
}
LWAPI.prototype.getMainCalendarForum = function(callback) {
	LWAPI.NA('LWAPI.prototype.getMainCalendarForum');
}

//
// support boards
//

LWAPI.prototype.getFeaturedSupportThreads = function(categoryID, start, numResults, callback) {
	LWAPI.NA('LWAPI.prototype.getFeaturedSupportThreads');
}

//
// segmented community
//
LWAPI.prototype.getSegmentCalendarForum = function(segmentID, callback) {
	LWAPI.NA('LWAPI.prototype.getSegmentCalendarForum');
}
LWAPI.prototype.getSegmentForumsCategory = function(segmentID, callback) {
	LWAPI.NA('LWAPI.prototype.getSegmentForumsCategory');
}
LWAPI.prototype.getSegmentGalleryCategories = function(callback) {
	LWAPI.NA('LWAPI.prototype.getSegmentGalleryCategories');
}
LWAPI.prototype.getSegmentPhotoAlbumsCategory = function(segmentID, callback) {
	LWAPI.NA('LWAPI.prototype.getSegmentPhotoAlbumsCategory');
}
LWAPI.prototype.getSegmentStoryboardsCategory = function(segmentID, callback) {
	LWAPI.NA('LWAPI.prototype.getSegmentStoryboardsCategory');
}
LWAPI.prototype.getSegmentVideosCategory = function(segmentID, callback) {
	LWAPI.NA('LWAPI.prototype.getSegmentVideosCategory');
}

//
// general
//

LWAPI.prototype.getCategory = function(id, callback) {
	this.getCategoryContent("getCategory", "categoryID=" + id, callback);
}

LWAPI.prototype.getSubcategories = function(id, callback) {
	this.getCategoryListContent("getSubcategories", "categoryID=" + id, callback);
}

LWAPI.prototype.getCategoryActivePolls = function(id, start, numResults, callback) {
	this.getPollListContent("getCategoryActivePolls",
		"categoryID=" + id + "&start=" + start + "&numResults=" + numResults,
		callback);
}

LWAPI.prototype.getForumActivePolls = function(id, start, numResults, callback) {
	this.getPollListContent("getForumActivePolls",
		"forumID=" + id + "&start=" + start + "&numResults=" + numResults,
		callback);
}

LWAPI.prototype.getCategoryAnnouncements = function(id, numResults, callback) {
	this.getAnnouncementListContent("getCategoryAnnouncements",
		"categoryID=" + id + "&numResults=" + numResults,
		callback);
}

LWAPI.prototype.getCategoryPopularTags = function(id, callback) {
	this.getTagListContent("getCategoryPopularTags",
		"categoryID=" + id,
		callback);
}

LWAPI.prototype.simpleSearch = function(id, start, numResults, query, callback) {
	this.getMessageListContent("simpleSearch",
		"categoryID=" + id + "&start=" + start + "&numResults="+ numResults + 
		"&query=" + encodeURIComponent(query),
		callback);
}

LWAPI.prototype.getAllFeatured = function(categoryID, start, numResults, callback) {
	LWAPI.NA('LWAPI.prototype.getAllFeatured');
}
LWAPI.prototype.getFeaturedContent = function(categoryID, start, numResults, forumType, callback) {
	LWAPI.NA('LWAPI.prototype.getFeaturedContent');
}
LWAPI.prototype.getFeaturedMessages = function(categoryID, start, numResults, forumType, callback) {
	LWAPI.NA('LWAPI.prototype.getFeaturedMessages');
}
LWAPI.prototype.getCategoryActiveUsers = function(categoryID, numResults, callback) {
	LWAPI.NA('LWAPI.prototype.getCategoryActiveUsers');
}
LWAPI.prototype.getCategoryMessagesFromTag = function(categoryID, tag, start, numResults, callback) {
	LWAPI.NA('LWAPI.prototype.getCategoryMessagesFromTag');
}
LWAPI.prototype.getForum = function(forumID, callback) {
	LWAPI.NA('LWAPI.prototype.getForum');
}
LWAPI.prototype.getForumActiveUsers = function(forumID, numResults, callback) {
	LWAPI.NA('LWAPI.prototype.getForumActiveUsers');
}
LWAPI.prototype.getForumAnnouncements = function(forumID, numResults, callback) {
	LWAPI.NA('LWAPI.prototype.getForumAnnouncements');
}
LWAPI.prototype.getForumMessagesFromTag = function(forumID, tag, start, numResults, callback) {
	LWAPI.NA('LWAPI.prototype.getForumMessagesFromTag');
}
LWAPI.prototype.getForumPopularTags = function(forumID, callback) {
	LWAPI.NA('LWAPI.prototype.getForumPopularTags');
}
LWAPI.prototype.getMessage = function(messageID, callback) {
	LWAPI.NA('LWAPI.prototype.getMessage');
}
LWAPI.prototype.getRecentCategoryThreads = function(categoryID, start, numResults, callback) {
	LWAPI.NA('LWAPI.prototype.getRecentCategoryThreads');
}
LWAPI.prototype.getThread = function(threadID, callback) {
	LWAPI.NA('LWAPI.prototype.getThread');
}



// Get each type of content from server

LWAPI.prototype.getCategoryContent = function(method, params, callback) {
	this.callContentService(method, params,
		function(data) {
//			var cat = new LWCategory(data);
			callback(data);
		}
	);
}

LWAPI.prototype.getCategoryListContent = function(method, params, callback) {
	this.callContentService(method, params,
		function(data) {
			callback(data);
		}
	);
}

LWAPI.prototype.getForumListContent = function(method, params, callback) {
	this.callContentService(method, params,
		function(data) {
			callback(data);
		}
	);
}

LWAPI.prototype.getThreadListContent = function(method, params, callback) {
	this.callContentService(method, params,
		function(data) {
			callback(data);
		}
	);
}

LWAPI.prototype.getMessageListContent = function(method, params, callback) {
	this.callContentService(method, params,
		function(data) {
			callback(data);
		}
	);
}

LWAPI.prototype.getMessageContent = function(method, params, callback) {
	this.callContentService(method, params,
		function(data) {
			callback(data);
		}
	);
}

LWAPI.prototype.getPollListContent = function(method, params, callback) {
	this.callContentService(method, params,
		function(data) {
			callback(data);
		}
	);
}

LWAPI.prototype.getAnnouncementListContent = function(method, params, callback) {
	this.callContentService(method, params,
		function(data) {
			callback(LWAPI.makeArray(data));
		}
	);
}

LWAPI.prototype.getTagListContent = function(method, params, callback) {
	this.callContentService(method, params,
		function(data) {
			callback(LWAPI.makeArray(data));
		}
	);
}

LWAPI.prototype.getListContent = function(method, params, callback) {
	this.callContentService(method, params,
		function(data) {
			callback(data);
		}
	);
}

LWAPI.prototype.getContent = function(method, params, callback) {
	this.callContentService(method, params,
		function(data) {
			callback(data);
		}
	);
}

LWAPI.prototype.getNameValueContent = function(method, params, callback) {
	this.callContentService(method, params,
		function(data) {
			callback(LWAPI.makeArray(data));
		}
	);
}


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

LWAPI.DEBUG = false;
LWAPI.DEBUGFUNC = null;

LWAPI.debug = function(msg) {
	if (LWAPI.DEBUG) {
		if (LWAPI.DEBUGFUNC) {
			LWAPI.DEBUGFUNC(msg);
		}
		else {
			alert(msg);
		}
	}
}

LWAPI.NA = function(fname) {
	alert(fname + " NOT YET IMPLEMENTED");
}

LWAPI.prototype.callAPI = function(request, method, callback) {
	var hashttp = request.indexOf("http:", 0);
	if (request.indexOf("http:") == 0) {
		request = request.substring(this.base.length);
	}
	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'];
				var obj = {};
				if (jsondata) {
					obj = LWAPI.makeObject(jsondata);
				}
				callback(obj);
			}
		}
	);
}

LWAPI.makeArray = function(data) {
	if (data instanceof Array) {
		return data;
	}
	else {
		var ar = new Array();
		if (data.type) {
			ar[0] = data;
		}
		return ar;
	}
}

LWAPI.makeObject = function(jsondata) {
	var obj;
	var type = "Object";
	if (jsondata && jsondata.type) {
		var typeParts = jsondata.type.split(".");
		if (typeParts.length > 0) {
			var type = "LW" + typeParts[typeParts.length-1];
			jsondata.type = type;
		}
	}
	obj = eval("new "+type+"(jsondata)");
	return obj;
}

LWAPI.fillValues = function(obj, data) {
	for (var key in data) {
		var val = data[key];
		if (val instanceof Object) {
			if (val['@nil'] == 'true') {
				obj[key] = "";
			}
			else if (key == 'attachments') {
				if (val instanceof Array) {
					var i = 0;
					for (var ind in val) {
						var elt = val[ind];
						if (elt instanceof Object) {
							elt.type = "Attachment";
							elt.URL = elt.name;
							elt.caption = elt.value;
							var innerObj = LWAPI.makeObject(elt);
							val[i] = innerObj;
						}
						i++;
					}
					obj[key] = val;
				}
				else {
					val.type = "Attachment";
					val.URL = val.name;
					val.caption = val.value;
					var innerObj = LWAPI.makeObject(val);
					obj[key] = new Array();
					obj[key][0] = innerObj;
				}
			}
			else if (val instanceof Array) {
				var i = 0;
				for (var ind in val) {
					var elt = val[ind];
					if (elt instanceof Object) {
						var innerObj = LWAPI.makeObject(elt);
						val[i] = innerObj;
					}
					else {
						val[i] = elt;
					}
					i++;
				}
				obj[key] = val;
			}
			else {
				obj[key] = LWAPI.makeObject(val);
			}
		}
		else {
			obj[key] = val;
		}
	}
}

// LWCategory

function LWCategory(obj) {
	LWAPI.fillValues(this, obj);
}

LWCategory.prototype.toString = function() {
	return "[LWCategory:"+this.categoryID+":"+this.name+"]";
}


// LWCategoryList 

function LWCategoryList(obj) {
	LWAPI.fillValues(this, obj);
	if (obj.forums) {
		obj = obj.categories;
	}
	else if (obj instanceof Array) {
		for (var i=0; i < obj.length; i++) {
			this.categories[i] = new LWCategory(obj[i]);
		}
	}
	else {
		this.categories[0] = new LWCategory(obj);
	}
}

LWCategoryList.prototype.toString = function() {
	return "[LWCategoryList:count="+this.categories.length+"]";
}

LWCategoryList.prototype.loadJSONCategoryList = function(jsondata) {
	if (jsondata instanceof Array) {
		for (var i=0; i < jsondata.length; i++) {
			this.categories[i] = new LWCategory(jsondata[i]);
		}
	}
	else {
		this.categories[0] = new LWCategory(jsondata);
	}
}


// LWForum

function LWForum(obj) {
	LWAPI.fillValues(this, obj);
}

LWForum.prototype.toString = function() {
	return "[LWForum:"+this.forumID+":"+this.name+"]";
}



// LWBlog - subclass of LWForum

function LWBlog(obj) {
	LWAPI.fillValues(this, obj);
}

LWBlog.prototype = new LWForum();
LWBlog.prototype.constructor = LWBlog;

LWBlog.prototype.toString = function() {
	return "[LWBlog:forumID="+this.forumID+"]";
}


// LWForumList

function LWForumList(obj) {
	LWAPI.fillValues(this, obj);
	/*
	if (obj.forums) {
		obj = obj.forums;
	}
	else if (obj instanceof Array) {
		for (var i=0; i < obj.length; i++) {
			this.forums[i] = new LWForum(obj[i]);
		}
	}
	else {
		this.forums[0] = new LWForum(obj);
	}*/
	if (!(this.forums instanceof Array)) {
		var tmp = this.forums;
		this.forums = new Array();
		this.forums[0] = tmp;
	}
}

LWForumList.prototype.toString = function() {
	return "[LWForumList:count="+this.forums.length+"]";
}

LWForumList.prototype.loadJSONForumList = function(jsondata) {
	if (jsondata.forums) {
		jsondata = jsondata.forums;
	}
	if (jsondata instanceof Array) {
		for (var i=0; i < jsondata.length; i++) {
			this.forums[i] = new LWForum(jsondata[i]);
		}
	}
	else {
		this.forums[0] = new LWForum(jsondata);
	}
}



// LWMessage

function LWMessage(obj) {
	LWAPI.fillValues(this, obj);
}

LWMessage.prototype.toString = function() {
	return "[LWMessage:"+this.messageID+":"+this.subject+"]";
}


// LWMessageList

function LWMessageList(obj) {
	LWAPI.fillValues(this, obj);
	if (!(this.messages instanceof Array)) {
		var msg = this.messages;
		this.messages = new Array();
		this.messages[0] = msg;
	}
}

LWMessageList.prototype.toString = function() {
	return "[LWMessageList:count="+(this.totalItemCount-1)+"]";
}

LWMessageList.prototype.loadJSONMessageList = function(jsondata) {
	for (var i=0; i < jsondata.length; i++) {
		this.messages[i] = new LWMessage(jsondata[i]);
	}
}

// LWThread

function LWThread(obj) {
	LWAPI.fillValues(this, obj);
}

LWThread.prototype.toString = function() {
	return "[LWThread:"+this.threadID+":"+this.subject+"]";
}


// LWThreadList

function LWThreadList(obj) {
	LWAPI.fillValues(this, obj);
	if (!(this.threads instanceof Array)) {
		var thr = this.threads;
		this.threads = new Array();
		this.threads[0] = thr;
	}
}

LWThreadList.prototype.toString = function() {
	return "[LWThreadList:count="+this.threads.length+"]";
}

LWThreadList.prototype.getThreads = function() {
	return this.threads;
}


// LWBlogEntryList

function LWBlogEntryList(obj) {
	LWAPI.fillValues(this, obj);
	if (!(this.blogEntries instanceof Array)) {
		var tmp = this.blogEntries;
		this.blogEntries = new Array();
		this.blogEntries[0] = tmp;
	}
	this.threads = this.blogEntries;
}

LWBlogEntryList.prototype = new LWThreadList();
LWBlogEntryList.prototype.constructor = LWBlogEntryList;

LWBlogEntryList.prototype.toString = function() {
	return "[LWBlogEntryList:count="+this.threads.length+"]";
}


// LWBookmarkList

function LWBookmarkList(jsondata) {
	LWAPI.fillValues(this, obj);
	if (!(this.bookmarks instanceof Array)) {
		var tmp = this.bookmarks;
		this.bookmarks = new Array();
		this.bookmarks[0] = tmp;
	}
	this.threads = this.bookmarks;
}

LWBookmarkList.prototype = new LWThreadList();
LWBookmarkList.prototype.constructor = LWBookmarkList;

LWBookmarkList.prototype.toString = function() {
	return "[LWBookmarkList:count="+this.threads.length+"]";
}


// LWEventList

function LWEventList(jsondata) {
	LWAPI.fillValues(this, obj);
	if (!(this.events instanceof Array)) {
		var tmp = this.events;
		this.events = new Array();
		this.events[0] = tmp;
	}
	this.threads = this.events;
}

LWEventList.prototype = new LWThreadList();
LWEventList.prototype.constructor = LWEventList;

LWEventList.prototype.toString = function() {
	return "[LWEventList:count="+this.threads.length+"]";
}

// LWBlogEntry - subclass of LWThread

function LWBlogEntry(obj) {
	LWAPI.fillValues(this, obj);
}

LWBlogEntry.prototype = new LWThread();
LWBlogEntry.prototype.constructor = LWBlogEntry;

LWBlogEntry.prototype.toString = function() {
	return "[LWBlogEntry:threadID="+this.threadID+"]";
}


// LWBookmark - subclass of LWThread

function LWBookmark(obj) {
	LWAPI.fillValues(this, obj);
}

LWBookmark.prototype = new LWThread();
LWBookmark.prototype.constructor = LWBookmark;

LWBookmark.prototype.toString = function() {
	return "[LWBookmark:"+this.bookmarkURL+":threadID="+this.threadID+"]";
}


// LWEvent - subclass of LWThread

function LWEvent(obj) {
	LWAPI.fillValues(this, obj);
}

LWEvent.prototype = new LWThread();
LWEvent.prototype.constructor = LWEvent;

LWEvent.prototype.toString = function() {
	return "[LWBookmark:"+this.bookmarkURL+":threadID="+this.threadID+"]";
}



// LWVideo - subclass of LWThread

function LWVideo(obj) {
	LWAPI.fillValues(this, obj);
}

LWVideo.prototype = new LWThread();
LWVideo.prototype.constructor = LWVideo;

LWVideo.prototype.toString = function() {
	return "[LWVideo:"+this.videoID+":threadID="+this.threadID+"]";
}


// LWVideoList - subclass of LWThreadList

function LWVideoList(obj) {
	LWAPI.fillValues(this, obj);
	if (!(this.videos instanceof Array)) {
		var video = this.videos;
		this.videos = new Array();
		this.videos[0] = video;
	}
}

LWVideoList.prototype.toString = function() {
	return "[LWVideoList:count="+(this.totalItemCount-1)+"]";
}

LWMessageList.prototype.loadJSONMessageList = function(jsondata) {
	for (var i=0; i < jsondata.length; i++) {
		this.messages[i] = new LWMessage(jsondata[i]);
	}
}


// LWPoll

function LWPoll(obj) {
	LWAPI.fillValues(this, obj);
}

LWPoll.prototype.toString = function() {
	return "[LWPoll:"+this.pollID+":"+this.name+":options="+this.options.length+"]";
}

LWPoll.prototype.loadPollOptions = function(jsondata) {
	LWAPI.fillValues(this, obj);
}

LWPoll.loadPollList = function(jsondata) {
	var pollList = [];
	if (jsondata instanceof Array) {
		for (var i=0; i<jsondata.length; i++) {
			pollList[i] = new LWPoll(jsondata[i]);
		}
	}
	else {
		pollList[0] = new LWPoll(jsondata);
	}
	return pollList;
}

// LWPollOption

function LWPollOption(obj) {
	this.name = jsondata.name;
	this.option = jsondata.value;
}

LWPollOption.prototype.toString = function() {
	return "[LWPollOption:"+this.name+":"+this.option+"]";
}


// LWAnnouncement

function LWAnnouncement(obj) {
	LWAPI.fillValues(this, obj);
}

LWAnnouncement.prototype.toString = function() {
	return "[LWAnnouncement:"+this.subject+":"+this.body+"]";
}


// LWNameValue

function LWNameValue(obj) {
	LWAPI.fillValues(this, obj);
}

LWNameValue.prototype.toString = function() {
	return "[LWNameValue:"+this.name+":"+this.value+"]";
}



// LWTag

function LWTag(obj) {
	LWAPI.fillValues(this, obj);
}

LWTag.prototype.toString = function() {
	return "[LWTag:"+this.name+":"+this.level+"]";
}


LWTag.loadTagList = function(jsondata) {
	var tagList = [];
	if (jsondata == null) {
		return tagList;
	}
	if (jsondata instanceof Array) {
		for (var i=0; i<jsondata.length; i++) {
			tagList[i] = new LWTag(jsondata[i]);
		}
	}
	else {
		tagList[0] = new LWTag(jsondata);
	}
	return tagList;
}


// LWAttachment

function LWAttachment(obj) {
	LWAPI.fillValues(this, obj);
}

LWTag.prototype.toString = function() {
	return "[LWAttachment:"+this.URL+":"+this.caption+"]";
}


// LWAttachmentList

function LWAttachmentList(obj) {
	LWAPI.fillValues(this, obj);
}

LWAttachmentList.prototype.toString = function() {
	return "[LWAttachmentList:count="+this.attachments.length+"]";
}


// LWUser

function LWUser(obj) {
	LWAPI.fillValues(this, obj);
}

LWUser.prototype.toString = function() {
	return "[LWUser:"+this.userID+":"+this.username+"]";
}



// LWNameValue - util to hold name-value pairs

function LWNameValue(obj) {
	LWAPI.fillValues(this, obj);
}

// LWPhoto

function LWPhoto(obj) {
	LWAPI.fillValues(this, obj);
}

LWMessage.prototype.toString = function() {
	return "[LWMessage:"+this.messageID+":"+this.subject+"]";
}


// LWPhotoList 
function LWPhotoList(obj) {
	LWAPI.fillValues(this, obj);
	if (!(this.photos instanceof Array)) {
		var photo = this.photos;
		this.photos = new Array();
		this.photos[0] = photo;
	}
}

LWPhotoList.prototype.toString = function() {
	return "[LWPhotoList:count="+(this.totalItemCount-1)+"]";
}

LWPhotoList.prototype.loadJSONPhotoList = function(jsondata) {
	for (var i=0; i < jsondata.length; i++) {
		this.photos[i] = new LWPhoto(jsondata[i]);
	}
}



// LW utilities for extracting data

var LWUtil;
LWUtil = {};

LWUtil.getStringValue = function (jsondata) {
	if (jsondata == null || jsondata['@nil'] == 'true') {
		return "";
	}
	return jsondata;
}

//
// UTIL for handling strings
//

var UTIL;
UTIL = {};

// add backslash before any instance of the quotChar in the str
UTIL.escapeAttribute = function(str, quotChar) {
	var parts = str.split(quotChar);
	var result = "";
	for (var i=0; i<parts.length; i++) {
		if (i > 0) {
			result += "\\" + quotChar;
		}
		result += parts[i];
	}
	return result;
}


//
// 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;




//
// URL parsing code from:
// http://www.eggheadcafe.com/articles/20020107.asp
//
// Parsing The Querystring with Javascript
// By Peter A. Bromberg, Ph.D.


function PageQuery(q) {
	if(q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;
	this.keyValuePairs = new Array();
	if(q) {
		for(var i=0; i < this.q.split("&").length; i++) {
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	this.getValue = function(s) {
		for(var j=0; j < this.keyValuePairs.length; j++) {
			if(this.keyValuePairs[j].split("=")[0] == s)
			return this.keyValuePairs[j].split("=")[1];
		}
		return false;
	}
	this.getParameters = function() {
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) {
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}
	this.getLength = function() { return this.keyValuePairs.length; } 
}


