if (typeof Photozou == 'undefined') Photozou = {};

JSAN.require('Photozou.Model');

Photozou.Model.PhotoComment = function() {}

Photozou.Model.PhotoComment.prototype = new Photozou.Model

Photozou.Model.PhotoComment.prototype.find_by_comment_id = function(photo_id, comment_id, opt) {
  var self = this;
  var opt = Object.extend({
    limit: false,
    offset: false,
    callback: new Function,
    errback: self.raiseError
  }, opt || {});
  var url = '/xml/photo_comment/' + photo_id + '/' + comment_id + '?v=' + (new Date).getTime();
  new Ajax.Request(url, {
    method: 'get',
    onComplete: function(req) {
      if (self.isOk(req)) {
        opt.callback(self.req2comment(req))
      } else {
        opt.errback(req)
      }
    },
    onFailure: opt.errback
  });
}

Photozou.Model.PhotoComment.prototype.save = function(user_id, photo_id, comment, posted_from, ott, opt) {/*{{{*/
  var self = this
  var opt = Object.extend({
    'callback': new Function,
    'errback': self.raiseError
  }, opt || {});
  var url = '/xml/photo_comment_add/'+user_id+'/'+photo_id;
  new Ajax.Request(url, {
    method: 'post', 
    parameters: {'comment':comment, 'ott':ott, 'posted_from':posted_from},
    onComplete: function(req) {
      if (self.isOk(req)) {
        opt.callback(self.req2comment(req))
      } else {
        opt.errback(req)
      }
    },
    onFailure: opt.errback
  });
}/*}}}*/

Photozou.Model.PhotoComment.prototype.update = function(photo_comment_id, comment, ott, opt) {
  var self = this
  var opt = Object.extend({
    callback: new Function,
    errback: self.raiseError
  }, opt || {});
  var url = '/xml/photo_comment_update/' + photo_comment_id;
  new Ajax.Request(url, {
    method: 'post',
    parameters: {comment: comment, ott: ott},
    onComplete: function(req) {
      if (self.isOk(req)) {
        opt.callback(self.req2comment(req))
      } else {
        opt.errback(req)
      }
    },
    onFailure: opt.errback
  });
}

Photozou.Model.PhotoComment.prototype.remove_by_commenter = function(photo_comment_id, ott, opt) {/*{{{*/
  var self = this
  var opt = (function(d,s){for(var p in s)d[p]=s[p];return d})({
    'callback': new Function,
    'errback': self.raiseError
  }, opt || {});
  var url = '/xml/photo_comment_remove/'+photo_comment_id+'?v='+(new Date).getTime()
  new Ajax.Request(url, {
    method: 'post',
    parameters: {'ott':ott},
    onComplete: function(req) {
      if (self.isOk(req)) {
        opt.callback(req)
      } else {
        opt.errback(req)
      }
    },
    onFailure: opt.errback
  });
}/*}}}*/

Photozou.Model.PhotoComment.prototype.isOk = function(req) {/*{{{*/
  var result = this.dom2obj(req.responseXML.documentElement)
  return result['@stat'] == 'ok' ? true : false
}/*}}}*/

Photozou.Model.PhotoComment.prototype.req2comments = function(req) {/*{{{*/
  var obj = this.dom2obj(req.responseXML.documentElement)
  var comments = []
  if (typeof obj.comments == 'undefined') return []
  if (typeof obj.comments.comment != 'undefined') {
    if (obj.comments.comment.length > 0) {
      comments = obj.comments.comment
    } else {
      comments.push(obj.comments.comment)
    }
  }
  return comments
}/*}}}*/

Photozou.Model.PhotoComment.prototype.req2comment = function(req) {/*{{{*/
  var obj = this.dom2obj(req.responseXML.documentElement)
  var comment = null
  if (typeof obj.comment != 'undefined') {
    comment = obj.comment
  }
  if (typeof obj.err != 'undefined' && typeof obj.err['@message'] != 'undefined') {
    comment = {}
    comment.error_message = obj.err['@message']
  }
  return comment
}/*}}}*/


