function g(o) {return document.getElementById(o);}

function pad(chr, times) {
	var output = '';
	for(var i = 0; i < times; i++) {
		output += chr;	
	}
	return output;
}

var mousex, mousey;
function mousemoved(evt) {
	if(document.all) {
		mousex = window.event.clientX+document.body.scrollLeft;
		mousey = window.event.clientY+document.body.scrollTop;
	}
	else {
		mousex = evt.pageX;
		mousey = evt.pageY;
	}
	if(activeelement) {
		var wx = mousex - offsetX;
		var wy = mousey - offsetY;
		wx = (wx>0) ? wx:0;
		wy = (wy>0) ? wy:0;
		shiftTo(activeelement,wx, wy);
		return false;
	}
	if(engaged.type == 'resize') {
		var hpos = itempos('header'+engaged.id);
		var wx = mousex - hpos.x;
		var minx = itemsize('fieldtitle'+engaged.id);
		minx = parseInt(minx.width) + 32;
		if(wx < minx) wx = minx;
		g('header'+engaged.id).width = wx;
		fields[engaged.id].width = wx;
	}
}

function itempos(o) {
	var item = g(o);
	var coords = {x: 0, y: 0 };
	while(item) {
		coords.x += item.offsetLeft;
		coords.y += item.offsetTop;
		item = item.offsetParent;
	}
	return coords;
}

function itemsize(o) {
	var size = {width: 0, height: 0 };
	size.width = g(o).offsetWidth;//(document.all) ? g(o).clientWidth : g(o).offsetWidth;
	size.height = g(o).offsetHeight;//(document.all) ? g(o).clientHeight : g(o).offsetHeight;
	return size;
}


function shiftTo(obj, x, y) {
	obj.style.top = y + 'px';
	obj.style.left = x + 'px';
}

// START DRAGGABLE SPECIFIC CODE
var offsetX, offsetY, curz = 1;
var mx, my, activeelement;

function grab(thing) {
	activeelement = g(thing);
	if(document.all) {
		offsetX = window.event.offsetX + 2;
		offsetY = window.event.offsetY + 2;	
	}
	else {
		offsetX = mousex - parseInt(activeelement.style.left);
		offsetY = mousey - parseInt(activeelement.style.top);
	}
}
		
function release() {
	activeelement = null;
}

document.onmousemove = mousemoved;

function openpanel(panelname) {
	g('helppanel').style.display = 'none';
	g(panelname).style.left = (mousex - 20) + 'px';
	g(panelname).style.top = (mousey - 20) + 'px';
	g(panelname).style.display = 'block';
}

function closepanel(panelname) {
	g(panelname).style.display = 'none';
}
