var SlideAnimator = {
	DefaultAnimLength: 750,
	DefaultAnimTimerInterval: 5,
	Animations: new Array(),
	SlideDown: function(objname, timerInterval, animLength, callbackFunction)
	{
		if(SlideAnimator.Animations[objname])
			return false;

		var elem = document.getElementById(objname);

		// cannot slide down something that is already visible
		if(elem.style.display != "none")
			return false;

		var obj = SlideAnimator.Animations[objname] = {
			AnimElement: elem,
			Direction: "down",
			AnimLength: animLength || SlideAnimator.DefaultAnimLength,
			TimerInterval: timerInterval || SlideAnimator.DefaultAnimTimerInterval,
			StartTime: 0,
			EndHeight: 0,
			TimerID: 0,
			CallbackFunction: callbackFunction
		}

		SlideAnimator.StartSlide(obj);
		return true;
	},

	SlideUp: function(objname, timerInterval, animLength, callbackFunction)
	{
		if(SlideAnimator.Animations[objname])
			return false;

		var elem = document.getElementById(objname);

		// cannot slide up something that is already hidden
		if(elem.style.display == "none")
			return false;

        if(!elem.style.height)
            elem.style.height = elem.offsetHeight + "px";
        if(!elem.style.overflow)
            elem.style.overflow = "auto";

		var obj = SlideAnimator.Animations[objname] = {
			AnimElement: elem,
			Direction: "up",
			AnimLength: animLength || SlideAnimator.DefaultAnimLength,
			TimerInterval: timerInterval || SlideAnimator.DefaultAnimTimerInterval,
			StartTime: 0,
			EndHeight: 0,
			TimerID: 0,
			CallbackFunction: callbackFunction
		}

		//SlideAnimator.StartSlide(SlideAnimator.Animations[objname]);
		SlideAnimator.StartSlide(obj);
		return true;
	},

	StartSlide: function(obj)
	{
		obj.EndHeight = parseInt(obj.AnimElement.style.height);
		obj.StartTime = (new Date()).getTime();

		if(obj.Direction == "down")
			obj.AnimElement.style.height = "1px";

		obj.AnimElement.style.display = "block";

		obj.TimerID = setInterval( function(){ SlideAnimator.SlideTick(obj); }, obj.TimerInterval);
	},

	SlideTick: function(obj)
	{
		var elapsed = (new Date()).getTime() - obj.StartTime;

		if (elapsed > obj.AnimLength)
			SlideAnimator.EndSlide(obj);
		else
		{
			var d = Math.round(elapsed / obj.AnimLength * obj.EndHeight);
			if(obj.Direction == "up")
				d = obj.EndHeight - d;

			obj.AnimElement.style.height = d + "px";
		}

		return;
	},

	EndSlide: function(obj)
	{
		clearInterval(obj.TimerID);

		if(obj.Direction == "up")
			obj.AnimElement.style.display = "none";

		obj.AnimElement.style.height = obj.EndHeight + "px";
		
		var callbackFunction = obj.CallbackFunction;
		if(callbackFunction && typeof callbackFunction == "function")
		    callbackFunction(obj);

		delete(SlideAnimator.Animations[obj.AnimElement.id]);

		return;
	},
	
	Toggle: function(objname, timerInterval, animLength, callbackFunction)
	{
	    var elem = document.getElementById(objname);

		// cannot slide up something that is already hidden
		if(elem.style.display == "none")
		{
		    if(!elem.style.height)
		    {
		        // make visible, but hide content
		        elem.style.visibility = "hidden";
		        elem.style.display = "block";
		        // set height and overflow
		        elem.style.height = elem.offsetHeight + "px";
                elem.style.overflow = "auto";
                // re-hide element
                elem.style.visibility = "visible";
		        elem.style.display = "none";
		    }
			SlideAnimator.SlideDown(objname, timerInterval, animLength, callbackFunction);
		}
		else
		{
		    elem.style.height = elem.offsetHeight + "px";
            elem.style.overflow = "auto";
			SlideAnimator.SlideUp(objname, timerInterval, animLength, callbackFunction);
		}
	}
}