/* John Holland - MegaMenu.js */

var MegaMenu;
var MegaMenuItem;
var MegaMenuTimer;
var MenuWasCurrent;

var MegaSubMenu;

function SetupMenu() {
	var menuH = 0;
	var menuH_type = null;
	$('.MegaMenuItems').each(function() {
		if($(this).height() > menuH) {
			menuH = $(this).height();
			menuH_type = 'menu_section';
		}
	});
	$('.MegaMenuPics').each(function() {
		if($(this).height() > menuH) {
			menuH = $(this).height();
			menuH_type = 'menu_recent';
		}
	});
	$('.MegaSubMenuItems').each(function() {
		if($(this).height() > menuH) {
			menuH = $(this).height();
			menuH_type = 'menu_sub';
		}
	});
	$('.MegaMenuSectionPic').each(function() {
		if($(this).height() > menuH) {
			menuH = $(this).height();
			menuH_type = 'menu_pic';
		}
	});
	$('.MegaSubMenuPics').each(function() {
		if($(this).height() > menuH) {
			menuH = $(this).height();
			menuH_type = 'menu_subpics';
		}
	});
	
	if(menuH_type == 'menu_section') {
		$('.MegaMenuItems').each(function() {$(this).height(menuH + 20);});
		$('.MegaMenuPics').each(function() {$(this).height(menuH - 10);});
		$('.MegaSubMenuItems').each(function() {$(this).height(menuH);});
		$('.MegaMenuSectionPic').each(function() {$(this).height(menuH - 20);});
		$('.MegaSubMenuPics').each(function() {$(this).height(menuH - 10);});
		if(isIE == true && IEvers <= 6) {
			$('#Menu_OurWork').height(menuH + 10);
		}
	}
	if(menuH_type == 'menu_recent') {
		$('.MegaMenuItems').each(function() {$(this).height(menuH + 30);});
		$('.MegaMenuPics').each(function() {$(this).height(menuH);});
		$('.MegaSubMenuItems').each(function() {$(this).height(menuH + 10);});
		$('.MegaMenuSectionPic').each(function() {$(this).height(menuH - 10);});
		$('.MegaSubMenuPics').each(function() {$(this).height(menuH);});
		if(isIE == true && IEvers <= 6) {
			$('#Menu_OurWork').height(menuH + 30);
		}
	}
	if(menuH_type == 'menu_sub') {
		$('.MegaMenuItems').each(function() {$(this).height(menuH + 20);});
		$('.MegaMenuPics').each(function() {$(this).height(menuH - 10);});
		$('.MegaSubMenuItems').each(function() {$(this).height(menuH);});
		$('.MegaMenuSectionPic').each(function() {$(this).height(menuH - 20);});
		$('.MegaSubMenuPics').each(function() {$(this).height(menuH - 10);});
		if(isIE == true && IEvers <= 6) {
			$('#Menu_OurWork').height(menuH + 20);
		}
	}
	if(menuH_type == 'menu_pic') {
		$('.MegaMenuItems').each(function() {$(this).height(menuH + 40);});
		$('.MegaMenuPics').each(function() {$(this).height(menuH + 10);});
		$('.MegaSubMenuItems').each(function() {$(this).height(menuH + 20);});
		$('.MegaMenuSectionPic').each(function() {$(this).height(menuH);});
		$('.MegaSubMenuPics').each(function() {$(this).height(menuH + 10);});
		if(isIE == true && IEvers <= 6) {
			$('#Menu_OurWork').height(menuH + 40);
		}
	}
	if(menuH_type == 'menu_subpics') {
		$('.MegaMenuItems').each(function() {$(this).height(menuH + 30);});
		$('.MegaMenuPics').each(function() {$(this).height(menuH);});
		$('.MegaSubMenuItems').each(function() {$(this).height(menuH + 10);});
		$('.MegaMenuSectionPic').each(function() {$(this).height(menuH - 10);});
		$('.MegaSubMenuPics').each(function() {$(this).height(menuH);});
		if(isIE == true && IEvers <= 6) {
			$('#Menu_OurWork').height(menuH + 30);
		}
	}
}

function MenuShow(whoMenu, whoMenuItem) {
	clearTimeout(MegaMenuTimer);
	MenuKill();
	var mPics = 0;
	$('#blackOverlay').css('visibility','visible');
	if(isIE == true && IEvers <= 6) {
		$('select').css({ 'visibility' : 'hidden' });
	}
	var Wmega = document.getElementById(whoMenu);
	if(Wmega) {
		MegaMenu = Wmega;
		MegaMenu.style.visibility = 'visible';
	}
	$('.MegaSubMenu').each(function() {
		$(this).css('visibility','hidden');
	});
	$('#'+MegaMenu.id+' .MegaMenuPics').each(function() {
		if(mPics == 0) {
			mPics += 1;
			$(this).css('visibility','visible');
			//$('#'+MegaMenu.id).height($(this).height() + 44);
		} else {
			$(this).css('visibility','hidden');
		}
	});
	var mAnchors = 0;
	$('#'+MegaMenu.id+' .MegaMenuItems a').each(function() {
		if(mAnchors == 0) {
			mAnchors += 1;
			$(this).context.className = "current";
			$(this).context.parentNode.style.backgroundColor = '#ed1b2d';
		} else {
			$(this).context.className = "";
		}
	});
	MegaMenuItem = whoMenuItem;
	if(whoMenuItem.className == 'current') {
		MenuWasCurrent = true;
	} else {
		MenuWasCurrent = false;
	}
	whoMenuItem.className = "MegaMenuTab";
}

function MenuOver() {
	clearTimeout(MegaMenuTimer);
}

function MenuHide() {
	MegaMenuTimer = setTimeout(MenuKill,500);
}

function MenuKill() {
	clearTimeout(MegaMenuTimer);
	if(MegaMenu != undefined) {
		MegaMenu.style.visibility = 'hidden';
		$('.MegaMenuPics').each(function() {
			$(this).css('visibility','hidden');
		});
		$('.MegaSubMenu').each(function() {
			$(this).css('visibility','hidden');
		});
		$('.MegaSubMenuPics').each(function() {
			$(this).css('visibility','hidden');
		});
		$('.MegaMenuSectionPic').each(function() {
			$(this).css('visibility','hidden');
		});
		$('.SubMenuPics').each(function() {
			$(this).css('visibility','hidden');
		});
		$('.MegaMenuItemWrapper').each(function() {
			$(this).css('background-color','#ffffff');
		});
		$('.MegaSubMenuItemWrapper').each(function() {
			$(this).css('background-color','#F4F4F4');
		});
		if(MenuWasCurrent == true) {
			MegaMenuItem.className = 'current';
		} else {
			MegaMenuItem.className = "";
		}
	}
	$('#blackOverlay').css('visibility','hidden');
	if(isIE == true && IEvers <= 6) {
		$('select').css({ 'visibility' : 'visible' });
	}
}

function ShowMenu(whoMenuSect, whoMenuSectItem) {
	$('.MegaMenuPics').each(function() {
		if($(this).context.id != whoMenuSect) {
			$(this).css('visibility','hidden');
		} else {
			$(this).css('visibility','visible');
		}
	});
	$('.MegaSubMenuItems a').each(function() {
		$(this).context.className = "";
	});
	$('.MegaMenuSectionPic').each(function() {
		$(this).css('visibility','hidden');
	});
	$('.MegaMenuItemWrapper').each(function() {
		$(this).css('background-color','#ffffff');
	});
	$('.MegaSubMenuItemWrapper').each(function() {
		$(this).css('background-color','#f4f4f4');
	});
	var Smega = document.getElementById(whoMenuSect);
	if(Smega) {
		MegaSubMenu = Smega;
	}
	$('.MegaSubMenuPics').each(function() {
		$(this).css('visibility','hidden');
	});
	$('#'+ whoMenuSect + ' .MegaMenuSectionPic').each(function() {
		$(this).css('visibility','visible');
	});
	$('.MegaSubMenu').each(function() {
		if($(this).context.id != whoMenuSect) {
			$(this).css('visibility','hidden');
		} else {
			$(this).css('visibility','visible');
		}
	});
	$('#'+MegaMenu.id+' .MegaMenuItems a').each(function() {
		if($(this).context == whoMenuSectItem) {
			$(this).context.className = "selected";
		} else {
			$(this).context.className = "";
		}
	});
	whoMenuSectItem.parentNode.style.backgroundColor = '#ed1b2d';
}

function ShowSubMenu(whoMenuSub, whoMenuSubItem) {
	$('#'+MegaMenu.id+' .MegaSubMenuItems a').each(function() {
		if($(this).context == whoMenuSubItem) {
			$(this).context.className = "selected";
		} else {
			$(this).context.className = "";
		}
	});
	$('.MegaSubMenuItemWrapper').each(function() {
		$(this).css('background-color','#f4f4f4');
	});
	var Smega = document.getElementById(whoMenuSub);
	if(Smega) {
		$('.MegaSubMenuPics').each(function() {
			if($(this).context.id != whoMenuSub) {
				$(this).css('visibility','hidden');
			} else {
				$(this).css('visibility','visible');
			}
		});
		$('#'+ MegaSubMenu.id + ' .MegaMenuSectionPic').css('visibility','hidden');
	} else {
		$('.MegaSubMenuPics').each(function() {
			$(this).css('visibility','hidden');
		});
		$('#'+ MegaSubMenu.id + ' .MegaMenuSectionPic').css('visibility','visible');
	}
	whoMenuSubItem.parentNode.style.backgroundColor = '#ed1b2d';
}

function MegaMenuPicHilite(who) {
	$('.MenuPicOver').each(function() {
		$(this).context.className = "MenuPic";
	});
	$('.MenuPic').each(function() {
		if($(this).context == who) {
			$(this).context.className = "MenuPicOver";
		}
	});
}

function MegaMenuPicUnHilite() {
	$('.MenuPicOver').each(function() {
		$(this).context.className = "MenuPic";
	});
}

function ShowDropMenu(whoMenu, whoMenuItem) {
	clearTimeout(MegaMenuTimer);
	MenuKill();
	var menuPos = GetObjPosMenu(whoMenuItem);
	$('#blackOverlay').css('visibility','visible');
	if(isIE == true && IEvers <= 6) {
		$('select').css({ 'visibility' : 'hidden' });
	}
	$('#'+whoMenu).css('left',menuPos[0]+'px');
	$('#'+whoMenu).css('top',(menuPos[1]+29)+'px');
	var hasContent = document.getElementById(whoMenu);
	if(hasContent.className == 'MenuDropContent') {
		$('#'+whoMenu).height($('#'+whoMenu+' .MenuDropItems').height()+20);
	} else {
		$('#'+whoMenu).height($('#'+whoMenu+' .MenuDropItems').height()-2);
	}
	$('#'+whoMenu+' .MenuDropItem').each(function() {
		$(this).css('background-color','#ffffff');
	});
	$('#'+whoMenu+' .MenuDropItems a').each(function() {
		$(this).css('color','#595959');
	});
	var Wmega = document.getElementById(whoMenu);
	if(Wmega) {
		MegaMenu = Wmega;
		MegaMenu.style.visibility = 'visible';
	}
	MegaMenuItem = whoMenuItem;
	if(whoMenuItem.className == 'current') {
		MenuWasCurrent = true;
	} else {
		MenuWasCurrent = false;
	}
	whoMenuItem.className = "MegaMenuTab";
}

function DropMenuHover(whoMenu, who) {
	$('#'+whoMenu+' .MenuDropItem').each(function() {
		$(this).css('background-color','#ffffff');
	});
	$('#'+whoMenu+' .MenuDropItems a').each(function() {
		$(this).css('color','#595959');
	});
	who.parentNode.style.backgroundColor = '#ed1b2d';
	who.style.color = '#ffffff';
}

function MenuSubShow(whoMenu, whoMenuItem) {
	clearTimeout(MegaMenuTimer);
	MenuKill();
	var mPics = 0;
	var menuPos = GetObjPosMenu(whoMenuItem);
	$('#blackOverlay').css('visibility','visible');
	$('#'+whoMenu).css('left',menuPos[0]+'px');
	$('#'+whoMenu).css('top',(menuPos[1]+29)+'px');
	$('#'+whoMenu).height($('#'+whoMenu+' .SubMenuPics').height() + 10);
	var Wmega = document.getElementById(whoMenu);
	if(Wmega) {
		MegaMenu = Wmega;
		MegaMenu.style.visibility = 'visible';
	}
	$('#'+MegaMenu.id+' .SubMenuPics').each(function() {
		if(mPics == 0) {
			mPics += 1;
			$(this).css('visibility','visible');
			//$('#'+MegaMenu.id).height($(this).height() + 44);
		} else {
			$(this).css('visibility','hidden');
		}
	});
	MegaMenuItem = whoMenuItem;
	if(whoMenuItem.className == 'current') {
		MenuWasCurrent = true;
	} else {
		MenuWasCurrent = false;
	}
	whoMenuItem.className = "MegaMenuTab";
}

function ProjectPicHilite(who) {
	$('.ProjectPic').each(function() {
		if($(this).context == who) {
			$(this).context.className = "ProjectPicOver";
		}
	});
}

function ProjectPicUnHilite() {
	$('.ProjectPicOver').each(function() {
		$(this).context.className = "ProjectPic";
	});
}

function GetObjPosMenu(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
		curleft += obj.offsetLeft;
		curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
		return [curleft,curtop];
	}
}

