$.ajaxSetup({
	type: 'get',
	dataType: 'json',
	
	error: function() { alert('Hiba!'); }
});

function checkForm(form)
{
	var $form = $(form),
		$submit = $form.find(':submit'),
		submitText = $submit.text();
	
	$.ajax({
		url: $form.attr('action'),
		data: $form.serialize(),
		type: 'post',
		
		beforeSend: function() { $submit.text('Feldolgozás...').attr('disabled','disabled'); },
		
		success: function(response)
		{
			if (response.success) form.submit();
			else if (response.redirect) window.location.href = response.redirect;
			else
			{
				$submit.text(submitText).removeAttr('disabled');
				
				$form.find('.placeholder').each( function()
				{
					$(this).val($(this).attr('data-placeholder'));
				});
				
				showError(response.value);
			}
		},
		
		error: function() { form.submit(); }
	});
	
	return false;
}

function showError(text)
{
	$('#error').html(text).show();
	$('html,body').animate({scrollTop: $('#error').offset().top - 5},500);
	
	return false;
}


function toggleSearch(obj)
{
	if ($(document.search_form).css('display') == 'none')
	{
		$(obj).text('Kereső elrejtése');
		$(document.search_form.search_display).val('block');
	}
	else
	{
		$(obj).text('Kereső mutatása');
		$(document.search_form.search_display).val('none');
	}
	
	$(document.search_form).stop(true,true).slideToggle('fast');
}


function deleteFile(obj,name)
{
	$(obj).next().remove();
	$(obj).replaceWith($('<input />',{
		type: 'file',
		name: name
	}));
}


function serializeNestedSortable(obj)
{
	var $obj = $(obj),
		hash = '';
	
	$obj.find('li').each( function()
	{
		var $item = $(this);
		hash += $item.attr('id').split('-')[0] + '[' + $item.attr('id').split('-')[1] + ']=' + ($item.parents('li')[0] ? $item.parents('li:first').attr('id').split('-')[1] : 0) + '&';
	});
	
	return hash.substr(0,hash.length - 1);
}


jQuery.cookie = function(key,value,options)
{
	if (arguments.length > 1 && String(value) !== "[object Object]")
	{
		options = jQuery.extend({},options);
		if (value === null || value === undefined) options.expires = -1;
		
		if (typeof options.expires === 'number')
		{
			var days = options.expires, t = options.expires = new Date();
			t.setDate(t.getDate() + days);
		}
		
		value = String(value);
		
		return (document.cookie = [encodeURIComponent(key),'=',options.raw ? value : encodeURIComponent(value),options.expires ? '; expires=' + options.expires.toUTCString() : '',options.path ? '; path=' + options.path : '',options.domain ? '; domain=' + options.domain : '',options.secure ? '; secure' : ''].join(''));
	}
	
	options = value || {};
	var result, decode = options.raw ? function(s) { return s } : decodeURIComponent;
	
	return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};
