var post;
var id;

if (!defaultAjax) {
	var defaultAjax = 0;
}

jQuery(document).ready(function() { 
	
	jQuery("input[name='ajax']").attr('value', defaultAjax);

	jQuery('#comments-filter-submit').remove();
	jQuery('#comments-filter select').change(function(){
        if (defaultAjax) {
            var options = {
                beforeSubmit: showPreloader('#filter-preloader'),
                dataType:   'http',
                success:    updateComments
            };
            jQuery('#comments-filter form').ajaxSubmit(options);
        } else {
            jQuery('#comments-filter form').submit();                
        }
	});

	updateCommentsJS();

}); // document ready

function resetMessage() {
	jQuery("#comments-messages").html("");
}
function showResponseReaction(response, status) {
	if (response == "FALSE") {
		jQuery("#comments-messages").html("Bohužel, na daný příspěvek nelze odpovědět");
		window.location.hash="comments-top";
	} else {
		jQuery("#new-comment-form").html(response);
		window.location.hash="new_comment";
	}
}

function showResponseRating(response) {
	if (response != "FALSE") {
		var rating = parseInt(jQuery(id).html()) + parseInt(response);
		rating = (rating == 0 ? "0" : rating);

		jQuery(id).html(rating);
		
		if (rating > 0) {
			jQuery(id).removeClass('negative').addClass('positive');
		} else if (rating < 0) {
			jQuery(id).removeClass('positive').addClass('negative');
		} else {
			jQuery(id).removeClass('positive').removeClass('negative');
		}
	}
	id = 0;
}

function updateComments(response, status){
	if (status = 'success') {
		jQuery('#comments-wrapper').html('');
		jQuery('#comments-list').html(response); 

		updateCommentsJS();
	};

	hidePreloader('#filter-preloader');

	return false;
}

function updateCommentsJS() {
	var options = {
		beforeSubmit:       resetMessage,
		resetForm:			true,
		dataType:			"http"
	} 

	jQuery("input[name='ajax']").attr('value', defaultAjax);

	var options = {
		success:			showResponse,
		resetForm:			true,
		dataType:			"http"
	} 

	jQuery(".comments-reaction").map(function () {
		jQuery(this).submit(function() { 
			if (!defaultAjax) {
				return true;
			}

			options['success'] = showResponseReaction;
			jQuery(this).ajaxSubmit(options); 

			return false; // musime vratit false, aby se formular neodeslal
		});
	}); 

	jQuery(".comments-rating form input[type='submit']").map(function() {
		jQuery(this).click(function() {
			var name = jQuery(this).attr('name');
			post = name;
		});
	});

  jQuery(".comments-rating form").map(function () {
		jQuery(this).submit(function() { 
			if (!defaultAjax) {
				return true;
			}

			var datapost = jQuery(this).formSerialize() + '&' + post + "=1";
			id = jQuery(this).parent().find("span");

			jQuery.ajax({
				type: "post",
				url:"/index.php",
				data: datapost, 
				success: showResponseRating
				}); 
			post = "";

			return false;
		});
	});
}

function disableButtons(e) {
	jQuery(e).find("input[type='submit']").attr('disabled', 1);
}

function enableButtons(e) {
	jQuery(e).find("input[type='submit']").attr('disabled', 0);
}


function showPreloader(e){
	jQuery(e).removeClass('comments-preloader');
}

function hidePreloader(e){
	jQuery(e).addClass('comments-preloader');
}

