// accept a number of pixels to move as an argument
// move a fixed percentage of that many pixels
// unless that would mean moving less that the threshold
// the simply go the whole number of pixels
function smoothMoveByX( id, totalAmount, soFarAmount, proportion, threshold, execBeforeMove ) {

	//how far do we have to go
	toGo = totalAmount - soFarAmount;

	//apply the minumum threshold of 1
	threshold = Math.max( threshold, 1);

	if( Math.abs(toGo) < threshold ) {
		//simply go the rest of the way and exit
		moveXY( id, xOf(id)+toGo, yOf(id) );
//		debuga( 'moved all ' + totalAmount + 'px' );
		return;
	} else {
		//go a proportion of the rest of the way
		if ( totalAmount > 0 ) {
			//ceil
			deltaX = Math.ceil(toGo * proportion);
		} else {
			//floor
			deltaX = Math.floor(toGo * proportion);
		}

		eval( execBeforeMove );
		moveXY( id, xOf(id)+deltaX, yOf(id) );
		soFarAmount += deltaX;
	}

	//now wait a while before recursing	
	timeout = 'smoothMoveByX(\'' + id + '\',' + totalAmount + ','  + soFarAmount + ',' + proportion + ',' + threshold + ', \'' + execBeforeMove + '\')';
	//debug( timeout );
	setTimeout(timeout,1);
}

function moveXY(myObject, x, y) {
	getStyleObject(myObject).top = y + 'px';
	getStyleObject(myObject).left = x + 'px';
}

function getStyleObject(objectId) {
	if(document.getElementById && document.getElementById(objectId)) {
		return document.getElementById(objectId).style;
	} else if (document.all && document.all(objectId)) {
		return document.all(objectId).style;
	} else if (document.layers && document.layers[objectId]) {
		return getObjNN4(document,objectId);
	} else {
		return false;
	}
}



function getObjNN4(obj,name)
{
	var x = obj.layers;
	var foundLayer;
	for (var i=0;i<x.length;i++)
	{
		if (x[i].id == name)
		 	foundLayer = x[i];
		else if (x[i].layers.length)
			var tmp = getObjNN4(x[i],name);
		if (tmp) foundLayer = tmp;
	}
	return foundLayer;
}

function xOf(id) {
	currentPos = document.getElementById(id).style.left;
	currentPos = parseInt(currentPos.substring(0, currentPos.length-2));
	//debug( currentPos );
	return currentPos;
}

function yOf(id) {
	currentPos = document.getElementById(id).style.top;
	currentPos = parseInt(currentPos.substring(0, currentPos.length-2));
	//debug( currentPos );
	return currentPos;
}

function debugo( msg ) {
	document.getElementById('content').innerHTML = '| ' + msg.replace('/</g', '&lt;') + '<br/>';
}


function debug( msg ) {
	document.getElementById('content').innerHTML = '|' + msg + '<br/>';
}

function debuga( msg ) {
	document.getElementById('content').innerHTML += '|' + msg + '<br/>';
}
