var activeForm = '';
function checkComment(){
	var data = $(this)[0].data.split('&');
	var keyValue;
	for (var i=0; i< data.length; i++)
	{
		keyValue = data[i].split('=');
		if (keyValue[0] == 'comment'){
			if ($.trim(keyValue[1]) === ''){return false;}
			return true;
		}
	}
	return false;
}
function updateCommentsSubmit(){
   $('.new_comment_form').ajaxForm(      
        {
          beforeSend:checkComment,
          success: function(responseText, statusText, xhr, form){
            $(form).children('textarea').attr('value','');
            $('input, textarea').placeholder();
            $(form).parents('li.activity-item').find('.responses').html(responseText);
            prepareTimeInfo();          
            }     
        });
}
function updateCommentFunctionality(){
      $('.pre_comment_textarea').unbind('focus');
      $('.comment_textarea').unbind('focusout');
      $('.pre_comment_textarea').focus(function(event){                
                    $(event.target).toggle();
                    var comment = $(event.target).parent().children('.comment_form');
                    comment.toggle();
                    comment.children().children('textarea').focus();
          });
    $('.comment_textarea').focusout(function(){
        activeForm = $(this).parents('.comment_form')[0].id;
        return false;
    });
    updateCommentsSubmit();
}
function hideCommentWidget(){
  $('.comment_form').hide();
  $('.pre_comment_textarea').show();
}
$(document).ready(function() {
	updateCommentsSubmit();
	updateCommentFunctionality();  
	prepareTimeInfo();
	$(this).click(function(e){
           var target;
           if ($(e.srcElement).attr('class')!==undefined ){
              target = $(e.srcElement);
           } else if ($(e.target).attr('class')!==undefined ) 
            {
               target = $(e.target);
            }
           if (activeForm !== ''){
                    hideCommentWidget();
                    activeForm = '';
                  }
    });
    $('.clearfix .toolbar a[name="delete"]').live('click', function(event){
		var this_obj = $(this);
		var url = this_obj.attr("href");
		var loader = this_obj.siblings('img[name="loading"]');
		this_obj.hide();
		loader.show();
		$.ajax({
			url: url,
			type: "POST",
			success: function(resp){
				this_obj.parents('.clearfix').slideUp('fast', function(){$(this).remove()});
			}
		})
		event.preventDefault();
	});
});
