/* 
 * Object to create a new post on the view
 * And set the finishUpdate function
 * Friday, 29 August 2008:    Created
 *
 * Version 1
 * */

var Post = {

  Version : '1', // Allows me to test it exists

  busyImage : '<img src="/images/waiting_anim.gif" alt="loading data" class="waitingimg" /><br />',

  /* 
   * Put new post on the view
   * */
  newPost : function (type, id) {
    var str = '';
    var i = new Ajax.Request('includes/inc_eip_new_post.php', {
      parameters: {'type': type, 'id': id},
      method: 'post',
      onSuccess:  function (t) {
        str = t.responseText;
        $('profile').insert({'top':str});
      }
    });

    return;
  },

  updatePost : function (h) {
    // h sent to save field, to read
    //alert($H(h).inspect()); return;
    var i = new Ajax.Request('includes/inc_eip_save.php', {
      parameters: h,
      method: 'post',
      onComplete:  function (t) {
        var str = t.responseText;
        //alert(str);
        var j = $H(str.evalJSON(true));
        j.set('view' , $H(j.get('view'))); 
        h = {'type':j.get('view').get('type'), 'pid':j.get('id'), 'fid':j.get('view').get('fellow_id')};
        Post.readPost(j.get('id'), h);
      }
    });
  },

  readPost : function (post_id, h) {
    $('post'+post_id).insert({'top': Post.busyImage});

    var i = new Ajax.Request('includes/inc_eip_read_post.php', {
      parameters: h,
      method: 'post',
      onComplete:  function (t) {
        str = t.responseText;
        //alert($('post'+post_id).tagName);
        $('post'+post_id).replace(str);
      }
    });
  },

  deletePost : function (id, fellow_id) {
    if (confirm('Are you sure?')) {
      var i = new Ajax.Request('includes/inc_eip_delete_post.php', {
        parameters: {'fellow_id': fellow_id, 'post': id},
        method: 'post',
        onComplete:  function (t) {
          $('post'+id).remove();
        }
      });
    }
  } 
};
