
function handleInfo(info)
{
	var infoTable = document.getElementById('info_dragged');
	infoTable.style.visibility = 'visible';
	infoTable.style.zIndex = ++drag.zIndex;		
	var infoTitle = document.getElementById('info_title');
	var infoText = document.getElementById('info_text');
	var ss = info.responseText.split("|");
	if (ss.length > 0)
		infoTitle.innerHTML = ss[0];
	if (ss.length > 1)
		infoText.innerHTML = ss[1];
	else
		infoText.innerHTML = '';
		
	drag.init('info');
//	var imgs = infoText.getElementsByTagName('img');
//	for (var i = 0;i < imgs.length;i++)
//	{
//		if (imgs[i].alt == 'info')
//			imgs[i].onclick = infoRequest;
//	}
}

function shutdown_info()
{
	var infoTable = document.getElementById('info_dragged');
	infoTable.style.visibility = 'hidden';
}

function infoRequest(el)
{
	sendRequest('/cms/info.php?i=' + el.title,handleInfo);
	
}

function sendRequest(url,callback,postData) {
	var req = createXMLHTTPObject();
	if (!req) return;
	var method = (postData) ? "POST" : "GET";
	req.open(method,url,true);
	req.setRequestHeader('User-Agent','XMLHTTP/1.0');
	if (postData)
		req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
		
	req.onload = function () 
	{
		if (req.readyState != 4) return;
		if (req.status != 200 && req.status != 304) 
		{
			alert('HTTP error ' + req.status);
			return;
		}
		callback(req);
	}
	req.onreadystatechange = function () 
	{
		if (req.readyState != 4) return;
		if (req.status != 200 && req.status != 304) 
		{
			alert('HTTP error ' + req.status);
			return;
		}
		callback(req);
	}
	if (req.readyState == 4) return;
	req.send(postData);
}

var XMLHttpFactories = [
	function () {return new XMLHttpRequest()},
	function () {return new ActiveXObject("Msxml2.XMLHTTP")},
	function () {return new ActiveXObject("Msxml3.XMLHTTP")},
	function () {return new ActiveXObject("Microsoft.XMLHTTP")}
];

function createXMLHTTPObject() {
	var xmlhttp = false;
	for (var i=0;i<XMLHttpFactories.length;i++) {
		try {
			xmlhttp = XMLHttpFactories[i]();
		}
		catch (e) {
			continue;
		}
		break;
	}
	return xmlhttp;
}


var drag = {

	obj : null,

	drager : Array(),
	drageri : 0,
	zIndex : 2001,
	
	init : function(id)
	{

//		alert('draginit');
		var o = document.getElementById(id + "_dragger");
		var oRoot = document.getElementById(id + "_dragged");
		o.onmousedown	= drag.start;
		o.onmouseover = null;
		
		o.root = oRoot && oRoot != null ? oRoot : o ;
		o.cmsid = id;
		

		drag.drager[drag.drageri] = o;
		drag.drageri++;

		var y = parseInt(o.root.style.top);
		var x = parseInt(o.root.style.left);

//		if (isIE)
		{
			o.root.ztop = y;
			o.root.zleft = x;
			o.root.style["left"] = "";
			o.root.style["top"] = ""; // y + obj.scrolltop?
		}
		
		var wW = document.documentElement.clientWidth || document.body.clientWidth;
		var wH = document.documentElement.clientHeight || document.body.clientHeight;
		
		if (x + o.root.offsetWidth + 10 > wW)
		{
			x = wW - o.root.offsetWidth - 10;
		}
	
		if (y + o.root.offsetHeight + 10 > wH)
		{
			y = wH - o.root.offsetHeight - 10;
		}

		if (x < 10)
			x = 10;
		if (y < 10)
			y = 10;
		drag.updatexy(o,x,y);
	},

	startup : function()
	{
//		window.onscroll = drag.scroll;
	

//		if (y)
//			window.scrollTo(0,y);

		for (i = 0;i < document.forms.length;i++)
		{
			document.forms.item(i).onsubmit = drag.setscroll;
		}
		var tables = document.getElementsByTagName('table');
		for (var i = 0;i < tables.length;i++)
		{
			var ss = tables[i].id.split('_');
			if (ss.length == 2)
			{
				if (ss[1] == 'dragged') // dragged
					drag.init(ss[0]);
			}
			
		}
		
		var imgs = document.getElementsByTagName('img');
//		for (var i = 0;i < imgs.length;i++)
//		{
//			if (imgs[i].alt == 'info')
//				imgs[i].onclick = infoRequest;
//		}
		
		
	},
	

	/*	
	scroll : function(e)
	{
		image = new Image();
		image.src="report_scroll.html";
		if (isIE)
		{
		}
	},
	*/
	
	yTop: function()
	{
		if (self.pageYOffset) 
		{
			return self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop || document.documentElement.scrollLeft )
		{      // Explorer 6 Strict
			return document.documentElement.scrollTop;
		} else if (document.body) 
		{	// all other Explorers
		    return document.body.scrollTop;
		}
	},
	
	setscroll : function(e)
	{
		e = e || window.event;

//		var a = this.action;
//		a += "?scrolly=" 
//		a += drag.yTop();
//		this.action = a;
	},
	
	start : function(e)
	{
		e = e || window.event;

		nn = e.target || e.srcElement;
//		alert(nn.nodeName);
		if (nn.nodeName.toLowerCase() != 'td')
			nn = nn.parentNode;
//			return false;
		
		document.onmousemove = drag.dragcan;
		document.body.onmousemove	= drag.drag;
		document.onmouseup		= drag.end;
		
		var o = drag.obj = this;
//		var o = drag.obj = nn;
		var y = parseInt(o.root.style.top);
		var x = parseInt(o.root.style.left);

		o.lastMouseX	= e.clientX;
		o.lastMouseY	= e.clientY;

  		o.root.style.zIndex = ++drag.zIndex;		
		return false;
//		return true;
	},

	
	updatexy: function(obj,x,y)
	{
		obj.root.ztop = y;
		obj.root.zleft = x;

//				if (!isIE)
		{		
			obj.root.style["left"] = x + "px";
			obj.root.style["top"] = (y) + "px"; 
		}
		
	},
	
	dragcan : function(e)
	{
		// stops IE from fiddling round with the selection and scroll positions then drag cursor is outside body
		e = e || window.event;
		
		e.cancelBubble = true;
		return false;
	},
	
	drag : function(e)
	{
		e = e || window.event;

//		return;
		var ey	= e.clientY;
		var ex	= e.clientX;

		if (ey < 10)
			ey = 10;

		if (ex < 10)
			ex = 10;
			
		if (ex + 10 > document.documentElement.clientWidth)
//		{
			ex = document.documentElement.clientWidth - 10;
//			drag.end();
//			return false;
//		}
		
		if (ey + 40 > document.documentElement.clientHeight)
//		{
			ey = document.documentElement.clientHeight - 40;
//			drag.end();
//			return false;
//		}
		
			
			
		var y = drag.obj.root.ztop;
		var x = drag.obj.root.zleft;

				var nx, ny;

		nx = x + (ex - drag.obj.lastMouseX);
		ny = y + (ey - drag.obj.lastMouseY);

		drag.updatexy(drag.obj,nx,ny);
		
		drag.obj.lastMouseX	= ex;
		drag.obj.lastMouseY	= ey;

		e.cancelBubble = true;
		return false;
//		return true;
	},

	end : function(e)
	{
		e = e || window.event;
		document.body.onmousemove = null;
		document.onmousemove = null;
		document.onmouseup   = null;
		image = new Image();
		image.src="report_pos.html?panel=" + drag.obj.cmsid + "&x=" + drag.obj.root.zleft + "&y=" + drag.obj.root.ztop;
		
		drag.obj = null;
		
	}
	
};




