/*
Expanding and Collapasing Div
Author: Brian Offenheim (brian@es3.ca)

Usage:
----------------------------------------------------------------------------------------------------------------------------
hideMenu(objID, maxSrc, maxText, minSrc, minText, closedHeight, openedHeight, expandCollapse, closedText, openedText, speed)
----------------------------------------------------------------------------------------------------------------------------

objID :				The ID of the object (div_objID)
maxSrc :			Source file for the maximize image (minmax_objID)
maxText :			Title to display for the maximize image 
minSrc :			Source file for the minimize image (minmax_objID)
minText :			Title text to display for the minimize image 
closedHeight :		Height of the DIV when fully closed
openedHeight :		Height of the DIV when fully opened
expandCollapse :	Two Options (expand / collapse)
closedText :		Text to display in text DIV when expand/collapse DIV fully closed (text_objID)
openedText			Text to display in text DIV when expand/collapse DIV fully epened (text_objID)
speed :				Speed of the animation

** NOTE:
All variables may be left blank with the exception of the "objID"; 
*/

// Quick run functions for easy placement in code add as many as you need
function dropMenu() { hideMenu('sub1','/images/maximize.gif','More...','/images/minimize.gif','Less...','','','','More...','Less...','25'); }
function dropMenu2(){ hideMenu('sub2','/images/maximize.gif','More...','/images/minimize.gif','Less...','','','','More...','Less...','25'); }
function dropMenu3(){ hideMenu('sub3','/images/maximize.gif','More...','/images/minimize.gif','Less...','','','','More...','Less...','25'); }

/*-------------------
 DO NOT TOUCH BELOW:
-------------------*/

// Global Variables
var cnt = new Array();;	
var position = new Array();
var openedHeightNum = new Array();
var closedHeightNum = new Array();
var speedNum = new Array();


// Main Function
function hideMenu(objID, maxSrc, maxText, minSrc, minText, closedHeight, openedHeight, expandCollapse, closedText, openedText, speed){
	var obj = document.getElementById("div_"+objID);
	
	// Check if function has been run before and set static variables
	if (typeof(position[objID]) == 'undefined')
	{
		// Check object's full opened height
		if (openedHeight == "")	openedHeightNum[objID] = ""+parseInt(obj.scrollHeight);
		else openedHeightNum[objID] = openedHeight;

		// Check object's closed height
		if (closedHeight == "") closedHeightNum[objID] = ""+parseInt(obj.offsetHeight);
		else closedHeightNum[objID] = closedHeight;

		// Check whether object should collapse
		if (expandCollapse == "collapse" && expandCollapse != "expand"){
			
			// Set end height to 0 and opened height to current visible height
			closedHeightNum[objID] = "0";
			openedHeightNum[objID] = ""+parseInt(obj.offsetHeight);
		}
		
		// Set the animation speed of the object
		if (speed == "") speedNum[objID] = 15;
		else speedNum[objID] = speed;
		
		// Set the objects style height
		obj.style.height = parseInt(obj.offsetHeight)+"px";
		
		//Start the animation counter
		cnt[objID] = obj.style.height;
	}
	
	// Create the object that changes minimize/maximize image
	if (maxSrc != "" && minSrc != "") var minmax = document.getElementById("minmax_"+objID);

	// Check if object is in the original(closed) state
	if (parseInt(obj.style.height) == parseInt(closedHeightNum[objID]))
	{
		// Change the text for the minimize/maximize caption to the opened string
		if ((closedText != "") && openedText != "") document.getElementById("text_"+objID).innerHTML = openedText;
		
		// change the minimize/maximize image to the "minimize" source
		if (maxSrc != "" && minSrc != "")
		{
			minmax.src = minSrc;
			minmax.title = minText;
		}
		
		// Set the current opened/closed state of the object to original (closed)
		position[objID] = 1;
		
	}
	// Check if object is in the full state
	else if (parseInt(obj.style.height) == parseInt(openedHeightNum[objID]))
	{
		// Change the text for the minimize/maximize caption to the closed string
		if ((closedText != "") && openedText != "") document.getElementById("text_"+objID).innerHTML = closedText;
		
		// change the minimize/maximize image to the "maximize" source
		if (maxSrc != "" && minSrc != "")
		{
			minmax.src = maxSrc;
			minmax.title = maxText;
		}
		
		// Set the current opened/closed state of the object to full.
		position[objID] = 2;
	}
	
	// Check if object is in an animation state from position 1 --> 2
	if ((parseInt(cnt[objID]) <= openedHeightNum[objID]) && (position[objID] == 1))
	{
		
		// Turn on display of image (used when image is hidden or in collapsed mode and starts to animate
		obj.style.display = "block";
		
		// check if animation is approching full state on next run cycle of code
		if ((parseInt(cnt[objID]) + parseInt(speedNum[objID])) >= openedHeightNum[objID])
		{
			// End Animation and change position state to full
			cnt[objID] = parseInt(openedHeightNum[objID]);
			position[objID] = 2;

		}else {
			// If animation needs to continue then re-run code
			cnt[objID] = parseInt(cnt[objID]) + parseInt(speedNum[objID]);
			setTimeout("hideMenu('"+objID+"','"+maxSrc+"','"+maxText+"','"+minSrc+"','"+minText+"','"+closedHeightNum[objID]+"','"+openedHeightNum[objID]+"','"+expandCollapse+"','"+closedText+"','"+openedText+"','"+speedNum[objID]+"')",30)
		}
		obj.style.height = cnt[objID] +"px";
		
		// Testing display for status bar
		//window.status = "closedHeightNum: "+ closedHeightNum[objID] + "- openedHeightNum: "+ openedHeightNum[objID]+ "- cnt[objID]: "+ cnt[objID]+ "- speedNum[objID]: "+ speedNum[objID]+ "- position: "+ position[objID]+ "- objStyleHeight: "+ obj.style.height;
		
		// Exit out of function because action completed
		return false;
	}
	
	// Check if object is in an animation state from position 2 --> 1
	if ((parseInt(cnt[objID]) >= closedHeightNum[objID]) && (position[objID] == 2))
	{
		// check if animation is approching original(closed) state on next run cycle of code
		if ((parseInt(cnt[objID]) - parseInt(speedNum[objID])) <= closedHeightNum[objID])
		{
			// End Animation and change position state to original(closed)
			cnt[objID] = parseInt(closedHeightNum[objID]);
			position[objID] = 1;
			if ((parseInt(cnt[objID]) - parseInt(speedNum[objID])) <= 0) obj.style.display = "none";

		}else {
		// If animation needs to continue then re-run code
			cnt[objID] = parseInt(cnt[objID]) - parseInt(speedNum[objID]);
			setTimeout("hideMenu('"+objID+"','"+maxSrc+"','"+maxText+"','"+minSrc+"','"+minText+"','"+closedHeightNum[objID]+"','"+openedHeightNum[objID]+"','"+expandCollapse+"','"+closedText+"','"+openedText+"','"+speedNum[objID]+"')",30)
		}
		
		// Resize the object
		obj.style.height = cnt[objID] +"px";
		
		// Testing display for status bar
		//window.status = "closedHeightNum: "+ closedHeightNum[objID] + "- openedHeightNum: "+ openedHeightNum[objID]+ "- cnt[objID]: "+ cnt[objID]+ "- speedNum[objID]: "+ speedNum[objID]+ "- position: "+ position+ "- objStyleHeight: "+ obj.style.height;
		
		// Exit out of function because action completed		
		return false;
	}	
}