function showError(message, timeout){
	return showMessage(message, timeout, "ui-state-error", "ui-icon-alert");
}

function showInfo(message, timeout){
	return showMessage(message, timeout, "ui-state-highlight", "ui-icon-info");
}

function showMessage(message, timeout, className, icon){
	if(!timeout){
		timeout = 5000;
	}

	$.tmpl("message", {
		className: className,
		icon: icon,
		message: message
	})
	.click(function(){$(this).clearQueue().slideUp(200);})
	.slideDown(200).delay(timeout).slideUp(200)
	.appendTo($(".notificationsArea"));
}

function handleErrorResponse(xhr, textStatus, errorThrown){
	switch (textStatus){
		case "error":
				if(xhr != null && xhr.status == 401){
					// logged out
					doLogout();
					return;
				}
		case "timeout":
		case "notmodified":
		case "parsererror":
			break;
	}
}

function parseResponse(json, silent){
	if(
		json != null &&
		(
			typeof json.httpErrorCode != 'undefined' && json.httpErrorCode ||
			typeof json.errorCode != 'undefined' && json.errorCode ||
			typeof json.errorDescription != 'undefined' && json.errorDescription
		)
	){
		var strError = null;
		if(json.httpErrorCode != null){
			// http error happened
			strError = json.httpErrorCode + ": " + (json.errorDescription ? json.errorDescription : "");
		} else {
			// server error
			var errorMessage = json.errorCode != null ? L('errors.' + json.errorCode) : null;
			if(errorMessage == null){
				strError = json.errorDescription ? json.errorDescription : "Something went wrong";
			} else {
				strError = errorMessage;
			}
		}

		if(!silent){
			showError(strError);
			if (typeof window.console != 'undefined' && console.log != null){
				console.error(strError);
			}
		}
		return strError;
	}

	return null;
}

function initTemplates(){
	// auto-init all templates
	$('[type="text/x-jquery-tmpl"]').each(function(ii, el){
		$.template(el.id.replace(/Template$/, ""), el);
	});
}

$.fn.defaultValue = function(label){
	this.focus(function(){
		if($(this).val() == label){
			$(this).removeClass("hasDefaultValue").val("");
		}
	}).blur(function(){
		if($(this).val() == ""){
			$(this).addClass("hasDefaultValue").val(label);
		}
	}).blur();
	return this;
};

function createGenericData4Grid(data) {
	if (data instanceof Array) {
		return {
			"page":  1,
			// TODO make '10' configurable
			"total":  Math.ceil(data.length / 20),
			// TODO do we need a true copy here?
			"rows": $.merge([], data),
			"records": data.length
		};
	}
}

function getSelectedRowsId(gridEl, colName) {
	if (typeof gridEl === 'object' && typeof colName === 'string' && colName.length > 0) {
		return _.reduce(
			gridEl.jqGrid("getGridParam", "selarrrow"),
			function(memo, row) {
				var val = gridEl.jqGrid("getCell", row, colName);
				// avoid storing empty checkboxes - for example select all
				if (val !== "") memo.push(val);

				return memo;
		}, []);
	}
}

function generateAttachmentUid(addButton) {
	var attachmentUid =
		new Date().getTime() + '_' +
		(addButton.closest('li').index() + 1) + '_' +
		(
			addButton.closest('td').find('.addBlock li').length +
			addButton.closest('td').find('.addedBlock li').length + 1
		);

	return attachmentUid;
}

function setPinnerHeight() {
	$('div.pinner').height(Math.max(
		$('#gbox_' + 'testsListGridContainer').height(),
		$('div.filters').height()
	));
}


