/**
 * @author Colin McCrory
 */

// Build sub navigation menus
// Theres got to be a better way to do this!
$(document).ready(function() {
	$("#nav li:first").next().addClass("top")
	$("#nav li:first").next().append("<ul class=\"submenu\"><li><a href=\"/pensions/saving-for-retirement/index.html\">Saving for retirement</a></li><li><a href=\"/pensions/income-in-retirement/index.html\">Income in retirement </a></li><li class=\"last\"><a href=\"/pensions/corporate-pensions/index.html\">Corporate pensions for business</a></li></ul>");
	$("#nav li:first").next().next().addClass("top")
	$("#nav li:first").next().next().append("<ul class=\"submenu\"><li><a href=\"/investments/onshore-investments/investment-control/index.html\">Onshore investments </a></li><li><a href=\"/investments/offshore-investments/index.html\">International investments</a></li><li><a href=\"/investments/iht/index.html\">Inheritance tax planning</a></li><li><a href=\"/investments/simply-investments/index.html\">Why should I invest?</a></li></ul>");
	$("#nav li:first").next().next().next().addClass("top")
	$("#nav li:first").next().next().next().append("<ul class=\"submenu\"><li><a href=\"/protection/individual-protection/index.html\">Individual protection</a></li><li class=\"last\"><a href=\"/protection/employee-benefits/index.html\">Employee benefits</a></li></ul>");
	$("#nav li:first").next().next().next().next().addClass("top")
	$("#nav li:first").next().next().next().next().append("<ul class=\"submenu\"><li><a href=\"/funds/range/\">Fund range</a></li><li><a href=\"/funds/data/\">Prices and performance</a></li><li><a href=\"/funds/governance/\">Governance</a></li><li><a href=\"/funds/news-and-views/\">News and views</a></li><li><a href=\"/funds/resources/\">Resources</a></li><li class=\"last\"><a href=\"/funds/managers/\">Fund Managers</a></li></ul>");
	$("#nav li:first").next().next().next().next().next().addClass("top")
	$("#nav li:first").next().next().next().next().next().append("<ul class=\"submenu\"><li><a href=\"/with-profits/with-profits-overview/index.html\">With-profits overview</a></li><li><a href=\"/with-profits/2008-bonus-declaration/index.html\">2008 bonus declaration</a></li><li><a href=\"/with-profits/managing-with-profits-business/index.html\">Managing with-profits business</a></li><li class=\"last\"><a href=\"/with-profits/funds/index.html\">Funds</a></li></ul>");
	$("#nav li:first").next().next().next().next().next().next().addClass("top")
	$("#nav li:first").next().next().next().next().next().next().append("<ul class=\"submenu\"><li><a href=\"/existing-customers/individual-customers/index.html\">Individual customers</a></li><li class=\"last\"><a href=\"/existing-customers/business-customers/index.html\">Business customers</a></li></ul>");
});

hide_menus = function() {
	$(".openMenu").removeClass("openMenu")
	$(".submenu").hide();
} 

//timeouts stop menus from prematurely closing
menu_timeouts = [];
clear_timeouts = function() {
	var menu_timeouts_copy = menu_timeouts;
	menu_timeouts = [];
	for (var i=0; i < menu_timeouts_copy.length; i++) {
		window.clearTimeout(menu_timeouts_copy[i]);
	};
}

$(document).ready(function() {
	//topnav on mouse over
	$("#nav li.top").mouseover (function(){
		clear_timeouts()
		$(".openMenu").removeClass("openMenu")
		$(this).addClass("openMenu");
		$(this).children().addClass("openMenu");
		$(".submenu").not(".openMenu").hide();
		$(".subsubmenu").hide();
		$(this).children(".submenu").show();
		return false;
    })
	
	//topnav on mouseout
	$("#nav li").mouseout (function(){
		menu_timeouts.push(window.setTimeout(hide_menus,400));
		return false;
    })
});