(function($, window, undefined){
$.fn.center = function() {
this.css("top", ($(window).height() - this.height()) / 2 + $(window).scrollTop() + "px");
this.css("left", ($(window).width() - this.width()) / 2 + $(window).scrollLeft() + "px");
return this;
};
$.fn.applyModelErrors = function(errorMessage, errorData) {
var $this = this;
if (errorMessage !== null) {
$this.find(".error-message").first().text(errorMessage).show();
}
$.each(errorData, function(inputFieldName, value) {
var $input = $this.find("input[name='" + inputFieldName + "']");
if ($input.length > 1) {
$input = $("#" + inputFieldName);
}
if ($input.length) {
$input.addClass("input-validation-error");
$("")
.addClass("field-validation-error")
.text(value)
.insertAfter($input);
}
});
};
$.fn.clearModelErrors = function() {
this
.find(".field-validation-error")
.remove()
.end()
.find(".input-validation-error")
.removeClass("input-validation-error")
.end()
.find(".error-message")
.empty()
.hide();
};
$.vgquotes = function() {
var callApi = function(url, type, data, callback) {
$.ajax(url, {
type: type,
data: data,
success: callback,
error: callback
});
};
var cache = {
game: { },
system: { },
category: { },
publisher: { }
};
return {
refresh: function() { },
parseDate: function(jsonDate) { return new Date(parseInt(jsonDate.substr(6))); },
formatDate: function(date) { return date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate(); },
parseAndFormatDate: function(jsonDate) { return $.vgquotes.formatDate($.vgquotes.parseDate(jsonDate)); },
ajaxErrorHandler: function(xhr) {
console.dir(xhr);
alert("An error occurred (" + xhr.statusCode + ")");
},
preload: function(images) {
//MM_preloadImages(lulz)
$.each(images, function() {
$('')[0].src = this;
});
},
loadingGif: "/media/images/loading.gif",
editIcon: "/media/images/pencil.png",
getResourceById: function(type, id, callback) {
id = id.toString();
// if (typeof(cache[type][id]) !== "undefined") {
// callback.call(null, cache[type][id]);
// return;
// }
callApi("/api/" + type + "/" + id, "GET", null, function(data) {
if (typeof(data) === "undefined" || typeof(data.Error) === "undefined" || data.Error !== null) {
callback.call(null, null);
return;
}
cache[type][id] = data.Data[type + "s"][0];
callback.call(null, cache[type][id]);
});
}
};
}();
(function(){
var refreshCookie = "vgquotes.refreshFragment";
var refresh = function() {
var url = window.location.href;
var fragmentPosition = url.lastIndexOf("#");
if (fragmentPosition >= 0) {
if (fragmentPosition !== url.length - 1) {
$.cookie(refreshCookie, url.substring(fragmentPosition + 1)); //store the fragment in a cookie
}
url = url.substring(0, fragmentPosition);
}
window.location.href = url;
};
var applyFragmentFromCookie = function() {
var fragment = $.cookie(refreshCookie);
if (fragment !== null) {
window.location.href += "#" + fragment;
$.cookie(refreshCookie, null); //delete cookie
}
};
$(document).ready(applyFragmentFromCookie);
$.vgquotes.refresh = refresh;
}());
var setupSearch = function() {
var submitSearch = function() {
var searchQuery = $.trim($("#search-query").val());
if (searchQuery.length > 0) {
window.location = "/search/" + searchQuery;
}
return false;
};
$(document).ready(function() {
$("#search-query").keypress(function(e) {
if (e.which === 13) {
submitSearch();
}
});
$("#search-submit").click(submitSearch);
});
};
var setupVoting = function() {
var voting = false;
$(".vote-for, .vote-against").live("click", function() {
if (voting) {
alert("Please wait for the current vote to process before voting again");
return false;
}
voting = true;
var $votingLink = $(this);
var $container = $votingLink.parents(".quote-container");
var direction = $votingLink.hasClass("vote-for") ? 1 : 0;
var quoteId = $container.find("input.quote-id").val();
var $score = $container.find(".quote-score");
var score = $score.text();
$.ajax("/vote", {
type: "POST",
data: { QuoteId: quoteId, Direction: direction },
beforeSend: function() {
$score.empty().append($("").attr({ src: $.vgquotes.loadingGif, title: "submitting your vote" + String.fromCharCode(0x2026) }));
},
success: function(data, status, $xhr) {
if (data.Error !== null) {
window.alert(data.Error);
return;
}
score = data.Data.score;
$score.attr("title", "+" + data.Data.upVotes + ", -" + data.Data.downVotes);
//remove the voting arrow, and add the other one if needed
$votingLink.remove();
if (direction === 1) {
if ($container.find(".vote-against").length === 0) {
$("")
.addClass("vote-against")
.attr("title", "I hate this quote")
.text(String.fromCharCode(0x25BC))
.appendTo($container.find(".vote-container:last"));
}
} else {
if ($container.find(".vote-for").length === 0) {
$("")
.addClass("vote-for")
.attr("title", "I like this quote")
.text(String.fromCharCode(0x25B2))
.appendTo($container.find(".vote-container:first"));
}
}
},
complete: function() {
voting = false;
var $img = $score.find("img");
if ($img.length) {
$img.remove();
}
$score.text(score);
}
});
return false;
});
};
var setupReportLink = function() {
$(".quote-report-link").click(function() {
if ($(".report-dialog").length > 0) {
return false;
}
var $link = $(this);
var $container = $link.parents(".quote-container");
var quoteId = $container.find("input.quote-id").val();
var $row = $("
");
var flagTypes = [ [1, "Inaccurate"], [2, "Duplicate"], [3, "Spam"], [4, "Fake"], [0, "Other"] ];
for (var i = 0; i < flagTypes.length; i++) {
var html = "