pandotdoslashra/js/pandora.js
2010-08-03 20:35:31 +02:00

77 lines
2.5 KiB
JavaScript

$(function() {
$("#title")
.click(function() {
getDefinition("pandora");
});
$(".menu")
.click(function() {
var $this = $(this);
$(".menu").removeClass("selected");
$this.addClass("selected");
})
var hash = getHash(),
history = new History(),
terms = {},
$back = $("#back"),
$forward = $("#forward"),
$definition = $("#definition");
history.add(hash);
getDefinition(hash);
function getDefinition(term) {
var terms = history.get();
$back.empty();
$forward.empty();
$definition.empty();
terms.back && $back.append("&lt;&nbsp; <a class=\"back\" href=\"#" + terms.back + "\">" + toTitleCase(terms.back) + "</a>");
terms.forward && $forward.append("<a class=\"forward\" href=\"#" + terms.forward + "\">" + toTitleCase(terms.forward) + "</a> &nbsp;&gt;");
$.get("html/" + term + ".html", function(data) {
$definition.html(data);
$("#main a").click(function() {
if($(this).attr("href").substr(0, 4) == 'http')
return;
var term = $(this).attr("href").substr(1);
if (term == terms.back) {
history.back();
} else if (term == terms.forward) {
history.forward();
} else {
history.add(term);
}
getDefinition(term);
});
});
}
function getHash() {
return window.location.hash.substr(1) || "pandora";
}
function toTitleCase(str) {
return str.substr(0, 1).toUpperCase() + str.substr(1);
}
function History() {
var items = [],
position = -1;
return {
add: function(item) {
position++;
items.splice(position, items.length - position, item);
},
back: function() {
position > 0 && position--;
},
forward: function() {
position < items.length - 1 && position++;
},
get: function() {
return {
back: items[position - 1] || "",
forward: items[position + 1] || ""
}
}
};
}
window.onhashchange = function() {
var hash = getHash();
//history.add(hash);
getDefinition(hash);
}
});