/*
 * @(#) pwall.js
 */

var net;
if (!net)
    net = {};
else if (typeof net != 'object')
    throw new Error('net prefix exists but is not an object');
if (!net.pwall)
    net.pwall = {};
else if (typeof net.pwall != 'object')
    throw new Error('net.pwall prefix exists but is not an object');
if (net.pwall.Website)
    throw new Error('net.pwall.Website already exists');

net.pwall.Website = {

    topicClass: 'topic-all',
    hoverClass: 'std-hover',
    unhovClass: 'std-topic',

    topicHover: function(e) {
        e.className = net.pwall.Website.hoverClass + ' ' + net.pwall.Website.topicClass;
        window.status = "Go to " + e.parentNode.title;
        return true;
    },

    topicUnhov: function(e) {
        e.className = net.pwall.Website.unhovClass + ' ' + net.pwall.Website.topicClass;
        window.status = "";
        return true;
    },

    topicClick: function(a) {
        window.location = a;
    },

    pageLoad: function(h) {
        var r = document.referrer;
        if (r == null || r.length == 0)
            return;
        var i = h.length + 8;
        if (r.length >= i && r.substring(0, i) == 'http://' + h + '/')
            return;
        if (r.length >= 17) {
            var r17 = r.substring(0, 17);
            if (r17 == 'http://pwall.net/' || r17 == 'http://localhost:' ||
                    r17 == 'http://127.0.0.1:')
                return;
            if (r.length >= 21 && r.substring(0, 21) == 'http://www.pwall.net/')
                return;
        }
        var a = [ '<img src="http://', h, '/SP?r=', net.pwall.StringUtil.escapeURI(r),
                '" width="1" height="1" border="0">' ];
        document.write(a.join(''));
    },

    pageInit: function(name, domain, linkClass) {
        var addr = name + '@' + domain;
        var h = [ '<a href="mailto:', addr, '" class="', linkClass, '">', addr, '</a>' ];
        document.getElementById('email-addr').innerHTML = h.join('');
        net.pwall.Website.rewriteTopics();
    },

    rewriteTopics: function() {
        var topicNodes = document.getElementById('topic-column').childNodes;
        for (var i = 0; i < topicNodes.length; i++) {
            var topicNode = topicNodes[i];
            if (topicNode.nodeType == 1 && topicNode.tagName == 'DIV' &&
                    topicNode.className != 'topic-head') {
                var aNode = topicNode.getElementsByTagName('A')[0];
                var href = aNode.href;
                var content = '';
                var contentNodes = aNode.childNodes;
                for (var j = 0; j < contentNodes.length; j++) {
                    var contentNode = contentNodes[j];
                    if (contentNode.nodeType == 3)
                        content += contentNode.data;
                }
                var h = [ '<div class="', net.pwall.Website.unhovClass, ' ',
                        net.pwall.Website.topicClass, '" ',
                        'onmouseover="net.pwall.Website.topicHover(this)" ',
                        'onmouseout="net.pwall.Website.topicUnhov(this)" ',
                        'onclick="net.pwall.Website.topicClick(\'', href,
                        '\')">', content, '</div>' ];
                topicNode.innerHTML = h.join('');
            }
        }
    },

    initExpandChar: function() {
        var tds = document.getElementsByTagName('TD');
        for (var i = 0; i < tds.length; ++i) {
            var td = tds[i];
            if (td.className == 'code-entry-ch') {
                td.onmouseover = net.pwall.Website.expandChar;
            }
        }
    },

    expandTimeoutId: 0,

    expandClear: function() {
        if (net.pwall.Website.expandTimeoutId != 0) {
            window.clearTimeout(net.pwall.Website.expandTimeoutId);
            net.pwall.Website.expandTimeoutId = 0;
        }
        document.getElementById('expanded-char').style.visibility = 'hidden';
    },

    expandChar: function() {
        var td = this;
        net.pwall.Website.expandClear();
        var div = document.getElementById('expanded-char');
        div.innerHTML = td.innerHTML;
        var x = -12;
        var y = -12;
        for (;;) {
            x += td.offsetLeft;
            y += td.offsetTop;
            td = td.offsetParent;
            if (td.nodeName == 'BODY')
                break;
        }
        div.style.left = x;
        div.style.top = y;
        div.style.visibility = 'visible';
        net.pwall.Website.expandTimeoutId =
                window.setTimeout(net.pwall.Website.expandClear, 4000);
    }

};

net.pwall.Website.initClocks = function() {
    var clock = new net.pwall.Clock('clock0', 'GMT');
    clock.displayZone = net.pwall.Clock.displayZoneTooltip;
    net.pwall.Clock.clocks.push(clock);
    clock = new net.pwall.Clock('clock1', 'Pacific/Auckland');
    clock.displayZone = net.pwall.Clock.displayZoneTooltip;
    net.pwall.Clock.clocks.push(clock);
    clock = new net.pwall.Clock('clock2', 'Australia/Sydney');
    clock.displayZone = net.pwall.Clock.displayZoneTooltip;
    net.pwall.Clock.clocks.push(clock);
    clock = new net.pwall.Clock('clock3', 'Europe/London');
    clock.displayZone = net.pwall.Clock.displayZoneTooltip;
    net.pwall.Clock.clocks.push(clock);
    clock = new net.pwall.Clock('clock4', 'America/New_York');
    clock.displayZone = net.pwall.Clock.displayZoneTooltip;
    net.pwall.Clock.clocks.push(clock);
    net.pwall.Clock.refreshEachMinute();
    window.onfocus = net.pwall.Clock.refreshEachMinute;
    document.getElementById('pwall-clocks').style.display = 'block';
};
