function Async()
{
}

Async.request = function()
{
    var async;

    try {
        async = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (_) {
        try {
            async = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (_) {
            try {
                async = new XMLHttpRequest();
            } catch (_) {
                return null;
            }
        }
    }

    async.onreadystatechange = function()
    {
        if (async.readyState == 4)
            /*async.abort()*/;
    };

    return async;
}

Async.post = function(url, data)
{
    var async = Async.request();

    async.open(data ? "POST" : "GET", url, true);
    if (data)
        async.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    async.send(data);
}

Async.submit = function(form)
{
    var list = new Array;
    var items = form.elements;

    for (var i = 0; i < items.length; i++)
    {
        var act = encodeURIComponent(items[i].name) + '=';

        switch (items[i].nodeName.toLowerCase())
        {
            case "input":
                switch (items[i].type.toLowerCase())
                {
                    case "radio":
                    case "checked":
                        if (items[i].checked)
                            act += encodeURIComponent(items[i].value);
                        else
                            act = null;
                        break;
                    case "hidden":
                    case "password":
                    case "text":
                        act += encodeURIComponent(items[i].value);
                        break;
                    case "submit":
                        if (items[i].name && items[i].name.length)
                            act += encodeURIComponent(items[i].value);
                        else
                            act = null;
                        break;
                }
                break;
            case "select":
                act += encodeURIComponent(items[i].options[items[i].selectedIndex].value);
                break;
            case "textarea":
                act += encodeURIComponent(items[i].value);
                break;
        }

        if (act)
            list.push(act);
    }

    var data = list.join('&');

    if (form.method.toLowerCase() == "post")
        Async.post(form.action, data);
    else
        Async.post(A.amp(form.action, data), null);
}

function Event()
{
}

Event.listen = function(obj, event, handler)
{
    try
    {
        if (obj.addEventListener)
            obj.addEventListener(event, handler, false);
        else if (obj.attachEvent)
            obj.attachEvent("on" + event, handler);
        else
            obj["on" + event] = handler;
    }
    catch (_)
    {
    }
}

Event.forget = function(obj, event, handler)
{
    try
    {
        if (obj.removeEventListener)
            obj.removeEventListener(event, handler, false);
        else if (obj.detachEvent)
            obj.detachEvent("on" + event, handler);
        else
            obj["on" + event] = null;
    }
    catch (_)
    {
    }
}

Event.hover = function(span, over)
{
    if (span.children)
        for (var i = 0; i < span.children.length; i++)
        {
            var hover = span.children[i];

            if (hover.className == "hover")
                hover.style.visibility = over ? "visible" : "hidden";
        }
}

function A()
{
}

A.active = null;

A.onload = function()
{
    if (A.base && A.path)
    {
        var mover = new Function("A.over(this);");
        var mout = new Function("A.over();");
        var click = new Function("return A.click(this);");

        var links = document.links;
        var servlet = A.base + A.path;

        for (var i = 0; i < links.length; i++)
        {
            var a = links[i];
            var l = links.length;

            if (a.href.indexOf(servlet) == 0 && !Menu.uses(a) && a.href.indexOf("&escape") < 0)
            {
                if (!a.onmouseover)
                    a.onmouseover = mover;
                if (!a.onmouseout)
                    a.onmouseout = mout;
                if (!a.onclick)
                    a.onclick = click;
            }
            else if (a.href.indexOf("javascript:") == 0)
            {
                if (!a.onmouseover)
                    a.onmouseover = mover;
                if (!a.onmouseout)
                    a.onmouseout = mout;
            }

            i -= l - links.length;	// %% shame
        }
    }
}

A.amp = function(href, token, value)
{
    if (href && token)
    {
        var amp = href.indexOf('?') < 0 ? '?' : '&';
        if (value)
            amp += token + '=' + value;
        else
            amp += token;

        var hash = href.indexOf('#');
        if (hash < 0)
            href += amp;
        else
            href = href.substring(0, hash) + amp + href.substring(hash);
    }

    return href;
}

A.over = function(a)
{
    A.active = a;
}

A.click = function(a)
{
    a.focus();	// %% ough'ta sync

    var f = Focus.prior;
    if (f && typeof f.sync == 'function')
        f.sync();
    else
    {
        f = Focus.owner;
        if (f && typeof f.sync == 'function')
            f.sync();
    }

    a.href = Page.amp(a.href);

    if (Focus.after)
        a.href = A.amp(a.href, "focus", Focus.after.name + ',');
  /*else if (Focus.prior)
        a.href = A.amp(a.href, "focus", Focus.prior.name);*/

    return Past.click(a.href);
}

A.flip = function(list, from, over)
{
    var a = A.active;
    if (a)
    {
        var item = from;
        if (over)
            item += '-' + over;

        var flip = (a.className == "flip");

        Async.post(A.amp(A.base + A.path, list + "flip", item + ':' + (flip ? 0 : 1)), null);

        a.className = flip ? "flop" : "flip";

        a.style.visibility = "hidden";	// %% shame
        a.style.visibility = "visible";
    }
}

function Page()
{
}

Page.bground = null;

Page.onload = function()
{
    //Page.onresize();

    Page.state_();
}

Page.onresize = function()
{
    var body = document.getElementsByTagName("body")[0];

    if (!Page.bground)
        Page.bground = body.style.background;
    var bg = Page.bground;

    var innerW = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth;
    if (innerW < 1000)
    {
        var url = bg.indexOf(" url(");
        if (0 <= url)
            bg = bg.substring(0, url);
    }

    body.style.background = bg;
}

Page.state_ = function()
{
    if (Page.state)
    {
        var S = Page.state.split(',');

        for (var i = 0; i < S.length; i += 3)
        {
            var e = Page.__elementById(S[i]);
            if (e)
            {
                try
                {
                    e.scrollTop = (S[i+1] == "00") ? e.scrollHeight : parseInt(S[i+1]);
                    e.scrollLeft = (S[i+2] == "00") ? e.scrollWidth : parseInt(S[i+2]);
                }
                catch (_)
                {
                }
            }
        }
    }
}

Page.__elementById = function(id)
{
    var e = document.getElementById(id);

    if (!e && id && id.indexOf("body") == 0)	// %%
        e = document.documentElement;

    return e;
}

Page._state = function()
{
    if (Page.state)
    {
        var S = Page.state.split(',');

        for (var i = 0; i < S.length; )
        {
            var e = Page.__elementById(S[i]);
            if (e)
            {
                try
                {
                    var top = e.scrollTop, left = e.scrollLeft;

                    top = (top == e.scrollHeight) ? "00" : new String(top);
                    left = (left == e.scrollWidth) ? "00" : new String(left);

                    if (S[i+1] != top || S[i+2] != left)
                    {
                        S[i+1] = top;
                        S[i+2] = left;
                        i += 3;
                        continue;
                    }
                }
                catch (_)
                {
                }
            }

            S.splice(i, 3);
        }

        if (0 < S.length)
            return S.join(',');
    }

    return null;
}

Page.selection = function()
{
    if (window.getSelection)
        return window.getSelection();

    if (document.getSelection)
        return document.getSelection();

    if (document.selection)
        return document.selection.createRange().text;

    return null;
}

Page.amp = function(href)
{
    if (href && /[\?&]_?text$/.test(href))	// %% strict$
    {
        var s = Page.selection();
        if (s)
            href += '=' + encodeURIComponent(s.toString().replace(/[\r\n]/g, ' '));
    }

    var s = Page._state();
    if (s && href && href.indexOf("/.") < 0)
        href = A.amp(href, "state", s);

    return href;
}

Page.close = function(id)
{
    var div = document.getElementById(id);
    if (div)
    {
        div.style.display = "none";

        var fog = document.getElementById(id + 'F');
        if (fog)
            fog.style.display = "none";
    }
}

function Focus()
{
}

Focus.prior = Focus.owner = null;
Focus.after = null;

Focus.onload = function()
{
    if (Focus.state)
    {
        var e = document.getElementById(Focus.state);
        if (e)
        {
            e.focus();

            if (e.type == "select-one")
            {
                if (e.click)
                    e.click();	// %% shame
                else
                {
                    var click = document.createEvent("MouseEvents");
                    click.initEvent("click", true, false);
                    e.dispatchEvent(click);
                }
            }
        }
    }
}

Focus.over = function(input)
{
    if (typeof input == "string")
        input = document.getElementById(input);

    Focus.prior = Focus.owner;
    Focus.owner = input;
}

Focus.paste = function(chars, where)
{
    if (where)
    {
        var e = document.getElementById(where);
        if (e)
        {
            e.focus();
            Focus.prior = Focus.owner = e;	// %%

            e = Menu["M_" + where];
            if (e)
                e.dismiss();
        }
    }

    var f = Focus.prior;
    if (f)
    {
        if (typeof f.paste == 'function')
            f.paste(chars);

        else if (f.type == "text" || f.type == "textarea")
        {
            if (f.selectionStart || f.selectionEnd)
            {
                var start = f.selectionStart;

                f.value = f.value.substring(0, start) + chars + f.value.substring(f.selectionEnd);
                f.selectionStart = f.selectionEnd = start + chars.length;
            }
            else
                f.value += chars;

            f.focus();
        }
    }
}

function Button()
{
}

Button.active = null;

Button.over = function(button)
{
    Button.active = button;
}

Button.click = function(button)
{
    var form = button.form;

    form.action = Page.amp(form.action);
}

function Radio()
{
}

Radio.previa = function()
{
    var radio = A.active.previousSibling;	// %% fragile

    if (radio.type == "radio")
    {
        radio.checked = "checked";
        radio.focus();
    }
}

function Select()
{
}

Select.active = null;

Select.over = function(select)
{
    Select.active = select;
}

Select.change = function(select)
{
    var form = select.form;

    form.action = Page.amp(form.action);
    form.action = A.amp(form.action, "focus", select.name + ',');

    form.submit();
}

function Text()
{
}

Text.active = null;

Text.over = function(text)
{
    Text.active = text;
}

Text.keypress = function(text, event, action)
{
    var form = text.form;

    if (event.keyCode == 9)
        Focus.after = text;
    else if (event.keyCode == 13)
    {
        if (action)
            form.action = A.amp(form.action, action);
        else
            Focus.after = text;
    }
}

Text.keyup = function(text)
{
    if (text.readOnly)
        return;

    var e = document.getElementById("q_" + text.name);	// %% slow
    if (e)
    {
        var value = text.value;
        if (e.className == "q__" && value.length == 0)
            value = text.value = "?";

        e.style.display = (value.length == 0 || value.charAt(0) == '?') ? "none" : "inline";
    }
}

Text.blur = function(text, async)
{
    if (text.value != text.defaultValue)
        Text.change(text, async);

    Focus.over();
}

Text.change = function(text, async)
{
    if (Button.active)
    {
        var form = Button.active.form;
        if (form.hidden)
        {
            form.hidden.value = text.value;
            form.hidden.name = text.name;	// %%
        }
        //form.action = Page.amp(form.action);
        form.submit();
    }
    else
    {
        var form = text.form;

        if (A.active)
        {
            if (A.active.href.indexOf("javascript:") == 0)
                return;

            var i = A.active.href.indexOf(form.action);

            if (i == 0 || i == A.base.length)	// %%
            {
                form.action = A.active.href;
                A.active.onclick = new Function("return false;");
            }
        }

        form.action = Page.amp(form.action);

        var f;
        if (Select.active)
            f = Select.active.name;
        else if (Text.active && Text.active != text)
            f = Text.active.name;
        else if (Focus.after)
            f = Focus.after.name + ',';
        else if (Focus.owner)
            f = Focus.owner.name;
        else if (Focus.prior)
            f = Focus.prior.name;
        if (f)
            form.action = A.amp(form.action, "focus", f);

        if (async && !A.active)
            Async.submit(form);
        else
            form.submit();
    }
}

Menu.uses = function(a)
{
    var hash = a.href.indexOf("#M_");
    if (hash < 0)
        return false;

    var id = a.href.substring(hash + 1);

    if (!Menu[id])
        Menu[id] = new Menu(id);

    if (!a.onmouseover)
        a.onmouseover = function(event) { Menu[id].expose(event ? event : window.event); };
    if (!a.onmouseout)
        a.onmouseout = function(event) { Menu[id].dismiss(); };
    if (!a.onclick)
        a.onclick = function(event) { Menu[id].hurry(); };

    a.removeAttribute("href");

    return true;
}

Menu.duration = 200;
Menu.interval = 20;
Menu.moving = true;
Menu.fading = true;

function Menu(id)
{
    this.id = id;

    this.horizontal = false;

    this.over = false;
    this.open = false;

    this.waiter = null;
    this.started = 0;
    this.slider = null;

    this.menu = document.getElementById(this.id);

    this.items = this.menu.firstChild;
    while (this.items.className != "items")
        this.items = this.items.nextSibling;

    this.shield = this.items.lastChild;
    while (this.shield && this.shield.className != "shield")
        this.shield = this.shield.previousSibling;

    this.shadow = this.items.lastChild;
    while (this.shadow && this.shadow.className != "shadow")
        this.shadow = this.shadow.previousSibling;

    this.menu.onmouseover = function(event) { Menu[id].expose(); };
    this.menu.onmouseout = function(event) { Menu[id].dismiss(); };
}

Menu.prototype.init = function(x, y)
{
    var style = this.menu.style;

    style.width = "100%";
    style.height = "100%";

    var top = y, left = x, width = this.items.offsetWidth, height = this.items.offsetHeight;

    var innerW = window.innerWidth ? window.innerWidth : document.documentElement.clientWidth;
    var innerH = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;

    var grow;

    if (this.horizontal)
    {
        grow = (left + width <= innerW);
        this.home = grow ? -width : width;
    }
    else
    {
        grow = (top + height <= innerH);
        this.home = grow ? -height : height;
    }

    this.accel = -this.home / Menu.duration / Menu.duration;
    this.acce1 = -1.0 / Menu.duration / Menu.duration;

    if (this.horizontal)
        top -= Math.min(top, Math.max(height / 2, top + height - innerH));
    else if (!grow)
        top -= height;

    if (!this.horizontal)
        left -= Math.min(left, Math.max(width / 2, left + width - innerW));
    else if (!grow)
        left -= width;

    style.top = top + "px";
    style.left = left + "px";
    style.width = width + "px";
    style.height = height + "px";
}

Menu.prototype.expose = function(event)
{
    if (event)
    {
        var x = 0, y = 0;

        if (event.pageX || event.pageY)
        {
            x = event.pageX;
            y = event.pageY;
        }
        else if (event.clientX || event.clientY)
        {
            var body = document.body, html = document.documentElement;

            x = event.clientX + body.scrollLeft + html.scrollLeft;
            y = event.clientY + body.scrollTop + html.scrollTop;
        }

        this.init(x, y);
    }

    this.over = true;

    if (this.waiter)
        window.clearTimeout(this.waiter);

    this.waiter = window.setTimeout("Menu." + this.id + ".show()", 600);
}

Menu.prototype.exposeAt = function(id, x, y)
{
    var e = document.getElementById(id);
    if (e)
    {
        if (e.getBoundingClientRect)
        {
            var r = e.getBoundingClientRect();

            x += r.left;
            y += r.top;
        }
        else if (e.offsetParent)
        {
            do
            {
                x += e.offsetLeft;
                y += e.offsetTop;
            }
            while (e = e.offsetParent);
        }
    }

    this.init(x, y);

    this.accel = -this.home;

    this.open = this.over = true;

    this.position(0, 1.0);
    this.visible(true);
}

Menu.prototype.dismiss = function()
{
    if (this.shield && this.slider)	// %% spurious Ff 1.0.7
        return;

    this.over = false;

    if (this.waiter)
        window.clearTimeout(this.waiter);

    this.waiter = window.setTimeout("Menu." + this.id + ".hide()", 100);
}

Menu.prototype.show = function()
{
    if (this.waiter)
        this.waiter = window.clearTimeout(this.waiter);

    if (!this.open && !this.slider)
        this.start(true);
}

Menu.prototype.hide = function()
{
    if (this.waiter)
        this.waiter = window.clearTimeout(this.waiter);

    if (this.open && !this.slider)
        this.start(false);
}

Menu.prototype.start = function(open)
{
    this.open = open;

    if (open)
    {
        this.position(this.home, 0.0);
        this.visible(true);
    }

    this.started = (new Date()).getTime();
    this.slider = window.setInterval("Menu." + this.id + ".slide()", Menu.interval);
}

Menu.prototype.slide = function()
{
    var elapsed = (new Date()).getTime() - this.started;

    if (Menu.duration < elapsed)
        this.stop();
    else
    {
        var t2 = Math.pow(Menu.duration - elapsed, 2);

        var px = Math.round(t2 * this.accel);
        var op = t2 * this.acce1;

        this.position(this.open ? -px : this.home + px, this.open ? 1.0 + op : -op);
    }
}

Menu.prototype.hurry = function()
{
    if (this.waiter)
        this.waiter = window.clearTimeout(this.waiter);

    this.accel = -this.home;
    this.acce1 = -1.0;

    if (this.open)
        this.hide();
    else
        this.show();
}

Menu.prototype.stop = function()
{
    this.slider = window.clearInterval(this.slider);

    this.position(this.open ? 0 : this.home, this.open ? 1.0 : 0.0);

    if (!this.open)
        this.visible(false);

    if ((this.open && !this.over) || (!this.open && this.over))
        this.start(!this.open);
}

Menu.prototype.visible = function(visible)
{
    var d = visible ? "block" : "none";

    if (this.shield)
        this.shield.style.display = d;
    if (this.shadow)
        this.shadow.style.display = d;

    this.menu.style.visibility = visible ? "visible" : "hidden";
}

Menu.prototype.position = function(px, op)
{
    if (Menu.moving && (!this.shadow || this.open))
        this.items.style[this.horizontal ? "left" : "top"] = px + "px";
    if (Menu.fading)
        this.menu.style.opacity = op;
}

Menu.flip = function(hide, show, menu)
{
    hide = document.getElementById(hide);
    if (hide)
        hide.style.display = "none";

    show = document.getElementById(show);
    if (show)
        show.style.display = "block";

    menu = Menu[menu].menu;
    if (menu)
        menu.style.width = menu.style.height = "100%";
}

Idle.duration = 0;
Idle.interval = 0;

function Idle()
{
}

Idle.clock = null;
Idle.steps = 0;

Idle.start = function(n, m)
{
    Idle.stop();

    if (0 < n && 0 < m)
    {
        Idle.duration = n;
        Idle.interval = m;

        Idle.clock = window.setInterval("Idle.step()", Idle.interval * 60 * 1000);
    }
}

Idle.step = function()
{
    var n = ++Idle.steps;

    if (Idle.duration <= n)
        Idle.stop();

    Async.post(A.amp(A.base + A.path, "idle", n * Idle.interval), null);
}

Idle.stop = function()
{
    if (Idle.clock)
        Idle.clock = window.clearInterval(Idle.clock);

    Idle.steps = 0;
}

Past.times = [ "/bodza/page/past.html", "/bodza/page/present.html" ];
Past.now = 0;
Past.loaded = false;
Past.href = null;

function Past()
{
    if (!Past.href)
        Past.href = A.base + A.path + "?pan=-1";

    location.replace(Past.href);
    Past.href = null;
}

Past.onload = function()
{
    var iframe = frames["hi_frame"];

    if (iframe)
    {
        var state = document.forms["hi_state"].hidden;

        if (state.value == "init")
        {
            state.value = state.defaultValue = "";

            Past.load();
        }
        else if (0 <= iframe.location.href.lastIndexOf(Past.times[0]))
            history.forward();

        Past.loaded = true;
    }
}

Past.load = function()
{
    if (Past.now < Past.times.length)
    {
        var url = Past.times[Past.now++];

        var iframe = document.getElementById("hi_frame");

        if (iframe.readyState)	// IE
        {
            complete = false;
            iframe.onreadystatechange = function()
            {
                if (iframe.readyState == "complete")
                {
                    iframe.onreadystatechange = null;
                    Past.load();
                }
            };
        }
        else
            iframe.onload = Past.load;

        iframe.src = url;
    }
}

Past.click = function(href)
{
    if (frames["hi_frame"])
    {
        Past.href = href;
        history.back();
        return false;
    }

    return true;
}

Past.onunload = function()
{
    Past.loaded = false;
}

window.onload = function()
{
    if (Editor)
        Editor.onload();

    A.onload();

    Page.onload();
    Focus.onload();
    Past.onload();
}

//window.onresize = Page.onresize;

window.onunload = function()
{
    Past.onunload();
}

