//var kategorie = '';
//
//$(document).ready(function() {
//	kategorie= $('ul.editcat').html();
//});

function addCategory() {
	$('.ajaxResponseMsg').hide();

	if (/^\w\s{,30}$/.test($('#nkat').val())) {
		$('#addNameValid').show();
		return;
	}
	//$('#submitAdd').hide();
	$('#loadingAdd').show();
	sPost ='name='+encodeURIComponent($('#nkat').val());
	$.ajax({
		type: "POST",
		url: "dodaj_kategorie.html",
		data: sPost,
		success: function(response) {
			$('#loadingAdd').hide();
			//$('#submitAdd').show();
			addCategorySuccess(response);
		},
		error: function() {
			$('#loadingAdd').hide();
			//$('#submitAdd').show();
			$('#addConn').show();
		}
	});

}
function addCategorySuccess(response) {
	if (response=='success') {
		$('addSuccess').show();
		$('#nkat').val('');
		//kategorie= $('ul.editcat').html();
		getCategories();
	}
	else if (response == 'error_limits') {
		$('#addLimits').show();
	}
	else if (response == 'error_exists') {
		$('#addExists').show();
	}
	else if (response == 'error_not_valid' || response == 'error_no_post') {
		$('#addValid').show();
	}
 	else {
		$('#addConn').show();
	}
}
function getCategories() {
	$.ajax({
		type: "POST",
		url: "pobierz_kategorie.html",
		success: function(response) {
			$('.editcat').html(response);
		},
		error: function(response) {
			//$('#addConn').show();
			window.location.reload();
		}
	});
}
function editCategory(identifier,key) {
	pattern = new RegExp(/^[0-9]+$/);
	$('ajaxResponseMsg').hide();
	if (pattern.test(identifier.toString()) && pattern.test(key.toString()) && ($('#category_'+key).length>0)) {
		getCategories();
		//$('ul.editcat').html(kategorie);
		$.ajax({
			type: "POST",
			url: "edytuj_kategorie.html",
			data: 'identifier='+identifier,
			success: function(response) {
				editCategorySuccess(response,key);
			},
			error: function() {
				getCategories();
				//$('ul.editcat').html(kategorie);
				$('#addConn').show();
			}
		});
	}
	else {
		$('#editValid').show();
	}
}
function editCategorySuccess(response,key) {
	if (response=='error_owner') {
		$('#editOwner').show();
	}
	else if (response == 'error_valid') {
		$('#editValid').show();
	}
	else if (response == 'error_not_logged') {
		$('#notLogged').show();
	}
	else {
		$('#category_'+key).empty();
		$('#category_'+key).attr('class','active');
		$('#category_'+key).append(response);
	}
}
function submitEdit(identifier,key) {
	$('.ajaxResponseMsg').hide();
	pattern = new RegExp(/^[0-9]+$/);
	if (/^\w\s{1,30}$/.test($('#editcategory').val())) {
		$('#editValid').show();
		return;
	}
	if (pattern.test(identifier.toString())) {
		$.ajax({
			type: "POST",
			url: "edytujkategorie.html",
			data: 'identifier='+parseInt(identifier)+'&name='+encodeURIComponent($('#editcategory').val()),
			success: function(response) {
				submitEditResponse(response);
			},
			error: function() {
				$('#addConn').show();
				getCategories();
				//$('ul.editcat').html(kategorie);
			}
		});
	}
	else {
		$('#editValid').show();
	}
}
function submitEditResponse(response) {
	if (response == 'success') {
		getCategories();
	}
	else if (response=='error_not_logged') {
		$('#notLogged').show();
	}
	else if (response=='error_valid') {
		$('editValid').show();
	}
	else if (response == 'error_owner'){
		$('#editOwner').show();
	}
 	else if (response == 'error_db') {
		$('#addConn').show();
	}
	else if (response == 'error_no_change') {
		$('#editNoChange').show();
		getCategories();
	}
}
function deleteCategory(identifier, key) {
	if (!confirm($('#delEmpty').html())) {
		return;
	}
	$('ajaxResponseMsg').hide();
	pattern = new RegExp(/^[0-9]+$/);
	if (pattern.test(identifier.toString()) && pattern.test(key.toString()) && ($('#category_'+key).length>0)) {
		$.ajax({
			type: "POST",
			url: "usun_kategorie.html",
			data: 'identifier='+identifier,
			success: function(response) {
				deleteCategorySuccess(response,key);
			},
			error: function() {
				getCategories();
				//$('ul.editcat').html(kategorie);
				$('#addConn').show();
			}
		});
	}
	else {
		$('#editValid').show();
	}
}
function deleteCategorySuccess(response){
	if (response=='success') {
		getCategories();
	}
	else if(response=='error_owner') {
		$('#editOwner').show();
	}
	else if(response=='error_valid') {
		$('editValid').show();
	}
	else if(response=='error_not_logged') {
		$('#notLogged').show();
	}
	else {
		$('#addConn').show();
	}
}

var categoriesShown = false;
var timerID = 0;

function showCategories()
{
    if (categoriesShown == false) {
        if (timerID) {
            clearTimeout(timerID);
         }
        $("#blog-categories-off").hide();
        $("#blog-categories-on").show();
        $("#blog-categories-list").slideDown();
        categoriesShown = true;
    }
}

function hideCategoriesTimer()
{
    $("#blog-categories-on").hide();
    $("#blog-categories-off").show();
    $("#blog-categories-list").slideUp();
    categoriesShown = false;
}

function hideCategories()
{
    if (categoriesShown) {
        timerID = setTimeout("hideCategoriesTimer()", 250);
        categoriesShown = false;
    }
}

function addCategoryKeyPress(ev)
{
    var key = ev.keyCode;
    if (key==13){
        addCategory();
    }
}