// JavaScript Document

/********** MAIN MENU VARIABLES AND FUNCTIONS **********/
var imgLocation = 'http://www.childfund.org/uploadedImages/public_site/submenus/';
var MMvalue = null;
var imgClass = null;
var imgItem = null;
var imgData = null;
var imgName = null;
var imgActive = null;
var imgInactive = null;
var dropMenu = null;
var timeout = 250;
var closer = 0;

/*THIS FUNCTION SETS THE APPROPRIATE MENU ITEM TO ACTIVE */
function setActive() {
	MMvalue = document.getElementById("main-menu-control");
	// If the main-menu-control div exists, process the data; otherwise, do nothing.
	if (MMvalue != null)
	{
		imgClass = $('#main-menu-control').attr('class');
		imgItem = $('#main_menu > li#' + imgClass).find('img').attr('src');
		// If the Main Menu item exists, swap the image and apply a class; otherwise, do nothing.
		if (imgItem){
			imgName = imgItem.replace(imgLocation, '').replace('.jpg', '');
			imgActive = imgLocation + imgName + '_active.jpg';
			$('#main_menu > li#' + imgClass).find('img').attr('src', imgActive);
			$('#main_menu > li#' + imgClass).addClass('isActive');
		}
		else {}
	}
	else
	{}
}

/* THESE FUNCTIONS CONTROL THE IMAGE SWAP FOR THE MAIN MENU */
function swapIn(event) {
	// If this Main Menu item does not contain the class 'isActive', then swap the image and drop the dropdown menu if it exists;
	//    otherwise, do nothing and drop the dropdown menu if it exists.
	if ($(this).attr('class') != 'isActive'){
		imgData = $(this).find('img').attr('src');
		imgName = imgData.replace(imgLocation, '').replace('.jpg', '');
		imgActive = imgLocation + imgName + '_active.jpg';
	
		$(this).find('img').attr('src', imgActive);
	}
	else{}
		// Controls Drop Down functionality
		menuTimerCancel();
		menuClose();
		dropMenu = $(this).find('ul').css('visibility', 'visible');
}

function swapOut(event) {
	// If this Main Menu item does not contain the class 'isActive', then swap the image and remove the dropdown menu if it exists;
	//    otherwise, do nothing and remove the dropdown menu if it exists.
	if ($(this).attr('class') != 'isActive'){
		imgData = $(this).find('img').attr('src');
		imgInactive = imgData.replace('_active', '');
		
		$(this).find('img').attr('src', imgInactive);
	}
	else {}
		// Controls Drop Down functionality
		closer = window.setTimeout(menuClose, timeout);
//		$(this).find('ul').css('visibility', 'hidden');
}
/********** END MAIN MENU FUNCTIONS **********/

function menuClose() {
	if (dropMenu) {
		dropMenu.css('visibility', 'hidden');
	}
}

function menuTimerCancel() {
	if (closer) {
		window.clearTimeout(closer);
		closer = null;
	}
}

document.onclick = menuClose;










/********** LEFT NAVIGATION FUNCTIONS **********/
function setLeftNav() {
	var hasLeftNav = $('#left_nav').find('ul').attr('id');

	if ( hasLeftNav ) {
		var LnavValue = $('#subnavID').attr('class');
		$('#' + LnavValue).addClass('list_item-hover');
		$('#' + LnavValue + ' a').removeAttr('href');
	}
}
/********** END LEFT NAVIGATION FUNCTIONS **********/


/********** ELEMENT HEIGHT FUNCTIONS **********/
function setElementHeight() {
	var binarySwitch = 0; //binary value to determine what elements exist
	var hasLeftNav = $('#left_nav').attr('id'); //find the div with id="left_nav". if it doesn't exist, null is returned
	var mainContent = $('div[id*=main_content-]').attr('id'); //find the div with id="main_content-X" where X can be anything. if it doesn't exist, null is returned and errors will be alerted
	var hasAdSpace = $('#ad_space').attr('id'); //find the div with id="ad_space". if it doesn't exist, null is returned
	
	// If the elements exist, add the appropriate value to the binarySwitch and extract their height
	if (hasLeftNav) {  binarySwitch += 100;  }
	if (mainContent) {  binarySwitch += 10;  }
	if (hasAdSpace) {  binarySwitch += 1;  }
	
	// Compare the different heights to find the tallest
	switch (binarySwitch) {
		case 0: break;
		case 1: break;
		case 10: break;
		case 11: setHeight011(); break;
		case 100: break;
		case 101: break;
		case 110: setHeight110(); break;
		case 111: setHeight111(); break;
		default: break;
	} //end of switch
}//eof setElementHeight()

// this function sets the height of the mainContent and adSpace
function setHeight011() {
	var heightMainContent = 0; //height of the Main Content
	var heightAdSpace = 0; //height of the Ad Space
	var mainContent = $('div[id*=main_content-]').attr('id');
	heightMainContent = $('#' + mainContent).height();
	heightAdSpace = $('#ad_space').height();
	
	if (heightMainContent > heightAdSpace){
		$('#ad_space').height($('#' + mainContent).height() +  2);
	}
	else if (heightAdSpace > heightMainContent){
		$('#' + mainContent).height($('#ad_space').height() - 2);
	}
}//eof setHeight011

// this function sets the height of the leftNav and mainContent
function setHeight110() {
	var heightLeftNav = 0; //height of the Left Nav
	var heightMainContent = 0; //height of the Main Content
	var mainContent = $('div[id*=main_content-]').attr('id');
	heightLeftNav = $('#left_nav').height();
	heightMainContent = $('#' + mainContent).height();
	
	if (heightLeftNav > heightMainContent){
		$('#' + mainContent).height($('#left_nav').height() - 2);
	}
	else if (heightMainContent > heightLeftNav){
		$('#left_nav').height($('#' + mainContent).height() + 2);
	}
}//eof setHeight110()

// this function sets the height of the leftNav, mainContent, and adSpace
function setHeight111() {
	var heightLeftNav = 0; //height of the Left Nav
	var heightMainContent = 0; //height of the Main Content
	var heightAdSpace = 0; //height of the Ad Space
	var mainContent = $('div[id*=main_content-]').attr('id');
	heightLeftNav = $('#left_nav').height();
	heightMainContent = $('#' + mainContent).height();
	heightAdSpace = $('#ad_space').height();
	
	if ((heightLeftNav > heightMainContent) && (heightLeftNav > heightAdSpace)){
		$('#' + mainContent).height($('#left_nav').height() - 2);
		$('#ad_space').height($('#left_nav').height());
	}
	else if ((heightMainContent > heightLeftNav) && (heightMainContent > heightAdSpace)){
		$('#left_nav').height($('#' + mainContent).height() + 2);
		$('#ad_space').height($('#' + mainContent).height() +  2);
	}
	else if ((heightAdSpace > heightLeftNav) && (heightAdSpace > heightMainContent)){
		$('#left_nav').height($('#ad_space').height());
		$('#' + mainContent).height($('#ad_space').height() - 2);
	}
}//eof setHeight111()
/********** END -> ELEMENT HEIGHT FUNCTIONS **********/

