Element.implement({
	centerLeft:function() {
		this.setStyle('left', parseInt((window.getWidth() - this.getCoordinates().width) / 2, 10) + 'px');
	},
	centerTop:function()
	{
		this.setStyle('top', ((window.getHeight() - this.getCoordinates().height) / 2) + 'px');
	},
	fixIEAlpha:function() {
		if (!window.ie6) {
			return this;
		}
		var url = this.getStyle('background-image');
		if (url === null || url == '' || url.indexOf(".png") < 0) {
			return this;
		}
		url = url.replace(/\.\.\/?/g, "").replace(/^.*\(/, "").replace(/\).*$/, "").replace(/http:\/\/[^\/]+/, "");
		this.setStyle("background-image", "none");
		this.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=' + url + ', sizingMethod="image")';
		return this;
	},
	center:function()
	{
		this.centerLeft();
		this.centerTop();
	},
	addMousePopup:function(popup)
	{
		popup.bindTo(this);
	},
	toObject:function() {
		var data = {};
		this.getFormElements().each(function(el) {
			var name = el.name;
			var value = el.getValue();
			if (value === false || !name || el.disabled) {
				return;
			}
			var qs = function(val) {
				data[name] = val;
			};
			if ($type(value) == 'array') {
				value.each(qs);
			}
			else {
				qs(value);
			}
		});
		return data;
	},
	setUnselectable:function()
	{
		this.setAttribute('style', ((this.getAttribute('style') !== null) ? this.getAttribute('style') + ';' : '') +
		                           ' - moz - user - select:none;');
		if (window.ie) {
			this.setAttribute('unselectable', 'yes');
		}
	},
	bringToTop:function() {
		lastIndex++;
		this.setStyle('z-index', lastIndex);
	},
	cloneWithEvents:function() {
		var clone = this.clone(false);
		clone.cloneEvents(this);
		$each(this.childNodes, function(child) {
			switch ($type(child)) {case"element":clone.appendChild($(child).cloneWithEvents());
				break;
				default:clone.appendChild(child.cloneNode(true));
			}
		});
		return clone;
	}
});
var HumanMessage = new Class({
	initialize:function(msg, options) {
		this.msg_div =
		new Element('div', {styles:{position:'absolute',width:550,padding:15,'padding-left':30,'font-size':14,'font-weight':'bold',color:'#fff','z-index':1000,visibility:'hidden'}});
		if (options === undefined) {
			options = {};
		}
		if (!options.containerId) {
			options.containerId = 'container';
		}
		if (!options.type) {
			options.type = 'error';
		}
		if (!options.textAlign || (options.textAlign != 'center' && options.textAlign != 'left' && options.textAlign != 'right')) {
			options.textAlign = 'center';
		}
		switch (options.type) {case'success':this.msg_div.setStyle('background-color', '#074610');
			this.msg_div.setStyle('border', '3px solid #053b05');
			break;
			case'error':default:this.msg_div.setStyle('background-color', '#BF1717');
			this.msg_div.setStyle('border', '3px solid #3b0505');
			break;
		}
		this.msg_div.innerHTML = msg;
		this.msg_div.inject(options.containerId);
		this.msg_div.centerLeft();
		this.msg_div.setStyle('top', 50);
		this.msg_div.setStyle('text-align', options.textAlign);
		this.msg_div.setStyle('visibility', 'visible');
		this.frame_fix = new Element('div', {styles:{position:'absolute',left:0,top:0,width:800,height:600,'z-index':999}});
		this.frame_fix.inject('container');
		this.frame_fix.center();
		this.mx = null;
		this.my = null;
		this.handler_move = this.move_handler.create({bind:this,event:true});
		this.handler_down = this.kill_msg.bind(this);
		document.addEvent.delay(200, document, ['mousemove',this.handler_move]);
		document.addEvent('mousedown', this.handler_down);
		this.delay_kill = this.kill_msg.delay(30000, this);
	},
	move_handler:function(ev) {
		if (this.mx === null || this.my === null) {
			this.mx = ev.clientX;
			this.my = ev.clientY;
			return;
		}
		if (ev.clientX == 0 || ev.clientY == 0) {
			return;
		}
		var xdiff = this.mx - ev.clientX,ydiff = this.my - ev.clientY;
		if (Math.sqrt(xdiff * xdiff + ydiff * ydiff) > 70) {
			this.kill_msg();
		}
	},
	kill_msg:function() {
		document.removeEvent('mousemove', this.handler_move);
		document.removeEvent('mousedown', this.handler_down);
		$clear(this.delay_kill);
		if (this.frame_fix) {
			this.frame_fix.dispose();
			delete this.frame_fix;
		}
		this.msg_div.fade('out');
	}
});
function errorMessage(message, containerId) {
	var xhtml = message.split('\n');
	var msgContent = "";
	xhtml.each(function (item) {
		if (item.trim().length > 0) {
			msgContent += "<li>" + item + "</li>";
		}
	});
	new HumanMessage(msgContent, {type:'error', textAlign:'left', containerId:containerId});
}
function successMessage(message, containerId) {
	var xhtml = message.split('\n');
	var msgContent = "";
	xhtml.each(function (item, index) {
		if (item.trim().length > 0) {
			if (index > 0) {
				msgContent += "<br/>";
			}
			msgContent += item;
		}
	});
	new HumanMessage(message, {type:'success', textAlign:'center', containerId:containerId});
}