//$Date: Sun 31 Jan 2010 11:21:05 PM CET$
(function($) {
 //Method::comments
 $.fn.comments = function (s) {

   //Run through allmatching elements
   return $(this).each(function() {
     var $this = $(this);
     if ($this.loaded) return false; //Do this only once

     //Merges default settings with s
     var s = $.extend({
       'time': Math.round(new Date().getTime()),
       'text': ' Put your comment here...',
       'list': '/comments/list.php',
       'post': '/comments/post.php',
       'kick': '/comments/kick.php',
       'data': false
     }, s);

     //Merges element settings with s
     if ($this.attr('settings')) {
       eval('var o = ' + $this.attr('settings'));
       if (typeof(o) == typeof({})) $.extend(s,o);
     }

     //Local methods
     var obj = {
       init: function(json,text) {
         if(json.shown.length > 0) {
           s.data = {'from':json.shown[(json.shown.length-1)].cid,'to':json.shown[0].cid};
         }
         else {
           s.data = {'from':0,'to':0};
         }
         if (json.hidden > 0) {
           $this.append('<div class="c_hidden"><a href="javascript:void(0);">View '+json.hidden+' older comments</a></div>');
         }
         $this.loaded = true;
         obj.list(json);
         obj.drawForm(json);
       },
       list: function(json,elm) {
         var comments = new Array();

         $.each(json.shown,function () {
           comments.push('<div class="c_entry">'+
             '<img src="http://www.gravatar.com/avatar/'+this.md5+'?d=http://www.itsyourparliament.eu/img/ukuser.jpg&s=40" width="40" />'+
             '<div class="c_comment">'+
             '<div class="c_author"><a href="/users/'+this.uid+'.html">'+this.who+'</a></div>'+
             '<div class="c_time">' + this.at + '</div>'+
             '<div class="c_text">' + this.message + '</div>'+
             '</div>'+
             '</div>');
           s.data.from = this.cid;
         });

         if (!elm) {
          $this.append(comments.join(''));
         }
         else {
           elm.replaceWith(comments.join(''));
         }
       },
       click: function (event) {
         var $target = $(event.target);
         if ($target.is('.c_hidden a')) {
           $.post(s.list,{'c_to':s.c_to,'c_type':s.c_type,'to':s.data.to},obj.getHidden,'json');
         } else if($target.is('.kick')) {
           $target.parents('.c_entry').remove();
         }
       },
       getHidden: function(json,text) {
         var temp = s.data.from;
         obj.list(json,$('.c_hidden'));
         s.data.from = temp;
       },
       getUpdate: function(json,text) {
         obj.list(json,$('.comments form').hide("slow"));
         obj.drawForm(json);
       },
       drawForm: function(json) {
         if(json.userid) {
           var $form = $($('.comments .formTemplate').html()).hide();
           $form.
             submit(function() {
               var $error = $form.find('.error').empty();
               var uid = $(this).find('input[name=uid]').val();
               if(!(new RegExp( "^[0-9]*$" ).test(uid))) {
                 $error.append('<p>Invalid user</p>');
                 return false;
               }
               var msg = $(this).find('textarea[name=msg]').val();
               if(8 < msg.length > 255 || msg == s.text ) {
                 $error.append('<p>Invalid Comment</p>');
                 return false;
               }
               $.post(s.post,{'c_to':s.c_to,'c_type':s.c_type,'uid':uid,'msg':msg},function (data,text) {
                if (typeof(data) == typeof({})) {
                  $.each(data,function () {
                    $error.append('<p>'+this+'</p>');
                  });
                }
                else {
                  $.post(s.list,{'c_to':s.c_to,'c_type':s.c_type,'from':s.data.from},obj.getUpdate,'json');
                }
               },'json');
               return false;
             }).
             find('textarea[name=msg]').
             val(s.text).
             blur(function() { if($(this).val() == '') {$(this).val(s.text)} }).
             focus(function() { if($(this).val() == s.text) {$(this).val('')} }).
             end() ;
           if(json.shown.length < 1 )
             $form.prepend('<div class="info">There are no comments yet</div>')
           $this.append($form);
           $form.show();
         }
         else {
           if(json.shown.length < 1) {
             $this.append('<div class="info">There are no comments yet</div>');
           }
           $this.append('<div class="info"><a href="/yourpage/">You have to log in in order to comment</a></div>');
         }
       }
     };

     $.post(s.list,{'c_to':s.c_to,'c_type':s.c_type},obj.init,'json');

     $this.bind('click',obj.click);
     return $this;
   });

 };
})(jQuery);


$(function () {
  $('.comments').comments();
});

