// This file contains general-purpose LJ code
var LiveJournal = new Object;
// The hook mappings
LiveJournal.hooks = {};
LiveJournal.register_hook = function (hook, func) {
if (! LiveJournal.hooks[hook])
LiveJournal.hooks[hook] = [];
LiveJournal.hooks[hook].push(func);
};
// args: hook, params to pass to hook
LiveJournal.run_hook = function () {
var a = arguments;
var hookfuncs = LiveJournal.hooks[a[0]];
if (!hookfuncs || !hookfuncs.length) return;
var hookargs = [];
for (var i = 1; i < a.length; i++) {
hookargs.push(a[i]);
}
var rv = null;
hookfuncs.forEach(function (hookfunc) {
rv = hookfunc.apply(null, hookargs);
});
return rv;
};
LiveJournal.pageLoaded = false;
LiveJournal.initPage = function () {
// only run once
if (LiveJournal.pageLoaded)
return;
LiveJournal.pageLoaded = 1;
// set up various handlers for every page
LiveJournal.initPlaceholders();
LiveJournal.initLabels();
LiveJournal.initInboxUpdate();
LiveJournal.initAds();
LiveJournal.initPolls();
// run other hooks
LiveJournal.run_hook("page_load");
};
// Set up two different ways to test if the page is loaded yet.
// The proper way is using DOMContentLoaded, but only Mozilla supports it.
{
// Others
DOM.addEventListener(window, "load", LiveJournal.initPage);
// Mozilla
DOM.addEventListener(window, "DOMContentLoaded", LiveJournal.initPage);
}
// Set up a timer to keep the inbox count updated
LiveJournal.initInboxUpdate = function () {
// Don't run if not logged in or this is disabled
if (! Site || ! Site.has_remote || ! Site.inbox_update_poll) return;
// Don't run if no inbox count
var unread = $("LJ_Inbox_Unread_Count");
if (! unread) return;
// Update every five minutes
window.setInterval(LiveJournal.updateInbox, 1000 * 60 * 5);
};
// Do AJAX request to find the number of unread items in the inbox
LiveJournal.updateInbox = function () {
var postData = {
"action": "get_unread_items"
};
var opts = {
"data": HTTPReq.formEncoded(postData),
"method": "POST",
"onData": LiveJournal.gotInboxUpdate
};
opts.url = Site.currentJournal ? "/" + Site.currentJournal + "/__rpc_esn_inbox" : "/__rpc_esn_inbox";
HTTPReq.getJSON(opts);
};
// We received the number of unread inbox items from the server
LiveJournal.gotInboxUpdate = function (resp) {
if (! resp || resp.error) return;
var unread = $("LJ_Inbox_Unread_Count");
if (! unread) return;
unread.innerHTML = resp.unread_count ? " (" + resp.unread_count + ")" : "";
};
// Search for placeholders and initialize them
LiveJournal.initPlaceholders = function () {
var domObjects = document.getElementsByTagName("*");
var placeholders = DOM.filterElementsByClassName(domObjects, "LJ_Placeholder") || [];
Array.prototype.forEach.call(placeholders, function (placeholder) {
var parent = DOM.getFirstAncestorByClassName(placeholder, "LJ_Placeholder_Container", false);
if (!parent) return;
var container = DOM.filterElementsByClassName(parent.getElementsByTagName("div"), "LJ_Container")[0];
if (!container) return;
var html = DOM.filterElementsByClassName(parent.getElementsByTagName("div"), "LJ_Placeholder_HTML")[0];
if (!html) return;
var placeholder_html = unescape(html.innerHTML);
var placeholderClickHandler = function (e) {
Event.stop(e);
// have to wrap placeholder_html in another block, IE is weird
container.innerHTML = "" + placeholder_html + "";
DOM.makeInvisible(placeholder);
};
DOM.addEventListener(placeholder, "click", placeholderClickHandler);
return false;
});
};
// set up labels for Safari
LiveJournal.initLabels = function () {
// disabled because new webkit has labels that work
return;
// safari doesn't know what