
//_____[ PORTAL CHAT ]_____\\

var zsrx = 
{
	chat_container: null,
	chat_link: null,
	chat_widget: null,
	chat_handle: null,
	chat_close: null,
	cookie: null,
	
	assignElements: function()
	{
		zsrx.chat_container = document.getElementById('chat_container');
		zsrx.chat_link = document.getElementById('chat_link');
		zsrx.chat_widget = document.getElementById('chat_widget');
		zsrx.cookie = 'zsrx_chat_widget';
	},

	assignListeners: function()
	{
		if(zsrx.chat_link && zsrx.chat_widget)
		{
			zsrx.chat_link.onclick = function()
			{
				zsrx.enableChat();
				return false;
			};
		}
	},
	
	activateCloseLink: function()
	{
		zsrx.chat_close = document.createElement('a');
		zsrx.chat_close.setAttribute('id','chat_close');
		zsrx.chat_close.setAttribute('href','#');
		zsrx.chat_close.setAttribute('title','Close');
		var txt = document.createTextNode('x');
		zsrx.chat_close.appendChild(txt);
		zsrx.chat_close.onclick = function()
		{
			zsrx.disableChat();
			return false;	
		};
		zsrx.chat_widget.insertBefore(zsrx.chat_close,zsrx.chat_widget.firstChild);
	},
	
	buildChatWidgetHandle: function()
	{
		zsrx.chat_handle = document.createElement('div');
		zsrx.chat_handle.setAttribute('id','chat_handle');
		zsrx.chat_handle.setAttribute('title','Click and hold to drag');
		//zsrx.meebo_handle.setAttribute('href','#'); \u00A0
		//var txt = document.createTextNode('Chat w/ WGS librarian.');
		//zsrx.chat_handle.appendChild(txt);
		zsrx.chat_widget.insertBefore(zsrx.chat_handle,zsrx.chat_widget.firstChild);
	},
	
	enableChat: function()
	{
		zsrx.chat_widget.className = 'active';
		zsrx.chat_container.className = 'inactive';
		zsrx.activateCloseLink();
		zsrx.buildChatWidgetHandle();
		dragDrop.initElement(zsrx.chat_handle);
	},
	
	disableChat: function()
	{
		zsrx.chat_widget.className = 'inactive';
		zsrx.chat_container.className = 'active';
		zsrx.chat_close.parentNode.removeChild(zsrx.chat_close);
		cookie.erase(zsrx.cookie);
	},
	
	processCookie: function()
	{
		var c = cookie.get(zsrx.cookie);
		if(c)
		{
			var s = c.split(':');
			var x = s[0];
			var y = s[1];
			zsrx.chat_widget.style.top = y;
			zsrx.chat_widget.style.left = x;
			zsrx.enableChat();
		}
	},

	init: function()
	{
		zsrx.assignElements();
		zsrx.processCookie();
		zsrx.assignListeners();
	}
}
addLoadEvent(zsrx.init);

////////////////////////////////////////////////
// accessories
////////////////////////////////////////////////

//_____[ DRAG AND DROP ]_____\\

dragDrop = 
{
	initialMouseX: undefined,
	initialMouseY: undefined,
	startX: undefined,
	startY: undefined,
	draggedObject: undefined,

	initElement: function(element)
	{
		element.onmousedown = dragDrop.startDragMouse;
	},
	
	startDragMouse: function(e)
	{
		dragDrop.startDrag(this.parentNode);
		var evt = e || window.event;
		dragDrop.initialMouseX = evt.clientX;
		dragDrop.initialMouseY = evt.clientY;
		document.onmousemove = dragDrop.dragMouse;
		document.onmouseup = dragDrop.releaseElement;
		return false;
	},

	startDrag: function(obj)
	{
		if(dragDrop.draggedObject)
		{
			dragDrop.releaseElement();
		}
		dragDrop.startX = obj.offsetLeft;
		dragDrop.startY = obj.offsetTop;
		dragDrop.draggedObject = obj;
		obj.className += ' dragged';
	},
	
	dragMouse: function(e)
	{
		var evt = e || window.event;
		var dX = evt.clientX - dragDrop.initialMouseX;
		var dY = evt.clientY - dragDrop.initialMouseY;
		dragDrop.setPosition(dX,dY);
		return false;
	},

	setPosition: function(dx,dy)
	{
		dragDrop.draggedObject.style.left = dragDrop.startX + dx + 'px';
		dragDrop.draggedObject.style.top = dragDrop.startY + dy + 'px';
	},

	releaseElement: function()
	{
		cookie.create(zsrx.cookie,''+dragDrop.draggedObject.style.left+':'+dragDrop.draggedObject.style.top+'',1,'/');
		document.onmousemove = null;
		document.onmouseup = null;
		dragDrop.draggedObject.className = dragDrop.draggedObject.className.replace(/dragged/,'');
		dragDrop.draggedObject = null;
	}
}
