/*********************************************
*       Scripts Used For Sliding Forums      *
*********************************************/



//==========================================================
//  Recyclable Stuff 
// (these functions will come in handy all over the site)
//==========================================================


//SLIDE IN / OUT:

// store timeouts for each individual sliding item in array (stops them interfering with each other)
// in global so can be accessed by both slideIn and slideOut functions (so they never overlap and get stuck!)
var slideTimeout = new Array(); 



// slideOut function
// slides open a pane to the given height. 
function slideOut(itemID, toHeight, jumpSize, jumpSpeed) {
	
	//stop the slideIn function for this item (if it is still looping)
	clearTimeout(slideTimeout[itemID]);

	
	//get current height:
	var currentHeight = document.getElementById(itemID).offsetHeight;
		
	//if not already big enough: 
	if (currentHeight < toHeight) {
		
		//+ jumpSize px:
		var newHeight = currentHeight + (jumpSize * 1); //(multiplying converts jumpSize to int (otherwise JS would just append it))
		//if we've jumped above the desired height, shrink back down: 
		if (newHeight > toHeight) { newHeight = toHeight; }
			
		//make the height adjustment:
		document.getElementById(itemID).style.height = newHeight + 'px';
		
		//if height still isn't enough, wait jumpSpeed ms then go again:
		if (newHeight < toHeight) {
			slideTimeout[itemID] = setTimeout("slideOut('"+itemID+"', '"+toHeight+"', '"+jumpSize+"', '"+jumpSpeed+"')", jumpSpeed);
		}
	}		

}



// slideIn function
// slides closed a pane to the given height.
function slideIn(itemID, toHeight, jumpSize, jumpSpeed) {
	
	//stop the slideOut function for this item (if it is still looping)
	clearTimeout(slideTimeout[itemID]);


	//get current height:
	var currentHeight = document.getElementById(itemID).offsetHeight;
	
	//if not already small enough: 
	if (currentHeight > toHeight) {
		
		//- jumpSize px:
		var newHeight = currentHeight - (jumpSize * 1); //(multiplying converts jumpSize to int (otherwise JS would just append it))
		//if we've jumped below the desired height, grow back up: 
		if (newHeight < toHeight) { newHeight = toHeight; }
			
		//make the height adjustment:
		document.getElementById(itemID).style.height = newHeight + 'px';
		
		//if height is still too great, wait jumpSpeed ms then go again:
		if (newHeight > toHeight) {
			slideTimeout[itemID] = setTimeout("slideIn('"+itemID+"', '"+toHeight+"', '"+jumpSize+"', '"+jumpSpeed+"')", jumpSpeed);
		}
	}

}




//==========================================================
//  Forum Specific Stuff 
//==========================================================


// expandForum function
// slides out the threads for the given forum.
function expandForum(forumID, theTd) {
	
	var theTable = document.getElementById('forum'+forumID+'threadstable');
	
	//slide out the forum to the content height:
	slideOut('forum'+forumID+'threads', theTable.offsetHeight, 20, 30);
	
	//change the td onclick action to contract:
	theTd.onclick = function() { contractForum(forumID, theTd); };		

	//get the expand link (arrow in the top right) and change it to a contract link:
	var theLink = document.getElementById('forum'+forumID+'openlink1');
	theLink.title = 'collapse this forum';
	theLink.className = 'forumclose';
	
}



// contractForum function
// slides in the threads for the given forum.
function contractForum(forumID, theTd) {
		
	//slide in the forum to 0:
	slideIn('forum'+forumID+'threads', 1, 20, 30); // nb, this has been adjusted for IE quirks mode (can't handle 0 height)
	
	//change the td onclick action to expand:
	theTd.onclick = function() { expandForum(forumID, theTd); };		

	//get the contract link (arrow in the top right) and change it to an expand link:
	var theLink = document.getElementById('forum'+forumID+'openlink1');
	theLink.title = 'expand this forum';
	theLink.className = 'forumopen';
	
	//give the forumforumIDthreads div it's slideoutsection class (isn't preset on initially open forums)
	document.getElementById('forum'+forumID+'threads').className = 'slideoutsection';
	
}




