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

Updated: 21 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 = "http://www.occasionalhell.com/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 = "lastfm_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");

					// Add the URL to the link href
					a.href = itemData[1];		

					// I don't like the way the data is sent back, so I'm gonna mess with it.
					// Ditch the description that was sent back (which is the same as the URL), 
					// And split the title into song (title), and artist (description)
					var temp = itemData[0].split(" - ");

					var title = document.createTextNode(temp[0]);
					var description = document.createTextNode("by " + temp[1]);

					// Put it all together
					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 = "Nothing playing lately.";
			}

			else {
				var div = $(id);
				div.innerHTML = "No feed to read.";
			}

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

	new ajax(requestUrl, opt);
}

