$(document).ready(function() {
	//Append edges to navigation tabs
	$("nav").find("li").each(function(index) {
		$(this).append("<div class=\"edge\" />");
	});
	
	//Wrap search box in appropriate surrounding divs
	formatSearch();
	
	//Hide certain sub-menu items from naviation
	hideSubmenuItems();
});

$(window).load(function() {
	//Since the top image can fluctuate in height, adjust accordingly to avoid space between the header image and the navigation bar
	//Must call in window.load, or images may not be fully loaded when this function executes
	formatHeaderImg();
});

function showRegisterMenu(targetButton) {
	var httpPath = $("#localPath").val();
	
	//Change background images on button
	targetButton.css("background","url('http://" + httpPath + "/App_Themes/afamcongress/images/afam_buttonRed-on.png') repeat-x");
	targetButton.next(".btnEdge").css("background","url('http://" + httpPath + "/App_Themes/afamcongress/images/afam_buttonRedEdge-on.png') repeat-x");
	
	//Show menu
	targetButton.parents().next(".hidden").show();
}

function hideRegisterMenu(targetButton) {
	
	//Remove styles
	targetButton.attr("style","");
	targetButton.next(".btnEdge").attr("style","");
	
	//Hide menu
	targetButton.parents().next(".hidden").hide();
}

function formatSearch() {
	var textbox = $(".searchText");
	var submitBtn = $(".search").find("input[type='submit']");
	
	textbox.wrap("<div class=\"searchTextWrapper alignRight\">");
	
	submitBtn.wrap("<div class=\"button alignRight\" />");
	var submitBtnWrapper = submitBtn.parent();
	submitBtnWrapper.append("<div class=\"btnEdge\" />"); //Add button edge
	
	//Place submit button in front of textbox so float:right's align correctly
	submitBtnWrapper.prependTo(submitBtnWrapper.parent());
}

function formatHeaderImg() {
	var headerImg = $(".headerImg").find(".mainImage");
	var imgWrapper = headerImg.parent();
	var maxHeight = imgWrapper.css("max-height").replace("px","");
	var imgHeight = headerImg.height();
	
	if(imgHeight < maxHeight) 
	{
		imgWrapper.height(imgHeight);
	}
}

function hideSubmenuItems() {
	
	//Hide certain items from sidebar
	var hideSubmenuItems = new Array("Testimonials", "Videos");
	var highlightedItem = $(".subnav .CMSListMenuHighlightedLI");
	
	if(highlightedItem.length > 0) 
	{
		$.each(hideSubmenuItems, function(index, value) {
			if(highlightedItem.children("a").attr("href").indexOf(value) > -1) {
				highlightedItem.children("ul").hide();
			}
		});
	}
	
	//On the videos grid page, we want to hide the submenu completely
	var currentUrl = window.location.toString().toLowerCase();
	
	//Logic: if 'videos' is in url and you don't find the style used by detail videos (videoarea), hide submenu
	//and expand width of main area
	if(currentUrl.indexOf("videos") > -1 && $(".videoArea").length == 0) {
		$(".subnav").remove();
		$(".mainContent").children(".column").removeClass("alignRight");
		$(".mainContent").children(".column").width("100%");
		arrangeVideos();
	}
}

function arrangeVideos() {
	var videoDivs = $(".videos").children(".videoItem");
	var videoWrapper;
	
	videoDivs.each(function(index) {
		if(index > 0 && index % 4 == 0) {
			$(this).parents(".textSimple").append('<div class="videos" />');
			videoWrapper = $(".textSimple").children(".videos:last-child"); //assign new element as active wrapper	
			$(this).appendTo(videoWrapper);
		}
		//This will ensure all videos not included in the first row are moved to the new wrapper
		else if(index > 4) 
		{
			$(this).appendTo(videoWrapper);	
		}
	});
}

function arrangeImages() {
	var images = $(".thumbnail").find("img");
	
	//Get overlay values so we know what to position the image to
	var overlayWidth = $(".thumbnail").width();
	var overlayHeight = $(".thumbnail").height();
	
	images.each(function(index) {
		//Get width/height of image
		var width = $(this).width();	
		var height = $(this).height();
		
		//Determine if image is landscape or portrait. Resize smallest edge to max overlay width or height so large images don't overflow too much
		if(width > height) {
			$(this).attr("height", overlayHeight);
			width = $(this).width();
			height = $(this).height();
		}
		else {
			$(this).attr("width", overlayWidth);
			width = $(this).width();
			height = $(this).height();
		}
		
		//Calculate offsets
		var positionHorizontal = Math.round((overlayWidth - width)/2);
		var positionVertical = Math.round((overlayHeight - height)/2);
		
		//Apply offsets
		$(this).attr("style","margin: " + positionVertical + "px 0px 0px " + positionHorizontal + "px");
		
	});

}
