/*
 *  Copyright 2007 hkrn <hikarin@users.sourceforge.jp>
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 * Zeromin.Log.Thread
 * $Id: thread.js 109 2007-01-20 12:49:14Z hikarin $
 */

if ( typeof Zeromin.Log == "undefined" ) {
    Zeromin.Log = {};
}

Zeromin.Log.Thread = function(bbs) {
    this.bbsID  = bbs.getID();
    this.bbsDir = bbs.getBBS();
    this.loaded = false;
}

Zeromin.Log.Thread.prototype = {
    "getID": function () { this.bbsID },
    "getBBS": function () { this.bbsDir },
    "getCache": function () {},
    "setCache": function (cache) {},
    "setBBS": function (id, bbs) {
        if ( this.bbsDir != bbs ) {
            this.bbsID        = id;
            this.bbsDir       = bbs;
        }
        return this;
    },
    "load": function (entriesByPage) {
        var self = this;
        Zeromin.getIO().load(
            [
                { "name" : "C",    "value": "log"      },
                { "name" : "M",    "value": "load"     },
                { "name" : "type", "value": "thread"   },
                { "name" : "bbs",  "value": this.bbsID }
            ],
            function (storage) {
                var data = storage.read() || [];
                var root = document.getElementById("logThreadRoot");
                var ls   = $(document.getElementById("logThreadTable")).empty();
                $("ul.nav-page", root).remove();
                for ( var i in data ) {
                    var item    = data[i];
                    var tdIP = document.createElement("td");
                    var ip = item.ip;
                    tdIP.appendChild(document.createTextNode(ip));
                    tdIP.innerHTML = "<a href='javascript:void(0)' "
                        + "onclick='Zeromin.DOM.getRemoteHost"
                        + "(this, event, \"" + ip + "\")'>" + ip + "</a>";
                    var tdIPGroup = document.createElement("td");
                    tdIPGroup.appendChild(
                        document.createTextNode(item.ip_group)
                    );
                    var tdDate = document.createElement("td");
                    tdDate.appendChild(
                        document.createTextNode(Zeromin.getDate(item.atime))
                    );
                    var trBase = document.createElement("tr");
                    trBase.appendChild(tdIP);
                    trBase.appendChild(tdIPGroup);
                    trBase.appendChild(tdDate);
                    ls.append(trBase);
                    self.loaded = true;
                }
                Zeromin.DOM.makePager(ls.parent(),
                    (Zeromin.intval(entriesByPage) || 15));
                Zeromin.DOM.toggleMenu("bbs");
                Zeromin.DOM.renderLeftMenu("leftBBSLogMenu");
                Zeromin.DOM.setFunction(
                    "Log - Thread - " + Zeromin.getBBS().getName()
                );
                Zeromin.DOM.statusOK("スレッド作成ログ一覧を表示しました", 1000);
                $(".Container", document).hide();
                $(root).show();
            },
            this
        );
    },
    "render": function() {
    },
    "toString": function() { return this.getBBS() }
};
