var wall = {

init : function(op,wall_id) {
  this.wall_id=wall_id;
  this.on_profile=op;
  this.js_prefix=(op ? 'app7_' : '');
  this.attachSeq=0;
  this.attachLimit=3;
  this.attachFPML=new Hash();
  // dialog
  this.confirmDlg=new Dialog();
  this.confirmDlg.setButtons(Dialog.OK_AND_CANCEL);
  this.confirmDlg.setModal(true);
  this.okDlg=new Dialog();
  this.okDlg.setButtons(Dialog.OK);
  this.okDlg.setModal(true);
},


postMessage : function() {
  id=this.wall_id;
  js_p=this.js_prefix;
  limit=this.attachLimit;
  tthis=this;
  attaches=$A();
  attach_ids=$A();
  private = $('check_private') ? $('check_private').checked : false;
  Element.childElements(js_p+'attachlist').each(function(e) {
    if(attaches.length<limit) {
      Element.remove(e.id+'_rm');
      attach_ids.push(e.id);
      Element.remove(e.id);
    }
  });
  for(var i=0; i<attach_ids.length; i++){
    attaches.push(this.attachFPML[attach_ids[i]]);
  }

  if(msg=$('wall_editor').value) {
    new Ajax.Request(
      PEOPLE_P+'wall/wall.ajax.php',
      {
        method: 'post',
        parameters: {"post":1,"wall_id":id,"message":msg,"attachments[]":attaches,"private":private ? 1 : 0},
        onSuccess: function(rs) {
          rsobj=ej(rs.responseText);
          if(rsobj.rs) {
            new Insertion.Top(js_p+'wallmsg',rsobj.content);
            tthis.showOk('張貼留言','<center>留言張貼完成。</center>');
          }
        }
      }
    );
    $('wall_editor').value='';
  }
},

replyMessage : function (mid, confirm, reply) {
  id=this.wall_id;
  js_p=this.js_prefix;
  tthis=this;
  if(!reply && !confirm) reply = $('wallpost_reply_'+mid) ? $('wallpost_reply_'+mid).innerHTML : '';
  time=$('wallpost_reply_time_'+mid).innerHTML;
  if(!confirm) {
     form='<div class="wallpost" style="width:auto;">'+$('wallpost_msg_'+mid).innerHTML+'</div><div style="margin:20px;">請輸入回應訊息：<br/><textarea id="wallpost_reply" row="6" style="width:100%;">'+reply+'</textarea></div>';
     this.confirm('回應留言',form, 'wall.replyMessage('+mid+',1,$(\'wallpost_reply\').value);');
  }
  if($('wallpost_'+mid) && confirm) {
    new Ajax.Request(
      PEOPLE_P+'wall/wall.ajax.php',
      {
        method: 'post',
        parameters: {"reply":reply,"wall_id":id,"message_id":mid},
        onSuccess: function(rs) {
          rsobj=ej(rs.responseText);
          if(rsobj.rs && rsobj.message_id) {
            tthis.showOk('回應留言','<center>回應留言完成。</center>');
            Element.update('wallpost_'+rsobj.message_id, rsobj.content);
          } else {
            tthis.showOk('回應留言','回應留言失敗，請確定你已經登入後重新刪除。');
          }
        }
      }
    );
  }
},


deleteMessage : function(mid,confirm) {
  id=this.wall_id;
  js_p=this.js_prefix;
  tthis=this;
  if(!confirm) this.confirm('刪除留言','您確定要刪除此則留言？', 'wall.deleteMessage('+mid+',1);');
  if($('wallpost_'+mid) && confirm) {
    new Ajax.Request(
      PEOPLE_P+'wall/wall.ajax.php',
      {
        method: 'post',
        parameters: {"delete":1,"wall_id":id,"message_id":mid},
        onSuccess: function(rs) {
          rsobj=ej(rs.responseText);
          if(rsobj.rs && rsobj.message_id) {
            tthis.showOk('刪除留言','<center>留言已經成功刪除了。</center>');
            Element.remove('wallpost_'+rsobj.message_id);
          } else {
            tthis.showOk('刪除留言','刪除留言失敗，請確定你已經登入後重新刪除。');
          }
        }
      }
    );
  }
},

lockMessage : function(mid, lock, confirm) {
  id=this.wall_id;
  js_p=this.js_prefix;
  act=(lock ? '鎖上' : '解鎖');
  tthis=this;
  if(!confirm) this.confirm(act+'留言', '您確定要'+act+'此則留言？', 'wall.lockMessage('+mid+', '+lock+', 1);');
  if($('wallpost_'+mid) && confirm) {
    new Ajax.Request(
      PEOPLE_P+'wall/wall.ajax.php',
      {
        method: 'post',
        parameters: {"lock":lock,"wall_id":id,"message_id":mid},
        onSuccess: function(rs) {
          rsobj=ej(rs.responseText);
          if(rsobj.rs) {
            tthis.showOk(act+'留言','<center>留言已經成功'+act+'了。</center>');
            Element.update('wallpost_'+rsobj.message_id, rsobj.content);
          } else {
            tthis.showOk(act+'留言',act+'留言失敗，請確定你已經登入後重新'+act+'。');
          }
        }
      }
    );
  }
},

attachItem : function(content, fpml) {
  js_p=this.js_prefix;
  if( Element.childElements(js_p+'attachlist').length < this.attachLimit) {
    this.attachSeq++;
    new Insertion.Bottom(js_p+'attachlist', '<div class="wall_attachment" id="'+js_p+'attachment_'+this.attachSeq+'">'+content+'<a id="'+js_p+'attachment_'+this.attachSeq+'_rm" onclick="wall.removeAttached('+this.attachSeq+');">捨棄</a></div>');
    this.attachFPML[js_p+'attachment_'+this.attachSeq]=fpml;
  }
},

removeAttached : function(Seq) {
  Element.remove(js_p+'attachment_'+Seq);
},

requestApps : function () {
  id=this.wall_id;
  js_p=this.js_prefix;
  new Ajax.Request(
    PEOPLE_P+'wall/wall.ajax.php',
    {
      method: 'post',
      parameters: {"getapp":1,"wall_id":id},
      onSuccess: function(rs) {
        rsobj=ej(rs.responseText);
        if(rsobj.rs) {
          new Insertion.Bottom('attachapps', rsobj.apps);
        }
      }
    }
  );
},

attach : function (app_id) {
  id=this.wall_id;
  js_p=this.js_prefix;
  tthis=this;
  dlg=genericDlg;
  // find url to fetch
  app_params=$H($(js_p+'attach_content_form').serialize(true));
  curl=app_params.url;
  app_params=$(js_p+'attach_content_form').serialize();
  params=$H({ 'getapp':1,'attach':1,'wall_id':id,'app_id':app_id,'curl':curl,'params':app_params }); 
  genericDlg.show_loading('wall_dlg','載入中...');
  new Ajax.Request(
    PEOPLE_P+'wall/wall.ajax.php',
    {
      method: 'post',
      parameters: params,
      onSuccess: function(rs) {
        rsobj=ej(rs.responseText);
        if(rsobj.rs) {
          tthis.attachItem(rsobj.content, rsobj.fpml);
          dlg.fadeout();
        }
      }
    }
  );
},

attachGet : function (app_id) {
  id=this.wall_id;
  js_p=this.js_prefix;
  if( Element.childElements(js_p+'attachlist').length < this.attachLimit) {
    params=$H({ 'getapp':1,'request':1,'wall_id':id,'app_id':app_id }); 
    genericDlg.show_ajax_dialog('wall_dlg', PEOPLE_P+'wall/wall.ajax.php', params, genericDlg.gen_buttons([{name:'ok',label:'完成',onclick:'wall.attach('+app_id+');'},{name:'ok',label:'取消',onclick:'genericDlg.fadeout();'}]));
  } else {
    this.showOk('附件數量已滿','訊息附件已經滿'+this.attachLimit+'則了，請捨棄一些附件。');
  }
},

confirm : function (title, body, handler) {
  this.confirmDlg.setTitle(title);
  this.confirmDlg.setBody(body);
  this.confirmDlg.setHandler(handler);
  this.confirmDlg.show();
},

showOk : function (title, body) {
  this.okDlg.setTitle(title);
  this.okDlg.setBody(body);
  this.okDlg.show();
}
};
