

var iDivClosedSpacing
var iDivCount
var sNotCoverDivID

function generalAdjustDivs(sDir, oParentDiv, oExpandDiv){
	var oDiv
	var sDivObjName
	var sDivID
	var sDivName
	var blnMove = false
	var blnDivAfterContracted = false
	var iLastPosn
	var iLastHeight
	var collChildren = ns4 ? oParentDiv.ref.layers	: (ns5 ? oParentDiv.ref.childNodes : oParentDiv.ref.children);
	if(ns5)
	{
		var tempCollection = []
		for(i=0;i<collChildren.length;i++)
		{
			if(collChildren[i].id) tempCollection[tempCollection.length] = collChildren[i];
		}
		collChildren = tempCollection;
	}
	
	var sRegionName
	var blnIsCover
	var iLengthChange = 0
	var iChosenRegionDivPos
	var blnFirstItem = true	
		
	iDivCount = 0

	for(var i = 0 ; i < collChildren.length; i++){
		//take of the 'Div' part off id, to get the special object
		sDivID = collChildren[i].id
		sDivObjName = sDivID.substr(0, sDivID.length - 3)
		oDiv = eval(sDivObjName)

		blnIsCover = isTheCover(oDiv)
	
		if (blnIsCover) iLengthChange = -1*oDiv.y	
		//really awful ns6 hack, fix returning incorrect length for list
		if (ns5) iLengthChange = 0
		
		if (!blnIsCover){
			iDivCount +=1
			if(oDiv.id)	sNotCoverDivID = oDiv.id
		}

		if (sDir=='up')
		{
			if(blnMove){
	//compare top pos with last top pos to see if it needs moving up
			//check if the div in question actually needs contacting. if not, do nothing from now
				if(blnDivAfterContracted && oDiv.y == iDivClosedSpacing + iLastPosn){
					blnMove = false
				}else{
					moveUp(oDiv, iLastPosn, iDivClosedSpacing, iLastHeight)
				}
			}
			if(blnDivAfterContracted){
				//this can only be true for one Div, so if it's true set it to false
				blnDivAfterContracted = false
			}
			if(oDiv == oExpandDiv){
				blnMove = true
				blnDivAfterContracted = true
				sRegionName = getRegionFromDivID(oDiv.id)
				if(sRegionName){
					if(eval("typeof placeTxt"+sRegionName+"Link=='object'")){
						if(!ns5)eval("placeTxt"+sRegionName+"Link.ref.href=\"javascript:generalAdjustDivs('down', " + getDivObjName(oParentDiv) + ", results" + sRegionName + ");\"")
						else eval("document.links['placeTxt"+sRegionName+"'].href=\"javascript:generalAdjustDivs('down', " + getDivObjName(oParentDiv) + ", results" + sRegionName + ");\"")
					}
				}

				//callback to any special code that has to be pefrormed based on what div is collapsing			
				//return nothing	
				expandDiv_callback(oDiv, oParentDiv, sDir)
				
			}
			iLastPosn = getDivPosn(oDiv, oParentDiv, blnFirstItem)
			iLastHeight = oDiv.h
		}else if(sDir == 'down')
		{
			if(blnDivAfterContracted){
	//			alert(oDiv.id+" "+iLastHeight)
				moveDown(oDiv, iLastPosn, iDivClosedSpacing, iLastHeight)
			}else{
				
			//	alert(iLastPosn);
				if (iLastPosn+"" != 'undefined')
				{
					moveUp(oDiv, iLastPosn, iDivClosedSpacing, iLastHeight)
				}
			}
			if(blnDivAfterContracted){
				//this can only be true for one Div, so if it's true set it to false
				blnDivAfterContracted = false
			}
			if(oDiv == oExpandDiv){
		//		alert(oDiv.id+" "+oDiv.h)
				sRegionName = getRegionFromDivID(oDiv.id)
				iChosenRegionDivPos = oDiv.y
				if(sRegionName){
				//if there is no region name, it is the cover div
					if(eval("typeof placeTxt"+sRegionName+"Link=='object'")){
						if(!ns5) eval("placeTxt"+sRegionName+"Link.ref.href=\"javascript:generalAdjustDivs('up', " + getDivObjName(oParentDiv) + ", results" + sRegionName + ");\"")
						else eval("document.links['placeTxt"+sRegionName+"'].href=\"javascript:generalAdjustDivs('up', " + getDivObjName(oParentDiv) + ", results" + sRegionName + ");\"")
					}
				}
				blnDivAfterContracted = true


				//callback to any special code that has to be pefrormed based on what div is expanding			
				//return nothing	
				expandDiv_callback(oDiv, oParentDiv, sDir)

			}else{
				sRegionName = getRegionFromDivID(oDiv.id)
				if(sRegionName){
					if(eval("typeof placeTxt"+sRegionName+"Link=='object'"))
					{
						if(!ns5) eval("placeTxt"+sRegionName+"Link.ref.href=\"javascript:generalAdjustDivs('down', " + getDivObjName(oParentDiv) + ", results" + sRegionName + ");\"");
						else eval("document.links['placeTxt"+sRegionName+"'].href=\"javascript:generalAdjustDivs('down', " + getDivObjName(oParentDiv) + ", results" + sRegionName + ");\"");
					}
				}
			}
			iLastPosn = getDivPosn(oDiv, oParentDiv, blnFirstItem)
						
			//callback to page for any special div height calculations
			//must return the pixel height of oDiv
			//can call getDivHeight_default
			iLastHeight = getDivHeight_callback(oDiv)
		}
		if (blnIsCover) iLengthChange += oDiv.y	

		blnFirstItem = false
	}
	
	//cleanup functions
	//return nothing
	postExpand_callback(oParentDiv, oExpandDiv, iLengthChange)
	
}

function getDivPosn(oDiv, oParentDiv, blnFirstItem){
	var iPosn = oDiv.y
	
	if (ns5&&blnFirstItem){
//hack to get round ns5/divobject (incorrect y reported) problem
		if (iPosn==oParentDiv.y) iPosn = 0

	
	}
	return iPosn

}

function isTheCover(oDiv){
	if (oDiv.id == 'cover1Div' || oDiv.id == 'cover2Div')
		return true
	else
		return false
}

function getDivObjName(oDiv){
	var sDivID = oDiv.id

	if(sDivID.substr(sDivID.length-3, sDivID.length).toLowerCase() == 'div'){
		return sDivID.substring(0, sDivID.length-3)
	}

}

function getDivHeight_default(oDiv){
	return oDiv.h;
}


function moveUp(oDiv, iLastDivY, iDivClosedYSpace, iLastDivHeight){
	var newY = iDivClosedYSpace + iLastDivY
	if (oDiv.y != newY){
		oDiv.moveTo(0, newY)
	}
}

function moveDown(oDiv, iLastDivY, iDivClosedYSpace, iLastDivHeight){
	var newY = iLastDivHeight + iLastDivY;
	if (oDiv.y != newY){
		oDiv.moveTo(0,newY)
	}
}

function getRegionFromDivID(sDivID){
	if(sDivID.substr(0, 7) == 'results'){
		return sDivID.substring(7, sDivID.length-3)
	}
}

function initExpandDivs(oParentDiv, oDefaultExpandDiv){
		
	var collChildren = ns4 ? oParentDiv.ref.layers	: (ns5 ? oParentDiv.ref.childNodes : oParentDiv.ref.children);
	if(ns5)
	{
		var tempCollection = []
		for(i=0;i<collChildren.length;i++)
		{
			if(collChildren[i].id) tempCollection[tempCollection.length] = collChildren[i];
		}
		collChildren = tempCollection;
	}
	
	
		generalAdjustDivs('down', oParentDiv, oDefaultExpandDiv)
		//if there's only one div, expand it
		if(iDivCount == 1){
			sRegionName = getRegionFromDivID(sNotCoverDivID)
			if(eval("typeof results"+sRegionName+"=='object'")){
				generalAdjustDivs('down', oParentDiv, eval("results"+sRegionName))
			}
			
		}
	
	for(var i = 0 ; i < collChildren.length; i++)
	{
		//unhide the divs
		sDivID = collChildren[i].id;
		sDivObjName = sDivID.substr(0, sDivID.length - 3);
		oDiv = eval(sDivObjName);
		oDiv.show();
	}	
}
