(function($, window, undefined){
$(document).ready(function() {
var $browseMenu = $("#browse-default-menu");
var $container = $("#browse-default-container");
var $content = $("#browse-default-content");
var data = {
game: [],
system: [],
publisher: [],
category: []
};
var renderData = function(data, itemRenderer) {
var $list = $("
"), $item = $("");;
$.each(data, function() {
$list.append($item.clone().append(itemRenderer(this)));
});
$content.append($list);
};
var getDataAndRender = function(type, itemRenderer) {
return function() {
var render = function() {
$browseMenu.hide();
$container.show();
renderData(data[type], itemRenderer);
};
if (data[type].length === 0) {
var $link = $(this);
$.ajax("/api/" + type + "/all", {
data: { sort: "alphabetical" },
success: function(response, status, $xhr) {
if (response.Error !== null) {
alert(response.Error);
return;
}
data[type] = response.Data.records;
render();
},
beforeSend: function() { $link.toggleClass("loading-icon"); },
complete: function() { $link.toggleClass("loading-icon"); }
});
} else {
render();
}
return false;
};
};
var itemRendererByName = function(type) {
var $template = $("");
return function(dto, count) {
return $template
.clone()
.attr("href", "/browse/" + type + "/" + dto.Id)
.text(dto.Name);
};
};
var systemItemRenderer = function() {
var $template = $("");
return function(dto, count) {
return $template
.clone()
.attr({ href: "/browse/system/" + dto.Id, title: dto.Name })
.text(dto.Abbreviation);
};
}();
$("#show-default-menu").click(function() {
$content.empty();
$container.hide();
$browseMenu.show();
return false;
});
$("#browse-game").click(getDataAndRender("game", itemRendererByName("game")));
$("#browse-system").click(getDataAndRender("system", systemItemRenderer));
$("#browse-category").click(getDataAndRender("category", itemRendererByName("category")));
$("#browse-publisher").click(getDataAndRender("publisher", itemRendererByName("publisher")));
});
}(jQuery, window));