function setStatusMessages(data) {
	$('#error, #success, #message').children().empty().remove();
	addStatusMessages(data);
}

function addStatusMessages(data) {
	if (typeof data.messages != 'undefined') {
		$.each(data.messages, function(key, value) {
			if (typeof value == 'string') {
				$('#' + key).append('<li>' + value + '</li>');
			} else {
				$.each(value, function(key2, value2) {
					$('#' + key).append('<li>' + value2 + '</li>');
				});
			}
		});
		$('#statusMessages').show();
	} else {
		$('#statusMessages').hide();
	}
}

function urlize(txt) {
	sdiak = 'áäčďéěíĺľňóôőöŕšťúůűüýřžÁÄČĎÉĚÍĹĽŇÓÔŐÖŔŠŤÚŮŰÜÝŘŽ';
	bdiak = 'aacdeeillnoooorstuuuuyrzAACDEEILLNOOOORSTUUUUYRZ';

	tx = '';

	for (p = 0; p < txt.length; p++) {
		if (sdiak.indexOf(txt.charAt(p)) != -1)
			tx += bdiak.charAt(sdiak.indexOf(txt.charAt(p)));
		else
			tx += txt.charAt(p);
	}
	tx = trim(tx);
	var re = /\W+/g;

	tx = tx.replace(re, '-');
	return tx.toLowerCase();
}

function makeSaferPasswords(ids, hashedId) {
	for (var i = 0; i < ids.length; i++) {
		if (document.getElementById(ids[i]).value != '') {
			document.getElementById(ids[i]).value = hex_sha1(document.getElementById(ids[i]).value);
		}
	}
	document.getElementById(hashedId).value = 1;
}

function trim(str) {
	return str.replace(/^\s+|\s+$/g, '');
}

function callAjax(url, query, type, worker, dataType, startLoader, throwErrors)
{
	if (typeof startLoader == 'undefined') {
		startLoader = false;
	}
	if (typeof type == 'undefined') {
		type = 'GET';
	}
	if (typeof throwErrors == 'undefined') {
		throwErrors = false;
	}
	
	var options = {
		'type' : type,
		'url' : url,
		'data' : query,
		'dataType' : dataType,
		'error' : function (XMLHttpRequest, textStatus, errorThrown) {
			try {
				eval('var responseJSON = ' + XMLHttpRequest.responseText);
				setStatusMessages(responseJSON);
			} catch(e) {
				if (throwErrors) {
					alert(translate.getTranslation('Error occured while processing your request.'));
				}
			} finally {
				if (startLoader) {
					loader.finish();
					unsetProcessMessage();
				}
			}
		},
		'success' : function (data, textStatus) {
			setStatusMessages(data);
			if (worker) {
				worker(data);
			}
			if (startLoader) {
				loader.finish();
				unsetProcessMessage();
			}
		},
		'complete' : function (XMLHttpRequest, textStatus) {
		}
	};
	if (startLoader) {
		loader.start();
	}
	$.ajax(options);
}

function registerAutosave(form, url, interval)
{
	window.setInterval(function() {
		setProcessMessage(translate.getTranslation('Autosaving'));
		var query = form.serialize();
		if (typeof FCKeditorAPI != 'undefined') {
			for (fckeditorName in FCKeditorAPI.__Instances)	{
				query += '&' + fckeditorName + '=' + FCKeditorAPI.GetInstance(fckeditorName).GetXHTML();
			}
		}
		callAjax(url, query, 'POST', autosaveFinish, 'json', true);
	}, interval);
}

function autosaveFinish(data)
{
	$.each(data, function(key, value) {
		$('#' + key).val(value);
	});
	setProcessMessage(translate.getTranslation('Saved'));
}

function setProcessMessage(message)
{
	$('#processMessage').hide().text(message).show('slow');
}

function unsetProcessMessage(timeout)
{
	if (typeof timeout == 'undefined') {
		timeout = 3000;
	}
	
	setTimeout(function() {
		$('#processMessage').hide('slow').text('');
	}, timeout);
	
}

function preloadReferenceImages()
{
	$('div.ref-taska img').each(function() {
		$(this).triggerHandler('mouseover');
		$(this).triggerHandler('mouseout');
	});
}

function isEmpty(obj) {
	for(var prop in obj) {
		if(obj.hasOwnProperty(prop))
			return false;
	}
	return true;
}