// JavaScript Document



/**************************/
/**************************/
/******  Variables   ******/
/**************************/
/**************************/

var MINHEIGHT = 10;			// Constant value for collapsed div height
var SHOWMORETEXT = "show";	// Label for expand/collapse link in footers
var SHOWLESSTEXT = "hide";	// Label for expand/collapse link in footers
											
											
					
/**************************/
/**************************/
/******  Functions   ******/
/**************************/
/**************************/											
											
function flipOpenClosed (theAtag)
{
	//alert('flipOpenClosed(' + theAtag.parent().text() + ')');
	//toggle .open class on (this) A element
	theAtag.toggleClass('open');
	
	//execute switchMoreLess function on selected element
	theAtag.text(switchMoreLess(theAtag.parent().text()));
}//flipOpenClosed()


//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////


function switchMoreLess (theText)
{
	if (theText  == SHOWMORETEXT)
	{
		theText = SHOWLESSTEXT;
	}
	else if (theText == SHOWLESSTEXT)
	{
		theText = SHOWMORETEXT;
	}
	
	return theText;
}//switchMoreLess()

$(document).ready(function() 
{

	/************************************************/
	/************************************************/
	/******  Set up divs with .expander class  ******/
	/************************************************/
	/************************************************/
	
	var allExpanders = $('.expander');
	
	//alert('there are: ' + allExpanders.length + ' expanders');
	
	for (var i = 0; i < allExpanders.length; i++)
	{	
		//alert('divHeight = ' + $(allExpanders[i]).height());
		if ($(allExpanders[i]).height() <= MINHEIGHT)
		{
			// if it's less than or equal to MINHEIGHT, make it MINHEIGHT
			$(allExpanders[i]).height(MINHEIGHT);
		}
		else
		{
			// otherwise, add 'Show More' link and set alt equal to the .expander div's height
			$(allExpanders[i]).siblings('h4').find('.titleLink').html('<a class="expanderLink" divHeight="' + $(allExpanders[i]).height() + '" href="javascript:void(0)">' + SHOWMORETEXT + '</a>');
			// reduce to MINHEIGHT
			$(allExpanders[i]).height(MINHEIGHT);
		}
	
	}
	
	
	/*************************************************************/
	/*************************************************************/
	/******  Set up hover effect for expand links in footer ******/
	/*************************************************************/
	/*************************************************************/
			
	$('.moduleBox h4 a.expanderLink').hover(
		function()
		{
			////alert('mouseEnter: ' + $(this).find('a') + ' - DOM Element\'s text: ' + this.text);
			$(this).filter('a').toggleClass('hovered');
		},
		function()
		{
			////alert('mouseLeave: ' + this.text);
			$(this).filter('a').toggleClass('hovered');
		}
	);
	
	
	/************************************************************/
	/************************************************************/
	/******  Toggle .expander divs open or closed on click ******/
	/************************************************************/
	/************************************************************/
	
	$(".moduleBox h4 a.expanderLink").click(function() {
											 
		// report to console
		//alert('clicked: ' + $(this).html());
		
		var targetHeight = 0;	// height of div after animation

		if ($(this).parents().parents().siblings('.expander').height() == MINHEIGHT)
		{								
			// if the div's height is equal to MINHEIGHT, it must be closed, so get it ready to open
			targetHeight = parseInt($(this).attr('divHeight'));
		}
		else
		{
			// otherwise, it must be open, so get it ready to close
			targetHeight = MINHEIGHT;
		}
		
		////alert('the height of this div is: ' + $(this).parents().siblings('.expander').height());
		////alert('targetHeight = ' + targetHeight);
		
		// animate the div's height to expand or contract the module
		$(this).parents().parents().siblings().filter('.expander').animate(
		{
			"height": targetHeight
		}, 500, 'linear', function() 
		{
			//flip open/closed
			flipOpenClosed($(this).siblings('h4').find('a'));
		});
					
		// keep the link from jumping to the #
		return false;	
		
	});
	
});
