$(document).ready(function()  {


  $('.iconPostEdit').live('click',function(event) {
    event.preventDefault();
    var $icon = $('img',this);
    var $post = $icon.parents('.post');
    var $post_id = $('.post_id',$post).text();
    var $text_box = $('.text .inner',$post);
    var $edit_box = $('.text .edit',$post);

    if ($icon.attr('title')=='Edit Post') {
      $.post('/ajax/req/group/EditForm/'+$post_id+'/',
        '',
        function(data)  {
          if (data=='EPIC_FAIL')  {
            $icon.attr('title','there was an error =/');
            $icon.css('cursor','default');
          }
          else  {
            $icon.attr('src','/data/icons/disk.gif');
            $icon.attr('title','Save Post');
            //$icon.parent().attr('href','http:// Save your edit.');
            $text_box.slideUp('fast',function() {
              $edit_box.html(data).slideDown();
            });
          }
        }
      );
    }

    else if ($icon.attr('title')=='Save Post')  {
      $post_text = $('textarea',$post).val();
      $.post('/ajax/req/group/SavePost/',
        {
          'post_id':$post_id,
          'post_text':$post_text
        },
        function(data)  {
          $edit_box.slideUp('fast',function() {
            $text_box.html(data).slideDown();
            $icon.attr('src','/data/icons/pencil.gif').attr('title','Edit Post');
            $('.editMSG',$post).html(' :: edit saved ');
          });
        }
      );
    }

  });


});
