/*****************************************************************************

	TEMPLATE.JS
	Code to manage interaction on the overall site template. 
	06/08 - Timothy Krukowski for Primum Marketing Communications & gMed. Inc.

 *****************************************************************************/ 

$(document).ready(function() { 
	//Preload for the rollovers
	preloadRollover('#Menu > li a img');
	
	$('#Menu').addClass('hide');
	
	//highlight the current menu item
	if( $('#imgTitle').size() ) {
		var currentMenu = $('#imgTitle').attr('src').replace(/^Images\/([A-Za-z]*)_title.jpg$/,'$1');
		var menuImage = $("#Menu li a img[src*='Images/menu_" + currentMenu + "']");
		if ($(menuImage).size() > 0) {
			//make sure the image is on
			$(menuImage).attr('src', $(menuImage).attr('src').replace('_off.', '_on.'));
		}
	}
  
	//Menu rollovers
	$('#Menu > li').hover(
		function(){
				//hide all sub menus
				$(this).children('ul').addClass('hide');		
		
				//show the On image
				var menuImage = $(this).children('a').children('img');
				$(menuImage).attr('src', $(menuImage).attr('src').replace('_off.', '_on.'));
				
				//check for submenu
				var submenuSelector = $(this).children('ul');
				if( $(submenuSelector).size() > 0 ) {
					//show it and position it
					$(submenuSelector).removeClass('hide');
					$(submenuSelector).css("left", $(this).position().left);								
				}
		} ,
				  
		function(){
				//hide all sub menus
				$(this).children('ul').addClass('hide');
				
				//check if this is the current menu item
				var menuImage = $(this).children('a').children('img');
				var currentMenu = ' ';
				if($('#imgTitle').size() > 0) {
					currentMenu = $('#imgTitle').attr('src').replace(/^Images\/([A-Za-z]*)_title.jpg$/,'$1');
				}
				
				if ($(menuImage).attr('src').search(currentMenu) == -1) {							
					//show the Off image
					$(menuImage).attr('src', $(menuImage).attr('src').replace('_on.', '_off.'));
				}				
		}
	);
	
	//Submenu items rollovers
	setClassToggle('#Menu li ul li', 'highlight');
	
	//Submenu on clicks
	$('#Menu li ul li').click(
		function(){
			window.location = $(this).children('a').attr('href');
			//stop bubblin
			return false;
		}
	);
	
	$('#Menu').removeClass('hide');		
});
 
  
 
 //For all the selected images, create a temp <img> tag for the highlighted state
function preloadRollover(selector)
{
	if( $(selector).size() > 0 ) {
		$(selector).each(function() {
			(new Image()).src = $(this).attr('src').replace('_off.', '_on.');
		});
	}
}



//If given selectors exists, assign rollover behavior.  
function setRollover(selector)
{
	if( $(selector).size() > 0 ) {
		$(selector).hover(
			function(){ if( !$(this).hasClass('selected') ){
							$(this).attr('src', $(this).attr('src').replace('_off.', '_on.'));}
					  } ,
			function(){ if( !$(this).hasClass('selected') ){	
							$(this).attr('src', $(this).attr('src').replace('_on.' ,'_off.'));}
					  }				
			);
	}
}


//If given selectors exists, assign rollover behavior.  
function setClassToggle(selector, cssClass)
{
	if( $(selector).size() > 0 ) {
		$(selector).hover(
			function(){
				$(this).toggleClass(cssClass);
			} ,
			
			function(){
				$(this).toggleClass(cssClass);			
			}		
		);
	}
}



/*
 *	set_bottom
 *
 *	
 */
function set_bottom()
{	
	//show content
	var content = document.getElementById("Content");
	if ( content.className == "hide" ) {
		content.className = "show";
	}
		
	//assign resizing behavior to document
	window.onresize = function(){resize_bottom();};
	resize_bottom();
}



/*
 *	resize_bottom
 *
 *		Figures out where to place the footer and how tall the page should be.
 *
 *		Needed because absolute positioning moves everything out of the page flow
 *		and the body needs to fit the entire screen or the content, whichever 
 *		is taller.
 */
function resize_bottom() 
{
	//which column is taller?
	var content = document.getElementById("Content");
	var sidebar = document.getElementById("Sidebar");
	var tallerColumn;
	var topPadding;
	if ( sidebar.clientHeight > content.clientHeight ) {
		tallerColumn = sidebar;
		topPadding = 25;
	}
	else {
		tallerColumn = content;
		topPadding = 25;
	}	
	
	//move the footer to the right spot depending on the content height
	var footer = document.getElementById("Footer");
	footer.style.top = tallerColumn.clientHeight + tallerColumn.offsetTop + topPadding + "px";
	footer.className = footer.className.replace(/hide/, "show");

	
	//if the height of window is less than the content height
	var pageHeight = footer.offsetTop + footer.clientHeight + 10;
	if( document.documentElement.clientHeight < pageHeight ) {
		//...set the body height to match the content height
		document.getElementsByTagName("body")[0].style.height = pageHeight + "px";
	}
	else {
		document.getElementsByTagName("body")[0].style.height = "100%";
	}
}