var SHOW_POPUP_LABELS = false;
function id(C) {
    if (arguments.length > 1) {
        for (var D = 0,
        A = [], B = arguments.length; D < B; D++) {
            A.push(id(arguments[D]))
        }
        return A
    }
    if (typeof C == "string") {
        C = document.getElementById(C)
    }
    return C
}
Array.prototype.contains = function(C) {
    for (var B = 0,
    A = this.length; B < A; B++) {
        if (C == this[B]) {
            return true
        }
        if (C.equals) {
            if (C.equals(this[B])) {
                return true
            }
        }
    }
    return false
};
Array.prototype.remove = function(D) {
    var B = -1;
    for (var C = 0,
    A = this.length; C < A; C++) {
        if (D == this[C]) {
            B = C
        } else {
            if (D.equals) {
                if (D.equals(this[C])) {
                    B = C
                }
            }
        }
        if (B != -1) {
            return this.splice(B, 1)
        }
    }
};
var Obj = {
    toQueryString: function(D) {
        assert(D, "Obj.toQueryString: Argument is undefined.");
        var C = [];
        for (var B in D) {
            var A = D[B];
            if (!A) {
                if ((typeof A == "undefined") || (A === null)) {
                    A = ""
                }
                if (A === false) {
                    A = "false"
                }
            }
            C.push(B + "=" + encodeURIComponent(A))
        }
        return C.join("&")
    }
};
var E = {
    hide: function(A) {
        A = id(A);
        if (A) {
            A.style.display = "none"
        }
        return A
    },
    show: function(A) {
        A = id(A);
        if (A) {
            A.style.display = ""
        }
        return A
    },
    getElementsByClassName: function(D, H) {
        H = id(H) || document.body;
        if (H.getElementsByClassName) {
            return H.getElementsByClassName(D)
        }
        var F = H.getElementsByTagName("*");
        var B = [],
        A;
        for (var G = 0,
        C = F.length; G < C; G++) {
            A = F[G];
            if (E.hasClassName(A, D)) {
                B.push(A)
            }
        }
        return B
    },
    getElement: function(G, B, D) {
        G = id(G) || document.body;
        var C = G.getElementsByTagName(B);
        if (C.length) {
            if (!D) {
                return C[0]
            }
            for (var F = 0,
            A = C.length; F < A; F++) {
                var H = C[F];
                if (E.hasClassName(H, D)) {
                    return H
                }
            }
        }
        return null
    },
    nodeBefore: function(A) {
        function B(D) {
            return ! (/[^\t\n\r ]/.test(D.data))
        }
        function C(D) {
            return (D.nodeType == 8) || ((D.nodeType == 3) && B(D))
        }
        while ((A = A.previousSibling)) {
            if (!C(A)) {
                return A
            }
        }
        return null
    },
    childOf: function(B, A) {
        B = id(B),
        A = id(A);
        while (B = B.parentNode) {
            if (B == A) {
                return true
            }
        }
        return false
    },
    hasClassName: function(B, A) {
        B = id(B);
        assert(B, "hasClassName: no element.");
        var C = B.className;
        if (C.length == 0) {
            return false
        }
        if (C == A || C.match(new RegExp("(^|\\s)" + A + "(\\s|$)"))) {
            return true
        }
        return false
    },
    addClassName: function(B, A) {
        B = id(B);
        var D = B.className.trim();
        if (!E.hasClassName(B, A)) {
            if (D.length == 0) {
                B.className = A
            } else {
                var C = D.split(" ");
                assert(!C.contains(A), "addClassName: Element already has class: " + A);
                C.push(A);
                B.className = C.join(" ")
            }
        }
        return B
    },
    removeClassName: function(B, A) {
        B = id(B);
        var D = B.className.trim();
        if (E.hasClassName(B, A)) {
            if (D == A) {
                B.className = ""
            } else {
                var C = D.split(" ");
                assert(C.contains(A), "removeClassName: Element doesn't have class: " + A);
                C.remove(A);
                B.className = C.join(" ")
            }
        }
        return B
    },
    cumulativeOffset: function(A) {
        var B = 0,
        C = 0;
        do {
            B += A.offsetTop || 0;
            C += A.offsetLeft || 0;
            A = A.offsetParent
        } while ( A );
        return [C, B]
    }
};
var Evt = {
    KEY_BACKSPACE: 8,
    KEY_TAB: 9,
    KEY_RETURN: 13,
    KEY_ESC: 27,
    KEY_LEFT: 37,
    KEY_UP: 38,
    KEY_RIGHT: 39,
    KEY_DOWN: 40,
    KEY_DELETE: 46,
    KEY_HOME: 36,
    KEY_END: 35,
    KEY_PAGEUP: 33,
    KEY_PAGEDOWN: 34,
    KEY_INSERT: 45,
    target: function(A) {
        return A.target || A.srcElement
    },
    stop: function(A) {
        A.preventDefault();
        A.stopPropagation()
    },
    observe: function(B, D, C) {
        if (B.addEventListener) {
            B.addEventListener(D, C, false)
        } else {
            if (!C.$$guid) {
                C.$$guid = Evt.guid++
            }
            if (!B.events) {
                B.events = {}
            }
            var A = B.events[D];
            if (!A) {
                A = B.events[D] = {};
                if (B["on" + D]) {
                    A[0] = B["on" + D]
                }
            }
            A[C.$$guid] = C;
            B["on" + D] = handleEvent
        }
    },
    unobserve: function(A, C, B) {
        if (A.removeEventListener) {
            A.removeEventListener(C, B, false)
        } else {
            if (A.events && A.events[C]) {
                delete A.events[C][B.$$guid]
            }
        }
    }
};
Evt.guid = 1;
function handleEvent(D) {
    var B = true;
    D = D || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
    var A = this.events[D.type];
    for (var C in A) {
        this.$$handleEvent = A[C];
        if (this.$$handleEvent(D) === false) {
            B = false
        }
    }
    return B
}
function fixEvent(A) {
    A.preventDefault = fixEvent.preventDefault;
    A.stopPropagation = fixEvent.stopPropagation;
    return A
}
fixEvent.preventDefault = function() {
    this.returnValue = false
};
fixEvent.stopPropagation = function() {
    this.cancelBubble = true
};
var Cookie = {
    get: function(B, F) {
        var H = document.cookie;
        if (H) {
            var G = H.split(";");
            for (var C = 0,
            A = G.length; C < A; C++) {
                var D = G[C].trim().split("=");
                if (D[0] == B) {
                    return D[1]
                }
            }
        }
        return F || null
    },
    set: function(B, D, F) {
        var C = "";
        if (F) {
            var A = new Date();
            A.setTime(A.getTime() + (F * 24 * 60 * 60 * 1000));
            C = "; expires=" + A.toGMTString()
        }
        document.cookie = B + "=" + D + C
    }
};
var Params = {
    get: function(B, C, G) {
        if (!B) {
            B = location.search
        }
        if (B) {
            B = B.substring(1);
            var H = B.split("&");
            for (var D = 0,
            A = H.length; D < A; D++) {
                var F = H[D].trim().split("=");
                if (F[0] == C) {
                    return F[1] || G
                }
            }
        }
        return G || null
    }
};
if (typeof XMLHttpRequest == "undefined") {
    XMLHttpRequest = function() {
        return new ActiveXObject(navigator.userAgent.indexOf("MSIE 5") >= 0 ? "Microsoft.XMLHTTP": "Msxml2.XMLHTTP")
    }
}
function ajax(F) {
    F = {
        type: F.type || "POST",
        url: F.url || "",
        params: F.params || "",
        timeout: F.timeout || 5000,
        onComplete: F.onComplete ||
        function() {},
        onError: F.onError ||
        function() {},
        onSuccess: F.onSuccess ||
        function() {},
        data: F.data || ""
    };
    var D = new XMLHttpRequest();
    var C = F.url;
    if (F.params) {
        C += "?" + F.params
    }
    D.open(F.type, C, true);
    var A = F.timeout;
    var G = false;
    setTimeout(function() {
        G = true
    },
    A);
    D.onreadystatechange = function() {
        if (D.readyState == 4 && !G) {
            if (B(D)) {
                F.onSuccess(D)
            } else {
                F.onError()
            }
            F.onComplete();
            D = null
        }
    };
    if (F.type == "POST") {
        if (D.overrideMimeType && (navigator.userAgent.match(/Gecko\/(\d{4})/) || [0, 2005])[1] < 2005) {
            D.setRequestHeader("Connection", "close")
        }
    }
    D.setRequestHeader("Cache-Control", "no-cache");
    D.send(null);
    function B(H) {
        try {
            return ! H.status && location.protocol == "file:" || (H.status >= 200 && H.status < 300) || H.status == 304 || navigator.userAgent.indexOf("Safari") >= 0 && typeof H.status == "undefined"
        } catch(I) {}
        return false
    }
    return D
}
var Layout = {
    width: 0,
    height: 0,
    _scratch: [0, 0],
    getSize: function(A) {
        if (!A) {
            A = []
        }
        if (typeof(window.innerWidth) == "number") {
            A[0] = window.innerWidth;
            A[1] = window.innerHeight
        } else {
            if (document.documentElement && document.documentElement.clientWidth) {
                A[0] = document.documentElement.clientWidth;
                A[1] = document.documentElement.clientHeight
            } else {
                if (document.body && document.body.clientWidth) {
                    A[0] = document.body.clientWidth;
                    A[1] = document.body.clientHeight
                } else {
                    assert(false, "Don't know how to get window w x h")
                }
            }
        }
        return A
    },
    checkSize: function() {
        Layout.getSize(Layout._scratch);
        if ((Layout.width == Layout._scratch[0]) && (Layout.height == Layout._scratch[1])) {
            return
        }
        Layout.width = Layout._scratch[0];
        Layout.height = Layout._scratch[1];
        if (typeof Layout.resized == "function") {
            Layout.resized()
        }
    },
    init: function() {
        Evt.observe(window, "resize", this.checkSize);
        this.checkSize()
    }
};
if (typeof HTMLElement != "undefined") {
    if (HTMLElement.prototype.__defineGetter__) {
        HTMLElement.prototype.__defineGetter__("innerText",
        function() {
            return (this.textContent)
        });
        HTMLElement.prototype.__defineSetter__("innerText",
        function(A) {
            this.textContent = A
        })
    }
}
function assert(A, B) {
    if (!A) {
        alert("Assertion Failed: " + B)
    }
}
function isIE() {
    return !! (window.attachEvent && !window.opera)
}
function isGecko() {
    return navigator.userAgent.indexOf("Gecko") > -1 && navigator.userAgent.indexOf("KHTML") == -1
}
function isWebKit() {
    return navigator.userAgent.indexOf("AppleWebKit/") > -1
}
function isMobileSafari() {
    return !! navigator.userAgent.match(/Apple.*Mobile.*Safari/)
}
String.prototype.trim = function() {
    var A = this;
    A = A.replace(/^\s*(.*)/, "$1");
    A = A.replace(/(.*?)\s*$/, "$1");
    return A
};
String.prototype.htmlEncode = function() {
    var B = this;
    var A = B.length;
    var D = "";
    for (var C = 0; C < A; C++) {
        var F = B.charAt(C);
        if (F == "&") {
            D += "&amp;"
        } else {
            if (F == "<") {
                D += "&lt;"
            } else {
                if (F == ">") {
                    D += "&gt;"
                } else {
                    D += F
                }
            }
        }
    }
    return D
};
function create(B, A) {
    var C = document.createElement(B);
    if (A) {
        C.className = A
    }
    return C
}
function createMeaning(A) {
    A.toElement = function() {
        var B = create("LI");
        assert(this.display, "Meaning has no display property: " + this.query);
        B.innerHTML = this.display.htmlEncode();
        B.query = this.query;
        return B
    };
    return A
}
function createMatch(B, A) {
    B.toElement = function() {
        var K = create("TD", "thumb");
        var C = create("A");
        C.href = this.url;
        var F = create("IMG");
        F.src = this.image_url;
        C.appendChild(F);
        K.appendChild(C);
        var G = create("TD", "desc");
        var J = create("DIV", "t");
        C = create("A");
        C.href = this.url;
        C.innerHTML = this.title;
        J.appendChild(C);
        G.appendChild(J);
        var H = create("P");
        H.innerHTML = this.snippet;
        G.appendChild(H);
        C = create("A", "url");
        C.title = this.url;
        var I = this.url;
        if (I.length > A) {
            I = I.substr(0, A) + "..."
        }
        I = I.replace(/^http:\/\//, "");
        C.innerHTML = I;
        G.appendChild(C);
        var D = create("DIV", "stack");
        D.innerHTML = '<a href="#">View similar</a><span>|</span><img src="/static/images/new/btn-back-off.png" /> <img src="/static/images/new/btn-fwd.png" /><span>1 of 9</span>';
        G.appendChild(D);
        return [K, G]
    };
    return B
}
var gAjaxReq = null;
function send(C) {
    if (gAjaxReq) {
        return
    }
    var B = C.q.trim();
    if ("" == B) {
        MsgBox.show("What would you like to find?");
        id("fld_q").select();
        return
    }
    MsgBox.clear();
    assert(C.sl, "No snippet length supplied.");
    var A = Obj.toQueryString(C);
    location.href = "/search?" + A
}
var TOTAL_TABS = 6;
var VISIBLE_TABS = 4;
function showResults(b) {
    var S = id("div_status");
    var Z = id("b_sq");
    var D = id("b_iq");
    function M(h, k) {
        var o = id("td_more");
        if (!o) {
            return
        }
        E.show(o);
        var n = id("ul_more");
        n.innerHTML = "";
        for (var l = k,
        j = h.length; l < j; l++) {
            var g = createMeaning(h[l]);
            var a = g.toElement();
            n.appendChild(a)
        }
        addEndcap(n)
    }
    function N() {
        var a = "";
        if (b.actual_docs > 0) {
            var g = b.meaning || b.query;
            a = "Results for <b>" + g.htmlEncode() + "</b>";
            a += " | " + b.num_docs_formatted + " matches"
        } else {
            assert(b.query, "Query is undefined.");
            var h = b.meaning || b.query;
            a = "No results for <b>" + h.htmlEncode() + "</b>."
        }
        S.innerHTML = a;
        if (Z) {
            Z.innerHTML = b.snippet_quality
        }
        if (D) {
            D.innerHTML = b.index_quality
        }
    }
    gQuery = b.query;
    gMeaning = b.meaning;
    var H = id("a_debug");
    if (H) {
        var U = H.href;
        var L = encodeURIComponent(gMeaning || gQuery);
        H.href = U.substring(0, U.indexOf("q=") + 2) + L
    }
    if (b.suggestion) {
        var J = "/search?q=" + encodeURIComponent(b.suggestion);
        MsgBox.show('Did you mean <a href="' + J + '">' + b.suggestion + "</a>?")
    }
    assert(b.snippet_length == gSnipLength, "Server and browser don't agree on snippet length: server=" + b.snippet_length + ", browser=" + gSnipLength);
    var e = b.meanings;
    if (e && e.length) {
        var G = e.length;
        E.hide("td_more");
        var B = id("tbl_tabs");
        var I = B.rows[0];
        var F = I.cells;
        for (var V = 0,
        O = F.length; V < O; V++) {
            var K = F[V];
            if (K.className != "tab") {
                continue
            }
            K.style.display = "none"
        }
        var d = Math.min(VISIBLE_TABS, TOTAL_TABS);
        d = Math.min(d, G);
        var P = G - d;
        var C = TOTAL_TABS - VISIBLE_TABS;
        assert(C > 0, "VISIBLE_TABS is too high: " + VISIBLE_TABS);
        if ((P > 0) && (P <= C)) {
            d += P
        }
        for (V = 0; V < G; V++) {
            var Y = e[V];
            if (V >= d) {
                M(e, V);
                break
            }
            K = F[V];
            var c = K.childNodes[0];
            c.innerHTML = Y.display.htmlEncode();
            c.query = Y.query;
            if (gMeaning == Y.query) {
                E.addClassName(c, "sel")
            } else {
                E.removeClassName(c, "sel")
            }
            E.show(K)
        }
        E.show("div_tabs")
    } else {
        E.hide("div_tabs")
    }
    var T = id("tbl_1");
    T.innerHTML = "";
    var X = id("tbl_2");
    X.innerHTML = "";
    if (b.results) {
        var W = b.results;
        var Q = W.length;
        for (V = 0; V < Q; V++) {
            var A = T.insertRow( - 1);
            if (V != 0) {
                A.className = "nf"
            }
            var R = createMatch(W[V], 64).toElement();
            A.appendChild(R[0]);
            A.appendChild(R[1])
        }
        rspan = parseInt((Q + 1) / 2);
        for (V = 0; V < Q; V += 2) {
            A = X.insertRow( - 1);
            if (V != 0) {
                A.className = "nf"
            }
            R = createMatch(W[V], 32).toElement();
            A.appendChild(R[0]);
            A.appendChild(R[1]);
            if (V == 0) {
                var K = create("TD", "gutter");
                K.rowSpan = rspan;
                var f = create("IMG", "gutter");
                f.src = "/static/images/spacer.gif";
                f.style.height = "1px";
                K.appendChild(f);
                A.appendChild(K)
            }
            if (V + 1 < Q) {
                R = createMatch(W[V + 1], 32).toElement();
                A.appendChild(R[0]);
                A.appendChild(R[1])
            } else {
                K = create("TD");
                K.colSpan = 2;
                A.appendChild(K)
            }
        }
    }
    if (b.query) {
        id("fld_q").value = b.query
    }
    N();
    if (b.actual_docs > 0) {
        id("ftr").style.visibility = "visible"
    } else {
        id("ftr").style.visibility = "hidden"
    }
    Layout.recalcLayout()
}
function Controller(L, W, D) {
    E.hide("td_no_js");
    this.doQuery = function(Y) {
        var X = id("fld_q");
        X.value = Y;
        send({
            q: X.value,
            sl: gSnipLength
        })
    };
    function S() {
        function b() {
            if (!E.hasClassName(this, "sel")) {
                send({
                    q: gQuery,
                    sl: gSnipLength,
                    m: this.query
                })
            }
            return false
        }
        var X = id("tbl_tabs");
        var g = X.rows[0];
        var f = g.cells;
        for (var c = 0,
        d = f.length; c < d; c++) {
            var Z = f[c];
            var e = Z.childNodes[0];
            if (e.tagName == "A") {
                e.onclick = b;
                var Y = Params.get(e.search, "m");
                if (Y) {
                    e.query = decodeURIComponent(Y)
                }
            }
        }
    }
    function T(Y, f) {
        var a = id("sel_more");
        if (a) {
            E.hide("td_no_js");
            var X = a.options;
            var g = X.length;
            for (var e = 0; e < g; e++) {
                var Z = X[e];
                if (Z.value === "") {
                    continue
                }
                var c = createMeaning({
                    display: Z.text,
                    query: Z.value
                });
                Y.appendChild(c.toElement())
            }
            addEndcap(Y);
            E.show(f)
        }
        f.onclick = function() {
            var l = E.cumulativeOffset(f);
            var k = Y.offsetWidth;
            Y.style.left = (l[0] + f.offsetWidth + 2 - k) + "px";
            Y.style.top = (l[1] + f.offsetHeight - 2) + "px";
            Y.activate();
            return false
        };
        var b = function(k) {
            Y.deactivate();
            Evt.stop(k)
        };
        var h = function(l) {
            var k = Evt.target(l);
            if ((k == f) || E.childOf(k, f) || (k == Y) || E.childOf(k, Y)) {
                return
            }
            Y.deactivate();
            Evt.stop(l)
        };
        var d = null;
        var j = function(l) {
            var k = Evt.target(l);
            if (k.tagName == "UL") {
                return
            }
            while (k.tagName != "LI") {
                k = k.parentNode
            }
            if (E.hasClassName(k, "cap")) {
                return
            }
            E.addClassName(k, "focused");
            if (d) {
                E.removeClassName(d, "focused")
            }
            d = k
        };
        Y.activate = function() {
            this.style.visibility = "visible";
            Evt.observe(document, "keydown", b);
            Evt.observe(document, "click", h);
            Evt.observe(this, "mouseover", j)
        };
        Y.deactivate = function() {
            d = null;
            this.style.visibility = "hidden";
            Evt.unobserve(document, "keydown", b);
            Evt.unobserve(document, "click", h);
            Evt.unobserve(this, "mouseover", j);
            for (var m = 0,
            l = this.childNodes.length; m < l; m++) {
                var k = this.childNodes[m];
                if (k.tagName != "LI") {
                    continue
                }
                E.removeClassName(k, "focused")
            }
        };
        Y.onclick = function(k) {
            k = k || window.event;
            var l = Evt.target(k);
            if (l.tagName != "LI" || !l.query) {
                this.deactivate();
                return false
            }
            send({
                q: gQuery,
                sl: gSnipLength,
                m: l.query
            });
            this.deactivate();
            return false
        }
    }
    function K() {
        var Z = E.getElementsByClassName("stack");
        for (var Y = 0,
        X = Z.length; Y < X; Y++) {
            new StackController(Z[Y])
        }
    }
    id("fld_q").onclick = function() {};
    var Q = false;
    var F = Cookie.get("cols", 3);
    var P = Params.get(location.search, "cols", F);
    if (P != "1" && P != "2" && P != "3") {
        P = F
    }
    var G = P;
    var N = id("ul_more");
    var U = id("td_more");
    document.body.style.overflow = "hidden";
    id("bdy").style.overflowY = "auto";
    id("bdy").style.height = "10px";
    Layout.resized = function() {
        if (isMobileSafari()) {
            return
        }
        var c = this.height;
        var b = id("hdr").offsetHeight;
        var Y = id("ftr").offsetHeight;
        var a = (c - Y - b);
        var Z = id("bdy");
        Z.style.height = a + "px";
        if (!Q) {
            return
        }
        var X = id("bdy").offsetWidth;
        if (3 == P) {
            if ((G == 3) && (X < 986)) {
                I(2)
            } else {
                if ((G != 3) && (X > 986)) {
                    I(3)
                }
            }
        }
        N.style.visibility = "hidden"
    };
    Layout.recalcLayout = Layout.resized;
    Layout.init();
    K();
    if (U) {
        T(N, U)
    }
    id("form_q").onsubmit = function() {
        if (gPopup) {
            gPopup.hide();
            if (gPopup.cancelSubmit) {
                gPopup.cancelSubmit = false;
                return false
            }
        }
        send({
            q: id("fld_q").value,
            sl: gSnipLength
        });
        return false
    };
    function V(X) {
        assert(X == 1 || X == 2 || X == 3, "Invalid column option. Valid options are 1, 2, 3.");
        var a = id("accordion");
        if (!a) {
            return
        }
        id("accordion").parentNode.removeChild(a);
        switch (parseInt(X)) {
        case 1:
            col1 = id("col1_r");
            col1.appendChild(a);
            break;
        case 2:
            var Z = id("col2_r");
            Z.insertBefore(a, Z.firstChild);
            break;
        case 3:
            var Y = id("col3_r");
            Y.insertBefore(a, Y.firstChild)
        }
    }
    function I(a, Y) {
        var X = id("tbl_2");
        var Z = id("tbl_3");
        if (!X || !Z) {
            return
        }
        if (a == 1) {
            assert(false, "Shouldn't get here because 1	column mode has	been disabled.");
            E.hide(X);
            E.hide(Z)
        } else {
            if (a == 2) {
                E.show(X);
                E.hide(Z)
            } else {
                E.show(Z);
                E.hide(X)
            }
        }
        G = a;
        A.select(a == 2);
        H.select(a == 3);
        V(a);
        if (Y) {
            P = a;
            Cookie.set("cols", a, 60)
        }
    }
    function B(X, Z) {
        gSnipLength = X;
        J.select("short" == X);
        R.select("med" == X);
        C.select("long" == X);
        if (!Z) {
            return
        }
        var a = 0;
        elems = E.getElementsByClassName("sel", "pages");
        if (elems.length > 0) {
            var Y = elems[0];
            assert(Y.tagName == "A", "Selected element is not a link.");
            if (typeof Y.index == "number") {
                a = Y.index
            }
        }
        send({
            q: gQuery,
            m: gMeaning,
            sl: X,
            pi: a
        })
    }
    var M = id("a_c1");
    var A = id("a_c2");
    var H = id("a_c3");
    var J = id("a_short");
    var R = id("a_med");
    var C = id("a_long");
    A.select = H.select = J.select = R.select = C.select = function(X) {
        if (X) {
            E.addClassName(this, "sel")
        } else {
            E.removeClassName(this, "sel")
        }
    };
    A.onclick = function() {
        if (!E.hasClassName(this, "sel")) {
            I(2, true)
        }
        return false
    };
    H.onclick = function() {
        if (!E.hasClassName(this, "sel")) {
            I(3, true)
        }
        return false
    };
    J.onclick = function() {
        if (!E.hasClassName(this, "sel")) {
            B("short", true)
        }
        return false
    };
    R.onclick = function() {
        if (!E.hasClassName(this, "sel")) {
            B("med", true)
        }
        return false
    };
    C.onclick = function() {
        if (!E.hasClassName(this, "sel")) {
            B("long", true)
        }
        return false
    };
    S();
    I(G);
    B(gSnipLength);
    var O = id("span_add_ext");
    if (O && isGecko()) {
        if (window.external && window.external.IsSearchProviderInstalled && window.external.IsSearchProviderInstalled("http://www.cuil.com") == 0) {
            id("a_add_ext").onclick = function() {
                window.external.AddSearchProvider("http://www.cuil.com/static/plugin.xml");
                return false
            };
            E.show(O)
        }
    }
    Q = true
}
var MsgBox = {
    show: function(A) {
        id("msg").innerHTML = A;
        E.show("msgbox");
        Layout.recalcLayout()
    },
    clear: function() {
        id("msg").innerHTML = "";
        E.hide("msgbox");
        Layout.recalcLayout()
    }
};
var gAutoReq = null;
function Popup(fld) {
    var ul = create("UL", "popup");
    ul.id = "menu_auto";
    E.hide(ul);
    document.getElementsByTagName("BODY")[0].appendChild(ul);
    var self = this;
    var val = fld.value;
    var selected = null;
    function _keydown(e) {
        var code = e.which || e.keyCode;
        switch (code) {
        case Evt.KEY_ESC:
            self.hide();
            Evt.stop(e);
            return;
        case Evt.KEY_UP:
            if (self.up()) {
                Evt.stop(e)
            }
            return;
        case Evt.KEY_DOWN:
            if (self.down()) {
                Evt.stop(e)
            }
            return
        }
    }
    function _keypress(e) {
        var code = e.which || e.keyCode;
        if (code == Evt.KEY_RETURN) {
            if (self.pick()) {
                Evt.stop(e)
            }
        } else {
            if ((code == Evt.KEY_UP) || (code == Evt.KEY_DOWN)) {
                if (!e.charCode && isGecko()) {
                    Evt.stop(e)
                }
            }
        }
    }
    function _keyup(e) {
        var code = e.which || e.keyCode;
        if (code == Evt.KEY_RETURN) {
            return
        }
        if (this.value != val) {
            val = this.value;
            if (val.length < 2) {
                self.hide();
                return
            }
            if (gAutoReq) {
                return
            }
            var params = Obj.toQueryString({
                q: this.value
            });
            var server = gUseProxy ? "/suggestproxy": "/suggest";
            gAutoReq = ajax({
                url: server,
                type: "GET",
                params: params,
                onSuccess: function(r) {
                    var text = r.responseText;
                    if (text && text.trim() != "") {
                        eval("var data = " + text);
                        self.set(data.suggests, data.navs)
                    }
                    gAutoReq = null
                },
                onError: function() {
                    gAutoReq = null
                }
            })
        }
    }
    function getTargetLI(e) {
        var elem = Evt.target(e);
        if (elem.tagName == "UL") {
            return elem
        }
        while (elem.tagName != "LI") {
            elem = elem.parentNode
        }
        assert(elem.tagName == "LI", "elem is not LI: " + elem.tagName);
        return elem
    }
    function isItem(elem) {
        return (elem.tagName == "LI") && !E.hasClassName(elem, "heading") && !E.hasClassName(elem, "cap")
    }
    function isHeading(elem) {
        return E.hasClassName(elem, "heading")
    }
    this.itemType = function(item) {
        return E.hasClassName(item, "nav") ? "nav": "suggest"
    };
    function getFirst() {
        var lis = ul.childNodes;
        if (!lis.length) {
            return null
        }
        var li = lis[0];
        if (isHeading(li)) {
            li = li.nextSibling
        }
        assert(li, "GetFirst: li is null.");
        return li
    }
    ul.onclick = function(e) {
        e = e || window.event;
        var target = getTargetLI(e);
        if (!isItem(target)) {
            return false
        }
        _keyEvent = true;
        self.pick(target);
        return false
    };
    ul.onmouseover = function(e) {
        e = e || window.event;
        var target = getTargetLI(e);
        if (!isItem(target)) {
            return false
        }
        self.select(target);
        return false
    };
    this.show = function() {
        if (this.visible()) {
            return
        }
        var search_bg = id("search_bg");
        var pos = E.cumulativeOffset(search_bg);
        ul.style.left = (pos[0] + 11) + "px";
        ul.style.top = (pos[1] + search_bg.offsetHeight - 6) + "px";
        ul.style.display = "";
        Evt.observe(fld, "keydown", _keydown);
        Evt.observe(fld, "keypress", _keypress)
    };
    this.hide = function() {
        if (!this.visible()) {
            return
        }
        ul.style.display = "none";
        selected = null;
        Evt.unobserve(fld, "keydown", _keydown);
        Evt.unobserve(fld, "keypress", _keypress)
    };
    this.visible = function() {
        return "none" != ul.style.display
    };
    this.set = function(items, navs) {
        var restore = null;
        if (selected) {
            restore = selected.innerText
        }
        selected = null;
        var html = "";
        var hasItems = false;
        var hasNav = false;
        if (navs.length) {
            hasNav = true;
            html = "";
            if (SHOW_POPUP_LABELS) {
                html += '<li class="heading">Go directly to...</li>'
            }
            for (var j = 0,
            nlen = navs.length; j < nlen; j++) {
                var nav = navs[j];
                var url = nav[0];
                var fave = "http://" + url + "/favicon.ico";
                html += '<li class="nav">';
                html += '<img src="' + fave + '" />';
                html += "<div>" + nav[1].htmlEncode() + "</div>";
                html += '<div class="url">' + url + "</div>";
                html += "</li>"
            }
        }
        if (items.length) {
            hasItems = true;
            var index = -1;
            if (SHOW_POPUP_LABELS) {
                html += '<li class="heading">Others have tried...</li>'
            }
            for (var i = 0,
            ilen = items.length; i < ilen; i++) {
                var item = items[i];
                if (restore == item) {
                    index = i
                }
                html += "<li>" + item + "</li>"
            }
        }
        ul.innerHTML = html;
        this.show();
        addEndcap(ul);
        if (!hasItems && !hasNav) {
            ul.innerHTML = "";
            this.hide()
        }
    };
    var _keyEvent = false;
    this.select = function(li, keyEvent) {
        if (li == selected) {
            return
        }
        assert(!isHeading(li), "Can't select a heading.");
        E.addClassName(li, "focused");
        if (selected) {
            E.removeClassName(selected, "focused")
        }
        selected = li;
        if (keyEvent) {
            _keyEvent = true
        }
    };
    this.up = function() {
        assert(this.visible(), "Shouldn't be getting an up request when menu is hidden.");
        var li = selected;
        if (!li) {
            return true
        }
        var prev = li.previousSibling;
        if (!prev) {
            return true
        } else {
            if (isHeading(prev)) {
                prev = prev.previousSibling;
                if (!prev) {
                    return true
                }
            }
        }
        assert(prev, "Previous item is null.");
        this.select(prev, true);
        return true
    };
    this.down = function() {
        assert(this.visible(), "Shouldn't be getting a down request when menu is hidden.");
        var li = selected;
        if (!li) {
            li = getFirst();
            if (!li) {
                return false
            }
            this.select(li, true);
            return true
        }
        var next = li.nextSibling;
        if (!next) {
            return true
        } else {
            if (isHeading(next)) {
                next = next.nextSibling
            } else {
                if (E.hasClassName(next, "cap")) {
                    return true
                }
            }
        }
        assert(next, "Next item is null.");
        this.select(next, true);
        return true
    };
    this.pick = function(item) {
        assert(this.visible(), "Picked shouldn't be called when menu is hidden.");
        if (!_keyEvent) {
            if (selected) {
                return true
            }
            return false
        }
        _keyEvent = false;
        item = item || selected;
        if (!item) {
            return
        }
        assert(isItem(item), "Trying to pick something that's not item.");
        if (typeof this.itemPicked == "function") {
            this.itemPicked(item)
        }
        this.hide();
        return true
    };
    Evt.observe(fld, "keyup", _keyup);
    Evt.observe(fld, "blur",
    function(e) {
        setTimeout(function() {
            self.hide()
        },
        300);
        Evt.stop(e)
    });
    Evt.observe(fld, "keydown",
    function() {
        self.cancelSubmit = false
    })
}
function addEndcap(D) {
    var H = create("LI", "cap");
    var C = create("CANVAS");
    var F = D.offsetWidth;
    if (! (F > 0)) {
        return
    }
    var A = 7;
    C.setAttribute("width", F);
    C.setAttribute("height", 2 * A);
    if (C.getContext) {
        var B = C.getContext("2d");
        B.beginPath();
        B.arc(A, -2, A, Math.PI, Math.PI / 2, true);
        var G = F - A;
        B.lineTo(G, 5);
        B.arc(G, -2, A, Math.PI / 2, 0, true);
        B.closePath();
        B.fillStyle = "#f5f5f5";
        B.fill();
        B.strokeStyle = "#a8a8a8";
        B.stroke()
    }
    H.appendChild(C);
    D.appendChild(H)
}
function StackController(div) {
    var self = this;
    var prevImg = null;
    var nextImg = null;
    var docs = null;
    var index = 0;
    function goPrevious() {
        if (!this.enabled) {
            return
        }
        index--;
        doRequest(index)
    }
    function goNext() {
        if (!this.enabled) {
            return
        }
        index++;
        doRequest(index)
    }
    function setEnabled(enabled) {
        if (enabled) {
            this.src = "/static/images/new/btn-" + this.name + ".png"
        } else {
            this.src = "/static/images/new/btn-" + this.name + "-off.png"
        }
        this.enabled = enabled
    }
    function doRequest(index) {
        if (!docs) {
            ajax({
                url: "/search",
                params: Obj.toQueryString({
                    sid: stackId,
                    q: gQuery
                }),
                onSuccess: function(r) {
                    eval("var json = " + r.responseText);
                    docs = json.results;
                    count = docs.length;
                    if (0 == count) {
                        alert("Sorry, server returned no documents.");
                        return
                    }
                    doRequest(index)
                },
                onError: function() {
                    alert("An error occurred.")
                }
            })
        } else {
            assert(index < docs.length, "doRequest: index out of bounds");
            var doc = docs[index];
            var a = create("A");
            a.href = doc.url;
            a.innerHTML = doc.title;
            var tDiv = E.getElement(parentTd, "DIV", "t");
            tDiv.innerHTML = "";
            tDiv.appendChild(a);
            var p = E.getElement(parentTd, "P");
            p.innerHTML = doc.snippet;
            var limit = 64;
            urlLink = E.getElement(parentTd, "A", "url");
            urlLink.title = doc.url;
            var shrt = doc.url;
            if (shrt.length > limit) {
                shrt = shrt.substr(0, limit) + "..."
            }
            shrt = shrt.replace(/^http:\/\//, "");
            urlLink.innerHTML = shrt;
            imgTd.innerHTML = "";
            if (doc.image_url) {
                var a2 = create("A");
                a2.href = doc.url;
                var img = create("IMG");
                img.src = doc.image_url;
                a2.appendChild(img);
                imgTd.appendChild(a2)
            }
            update()
        }
    }
    function update() {

        var num = index + 1;
        textSpan.innerText = num + " of " + count;
        prevImg.setEnabled(index > 0);
        nextImg.setEnabled(index < count - 1)
    }
    var scroller = E.getElementsByClassName("scroller", div)[0];
    E.show(scroller);
    var textSpan = scroller.getElementsByTagName("SPAN")[0];
    var count = textSpan.innerText - 0;
    textSpan.innerText = "1 of " + count;
    var link = div.getElementsByTagName("A")[0];
    var stackId = Params.get(link.search, "sid");
    var imgs = scroller.getElementsByTagName("IMG");
    for (var i = 0; i < imgs.length; i++) {
        var img = imgs[i];
        img.setEnabled = setEnabled;
        if ("p" == img.className) {
            prevImg = img;
            img.name = "back";
            img.onclick = goPrevious;
            img.setEnabled(false)
        }
        if ("n" == img.className) {
            nextImg = img;
            img.name = "fwd";
            img.onclick = goNext;
            img.setEnabled(true)
        }
    }
    var parentTd = div.parentNode;
    var imgTd = E.nodeBefore(parentTd)
}
var AccordionController = {
    init: function() {
        if (!id("accordion")) {
            return
        }
        this.tiles = $$("ul.tile_inner"),
        this.actuators = $$("span.actuator"),
        this.acc = new Accordion(this.actuators, this.tiles, {
            "opacity": false,
            "duration": 150,
            "start": false,
            "fixedHeight": 140,
            "transition": Fx.Transitions.Quad.easeOut,
            "display": false,
            "alwaysHide": false,
            onActive: function(H) {
                var G = $("last");
                if (G != H) {
                    G.className = "actuator open";
                    H.className = "actuator open"
                } else {
                    H.className = "actuator open"
                }
            },
            onBackground: function(H) {
                var G = $("last");
                if (G != H) {
                    H.className = "actuator"
                } else {
                    $("last").className = "actuator last"
                }
            }
        });
        var C = "";
        var F = $$("#accordion h2")[0];
        F.onmouseover = function() {
            C = setTimeout("AccordionController.acc.display(-1)", 100)
        };
        F.onmouseout = function() {
            clearTimeout(C)
        };
        var A;
        for (var B = 0; B < this.actuators.length; B++) {
            this.actuators[B].onmouseover = new Function("i", "tile_timeout = setTimeout('AccordionController.acc.display(" + B + ")', 100)");
            this.actuators[B].onmouseout = new Function("clearTimeout(tile_timeout)")
        }
        var D = id("show_more");
        if (D) {
            D.onclick = function() {
                AccordionController.toggle_tiles();
                return false
            }
        }
        if (navigator.userAgent.indexOf("MSIE 6") >= 0) {
            $("accordion").style.visibility = "visible"
        }
    },
    toggle_tiles: function() {
        var A = id("show_more");
        if (this.toggle_state == 1) {
            this.show_less_tiles();
            this.toggle_state = 0;
            if (A) {
                E.removeClassName(A, "show_less")
            }
        } else {
            this.show_more_tiles();
            this.toggle_state = 1;
            if (A) {
                E.addClassName(A, "show_less")
            }
        }
    },
    show_more_tiles: function() {
        var A = $$(".tile");
        for (i = 0; i < A.length; i++) {
            if (A[i].className == "tile hidden") {
                A[i].className = "tile extra";
                A[i].style.display = "inline"
            }
        }
        var B = $("last");
        B.className = "actuator";
        B.id = "";
        var A = $$(".tiles");
        A[0].lastChild.firstChild.id = "last";
        A[0].lastChild.firstChild.className = "actuator last";
        var C = id("show_more");
        if (C) {
            E.addClassName(C, "show_less")
        }
    },
    show_less_tiles: function() {
        var A = $$(".tile");
        for (i = 0; i < A.length; i++) {
            if ((A[i].className == "tile extra") && (A[i].previousSibling.className == "tile")) {
                A[i].previousSibling.className = "tile last_tile"
            }
        }
        var B = $("last");
        B.className = "actuator";
        B.id = "";
        var C = $$(".last_tile")[0];
        C.firstChild.id = "last";
        C.firstChild.className = "actuator last";
        A = $$(".extra");
        for (i = 0; i < A.length; i++) {
            if (A[i].className == "tile extra") {
                A[i].className = "tile hidden";
                A[i].style.display = "none"
            }
        }
    }
};
AccordionController.init()
