var Popup =  (
{
	popupId: 'popup',
	popupTitleId: 'popupTitle',
	popupContentId: 'popupContent',
	popupHeaderId: 'popupHeader',
	lastActiveInput: null,
		
	show: function(title, url)
	{
		$(Popup.popupTitleId).update(title);
		ajax(url, null, "get", Popup.showOnSuccess);
	},
	
	showOnSuccess: function(transport)
	{
		$(Popup.popupContentId).update(transport.responseText);
		if(!$(Popup.popupId).visible())
		{
			Popup.setCenterPosition();
		}
		
		new Draggable(Popup.popupId, {handle: Popup.popupHeaderId, starteffect: null, endeffect: null});
		runAutoFunctions();
		$(Popup.popupId).show();
	},
	
	close: function()
	{
		$(Popup.popupId).hide();
	},
	
	setCenterPosition: function()
	{
		var scrollOffset = document.viewport.getScrollOffsets();
		var top = (document.viewport.getHeight() / 2) - ($(Popup.popupId).getHeight() / 2) * 1.5  + scrollOffset.top;
		var left = (document.viewport.getWidth() / 2) - ($(Popup.popupId).getWidth() / 2) + scrollOffset.left;
		
		$(Popup.popupId).setStyle('left:' + Math.round(left) + "px");
		$(Popup.popupId).setStyle('top:' + Math.round(top) + "px");
	},
	
	send: function(url)
	{
		var params = $('popupForm').serialize(true);
		
		ajax(url, params, "post", Popup.showOnSuccess);
	},
	
	focus: function(element)
	{
		Popup.lastActiveInput = element;
	},
	
	blur: function()
	{
		Popup.lastActiveInput = null;
	},
	
	insertAt: function()
	{
		if(Popup.lastActiveInput)
		{
			Popup.lastActiveInput.value = Popup.lastActiveInput.value + '@';
			Popup.lastActiveInput.focus();
		}
	}
})