var site = {};
site.pageId = "";

/*
 * Jokes
 * *****************************************************************************
 */
site.jokes = {};
site.jokes.hideClass = "hide";
site.jokes.hoverClass = "hover";
site.jokes.list = [];

site.jokes.init = function() {
	site.jokes.list = $$(".joke");
	var titles = $$(".joke h3");
	var firstId = "";
	var content = $("content");
	var ul = $(document.createElement("ul"));
	ul.id = "jokeList";

	content.appendChild(ul);

	for (var i = 0; i < titles.length; i++) {
		var title = titles[i];
		var joke = title.up();
		var jokeId = joke.identify()

		if (firstId.empty()) {
			firstId = jokeId;
		}

		var li = $(document.createElement("li"));
		li.update(title.childNodes[0].nodeValue);
		li.observe("click", function() {
			site.jokes.showJoke(this.id);
			scrollTo(0, 0);
		}.bind({id: jokeId}));
		li.observe("mouseover", function() {
			if (!this.li.hasClassName(site.jokes.hoverClass)) {
				this.li.addClassName(site.jokes.hoverClass);
			}
		}.bind({li: li}));
		li.observe("mouseout", function() {
			if (this.li.hasClassName(site.jokes.hoverClass)) {
				this.li.removeClassName(site.jokes.hoverClass);
			}
		}.bind({li: li}));

		ul.appendChild(li);
	}

	site.jokes.showJoke(firstId);
};
site.jokes.showJoke = function(id) {
	for (var i = 0; i < site.jokes.list.length; i++) {
		var joke = site.jokes.list[i];
		if (!joke.hasClassName(site.jokes.hideClass)) {
			joke.addClassName(site.jokes.hideClass);
		}
	}

	var currentJoke = $(id);
	if (currentJoke.hasClassName(site.jokes.hideClass)) {
		currentJoke.removeClassName(site.jokes.hideClass);
	}
};

/*
 * Start up
 * *****************************************************************************
 */
site.startup = {};
site.startup["common"] = function() {
};

site.startup["jokes"] = function() {
	site.jokes.init();
};

Event.observe(window, "load", function() {
	site.pageId = document.body.id;
	if (site.startup["common"]) {
		site.startup["common"]();
	}
	// This section looks in the site.startup array for any objects with the
	// same name as the body id. If found, then fire the function.
	if (site.startup[site.pageId]) {
		site.startup[site.pageId]();
	}
});
