function message(id, name,e) {
	document.body.appendChild(inlineIM());
	xPos = Event.pointerX(e)-175;
	yPos = Event.pointerY(e)-70;
	if (xPos < 10) xPos = 10;
	if (yPos < 10) yPos = 10;
	
	$('imBoxWrapper').style.left = xPos+'px';
	$('imBoxWrapper').style.top = yPos+'px';
	//obj.parentNode.insertBefore(inlineIM(),obj.nextSibling);
	$('imStrap').innerHTML = 'Send a message to '+name;
	$('imRecip').value = id;
	new Effect.Grow('imBox', {afterFinish: function() { $('imText').focus(); }});
}
function initInlineMessages() {
	$A(document.getElementsByClassName('im')).each(
		function(el) {
			var temp = new Array();
			temp = el.title.split(',');
			el.onclick = function(e) { message(temp[0],temp[1],e); Event.stop(e); };
		}
	);
}
Event.observe(window,'load',initInlineMessages);
function closeMessage() {
	new Effect.Shrink('imBox', { afterFinish: function() { $('imText').value = ''; $('imRecip').value = ''; }});
}
function sendMessage() {
	form = Form.serialize($('imForm'));
	new Ajax.Request('/tools/messenger/send.php',{ postBody: form, onSuccess: closeMessage });
}

function ce (type, id) {
	var el = document.createElement(type);
	if (id != '')
		el.id = id;
	return el;
}

function inlineIM() {
	imSubEl = document.createElement('input');
	imSubEl.type = 'submit';
	imSubEl.value = 'Send Message';
	imStrapEl = ce('div','imStrap');
	imBoxEl = ce('div','imBox');
	imTextEl = ce('textarea','imText');
	imTextEl.cols = 40;
	imTextEl.rows = 5;
	imTextEl.name = 'msg';
	imRecipEl = ce('input','imRecip');
	imRecipEl.type = 'hidden';
	imRecipEl.name = 'recipient';
	imFormEl = ce('form','imForm');
	imFormEl.onsubmit = function(e) { sendMessage(); Event.stop(e); }
	imCloseEl = ce('a','');
	imCloseEl.href = '#';
	imCloseEl.onclick = function(e) { closeMessage(); Event.stop(e); }
	imCloseEl.appendChild(document.createTextNode('Close'));
	imBoxWrapperEl = ce('div','imBoxWrapper');
	
	imFormEl.appendChild(imTextEl);
	imFormEl.appendChild(imRecipEl);
	imFormEl.appendChild(imSubEl);
	imFormEl.appendChild(imCloseEl);
	imBoxEl.appendChild(imStrapEl);
	imBoxEl.appendChild(imFormEl);
	imBoxWrapperEl.appendChild(imBoxEl);
	imBoxEl.style.display = 'none';
	return(imBoxWrapperEl);
}