/************************************************
SIMPLEPIE AJAX HANDLER
Handles the sending and receiving of data.

Updated: 11 January 2006
By Ryan Parman
************************************************/

function process(id, feed, items) {

	// Enable the indicator
	$("indicator").style.display="block";

	// Construct the URL to send to the backend.
	var requestUrl = "./php/process.php?f=" + feed + "&q=" + items;

	// Set some options
	var opt = {

		// Use GET
		method: 'get',

		// Handle the data response
		onComplete: function(t) {

			// Grab the response we got from the backend.
			var data = t.responseText;

			// Break the data down into an array.
			data = data.split(":::");

			// As long as we got the data back (as noted by the "1")
			if (data[0] == "1") {

				// Store the length so we don't have to check it every time
				dataLen = data.length;

				// Create unordered list and add it to the document tree
				var div = $(id);
				var ul = document.createElement("ul")
				ul.id = "delicious_list";
				div.appendChild(ul);

				// Loop through and create our list items
				for (x=1; x<dataLen; x++) {
					var itemData = data[x].split("|||");
					var li = document.createElement("li");
					var a = document.createElement("a");
					a.href = itemData[1];
					var title = document.createTextNode(itemData[0]);
					var description = document.createTextNode(itemData[2]);
					a.appendChild(title);
					li.appendChild(a);
					li.appendChild(document.createTextNode(" "));
					li.appendChild(description);
					$(ul.id).appendChild(li);
				}
			}

			else if (data[0] == 0) {
				var div = $(id);
				div.innerHTML = "Sorry, there isn't anything in this feed.";
			}

			else {
				var div = $(id);
				div.innerHTML = "Sorry, but there's no feed to read.";
			}

			// Disable the indicator
			$("indicator").style.display="none";
		}
	}

	new ajax(requestUrl, opt);
}

