var Comment = Class.create({

   initialize: function(postId, author, email, website, comment){
      this.postId = postId;
      this.author = author;
      this.email = email;
      this.website = website;
      this.comment = comment;
      this.commentServiceUrl = $('templateDirectory').value + "/commentService.php";
      //this.commentServiceUrl = "http://localhost/wordpress/flex/commentService.php";
      this.commentsDisplay = $('commentsDisplay-'+this.postId);
   },
   
   send: function(){
      
      new Ajax.Request(this.commentServiceUrl, 
                        { 
                           method: 'post', 
                           parameters: {
                                          uncache: new Date(),
                                          postId: this.postId,
                                          name: this.author,
                                          email: this.email,
                                          website: this.website,
                                          comment: this.comment                                          
                                       },
                           onSuccess: this.successResponse.bind(this), 
                           onError: this.errorResponse.bind(this)
                        });  
   },
   
   successResponse: function(t){
      //alert(t.responseText);
      var resp = eval('('+t.responseText+')');

      if(resp.status == 1){//success
         var commentClass = "aCommentEven";

         var allCommentsPost = $$('#commentsDisplay-'+this.postId + ' div.aComment');
         var altIdx = 0;
         allCommentsPost.each(function(aComment){
            if(altIdx%2==0)
               aComment.className = 'aCommentOdd aComment';
            else
               aComment.className = 'aCommentEven aComment';
            altIdx++;
         });
            
         var commentHTML = '<div class="'+commentClass+' aComment" id="newComment-'+resp.commentId+'">'+
                           ' <a href="'+resp.website+'">'+resp.name+'</a> - '+
                           stripslashes(resp.comment) +
                           '&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #cccccc">' + resp.post_comment_date + '</span>' + 
                           '</div>';
         this.commentsDisplay.insert({top: commentHTML});

         $('numCommentsLabel-'+this.postId).update(resp.numComments);
         $('comments-'+this.postId).scrollTop = 0;
         $('newComment-'+resp.commentId).appear();
         $('commentForm-'+this.postId).reset();
         Effect.SlideUp($('commentsadd-'+this.postId), { duration: 0.2 });
         Effect.SlideDown($('comments-'+this.postId), { duration: 0.2 });
         if(resp.numComments == 1)
            $('commentOrCommentsLabel-'+this.postId).update(' comment');
         else
            $('commentOrCommentsLabel-'+this.postId).update(' comments');
      }
   },
   
   errorResponse: function(t){
   
   },
   
   hasErrors: function(){
   
   },
   
   errorCheck: function(){
      
      var msgObj = {"status":1,"errors":[]};
      if(this.author.empty()){
         var msg = "Please enter your name";
         var error = {"field":"name", "message":msg};
         msgObj.errors.push(error);
      }
      if(this.email.empty()){
         var msg = "Please enter your email";
         var error = {"field":"email", "message":msg};
         msgObj.errors.push(error);
      }
      if(this.comment.empty()){
         var msg = "Please enter your comment";
         var error = {"field":"comment", "message":msg};
         msgObj.errors.push(error);
      }
      
      if(msgObj.errors.length==0)
         msgObj.status = 1;
      else
         msgObj.status = 0;
         
      return msgObj;
   },
   
   toString: function(){
      var str = 'Post:   ' + this.postId + '\n' +
                'Author: ' + this.author + '\n' +
                'Email:  ' + this.email + '\n' +
                'Website:' + this.website + '\n' +
                'Comment:' + this.comment + '\n';
                
      return str;
   }
   
});