var slideSteps = 30; // <-- # of incremental steps
var slideDelay = 10; // <-- delay between steps

var _slideObjects = {};

function init_slide_object(id, steps, delay) {
	var obj = {
		'id': id,
		'elem': null,
		'content': null,
		'init': 0,
		'height': 0,
		'inc': 0,
		'diff': 0,
		'visible': 0,
		'steps': steps != null ? steps : slideSteps,
		'delay': delay != null ? delay : slideDelay
	};
	obj.elem = document.getElementById(id);
	if(obj.elem == null) {
		return false;
	}
	obj.content = obj.elem.getElementsByTagName('DIV').item(0);
	if(obj.content == null) {
		return false;
	}

	if(!obj.elem.style.position) { // set relative only if no position has been set
		obj.elem.style.position = 'relative';
	}
	obj.elem.style.overflow	= 'hidden';
	obj.elem.style.display	= 'block';

	obj.height = parseInt(obj.elem.offsetHeight);

	obj.content.style.top		= (-1 * obj.height) + 'px';
	obj.elem.style.height		= '0px';
	obj.elem.style.visibility	= 'visible';

	obj.inc = Math.max(Math.round(obj.height/obj.steps),1); // <-- ensure the increment is not less than 1
	obj.diff = Math.ceil((obj.height - (obj.inc*obj.steps)) / obj.inc); // <-- correct for rounding differences
	obj.visible = 0;
	obj.init	= 1;
	_slideObjects[id] = obj;
}

		function dump(what) {
			var str = '';
			for(var i in what) {
				str += i+': '+what[i]+"\\n";
			}
			alert(str);
		}


function slide(id, steps, delay) {
	if(_slideObjects[id] == null) {
		init_slide_object(id, steps, delay);
	}
	if(_slideObjects[id] == null) {
		return false;
	}
	var obj = _slideObjects[id]

	var mult = obj.visible == 1 ? -1 : 1; // <-- mult == 1 -> slide down; mult == -1 -> slide up

	var val = obj.inc * mult;
	
	if(mult == 1) {
		obj.elem.style.display	= 'block';
	}

	for(var i = 0; i <= obj.steps+obj.diff; i++) { // <-- an extra step to ensure completion
		setTimeout('_doSlide("'+id+'",'+val+')', obj.delay*i);
	}
	obj.visible = mult > 0 ? 1 : 0;
	if(slideCallback != null && typeof(slideCallback) == 'function') {
		slideCallback(obj);
	}
}

function _doSlide(id, value) {
	var obj = _slideObjects[id];
	var new_height = parseInt(obj.elem.style.height) + value;
	if(value < 0 && new_height < Math.abs(value)) { // <-- don't contract less than 0
		obj.content.style.top	= (obj.height * -1) + 'px';
		obj.elem.style.height	= '0px';
		obj.elem.style.display	= 'none';
	}
	else if(value > 0 && new_height > obj.height) { // <-- don't expand past the height
		obj.elem.style.height = obj.height+'px';
		obj.content.style.top = '0px';
	}
	else { // <-- do the slide
		obj.elem.style.height	= new_height + 'px';
		obj.content.style.top	= parseInt(obj.content.style.top) + value + 'px';
	}
}


