
function search() {
    this.string;
    this.strings = new Object;
    this.stringSearch;
    this.loaded;
    this.sites = new Object;
    this.results = new Array;
    this.filterTime;
    this.filters = new Array;
    this.category = "";
    this.categories = new Object;
    this.categories[''] = new Object;
    this.categories[''].results = new Array;
    this.categoriesIndex = new Array;
    this.sortColumn = "health";
    this.sortOrder = 1;
    this.done = 0;
    this.resultPage;
    this.resultMax = 50;
    this.resultCurrent = 0;
    this.pagination;
    this.cells = {health: "health", name: "string", size: "size", seeds: "color", peers: "color", engine: "engine", id: "id"};
    this.rows = new Object;
    this.go = search_go;
    this.stop = search_stop;
    this.sort = search_sort;
    this.display = search_display;
    this.cat = search_category;
    this.filter = search_filter;
    this.sitefilter = search_sitefilter;
    this.removefilter = search_removefilter;
    this._get = search_get;
    this._done = search_done;
    this._filter = search_pfilter;

    function search_go() {
        this.strings.include = "";
        this.strings.exclude = "";
        this.strings.words = this.string.split(" ");
        for (var x in this.strings.words) {
            var _2 = this.strings.words[x].substr(0, 1);
            if (_2 == "+") {
                this.strings.include += " " + this.strings.words[x].substr(1) + ".*";
            } else {
                if (_2 == "-") {
                    this.strings.exclude += "|" + this.strings.words[x].substr(1);
                }
            }
        }
        this.strings.include = this.strings.include.substr(1);
        this.strings.exclude = this.strings.exclude.substr(1);
        this.strings.include = this.strings.include ? new RegExp(this.strings.include, "gi") : "";
        this.strings.exclude = this.strings.exclude ? new RegExp(this.strings.exclude, "gim") : "";
        this.stringSearch = this.string.replace(new RegExp("\\+|\\-", "g"), "").replace(this.strings.exclude, "");
        this.strings.highlight = new RegExp("(" + this.stringSearch.replace(new RegExp("\\s", "g"), "|") + ")", "gi");
        document.getElementById("filter").value = "FILTER RESULTS";
        for (var x in this.sites) {
            this._get(x);
        }
    }


    function search_stop() {
        for (var x in this.sites) {
            if (this.sites[x].state == 1) {
                this.sites[x].request.abort();
            }
        }
    }


    function search_sort(_4, _5) {
        if (!_5) {
            this.sortOrder = this.sortColumn == _4 ? this.sortOrder == 1 ? -1 : 1 : 1;
        }
        document.getElementById(this.sortColumn).className = "";
        document.getElementById(_4).className = "sort_" + (this.sortOrder == 1 ? 1 : 0);
        this.sortColumn = _4;
        if (this.filters.length) {
            this.filters.sort(_sort);
        } else {
            if (typeof this.categories[this.category] != "undefined") {
                this.categories[this.category].results.sort(_sort);
            }
        }
        this.display(1);
    }


    function search_filter() {
        if (this.filterTime) {
            window.clearTimeout(this.filterTime);
        }
        this.filterTime = window.setTimeout("search._filter()", 500);
    }


    function search_pfilter() {
        this.removefilter(1);
        if (document.getElementById("filter").value) {
            var _6 = document.getElementById("filter").value;
            var _7 = this.stringSearch + " " + _6;
            this.strings.highlight = new RegExp("(" + _7.replace(new RegExp("\\s", "g"), "|") + ")", "gi");
            var _8 = new RegExp(_6.replace(new RegExp("\\s", "g"), ".*"), "gi");
            var _9 = this.categories[this.category].results;
            for (var x in _9) {
                var _b = _9[x].name.toString();
                if (_8 && _b.match(_8)) {
                    this.filters[this.filters.length] = _9[x];
                }
            }
        }
        if (this.filters.length ||
            !document.getElementById("filter").value) {
            document.getElementById("filter").className = "filter";
            this.display(1);
        } else {
            if (!this.filters.length) {
                document.getElementById("filter").className = "filterNo";
            }
        }
    }


    function search_sitefilter(_c) {
        if (this.sites[_c].state == 2 || this.sites[_c].state == 4) {
            this.sites[_c].state = 2;
        } else {
            return 1;
        }
        this.removefilter();
        var _d = this.categories[this.category].results;
        for (var x in _d) {
            if (_d[x].engine == _c) {
                this.filters[this.filters.length] = _d[x];
            }
        }
        for (var x in this.sites) {
            if (this.sites[x].state == 2 && x != _c) {
                this.sites[x].state = 4;
            }
        }
        document.getElementById("remove_filter").style.display = "block";
        if (!this.filters.length) {
            this.display(1, 1);
        } else {
            this.display(1);
        }
    }


    function search_removefilter(_f) {
        this.filters = new Array;
        if (!_f) {
            document.getElementById("filter").value = "FILTER RESULTS";
            this.strings.highlight = new RegExp("(" + this.stringSearch.replace(new RegExp("\\s", "g"), "|") + ")", "gi");
        }
        for (var x in this.sites) {
            if (this.sites[x].state == 4) {
                this.sites[x].state = 2;
            }
        }
        document.getElementById("remove_filter").style.display = "none";
        this.display(1);
    }


    function search_display(_11, _12) {
        if (_11 != this.resultPage &&
            typeof window.scroll == "function") {
            window.scroll(0, 0);
        } else {
            if (_11 != this.resultPage) {
                window.scrollTo(0, 0);
            }
        }
        if (_12) {
            this.results = new Array;
        } else {
            this.results = this.filters.length ? this.filters : typeof this.categories[this.category] != "undefined" ? this.categories[this.category].results : new Array;
        }
        this.resultPage = _11;
        while (document.getElementById("result").rows.length) {
            document.getElementById("result").deleteRow(document.getElementById("result").rows.length - 1);
        }
        resultStart = this.resultMax * (this.resultPage - 1);
        resultStop = this.results.length > this.resultMax ? this.resultMax * this.resultPage : this.results.length;
        document.getElementById("counter").innerHTML = "Results " + (resultStart + 1) + "-" + resultStop + " of " + this.results.length + " for <b>" + this.string + "</b>";
        if (this.done == count(this.sites)) {
            document.getElementById("status").innerHTML = "Search Complete <img src='/i/tick.gif' alt='' />";
            document.getElementById("status").className = "status_state_2";
            if (!this.results.length) {
                var row = document.getElementById("result").insertRow(0);
                cell = row.insertCell(0);
                cell.innerHTML = "<center><br /><br />No matching results were found, please redefine your query!<br /></center>";
                cell.className = "search_noResult";
                cell.colSpan = 6;
            }
        } else {
            if (!this.results.length) {
                var row = document.getElementById("result").insertRow(0);
                cell = row.insertCell(0);
                cell.innerHTML = "<center>Searching, please wait...</center>";
                cell.className = "search_wait";
                cell.colSpan = 6;
            }
            document.getElementById("status").innerHTML = "Searching engine " + (this.done + 1) + " of " + count(this.sites);
        }
        var _14 = "";
        this.categoriesIndex.sort(_sort2);
        for (var x in this.categoriesIndex) {
            var _16 = this.categoriesIndex[x];
            _14 += "<a href=\"javascript:search.cat('" + _16 + "')\"" + (_16 == this.category ? " class='active'" : "") + ">" + _16 + " (" + this.categories[_16].results.length + ")</a>";
        }
        if (_14) {
            _14 = "<a href=\"javascript:search.cat('')\"" + (!this.category ? " class='active'" : "") + ">All (" + this.categories[''].results.length + ")</a>" + _14;
        }
        document.getElementById("categories").innerHTML = _14;
        var _17 = this.results.slice(resultStart, resultStop);
        for (var x in _17) {
            var _18 = document.getElementById("result").rows.length;
            var row = document.getElementById("result").insertRow(_18);
            row.id = "results_" + x;
            if (typeof this.rows[row.id] != "object") {
                this.rows[row.id] = new Object;
            }
            this.rows[row.id].even = isEven(x) ? 1 : 2;
            row.className = "row_state_" + this.rows[row.id].even;
            row.onmouseover = function () {this.className = "row_state_3";};
            row.onmouseout = function () {this.className = "row_state_" + search.rows[this.id].even;};
            row.onclick = function () {window.location.href=search.rows[this.id].url + "" + search.rows[this.id].id;};
            var y = 0;
            for (var _16 in this.cells) {
                if (this.cells[_16] != "id") {
                    cell = row.insertCell(y);
                }
                switch (this.cells[_16]) {
                  case "id":
                    this.rows[row.id].id = _17[x][_16];
                    break;
                  case "health":
                    cell.innerHTML = "<span id='h" + _17[x][_16] + "'><!-- --></span>";
                    break;
                  case "string":
                    cell.innerHTML = "<div class='name'>" + _17[x][_16].replace(this.strings.highlight, "<b>$1</b>") + "</div>";
                    break;
                  case "size":
                    cell.innerHTML = convert_size(_17[x][_16]);
                    break;
                  case "color":
                    cell.innerHTML = _17[x][_16] > 0 ? "<div class='green'>" + _17[x][_16] + "</div>" : "<div class='red'>" + _17[x][_16] + "</div>";
                    break;
                  case "engine":
                    this.rows[row.id].url = this.sites[_17[x][_16]].url;
                    cell.innerHTML = "<span title='" + this.sites[_17[x][_16]].name + "' id='icon_" + this.sites[_17[x][_16]].name + "'><!-- --></span>";
                    break;
                  default:;
                }
                y++;
            }
        }
        var _14 = "";
        var y = 0;
        for (var x in this.sites) {
            var _1a = this.sites[x].state ? this.sites[x].state : 1;
            if (_1a != 1 && _1a != 3) {
                _14 += "<span onclick='search.sitefilter(" + x + ")' id='site_" + this.sites[x].name + "' class='state_" + (this.sites[x].state ? this.sites[x].state : 1) + "'></span>";
            } else {
                if (_1a != 3) {
                    _14 += "<span class='state_2'><img src='/i/load_small.gif' alt='' /></span>";
                }
            }
            y++;
        }
        document.getElementById("sites").innerHTML = _14;
        if (this.pagination) {
            var _11 = Math.ceil(this.results.length / this.resultMax);
            if (_11 > 1) {
                var _14 = "<ul>";
                for (var x = 1; x <= _11; x++) {
                    _14 += "<li><a" + (x == this.resultPage ? " class='active'" : "") + " href='javascript:search.display(" + x + ")'>" + x + "</a></li>";
                }
                for (var id in this.pagination) {
                    document.getElementById(this.pagination[id]).innerHTML = _14;
                }
                _14 += "</ul>";
            } else {
                for (var id in this.pagination) {
                    document.getElementById(this.pagination[id]).innerHTML = "";
                }
            }
        }
    }


    function search_category(cat) {
        this.removefilter();
        this.category = cat;
        this.sort(this.sortColumn, 1);
    }


    function search_get(_1d) {
        var _1e = new Array;
        _1e.id = _1d;
        _1e.xml = new XMLHttpRequest;
        _1e.xml.onreadystatechange = function () 
        {
            if (_1e.xml.readyState == 4 && _1e.xml.status == 200) 
            {
                var _1f = new Array;
                try {eval(_1e.xml.responseText + "_1f=results");
                } catch (err) {
                    search._done(_1e.id, 3);
                    return 0;
                }
                for (var x in _1f) 
                {
                    var _21 = _1f[x].name.toString();
                    if (search.strings.include) 
                    {
                        if (!_21.match(search.strings.include)) 
                        {
                            continue;
                        }
                    }
                    if (search.strings.exclude)
                    {
                        if (_21.match(search.strings.exclude)) 
                        {
                            continue;
                        }
                    }
                    _1f[x].health = convert_health(_1f[x].seeds, _1f[x].peers);
                    _1f[x].engine = _1e.id;
                    if (!search.categories[_1f[x].category]) 
                    {
                        search.categories[_1f[x].category] = new Object;
                        search.categories[_1f[x].category].results = new Array;
                        var _22 = search.categoriesIndex.length;
                        search.categoriesIndex[_22] = new Object;
                        search.categoriesIndex[_22] = _1f[x].category;
                    }
                    search.categories[''].results[search.categories[''].results.length] = _1f[x];
                    search.categories[_1f[x].category].results[search.categories[_1f[x].category].results.length] = _1f[x];
                }
                search._done(_1e.id, 2);
            }
            else 
            {
                if (_1e.xml.readyState == 4 && _1e.xml.status != 200)
                {
                    search._done(_1e.id, 3);
                }
            }
        };
        this.sites[_1d].state = 1;
        this.sites[_1d].request = _1e.xml;
        _1e.xml.open("GET", "/cgi-bin/getlist.cgi?site=" + _1d + "&query=" + this.string, true);
        _1e.xml.send("");
    }


    function search_done(_23, _24) {
        this.sites[_23].state = _24;
        this.done++;
        this.sort(this.sortColumn, 1);
    }

}

var search = new search;
window.onload = function () {if (document.getElementsByTagName) {var _25 = document.getElementsByTagName("a");for (var i = 0; i < _25.length; i++) {var _27 = _25[i];if (_27.getAttribute("href") && _27.getAttribute("rel") == "p") {_27.onclick = function () {search.stop();};}}}search.display(1);search.go();};
window.onunload = function () {search.stop();};

function count(_28) {
    var y = 0;
    if (_28.__count) {
        return _28.__count;
    } else {
        if (_28.length) {
            return _28.length;
        } else {
            for (var x in _28) {
                y++;
            }
            return y;
        }
    }
}


function isEven(num) {
    return !(num % 2);
}


function convert_health(_2c, _2d) {
    if (_2c > 0 && _2d > 0) {
        var _2e = Math.round((_2c / _2d) * 10) / 10;
        _2e = _2e >= 1.1 ? 10 : _2e;
        _2e = _2e == 1 ? 10 : _2e;
        _2e = _2e == 0.9 ? 9 : _2e;
        _2e = _2e == 0.8 ? 8 : _2e;
        _2e = _2e == 0.7 ? 9 : _2e;
        _2e = _2e == 0.6 ? 7 : _2e;
        _2e = _2e == 0.5 ? 6 : _2e;
        _2e = _2e <= 0.4 ? 5 : _2e;
        return _2e;
    } else {
        return 1;
    }
}


function convert_date(_2f) {
    if (_2f == 0) {
        return "-";
    }
    var _30 = new Date;
    dif = Math.round(_30 / 1000) - _2f;
    years = Math.floor(dif / 31535999);
    months = Math.floor(dif / 2591999);
    days = Math.floor(dif / 86399);
    hours = Math.floor(dif / 3599);
    if (years < 1) {
        if (months < 1) {
            if (days < 1) {
                if (hours < 1) {
                    stime = "today";
                } else {
                    stime = hours + " " + (hours == 1 ? "hour" : "hours") + " ago";
                }
            } else {
                stime = days + " " + (days == 1 ? "day" : "days") + " ago";
            }
        } else {
            stime = months + " " + (months == 1 ? "month" : "months") + " ago";
        }
    } else {
        stime = years + " " + (years == 1 ? "year" : "years") + " ago";
    }
    return stime;
}


function convert_size(_31) {
    var _32 = new Array("MB", "GB", "TB");
    for (val in _32) {
        if (_31 > 1024) {
            _31 = _31 / 1024;
        } else {
            break;
        }
    }
    return Math.round(_31 * 100) / 100 + " " + _32[val];
}


function _sort(a, b) {
    var x = a[search.sortColumn].toString().toLowerCase();
    var y = b[search.sortColumn].toString().toLowerCase();
    if (search.cells[search.sortColumn] != "string") {
        return (y - x) * search.sortOrder;
    } else {
        return ((x < y) ? -1 : (x > y) ? 1 : 0) * search.sortOrder;
    }
}


function _sort2(a, b) {
    var x = a.toString().toLowerCase();
    var y = b.toString().toLowerCase();
    return (x < y) ? -1 : (x > y) ? 1 : 0;
}

