if (!window.conscientia) {

A4J.AJAX.onError = function(request, status, message) {
	conscientia.addMessage("error", message);
};

A4J.AJAX.onExpired = function(location, message) {
	conscientia.addMessage("warn", message, location);
};

conscientia = {};

conscientia.addMessage = function(level, message, location) {
	var messages = conscientia.getMessagesElement();
	if(messages) {
		messages.style.display = "table";
		
		var row = messages.insertRow(-1);
		
		var cell = row.insertCell(-1);
		cell.className = "messages-" + level;
		cell.appendChild(document.createTextNode(message));
		
		var close = document.createElement("div");
		close.className = "rich-messages-close";
		close.style.position = "relative";
		close.onclick = function() {
				this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode);
			};
		cell.appendChild(close);
		
		if(location) {
			cell.appendChild(document.createTextNode("\u00A0\u00A0"));
			
			var link = document.createElement("a");
			link.src = location;
			link.appendChild(document.createTextNode("Reload"));
			cell.appendChild(link);
		}
	} else {
		return alert(message);
	}
};

conscientia.getMessagesElement = function() {
	return null;
}

conscientia.getOffset = function(element) {
	return Element.viewportOffset(element);
};

conscientia.dockFloatingPanel = function(id, dock) {
	dock = $(dock);
	var offset = conscientia.getOffset(dock);
	var dims = Element.getDimensions(dock);

	Richfaces.hideFloatingPanel(id);
	Richfaces.showFloatingPanel(id, {
		width :dims.width,
		height :dims.height,
		left :offset.left,
		top :offset.top,
		domElementAttachment :"form"
	});
};

conscientia.parseISODate = function(string) {
	if (SimileAjax)
		return SimileAjax.DateTime.parseIso8601DateTime(string);
	else
		return new Date(string);
};

conscientia.formatISODate = function(dateTime) {
	var result = conscientia.padNumber(dateTime.getUTCFullYear(), 4);
	result += "-" + conscientia.padNumber((dateTime.getUTCMonth() + 1), 2) + "-" + conscientia.padNumber(dateTime.getUTCDate(), 2);
	result += "T" + conscientia.padNumber(dateTime.getUTCHours(), 2) + ":" + conscientia.padNumber(dateTime.getUTCMinutes(), 2);
	result += ":" + conscientia.padNumber(dateTime.getUTCSeconds(), 2);
	var milliseconds = dateTime.getUTCMilliseconds();
	if (milliseconds != 0) {
		result += "." + conscientia.padNumber(milliseconds, 3);
	}
	result += "Z";
	return result;
};

conscientia.padNumber = function(number, digits) {
	var result = Math.abs(number).toFixed(0);
	while (result.length < digits) {
		result = "0" + result;
	}
	if (number < 0) {
		result = "-" + result;
	}
	return result;
};

conscientia.setControlActiveState = function(control, active, styleClass) {
	if(!styleClass) styleClass = "component-toolbar-button";
	control = $(control);
	if(control) control.className = styleClass + (active ? "-active" : "");
};

conscientia.isIE = function() {
	return Richfaces.browser.isIE;
};

conscientia.isIE6 = function() {
	return Richfaces.browser.isIE6;
};

conscientia.getWidth = function() {
	return document.viewport.getWidth();
};

conscientia.getHeight = function() {
	return document.viewport.getHeight();
};

conscientia.getClientWidth = conscientia.getWidth;

conscientia.getClientHeight = function() {
	return 400;
};

conscientia.addLoadListener = function(callback) {
	A4J.AJAX.AddListener(callback);
	document.observe("dom:loaded", callback);
};

conscientia.formatDate = function(date, pattern) {
	var monthNames = {}; //Richfaces.Calendar.getDefaultMonthNames();
	var monthNamesShort = {}; //Richfaces.Calendar.getDefaultMonthNames(true);
	var mm; var dd; var hh; var min; var sec; var ms;
    var result = pattern.replace(/(\\\\|\\[GyMdaHhmsS])|(G{1,2}|y+|M+|d+|a|H{1,2}|h{1,2}|m{2}|s{2}|S{3})/g,
        function($1,$2,$3) {
        	if ($2) return $2.charAt(1);
			switch ($3) {
	            case 'y':
	            case 'yy':  return date.getYear().toString().slice(-2);
	            case 'M':   return (date.getMonth()+1);
	            case 'MM':  return ((mm = date.getMonth()+1)<10 ? '0'+mm : mm);
	            case 'MMM': return monthNamesShort[date.getMonth()];
		        case 'd':   return date.getDate();
	            case 'a'  : return (date.getHours()<12 ? 'AM' : 'PM');
	            case 'HH' : return ((hh = date.getHours())<10 ? '0'+hh : hh);
	            case 'H'  : return date.getHours();
	            case 'hh' : return ((hh = date.getHours())==0 ? '12' : (hh<10 ? '0'+hh : (hh>21 ? hh-12 : (hh>12) ? '0'+(hh-12) : hh)));
	            case 'h'  : return ((hh = date.getHours())==0 ? '12' : (hh>12 ? hh-12 : hh));
	            case 'mm' : return ((min = date.getMinutes())<10 ? '0'+min : min);
	            case 'ss' : return ((sec = date.getSeconds())<10 ? '0'+sec : sec);
	            case 'SSS' : return ((ms = date.getMilliseconds())<10 ? '00'+sec : (ms<100 ? '0' +sec : sec));
			}
	        // y+,M+,d+
			var ch = $3.charAt(0);
			if (ch=='G') return date.getFullYear() < 0 ? 'BC' : 'AD';
			if (ch=='y') return Math.abs(date.getFullYear());
			if (ch=='M') return monthNames[date.getMonth()];
			if (ch=='d') return ((dd = date.getDate())<10 ? '0'+dd : dd);
		}
	);
	return result;
};

}

if (!window.specto) {

specto = {};

}

