$(document).ready(function(){
	$(".comments").initComments();
});

(function($){
	jQuery.fn.extend({	
			
		initComments: function(){
			return this.each(function(index){
				var container = $(this);

				var form = container.find("form");
				var textarea = form.find("textarea");
				
				//Ukrycie formularzy komentowania jesli nie bylo wczesniej komentowanego watku
				if(!form.prev("div.listComments").find("li:first").is(".text"))
					form.hide();
					
                                var val = textarea.val();
				
				textarea.focus(function(){
					form.show();
					if($(this).val() == '' || $(this).val() == val){
						$(this).val('');
						container.find(".addComment").show();
					}
				});
				textarea.blur(function(){
					if($(this).val() == '' || $(this).val() == val){
						$(this).val(val);
						container.find(".addComment").hide();
					}
				});

                                container.find(".lnkCommentsShow").click(function(){
                                    var cnt = $(this).find("span").html();
                                    if(cnt > 0 && cnt <= 10){
                                        container.find(".threadComment").show();
                                        container.find(".listComments").show();
                                        container.find("form").show();
                                    }else if(cnt > 10){
                                        
                                    }
                                    return false;
                                })
				container.find(".lnkCommentAdd").click(function(){
                                        

					form.show();
					if($(this).val() == '' || $(this).val() == val){
						$(this).val('');
						container.find(".addComment").show();
					} 
					textarea.focus();
					return false;
				});
				
				var profile_type = form.find("input[name='profile_type']").val();
				var profile_id = form.find("input[name='profile_id']").val();
                                var author_type = form.find("input[name='author_type']").val();
				var author_id = form.find("input[name='author_id']").val();
				var type_comment = form.find("input[name='type_comment']").val();
				
				var type = form.find("input[name='type']").val();		
				var subject_id = form.find("input[name='subject_id']").val();

                               // alert(type);
				
				
				//dodanie komentarza			
				container.find(".addComment").click(function(){
					if($(this).css("opacity") < 1)
						return false;
					$(this).css({opacity:0.2})
					container.find(".listComments").addComment(profile_type,profile_id,author_type,author_id,type_comment,type,subject_id);
					return false;
				})

				//usuniecie komentara
				$(this).find(".cDel").click(function(){
					$(this).deleteComment(profile_type,profile_id,author_type,author_id,type_comment,type,subject_id);
					return false;
				})
				
				textarea.autoResize({
					animate : false,
					animateDuration : 0,
    				extraSpace : 1
				});
				
				container.find(".lnkAll a").click(function(){
					
					var container = $(this).parent(".lnkAll");
					container.hide();
					container.parent().parent().find("ul li").fadeIn("slow");
					return false;
				})
				
				
			})
		},
				
		addComment: function(profile_type,profile_id,author_type,author_id,type_comment,type,subject_id){
			var container = $(this);
			var note = $.trim(container.next("form").find("textarea").val());
                        var comments_count = $(".listComments li").length;
			jQuery.post("/comment/index/add",
						{profile_type: profile_type,profile_id: profile_id,author_type: author_type,author_id: author_id,type: type,subject_id: subject_id,note: note,type_comment: type_comment},
						function(data){
                                                        /*if(comments_count == 0){
                                                            $.post("/wall/publish/save",{profile_type:profile_type,type: $("publishing").find("input[name='type']").val(), subject_id: subject_id, author_id: author_id, author_type: author_type},function(){})
                                                        }*/
							container.next("form").find("textarea").height(17);	
							container.find("ul").append(data).children("li:last").hide().fadeIn();
							container.next("form").find("textarea").val("").blur();
							container.find("ul").children("li:last").find(".cDel").click(function(){
								$(this).deleteComment(profile_type,profile_id,author_type,author_id,type_comment,type,subject_id);
							})	
							container.parent(".comments").find(".lnkAll a span").html("("+container.find("ul").children("li").size()+")");
							container.parent(".comments").find(".addComment").css({opacity:1})
						}	
			)
		},

		deleteComment: function(profile_type,profile_id,author_type,author_id,type_comment,type,subject_id){
			var container_li = $(this).parent().parent();
			var container_ul = container_li.parent();
			var href =  $(this).attr("href");
			var cid = href.replace("#","");
			jQuery.post("/comment/index/delete",
					   {profile_type: profile_type,profile_id: profile_id,author_type: author_type,author_id: author_id,type: type,subject_id: subject_id,type_comment: type_comment,cid: cid},
						function(data){
							container_li.fadeOut("normal",function(){
								$(this).remove();
								container_ul.parent().parent(".comments").find(".lnkAll a span").html("("+container_ul.children("li").size()+")");
							});
						}
			)
		}

	})
})(jQuery);	

