(function()
{
    // This function is designed to be registered as an onload event handler. It searches the page for an empty mediaControlBar
    // element and adds our custom controls.
    // TODO: Check to make sure the use of nested functions (closures) is not causing memory leaks in IE. If it is, we might
    // need to register an ununload handler to clean up after ourselves.
    function initMediaControlBar()
    {
	function createControlBar(mediaType) {
	    // We first search for a "mediaControlBarBooks" div element within our HTML document, then
	    // replace the empty element with the new DOM structure we've created. We also want to ensure that our control bar
	    // is placed within the normal document flow.
	    var elementID = "mediaControlBar" + mediaType;
	    var mediaControlContainer = document.getElementById(elementID);

	    // If we don't find any div with the required id, it presumably means that the user does not want to
	    // use our control bar - which means our work is done.
	    if(!mediaControlContainer)
	    {
		// DEBUG
		//alert("Media control bar container not found.");
		return;
	    }

	    mediaControlContainer.style.position = "relative";
	    mediaControlContainer.style.width = "132px";

	    var leftButton = document.createElement("img");

	    leftButton.src = "images/mediaControlBar/arrowLeftDeactivated.gif";
	    leftButton.alt = "Left Button";
	    leftButton.id = "leftButton" + mediaType;

	    leftButton.style.position = "absolute";
	    leftButton.style.width = "20px";
	    leftButton.style.height = "20px";
	    leftButton.style.left = "0px";
	    leftButton.style.top = "0px";

	    mediaControlContainer.appendChild(leftButton);
	    //mediaControlContainer.insertBefore(leftButton, mediaIconBooks);

	    var listInfo = document.createElement("div");

	    listInfo.id = "listInfo" + mediaType;

	    listInfo.style.position = "absolute";
	    listInfo.style.width = "45px";
	    listInfo.style.height = "20px";
	    listInfo.style.left = (leftButton.offsetLeft + leftButton.offsetWidth + 2) + "px";
	    listInfo.style.top = "0px";
	    listInfo.style.textAlign = "center";
	    listInfo.style.fontSize = "12px";
	    listInfo.style.fontWeight = "bold";
	
	    mediaControlContainer.appendChild(listInfo);
	    //mediaControlContainer.insertBefore(listInfo, mediaIconBooks);

	    var rightButton = document.createElement("img");

	    rightButton.src = "images/mediaControlBar/arrowRightDeactivated.gif";
	    rightButton.alt = "Right Button";
	    rightButton.id = "rightButton" + mediaType;

	    rightButton.style.position = "absolute";
	    rightButton.style.width = "20px";
	    rightButton.style.height = "20px";
	    rightButton.style.left = (listInfo.offsetLeft + listInfo.offsetWidth + 2) + "px";
	    rightButton.style.top = "0px";

	    mediaControlContainer.appendChild(rightButton);
	    //mediaControlContainer.insertBefore(rightButton, mediaIconBooks);
	}

	createControlBar("Books");
	createControlBar("Games");
	createControlBar("Music");
	createControlBar("Movies");

	//var xmlDOMTree = null;
	//var bookList = null;
	//var currentBookIndex = 0;

	function activateControlBars(xmlDOMRootNode) {
	    if(!xmlDOMRootNode) {
		return;
	    }

	    //var xmlDOMTree = xmlDOMRootNode;

	    function activateControlBar(mediaType, xmlDataTagName, mediaInfoUpdateCallback) {
		var mediaList = xmlDOMRootNode.getElementsByTagName(xmlDataTagName);
		var currentMediaIndex = mediaList.length - 1;

		var mediaListInfo = document.getElementById("listInfo" + mediaType);

		mediaListInfo.innerHTML = mediaList.length;//mediaList[0].getAttribute("id");
		//activateLeftButton();
		//activateRightButton();

		var leftArrowButton = document.getElementById("leftButton" + mediaType);
		var rightArrowButton = document.getElementById("rightButton" + mediaType);

		leftArrowButton.src = "images/mediaControlBar/arrowLeft.gif";

		function handleLeftButtonPress() {
		    if(currentMediaIndex > 0) {
			--currentMediaIndex;

			if(currentMediaIndex === 0) {
			    leftArrowButton.src = "images/mediaControlBar/arrowLeftDeactivated.gif";
			}

			rightArrowButton.src = "images/mediaControlBar/arrowRight.gif";
			//updateMediaInfo();
			mediaInfoUpdateCallback(mediaList[currentMediaIndex], currentMediaIndex + 1);
		    }
		}

		if(leftArrowButton.addEventListener)
		{
		    leftArrowButton.addEventListener("click", handleLeftButtonPress, false);
		}
		else if(leftArrowButton.attachEvent)
		{
		    leftArrowButton.attachEvent("onclick", handleLeftButtonPress);
		}
		else
		{
		    leftArrowButton.onclick = handleLeftButtonPress;
		}

		function handleRightButtonPress() {
		    if(currentMediaIndex < (mediaList.length - 1)) {
			++currentMediaIndex;

			if(currentMediaIndex === (mediaList.length - 1)) {
			    rightArrowButton.src = "images/mediaControlBar/arrowRightDeactivated.gif";
			}

			leftArrowButton.src = "images/mediaControlBar/arrowLeft.gif";
			//updateMediaInfo();
			mediaInfoUpdateCallback(mediaList[currentMediaIndex], currentMediaIndex + 1);
		    }
		}

		if(rightArrowButton.addEventListener)
		{
		    rightArrowButton.addEventListener("click", handleRightButtonPress, false);
		}
		else if(rightArrowButton.attachEvent)
		{
		    rightArrowButton.attachEvent("onclick", handleRightButtonPress);
		}
		else
		{
		    rightArrowButton.onclick = handleRightButtonPress;
		}
	    }

	    function updateBookInfo(currentBook, currentIndex) {
		var bookListInfo = document.getElementById("listInfoBooks");
		bookListInfo.innerHTML = currentIndex;//currentBook.getAttribute("id");

		var bookCoverImage = document.getElementById("bookImage");
		bookCoverImage.src = currentBook.getElementsByTagName("imagePath")[0].firstChild.data;

		var bookLink = document.getElementById("bookLink");
		bookLink.href = currentBook.getElementsByTagName("link")[0].firstChild.data;

		var bookTitle = document.getElementById("bookTitle");
		bookTitle.innerHTML = currentBook.getElementsByTagName("title")[0].firstChild.data;

		var bookAuthor = document.getElementById("bookAuthor");
		bookAuthor.innerHTML = currentBook.getElementsByTagName("author")[0].firstChild.data;
	    }

	    function updateGameInfo(currentGame, currentIndex) {
		var gameListInfo = document.getElementById("listInfoGames");
		gameListInfo.innerHTML = currentIndex;//currentGame.getAttribute("id");

		var gameCoverImage = document.getElementById("gameImage");
		gameCoverImage.src = currentGame.getElementsByTagName("imagePath")[0].firstChild.data;

		var gameLink = document.getElementById("gameLink");
		gameLink.href = currentGame.getElementsByTagName("link")[0].firstChild.data;

		var gameTitle = document.getElementById("gameTitle");
		gameTitle.innerHTML = currentGame.getElementsByTagName("title")[0].firstChild.data;

		var gamePlatform = document.getElementById("gamePlatform");
		gamePlatform.innerHTML = currentGame.getElementsByTagName("platform")[0].firstChild.data;
	    }

	    function updateMusicInfo(currentMusic, currentIndex) {
		var musicListInfo = document.getElementById("listInfoMusic");
		musicListInfo.innerHTML = currentIndex;//currentMusic.getAttribute("id");

		var musicCoverImage = document.getElementById("musicImage");
		musicCoverImage.src = currentMusic.getElementsByTagName("imagePath")[0].firstChild.data;

		var musicLink = document.getElementById("musicLink");
		musicLink.href = currentMusic.getElementsByTagName("link")[0].firstChild.data;

		var musicTitle = document.getElementById("musicTitle");
		musicTitle.innerHTML = currentMusic.getElementsByTagName("title")[0].firstChild.data;

		var musicArtist = document.getElementById("musicArtist");
		musicArtist.innerHTML = currentMusic.getElementsByTagName("artist")[0].firstChild.data;
	    }


	    function updateMovieInfo(currentMovie, currentIndex) {
		var movieListInfo = document.getElementById("listInfoMovies");
		movieListInfo.innerHTML = currentIndex;//currentMovie.getAttribute("id");

		var movieCoverImage = document.getElementById("movieImage");
		movieCoverImage.src = currentMovie.getElementsByTagName("imagePath")[0].firstChild.data;

		var movieLink = document.getElementById("movieLink");
		movieLink.href = currentMovie.getElementsByTagName("link")[0].firstChild.data;

		var movieTitle = document.getElementById("movieTitle");
		movieTitle.innerHTML = currentMovie.getElementsByTagName("title")[0].firstChild.data;
	    }

	    activateControlBar("Books", "book", updateBookInfo);
	    activateControlBar("Games", "game", updateGameInfo);
	    activateControlBar("Music", "music", updateMusicInfo);
	    activateControlBar("Movies", "movie", updateMovieInfo);
	}


	function activateBlogControlBar(xmlDOMBlogRootNode) {
	    if(!xmlDOMBlogRootNode) {
		return;
	    }

	    //var xmlDOMTree = xmlDOMRootNode;

	    var blogEntryList = xmlDOMBlogRootNode.getElementsByTagName("blogEntry");

	    var blogEntryListInfo = document.getElementById("blogInfo");

	    var currentBlogIndex = blogEntryListInfo.innerHTML - 1;

	    var leftBlogArrowButton = document.getElementById("blogButtonLeft");
	    var rightBlogArrowButton = document.getElementById("blogButtonRight");

	    var leftBlogArrowButtonImage = document.getElementById("blogButtonLeftImage");
	    var rightBlogArrowButtonImage = document.getElementById("blogButtonRightImage");

	    var leftBlogArrowButtonLink = document.getElementById("blogButtonLeftLink");

	    if( leftBlogArrowButtonLink ) {

		leftBlogArrowButton.appendChild(leftBlogArrowButtonLink.removeChild(leftBlogArrowButtonImage));
		leftBlogArrowButton.removeChild(leftBlogArrowButtonLink);

	    }

	    var rightBlogArrowButtonLink = document.getElementById("blogButtonRightLink");

	    if( rightBlogArrowButtonLink ) {

		rightBlogArrowButton.appendChild(rightBlogArrowButtonLink.removeChild(rightBlogArrowButtonImage));
		rightBlogArrowButton.removeChild(rightBlogArrowButtonLink);

	    }

	    leftBlogArrowButtonImage = document.getElementById("blogButtonLeftImage");
	    rightBlogArrowButtonImage = document.getElementById("blogButtonRightImage");

	    function updateBlogEntryInfo(currentBlogEntry, currentBlogEntryIndex) {

		// TODO: Figure out how to handle all of the special cases in here.
		// In particular, need to determine how to get the blog entry content
		// to the correct level of escaping, and how to rewire the DOM tree
		// in order to add/remove the blog entry image element depending on
		// whether the entry has a corresponding image. Also need to figure
		// out how to remove the links that are wrapped around the left and
		// right buttons so that there is no interference with the Ajax
		// callback behavior.

		var blogEntryListInfo = document.getElementById("blogInfo");
		blogEntryListInfo.innerHTML = currentBlogEntryIndex;//currentBook.getAttribute("id");

		var blogEntryDate = document.getElementById("blogEntryDate");
		blogEntryDate.innerHTML = currentBlogEntry.getElementsByTagName("formattedPublicationDate")[0].firstChild.data;

		var blogEntryPermalink = document.getElementById("blogEntryPermalink");
		blogEntryPermalink.href = "blog/" + currentBlogEntry.getElementsByTagName("permalinkURL")[0].firstChild.data;// + ".html";

		var blogContainer = document.getElementById("blogEntryContainer");

		var blogEntryImage = document.getElementById("blogEntryImage");

		if( blogEntryImage ) {

		    if( currentBlogEntry.getElementsByTagName("imagePath").length !== 0 ) {

			blogEntryImage.src = currentBlogEntry.getElementsByTagName("imagePath")[0].firstChild.data;

		    }
		    else {

			// Remove the blogEntryImage element from the blogEntryImageContainer element
			//var blogEntryImageContainer = document.getElementById("blogEntryImageContainer");
			//blogEntryImageContainer.removeChild(blogEntryImage);
			//blogEntryImageContainer.innerHTML = "";

			blogContainer.removeChild(blogEntryImage);

		    }

		}
		else {

		    if( currentBlogEntry.getElementsByTagName("imagePath").length !== 0 ) {

			// Create a new blogEntryImage element and append it to the blogEntryImageContainer element
			var blogImage = document.createElement("img");

			blogImage.src = currentBlogEntry.getElementsByTagName("imagePath")[0].firstChild.data;
			blogImage.alt = "Blog Image";
			blogImage.className = "blogimage";
			blogImage.id = "blogEntryImage";

			//var blogEntryImageContainer = document.getElementById("blogEntryImageContainer");
			//blogEntryImageContainer.appendChild(blogImage);

			blogContainer.insertBefore(blogImage, blogEntryDate);

		    }

		}

		var blogEntryTitle = document.getElementById("blogEntryTitle");
		blogEntryTitle.innerHTML = currentBlogEntry.getElementsByTagName("title")[0].firstChild.data;

		var blogEntryContent = document.getElementById("blogEntryContent");

		if( currentBlogEntry.getElementsByTagName("content")[0].textContent ) {

		    blogEntryContent.innerHTML = currentBlogEntry.getElementsByTagName("content")[0].textContent;

		}
		else {

		    blogEntryContent.innerHTML = currentBlogEntry.getElementsByTagName("content")[0].firstChild.data;

		}

	    }

	    function handleLeftBlogButtonPress() {
		if(currentBlogIndex > 0) {
		    currentBlogIndex = currentBlogIndex - 1;

		    if(currentBlogIndex === 0) {
			leftBlogArrowButtonImage.src = "images/mediaControlBar/arrowLeftDeactivated.gif";
		    }

		    rightBlogArrowButtonImage.src = "images/mediaControlBar/arrowRight.gif";
		    updateBlogEntryInfo(xmlDOMBlogRootNode.getElementsByTagName("blogEntry")[currentBlogIndex], currentBlogIndex + 1);
		}
	    }

	    if(leftBlogArrowButton.addEventListener)
	    {
		leftBlogArrowButton.addEventListener("click", handleLeftBlogButtonPress, false);
	    }
	    else if(leftBlogArrowButton.attachEvent)
	    {
		leftBlogArrowButton.attachEvent("onclick", handleLeftBlogButtonPress);
	    }
	    else
	    {
		leftBlogArrowButton.onclick = handleLeftBlogButtonPress;
	    }

	    function handleRightBlogButtonPress() {
		if(currentBlogIndex < (xmlDOMBlogRootNode.getElementsByTagName("blogEntry").length - 1)) {
		    currentBlogIndex = currentBlogIndex + 1;

		    if(currentBlogIndex === (xmlDOMBlogRootNode.getElementsByTagName("blogEntry").length - 1)) {
			rightBlogArrowButtonImage.src = "images/mediaControlBar/arrowRightDeactivated.gif";
		    }

		    leftBlogArrowButtonImage.src = "images/mediaControlBar/arrowLeft.gif";
		    updateBlogEntryInfo(xmlDOMBlogRootNode.getElementsByTagName("blogEntry")[currentBlogIndex], currentBlogIndex + 1);
		}
	    }

	    if(rightBlogArrowButton.addEventListener)
	    {
		rightBlogArrowButton.addEventListener("click", handleRightBlogButtonPress, false);
	    }
	    else if(rightBlogArrowButton.attachEvent)
	    {
		rightBlogArrowButton.attachEvent("onclick", handleRightBlogButtonPress);
	    }
	    else
	    {
		rightBlogArrowButton.onclick = handleRightBlogButtonPress;
	    }


	    HTTP.getXML("data/mediaConsumption.xml", activateControlBars);

	}

	HTTP.getXML("data/blogEntries.xml", activateBlogControlBar);
    }

    // TODO: Change event registration so that this module doesn't trample over existing onload handlers
    if(window.addEventListener)
    {
	window.addEventListener("load", initMediaControlBar, false);
    }
    else if(window.attachEvent)
    {
	window.attachEvent("onload", initMediaControlBar);
    }
    else
    {
	window.onload = initMediaControlBar;
    }
}
)();