  XBB.textarea_id = 'comment_textarea'; // id of a textarea
	XBB.area_width = '550px';
	XBB.area_height = '200px';
	XBB.state = 'plain'; // 'plain' or 'highlight'
	XBB.lang = 'en_utf8'; //

 var active_form_id = "replyForm";
 var active_form     = "";

 $(function() {
	XBB.init();
	$('.collaps').click(function(){
		var li = $(this).parents('li').first();
		li.children('ul.sublevel').hide();
		li.children('comment-left').children('comment-bottom').removeClass('parent');
		li.addClass('hid');
		return false;
	});
	$('.showcam').click(function(){
		var li = $(this).parents('li').first();
		li.children('ul.sublevel').show();
		li.children('comment-left').children('comment-bottom').addClass('parent');
		li.removeClass('hid');
		return false;
	});

	active_form = $('#'+active_form_id).html();
    $('#'+active_form_id).html("");

      $("#Comment").validate({
			submitHandler: function(form) {
					if(trim(strip_tags($(form).find('#comment_textarea').val())) == "") {
						$('#error_'+active_form_id).html('<div>comment can\'t be empty </div>').show();
						return false;
					}
					jQuery(form).ajaxSubmit({
						cache: false,
						beforeSubmit: bfSubmit,
						success: showResponse,
						debug: true
                    });
			},
            rules: {},
            messages: {}
		});
        //EOF Validation

});

function strip_tags( str ){	// Strip HTML and PHP tags from a string
	//
	// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)

	return str.replace(/<\/?[^>]+>/gi, '');
}

function trim(string) {
	return string.replace (/(^\s+)|(\s+$)/g, "");
}

function bfSubmit () {
	  $('.coment .submit_gray').animate({opacity:0},100, function() {
		  	$(this).css({display:'none'});
		  });
      $('#add_comment_form').hide();
      $('#comment_loader').show();

}

function cancel () {
	var iframe = document.getElementById(XBB.iframe_id).contentWindow;
	$('.coment .submit_gray').animate({opacity:0},100, function(){
		$(this).css({display:'none'});
	});
	iframe.document.forms.xbb.xbb_textarea.value = "";
	$('#add_comment_form').hide();
	$('#error').hide();
	$('#preview_'+active_form_id).html("");
	$('#error_'+active_form_id).html("");
}

function preview(){
	var iframe = document.getElementById(XBB.iframe_id).contentWindow;
	if(trim(strip_tags(iframe.document.forms.xbb.xbb_textarea.value)) == "") {
		$('#error_'+active_form_id).html('<div>comment can\'t be empty </div>').show();
		$('#preview_'+active_form_id).html("");
		return false;
	}
	$('#Comment .submit').animate({opacity:0},100, function(){
	  	$(this).css({display:'none'});
	});
	setTimeout("$('#preview_loader').fadeIn(100)", 100);
	$.post('/comments/preview'
		,{
			comment  : iframe.document.forms.xbb.xbb_textarea.value
        }
        ,function(response){
		   $('#preview_loader').fadeOut(100, function(){$('#Comment .submit').css({display:'inline-block'}).animate({opacity:1},100)});
		   $('#error_'+active_form_id).html("");
		   $('#preview_'+active_form_id).html('<strong>Preview:</strong><div>'+response+'</div>').show();
		});
	    return false;
}

function commentForm(comment_id) {
	$('.prev1').hide();
	$('.comment_error').hide();
	$('#reply_'+active_form_id).html("");
	$('#reply_'+comment_id).html(active_form);
	active_form_id = comment_id;
	$('#CommentParentId').val(comment_id);
}

function deleteComment(comment_id) {
    	$.getJSON("/comments/delete/"+comment_id,{
    	}, function(result){
    		if (result.error != "") {
    			alert(result.error);
    		} else {


				$('#comment_'+comment_id).hide("slow");
				$('#commentsCount').html(result.comments);
				if ($('#comment_counter_text').html()) {
							  		$('#comment_counter_val').html(result.comments);
							  		if (result.comments == 1) {
							  			$('#comment_counter_text').html('Comment');
									 } else {
								  			$('#comment_counter_text').html('Comments');
									}
							  }
			     }
    })
}
