/**************************************************
 * dom-drag.js
 * 09.25.2001
 * www.youngpup.net
 * Script featured on Dynamic Drive (http://www.dynamicdrive.com) 12.08.2005
 **************************************************
 * 10.28.2001 - fixed minor bug where events
 * sometimes fired off the handle, not the root.
 **************************************************/
var contents_temp;
var Drag = {

	obj : null,

	init : function(o, oRoot, minX, maxX, minY, maxY, bSwapHorzRef, bSwapVertRef, fXMapper, fYMapper)
	{
		//contents_temp = document.getElementById("contents").innerHTML;
		
		o.onmousedown	= Drag.start;
		
		o.hmode			= bSwapHorzRef ? false : true ;
		o.vmode			= bSwapVertRef ? false : true ;
		
		
		o.root = oRoot && oRoot != null ? oRoot : o ;

		if (o.hmode  && isNaN(parseInt(o.root.style.left  ))) o.root.style.left   = "0px";
		if (o.vmode  && isNaN(parseInt(o.root.style.top   ))) o.root.style.top    = "0px";
		if (!o.hmode && isNaN(parseInt(o.root.style.right ))) o.root.style.right  = "0px";
		if (!o.vmode && isNaN(parseInt(o.root.style.bottom))) o.root.style.bottom = "0px";

		o.minX	= typeof minX != 'undefined' ? minX : null;
		o.minY	= typeof minY != 'undefined' ? minY : null;
		o.maxX	= typeof maxX != 'undefined' ? maxX : null;
		o.maxY	= typeof maxY != 'undefined' ? maxY : null;

		o.xMapper = fXMapper ? fXMapper : null;
		o.yMapper = fYMapper ? fYMapper : null;

		o.root.onDragStart	= new Function();
		o.root.onDragEnd	= new Function();
		o.root.onDrag		= new Function();
	},

	start : function(e)
	{
		//document.getElementById("contents").style.visibility = 'hidden';
		
		var o = Drag.obj = this;
		e = Drag.fixE(e);
		
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		o.root.onDragStart(x, y);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

		if (o.hmode) {
			if (o.minX != null)	o.minMouseX	= e.clientX - x + o.minX;
			if (o.maxX != null)	o.maxMouseX	= o.minMouseX + o.maxX - o.minX;
		} else {
			if (o.minX != null) o.maxMouseX = -o.minX + e.clientX + x;
			if (o.maxX != null) o.minMouseX = -o.maxX + e.clientX + x;
		}

		if (o.vmode) {
			if (o.minY != null)	o.minMouseY	= e.clientY - y + o.minY;
			if (o.maxY != null)	o.maxMouseY	= o.minMouseY + o.maxY - o.minY;
		} else {
			if (o.minY != null) o.maxMouseY = -o.minY + e.clientY + y;
			if (o.maxY != null) o.minMouseY = -o.maxY + e.clientY + y;
		}

		document.onmousemove	= Drag.drag;
		document.onmouseup		= Drag.end;
		
		return false;
	},

	drag : function(e)
	{
		e = Drag.fixE(e);
		var o = Drag.obj;
		
		var ey	= e.clientY;
		var ex	= e.clientX;
		var y = parseInt(o.vmode ? o.root.style.top  : o.root.style.bottom);
		var x = parseInt(o.hmode ? o.root.style.left : o.root.style.right );
		var nx, ny;

		if (o.minX != null) ex = o.hmode ? Math.max(ex, o.minMouseX) : Math.min(ex, o.maxMouseX);
		if (o.maxX != null) ex = o.hmode ? Math.min(ex, o.maxMouseX) : Math.max(ex, o.minMouseX);
		if (o.minY != null) ey = o.vmode ? Math.max(ey, o.minMouseY) : Math.min(ey, o.maxMouseY);
		if (o.maxY != null) ey = o.vmode ? Math.min(ey, o.maxMouseY) : Math.max(ey, o.minMouseY);

		nx = x + ((ex - o.lastMouseX) * (o.hmode ? 1 : -1));
		ny = y + ((ey - o.lastMouseY) * (o.vmode ? 1 : -1));

		if (o.xMapper)		nx = o.xMapper(y)
		else if (o.yMapper)	ny = o.yMapper(x)

		Drag.obj.root.style[o.hmode ? "left" : "right"] = nx + "px";
		Drag.obj.root.style[o.vmode ? "top" : "bottom"] = ny + "px";
		Drag.obj.lastMouseX	= ex;
		Drag.obj.lastMouseY	= ey;

		Drag.obj.root.onDrag(nx, ny);
		return false;
	},

	end : function()
	{
		//document.getElementById("contents").style.visibility = 'visible';
		
		document.onmousemove = null;
		document.onmouseup   = null;
		Drag.obj.root.onDragEnd(	parseInt(Drag.obj.root.style[Drag.obj.hmode ? "left" : "right"]), 
									parseInt(Drag.obj.root.style[Drag.obj.vmode ? "top" : "bottom"]));
		Drag.obj = null;
	},

	fixE : function(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
};

////////////////////////////////////////////////////////////////////////////////////
function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure )
{
	var cookie_string = name + "=" + escape(value);

	if (exp_y)
	{
		var expires = new Date(exp_y, exp_m, exp_d);
		cookie_string += "; expires=" + expires.toGMTString();
	}

	if (path)
	{
		cookie_string += "; path=" + escape ( path );
	}
	else
	{
		cookie_string += "; path=/";
	}

	if (domain)
	{
		cookie_string += "; domain=" + escape ( domain );
	}

	if (secure)
	{
		cookie_string += "; secure";
	}

	document.cookie = cookie_string;
}

////////////////////////////////////////////////////////////////////////////////////
function get_cookie(cookie_name)
{
	var results = document.cookie.match ('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');

	if (results)
	{
		return (unescape(results[2]));
	}
	else
	{
		return null;
	}
}

////////////////////////////////////////////////////////////////////////////////////

var tmp_div_chat_body;

////////////////////////////////////////////////////////////////////////////////////
function minmax_click()
{
	var test = get_cookie("minmaxcls");
	
	switch (test)
	{
		case "maximise":
			document.getElementById("div_chat_body").style.visibility = "hidden";
			document.getElementById("img_minmax").src = "/images/maximise.jpg";
			
			var _height = get_screen_size("height");
			var _width = get_screen_size("width");
			var _top = (_height - 35) + "px";
			if ((_width - 1100) > 0)
			{
				//var _left = ((_width - 1100) / 2) + "px";
				var _left = "1px";
			}
			else
			{
				var _left = "1px";
			}
			var minmaxcls = "minimise";
		break;
		
		default:
			document.getElementById("div_chat_body").style.visibility = "visible";
			document.getElementById("img_minmax").src = "/images/minimise.jpg";
			var _height = get_screen_size("height");
			var _width = get_screen_size("width");
			//var _top = ((_height - 630) / 2) + "px";
			var _top = "1px";
			var _left = "1px";
			//var _left = ((_width - 374) / 2) + "px";
			var minmaxcls = "maximise";
		break;
	}
	set_cookie ("minmaxcls", minmaxcls);
	set_cookie ("top", _top);
	set_cookie ("left", _left);
	
	document.getElementById("root").style.top = _top;
	document.getElementById("root").style.left = _left;
}

////////////////////////////////////////////////////////////////////////////////////
function get_screen_size(width_height)
{
	var myWidth = 0;
	var myHeight = 0;
	
	if (typeof( window.innerWidth ) == 'number')
	{
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	}
	else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
	{
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	}
	else if (document.body && (document.body.clientWidth || document.body.clientHeight))
	{
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	
	if (width_height == "width")
	{
		//window.alert( 'Width = ' + myWidth );
		return myWidth;
	}
	else
	{
		//window.alert( 'Height = ' + myHeight );
		return myHeight;
	}
}

////////////////////////////////////////////////////////////////////////////////////
function open_chat()
{
	var _height = get_screen_size("height");
	var _width = get_screen_size("width");
	var _top = "1px";
	var _left = "1px";
	//var _top = ((_height - 630) / 2) + "px";
	//var _left = ((_width - 374) / 2) + "px";
	
	set_cookie ("minmaxcls", "maximise");
	set_cookie ("top", _top);
	set_cookie ("left", _left);
	
	document.getElementById("root").innerHTML = "<p align='center'><img src='/images/ajax-loader-scores.gif' /></p>";
	new Ajax.Updater
	(
		'root',
		'/ajax-showchat.php',
		{
			asynchronous:true,
			method:'get',
			encoding:'UTF-8',
			evalScripts:true
		}
	);
	
	//document.getElementById("div_chat_body").style.visibility = "visible";
	//document.getElementById("img_minmax").src = "/images/minimise.jpg";
	document.getElementById("root").style.top = _top;
	document.getElementById("root").style.left = _left;
}

////////////////////////////////////////////////////////////////////////////////////
function close_chat()
{
	set_cookie ("minmaxcls", "close");
	set_cookie ("top", "1");
	set_cookie ("left", "1");
	document.getElementById("root").innerHTML = "";
}

////////////////////////////////////////////////////////////////////////////////////