function dkNico(suser, login_user, page_user, id_set) {
    this.suser       = String.valueOf(login_user);
    this.login_user  = parseInt(login_user);
    this.page_user   = parseInt(page_user);
    this.default_set = parseInt(id_set);
    this.shown_set   = parseInt(id_set);
    this.list_amount = 0;
    
    this._API_BASE   = 'http://nico.deckkr.jp/API/nico/';
    this.URL_API_SET_NICOLIST  = this._API_BASE + 'setNicoList.php';
    this.URL_API_GET_NICOLIST  = this._API_BASE + 'getNicoList.php';
    this.URL_API_GET_NICOLISTS = this._API_BASE + 'getNicoLists.php';
    this.URL_API_DEL_NICOLIST  = this._API_BASE + 'delNicoList.php';
    this.URL_API_SET_NICOLIST_INFO  = this._API_BASE + 'setNicoListInfo.php';

    this.MAX_LIST_AMOUNT = 30;

    //this.startUP();
}

// member //
dkNico.prototype.waitMsg = '<div align=center style="padding: 20px;"><img src="/images/common/loading.gif" border=0><br /><br />接続中です。しばらくお待ちください。</div>';
dkNico.prototype.deleteMsg = '削除中です。しばらくお待ちください';
dkNico.prototype.delFailMsg = '削除に失敗しました。しばらく放置をお願いします';

// methods //
// get 'deckkrDeck' object indicate by id_deck.
dkNico.prototype.updateLists = function(id_user)
{
    id_user = parseInt(id_user);
    if (id_user < 0) return null;

    var self = this;
    var url = this.URL_API_GET_NICOLISTS;
    var pars = 'user=' + id_user;
    var opt  = {
        method: 'get', 
        parameters: pars,
        onComplete: function(req,json) { 
            if (req.status != 200) return;
            if (req.responseText == 0) retrun;
            if (json.amount) self.list_amount = json.amount;
            if (self.list_amount >= self.MAX_LIST_AMOUNT) {
                $('addListArea').style.display = 'none';
            }
            else {
                $('addListArea').style.display = 'block';
            }
        }
    };
    if ($('nicolists')) $('nicolists').innerHTML = this.waitMsg;
    var myAjax = new Ajax.Updater('nicolists', url, opt);
}    

// get 'deckkrDeck' object indicate by id_deck.
dkNico.prototype.updateList = function(id_set)
{
    id_set = parseInt(id_set);
    if (id_set < 0) return null;

    var self = this;
    var url = this.URL_API_GET_NICOLIST;
    var pars = 'set=' + id_set;
    var opt  = {
        method: 'get', 
        parameters: pars,
        onComplete: function(req) { 
            if (req.status != 200) return;
            if (req.responseText == 0) retrun;
            var id = 'SET' + self.shown_set;
            self.shown_set = id_set;
            var id_new = 'SET' + self.shown_set;
            if ( $(id) ) $(id).style.color = 'blue';
            if ( $(id_new) ) $(id_new).style.color = 'red';
        }
    };
    if ($('nicolist')) $('nicolist').innerHTML = this.waitMsg;

    var myAjax = new Ajax.Updater('nicolist', url, opt);
    var area = 'SET'+this.shown_set;
}    

dkNico.prototype.startUP = function()
{
    this.updateLists(this.page_user);
    this.updateList(this.default_set);
}

dkNico.prototype.addListBox = function(urlarea, errarea)
{
    if (!$F(urlarea)) {
        if ($(errarea)) $(errarea).innerHTML = '';
        return;
    }
    var url = $F(urlarea);

    return this.addList(url, errarea);
}

dkNico.prototype.addListURL = function(url)
{
    return this.addList(url, 'urlerr');
}


dkNico.prototype.addList = function(url, errarea)
{
    if (!url || url=='') return;
    var nicoptn = /^(http:\/\/www.nicovideo.jp\/mylist\/[0-9]{1,11}(\/[0-9]{1,11})?)/i;
    var nicoreg = new RegExp(nicoptn);
    var ret = false;
    var ret = nicoreg.exec(url);
    if (!ret) {
        if ($(errarea)) {
            $(errarea).innerHTML = 'ニコニコ動画のマイリストではないようです';
        }
        return;
    }

    $(errarea).innerHTML = '';
    var self = this;
    var url = this.URL_API_SET_NICOLIST;
    var pars = '?user=' + this.login_user + '&url='+encodeURIComponent(ret[0]);
    var opt  = {
        method: 'post',
        parameters: pars,
        //asynchronous: false,
        onComplete: function(req) { 
            if (req.status != 200) return;
            var ret = parseInt(req.responseText);
            if (ret > 0) {
                if ($(errarea)) {
                    $(errarea).innerHTML = 'マイリスト一覧に登録しました';
                    if (self.page_user == self.login_user) {
                        self.updateLists(self.login_user);
                    }
                    self.updateList(ret);
                }
            } else if (ret == -1) {
                if ($(errarea)) {
                    $(errarea).innerHTML = 'すでに登録されています';
                }
                self.updateList(ret);
            } else if (ret == -2) {
                if ($(errarea)) {
                    $(errarea).innerHTML = 'リストの上限に達しています(30)';
                }
                self.updateList(ret);
            } else {
                    $(errarea).innerHTML = 'うまく登録できませんでした。<br />ニコニコ動画がメンテ中かもしれません。';
            }
        }
    };

    if ($(errarea)) $(errarea).innerHTML = this.waitMsg;
	var myAjax = new Ajax.Request(url,opt); 

}

dkNico.prototype.showConfirm = function(id_set)
{
    id_set = parseInt(id_set);
    if (id_set < 0) return null;

    var html = 'このマイリストを削除します - ';
    html += '<a href="javascript:page.deleteList('+id_set+');">はい</a> / ';
    html += '<a href="javascript:page.removeDialog('+id_set+');">いいえ</a>';
    var areaID = 'delConfirm'+id_set;
    if ($(areaID)) {
        if ($(areaID).style.display == 'block') {
            this.removeDialog(id_set);
        }
        else {
            $(areaID).innerHTML = html;
            $(areaID).style.display = 'block';
        }
    }
}

dkNico.prototype.removeDialog = function(id_set)
{
    id_set = parseInt(id_set);
    if (id_set < 0) return null;

    var areaID = 'delConfirm'+id_set;
    if ($(areaID)) {
        $(areaID).innerHTML = '';
        $(areaID).style.display = 'none';
    }
}

dkNico.prototype.deleteList = function(id_set)
{
    if (page.login_user != page.page_user) {
        page.removeDialog();
        return null;
    }

    id_set = parseInt(id_set);
    if (id_set < 0) return null;

    var areaID = 'delConfirm'+id_set;
    if ($(areaID)) $(areaID).innerHTML = this.deleteMsg;

    var self = this;
    var url = this.URL_API_DEL_NICOLIST;
    var pars = 'set=' + id_set + '&user=' + page.login_user;
    var opt  = {
        method: 'get', 
        parameters: pars,
        onComplete: function(req) { 
            if (req.status != 200) return;
            if (req.responseText == 1) {
                self.removeDialog(id_set);
                self.updateLists(self.page_user);
            }
            else {
                self.removeDialog(id_set);
                if ($(areaID)) $(areaID).innerHTML = this.delFailMsg;
            }
        }
    };
    var myAjax = new Ajax.Request(url, opt);
}

dkNico.prototype.changeListTitle = function()
{

    $('listTitle').innerHTML = '<input type="text" size="40" maxlength="40"'+
                               ' id="newTitle"'+
                               ' value="'+ this.list_title+ '"> '+
                               '[<a href="javascript:page.fixListTitle();">決定</a>]';

    $('listTitleEdit').innerHTML = '';
}

dkNico.prototype.fixListTitle = function()
{
    var newTitle = $F('newTitle');
    var self = this;
    var url = this.URL_API_SET_NICOLIST_INFO;
    var pars = 'user=' + this.login_user;
    pars += "&title=" + encodeURIComponent(newTitle);
    var opt  = {
        method: 'post', 
        parameters: pars,
        onComplete: function(req,json) { 
            if (req.status != 200) return;
            if (req.responseText == 1) {
                if (json.title) {
                    self.list_title = json.title;
                } 
                else {
                    self.list_title = '';
                }
            }
            list_title_s = self.list_title.substr(0,36);
            if (list_title_s != self.list_title) {
                list_title_s += '...';
            }
            if (self.list_title == '') list_title_s = '名無しマイリスト集';
            $('listTitle').innerHTML = list_title_s;
            $('listTitleEdit').innerHTML = '[<a href="javascript:page.changeListTitle();">変更</a>]';
        }
    };
    var myAjax = new Ajax.Request(url, opt);
    


}

