
//-----------------------------------------------------------------
// slow box
// parameters:
// SPMaxStartHeight
//-----------------------------------------------------------------
var SBSlowInterval = 20; //ms
var SBSlowDivider = 1.4;
var SBCurrentSlowDownBoxID = null;
var SBCurrentSlowUpBoxID = null;
var SBSlowDownTimout = null;
var SBSlowUpTimout = null;
var SBSlowUpBoxHeight = 0;
var SBSlowUpBoxCurrentIteration = 0;
var SPSlowUpStart = 1;
//-----------------------------------------------------------------
function SBchangeBox (boxId)
{
	var boxlayer = document.getElementById(boxId);
	if (boxlayer)
	{ 
	    if (boxlayer.className != "sdiv")
	    {
	        SBopenBox (boxId);
	        //SBSlowDownBoxStart(boxId);
	    }
	    else
	    {
	        SBcloseBox(boxId);
	        //SBSlowUpBoxStart(boxId);
	    }
	}
}
//-----------------------------------------------------------------

function SBopenBox(boxId)
{
	var boxlayer = document.getElementById(boxId);
	if (boxlayer)
	{ 
		boxlayer.className = "sdiv";
		boxlayer.style.pixelHeight = "";
	}
	var im = document.images["__collapse_image" + boxId];
	if (im)
	{
		im.src = im.src.replace("expand", "collapse");
	}
}

//-----------------------------------------------------------------

function SBcloseBox(boxId)
{
	var boxlayer = document.getElementById(boxId);
	if (boxlayer)
	{ 
		boxlayer.className = "hdiv";
		boxlayer.style.pixelHeight = 0;
	}
	var im = document.images["__collapse_image" + boxId];
	if (im)
	{
		im.src = im.src.replace("collapse", "expand");
	}
}

//-----------------------------------------------------------------
function SBfinishAll()
{
	if (SBSlowUpTimout != null)
	{
		clearTimeout(SBSlowUpTimout);
		//alert("SBSlowUpTimout was " + SBCurrentSlowUpBoxID);
	}
	if (SBCurrentSlowUpBoxID != null)
	{
		//finish prev
		SBcloseBox(SBCurrentSlowUpBoxID);
	}
	
	if (SBSlowDownTimout != null)
	{
		clearTimeout(SBSlowDownTimout);
		//alert("SBSlowDownTimout was " + SBCurrentSlowDownBoxID);
	}
	if (SBCurrentSlowDownBoxID != null)
	{
		//finish prev
		SBopenBox(SBCurrentSlowUpBoxID);
	}

}
//-----------------------------------------------------------------
function SBSlowDownBoxStart(boxid)
{
	SBfinishAll();
	SBCurrentSlowDownBoxID = boxid;
	SBSlowDownBoxIteration(true);
}
//-----------------------------------------------------------------
function SBSlowDownBoxIteration(firstTime)
{
	if (!SBCurrentSlowDownBoxID) return;
	var boxlayer = document.getElementById(SBCurrentSlowDownBoxID);
	if (!boxlayer)
	{ 
		alert("no box layer");
		return;
	}
	var boxheight = firstTime ? getHeight(boxlayer) : boxlayer.style.pixelHeight;
	
	if (boxheight > window.SPMaxStartHeight) boxheight = window.SPMaxStartHeight;
	var newboxheight = Math.floor(boxheight / SBSlowDivider);
	if (newboxheight != 0 || firstTime)
	{
		SBSlowDownTimout = setTimeout("SBSlowDownBoxIteration(false)", SBSlowInterval);
	}
	else
	{
		SBopenBox(SBCurrentSlowDownBoxID);
		SBCurrentSlowDownBoxID = null;
	}
	boxlayer.style.pixelHeight = newboxheight;
}
//-----------------------------------------------------------------
function SBSlowUpBoxStart(boxid, minSteps)
{

	SBfinishAll();
	if (!minSteps) minSteps = 4;
	
	SBCurrentSlowUpBoxID = boxid;
	SBSlowUpBoxCurrentIteration = 0;
	SBSlowUpBoxIteration(true, minSteps);
}
//-----------------------------------------------------------------
function SBSlowUpBoxIteration(firstTime, minSteps)
{
	if (!SBCurrentSlowUpBoxID) return;
	var boxlayer = document.getElementById(SBCurrentSlowUpBoxID);
	if (!boxlayer)
	{ 
		alert("no box layer");
		return;
	}
	SBSlowUpBoxHeight = getHeight(boxlayer);
	var boxheight = firstTime ? SPSlowUpStart : boxlayer.style.pixelHeight;
	var newboxheight = Math.ceil(boxheight * SBSlowDivider);
	if (newboxheight < SBSlowUpBoxHeight || SBSlowUpBoxCurrentIteration < minSteps)
	{
		SBSlowUpBoxCurrentIteration += 1;
		SBSlowUpTimout = setTimeout("SBSlowUpBoxIteration(false, " + (minSteps-1) + ")", SBSlowInterval);
	}
	else
	{
		newboxheight = "";
		SBcloseBox(SBCurrentSlowDownBoxID);
		SBCurrentSlowUpBoxID = null;
	}
	//boxlayer.className = "sdiv";
	//boxlayer.style.pixelHeight = newboxheight;
}