Event.observe(window, 'load', function(e){
   var commentsLinks = $$('a.commentsLink');
   //alert(commentsLinks.length);
   var commentsButtons = $$('input.commentsButton');
   var commentsForms = $$('form.commentForm');
   
   var commentsAddLink = $$('a.commentsAddLink');
   
   
   commentsLinks.each(function(commentLink){
      var postId = commentLink.id.split("-")[1];
      commentLink.observe('click', function(e){
         commentLink_click(postId)
      });
   });
   /*
   commentsAddLink.each(function(commentAddLink){
      var postId = commentAddLink.id.split("-")[1];
      commentAddLink.observe('click', function(e){
         commentAddLink_click(postId);
      });
   });
   */
   commentsButtons.each(function(commentButton){
      var postId = commentButton.id.split("-")[1];
      commentButton.observe('click', function(e){
         addCommentButton_click(postId)
      });
   });
   
   commentsForms.each(function(aForm){
      aForm.reset();   
   });
   
});

function stripslashes(str) {
   str=str.replace(/\\'/g,'\'');
   str=str.replace(/\\"/g,'"');
   str=str.replace(/\\0/g,'\0');
   str=str.replace(/\\\\/g,'\\');
   return str;
}

function mysqlTimeStampToDate(timestamp) {
   //function parses mysql datetime string and returns javascript Date object
   //input has to be in this format: 2007-06-05 15:26:02
   var regex=/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/;
   var parts=timestamp.replace(regex,"$1 $2 $3 $4 $5 $6").split(' ');
   return new Date(parts[0],parts[1]-1,parts[2],parts[3],parts[4],parts[5]);
}

function commentAddLink_click(postId){
   var commentAddFormContainer = $("commentsadd-"+postId);
   Effect.toggle(commentAddFormContainer, 'slide', { duration: 0.2 });
   
   /*
   var allCommentsContainers = $$('div.commentsContainer');
   allCommentsContainers.each(function(aContainer){
      //Effect.toggle(aContainer, 'slide', { duration: 0.2 });
      if(aContainer.style.display != 'none')
         Effect.SlideUp(aContainer, { duration: 0.2 });
   });
   */
   var aContainer = $('comments-'+postId);
   if(aContainer.style.display != 'none')
      Effect.SlideUp(aContainer, { duration: 0.2 });
   
}


function commentLink_click(postId){
   //alert(postId);
   var commentContainer = $("comments-"+postId);
   Effect.toggle(commentContainer, 'slide', { duration: 0.2 });
   //Effect.SlideDown("comments-"+postId, {duration: 0.3});
   
   /*
   var allCommentsFormContainers = $$('div.commentsAddContainer');
   allCommentsFormContainers.each(function(aContainer){
      if(aContainer.style.display != 'none')
         Effect.SlideUp(aContainer, { duration: 0.2 });
      //Effect.toggle(aContainer, 'slide', { duration: 0.2 });
   });
   */
   
   var aContainer = $('commentsadd-'+postId);
   if(aContainer.style.display != 'none')
      Effect.SlideUp(aContainer, { duration: 0.2 });
   
}

function addCommentButton_click(postId){


   $('commentName-'+postId).addClassName('inputOk');
   $('commentEmail-'+postId).addClassName('inputOk');
   $('commentContent-'+postId).addClassName('inputOk');

   $('commentName-'+postId).removeClassName('inputError');
   $('commentEmail-'+postId).removeClassName('inputError');
   $('commentContent-'+postId).removeClassName('inputError');

   var commentName = $('commentName-'+postId).value;
   var commentEmail = $('commentEmail-'+postId).value;
   var commentWebsite = $('commentWebsite-'+postId).value;
   var commentContent = $('commentContent-'+postId).value;
   
   var c = new Comment(postId, commentName, commentEmail, commentWebsite, commentContent);
   var errors = c.errorCheck();
   if(errors.status == 0){//there were errors
      errors.errors.each(function(error){
         if(error.field == 'name'){
            $('commentName-'+postId).removeClassName('inputOk');
            $('commentName-'+postId).addClassName('inputError');
         }
         if(error.field == 'email'){
            $('commentEmail-'+postId).removeClassName('inputOk');
            $('commentEmail-'+postId).addClassName('inputError');
         }
         if(error.field == 'comment'){
            $('commentContent-'+postId).removeClassName('inputOk');
            $('commentContent-'+postId).addClassName('inputError');
         }
      });
   }
   else{//send the request
      c.send();
   }
}