/**
 *  Nicommons Tree
 *  (note: requires jQuery 1.2.x)
 */

function NicommonsTree( domain ){
  this.isOpen = {
    parent: false,    // 親作品一覧のオープン状態
    child:  false     // 子作品一覧のオープン状態
  };
  this.page  = 1;
  this.items = [];
  this.thumb_base = 'http://'+ domain +'/thumbnail/nc';
}
NicommonsTree.prototype.toggleList = function(prefix) {
  var name = (prefix == 'parent')? '親' : '子';
  
  if (this.isOpen[prefix]) {
    $j('.'+ prefix +'_tree_hidden').hide();
    $j('#'+ prefix +'_tree_toggle').text(name +'作品をもっと見る');
  } else {
    $j('.'+ prefix +'_tree_hidden').show();
    $j('#'+ prefix +'_tree_toggle').text(name +'作品を閉じる');
  }
  this.isOpen[prefix] = !this.isOpen[prefix];
};
NicommonsTree.prototype.toggleParent = function() {
  this.toggleList('parent');
};
NicommonsTree.prototype.toggleChild = function() {
  this.toggleList('child');
};
// 投稿作品リストを開く
NicommonsTree.prototype.openUploadList = function() {
  // リストウィンドウ表示
  $j('#list_upload').show();
  
  // リスト取得
  $j.ajax({
    url:      "/api/material/list",
    type:     "post",
    dataType: "json",
    data:     {"page": this.page},
    success: this.bind(
      this,
      function(resp) {
        if (resp.result == 'OK') {
          this.showItems(resp.list, resp.paginate);
        } else {
          alert(resp.error);
        }
      }
    ),
    error: function(resp) {
      alert(resp.error);
    }
  });
};
// アイテム表示
NicommonsTree.prototype.showItems = function(items, paginate) {
  // 既存リスト削除
  $j('#list_upload_line').empty();
  
  // 前ページへ
  if (paginate.has_prev) {
    $j('<td style="text-align:center;">'+
        '<a href="javascript:void(0);" id="showPrev">&lt;&lt;</a>'+
      '</td>'
    ).appendTo('#list_upload_line');
    
    $j('#showPrev').click(
      this.bind(this, this.showPrev)
    );
  }
  
  // リスト表示
  this.items = items;
  for (var i=0; i<this.items.length; ++i) {
    var img_url = '/images/base/128_128/music.gif';
    if (items[i].type != 2) {
      img_url =  this.thumb_base + items[i].id;
    }
    
    $j('<td style="text-align:center; vertical-align:top; border:1px solid #ccc; overflow:hidden">'+
      '<div style="width:150px">'+
        '<strong>nc'+ items[i].id +'</strong><br />'+
        '<img src="'+ img_url +'" /><br />'+
        '<a href="javascript:void(0);" id="selectItem_'+ i +'">'+ items[i].name +'</a>'+
      '</div>'+
      '</td>'
    ).appendTo('#list_upload_line');
    
    $j('#selectItem_'+ i).click(
      this.bind(this, this.selectItem, [i])
    );
  }
  
  // 次ページへ
  if (paginate.has_next) {
    $j('<td style="text-align:center;">'+
        '<a href="javascript:void(0);" id="showNext">&gt;&gt;</a>'+
      '</td>'
    ).appendTo('#list_upload_line');
    
    $j('#showNext').click(
      this.bind(this, this.showNext)
    );
  }
  
  return;
};

// 登録作品の選択
NicommonsTree.prototype.selectItem = function(idx) {
  var id   = this.items[idx].id;
  var name = this.items[idx].name;
  
  if (!confirm('nc'+ id +' 「'+ name +'」 のミラー作品として登録しますか？')) return;
  
  // 確定要求
  $j('#post_commons_id').val(id);
  $j('#form_similarity').submit();
};
// ページ移動
NicommonsTree.prototype.showNext = function() {
  this.page++;
  this.openUploadList();
};
NicommonsTree.prototype.showPrev = function() {
  this.page--;
  this.openUploadList();
};
// 投稿作品リストを閉じる
NicommonsTree.prototype.closeUploadList = function() {
  $j('#list_upload').hide();
  $j('#list_upload_line').empty();
};
// bind
NicommonsTree.prototype.bind = function(obj, func, args) {
  return function() {
    var _args = args || arguments;
    func.apply(obj, _args);
  }
};

/**
 *  初期化処理
 */
var _tree = new NicommonsTree( deliver_domain );

$j(function() {
  // イベントハンドラ設定
  $j('#parent_tree_toggle').click( function(){ _tree.toggleParent(); } );
  $j('#child_tree_toggle' ).click( function(){ _tree.toggleChild();  } );
  
  $j('#beginSelect').click( function(){ _tree.openUploadList();  } );
  $j('#endSelect'  ).click( function(){ _tree.closeUploadList(); } );
});

