$(document).ready(function(){
			setTimeout(showmessage,4000);
			function showmessage(){
				$("#message").fadeOut("slow")
			}
				        
	        var cancelReply=function(){
	        	$(this).parents(".replyCommentForm").remove();
	        	$(".replyBtn").removeClass('active');
	        	$(".replyBtn").show();
	        };
	        var showReplyForm=function(){
					$(this).addClass('active');
					$(this).hide();
					var clone=$(".replyComment").eq(0).clone();
					clone.find('textarea:first').addClass("count");
					clone.insertAfter(this);
					var parentID=$(this).parents(".subcomment").find("input[@name=parent]").val();
					if( parentID==undefined )
					{
						parentID=$(this).parents(".comment").find("input[@name=parent]").val();
					}
					clone.children("form").children("input[@name=parentID]").val(parentID);
					$(this).next().show();
					var target=$(this);
					$(this).next().find(".cancelReply").click(cancelReply);
					$(this).next().removeClass(".replyComment");
					$('.replyCommentForm').ajaxForm({ 
				        // dataType identifies the expected content type of the server response 
				        dataType:  'json', 
				        // success identifies the function to invoke when the server response 
				        // has been received 
				        success: function(json){
				        	if(json.status=='success')
				        	{
				        		target.parent("p").next(".children").find(".getsubComments").text('view '+json.total+' replies');
				        		target.parent("p").next(".children").children(".result").load("http://www.spicypolls.com/polls/category/getsubComments/"+parentID);
				        		target.next().remove();
				        		target.removeClass('active');
				        		target.show();
				        	}
				        }
				    }); 
					return false;
				};
			var hideReplyForm=function(){
					$(this).next().remove();
					$(this).removeClass('active');
			};
		    $(".replyBtn").livequery(function(){$(this).click(showReplyForm)});
		    var showsubComments=function(){
		    		var target=$(this);
		    	    $(this).addClass('active');
		    	    target.siblings(".result").children().toggleClass("display");
		    	    var parentID=$(this).parents(".subcomment").find("input[@name=parent]").val();
				if( parentID==undefined )
				{
					parentID=$(this).parents(".comment").find("input[@name=parent]").val();
				}
		    		$.ajax({
					  url: "http://www.spicypolls.com/category/getsubComments/"+parentID,
					  processData: false,
					  dataType:"html",
					  success:function(data){
					  		 target.siblings(".result").html(data);					  		 
					  		//a.find(".getsubComments").toggle(showsubComments,hidesubComments);
					  		//a.find(".replyBtn").toggle(showReplyForm,hideReplyForm);
					  }
					});
		    		return false;
		    };
		    var hidesubComments=function(){$(this).next(".result").children().toggleClass("display");$(this).removeClass('active');};
		    $(".getsubComments").livequery(function(){
		    $(this).toggle(showsubComments,hidesubComments);});
		    
		    var addabuse=function(){
			window.open(this.href,'abuse','height=550,width=550');
			return false;
		    }
		    $(".addabuse").click(addabuse);
		    $(".comments form").submit(function(){
		    	var comment = $(this).children('textarea').val().replace(/^\s+|\s+$/g, '');
		    	var commentSize=comment.length;

		    	if(commentSize<1)
		    	{alert("Please write something");return false;}
		    	if(commentSize>500)
		    	{alert("Your comment must be less than 500 characters");return false;}
		    	else{return true;}
		    });
		  
});