$(document).ready(function(){
	var menubar = [
		[ 'Home', '' ],
		[ 'Shipping Services', [
			[ 'Baggage Shipping', '/baggage_shipping.htm' ],
			[ 'International Removals', '/international_removals.htm' ],
			[ 'Car Shipping', '/car_shipping.htm' ],
			[ 'Trade Shipping ', '/trade_shipping.htm' ],
			[ 'Fine Art Shipping', '/fine_art_shipping.htm' ],
			[ 'Shows & Exhibitions', '/shows_and_exhibitions.htm' ],
			[ 'Imports', '/imports.htm' ]
		]],
		[ 'Complementary Services', [
			[ 'Migrant Airfares', '/airfares.htm' ],
			[ 'Banking Downunder', '/banking_downunder.htm' ],
			[ 'Foreign Currency', '/foreign_currency.htm' ],
			[ 'Jobs Abroad', '/jobs_abroad.htm' ],
			[ 'Overseas Property', '/overseas_property.htm' ],
			[ 'Pet Transportation', '/pettransportation.htm' ],
			[ 'Tax Rebates', '/tax_rebates.htm' ]
		]],
		[ 'Migration Events', '/migration_events.htm' ],
		[ 'About Us', [
			[ 'Contact the team', '/contact_the_team.htm' ],
			[ 'Company Profile', '/company_profile.htm' ],
			[ 'Careers', '/careers.htm' ],
			[ 'Location Maps', '/location_maps.htm' ],
			[ 'Testimonials', '/testimonials.htm' ],
			[ 'Terms & Conditions', '/terms_and_conditions.htm' ],
			[ 'Legal Notice', '/legal_notice.htm' ],
			[ 'Privacy Policy', '/privacy_policy.htm' ],
			[ 'Latest News', '/newsletter.htm' ],
			[ 'Useful links', '/links.htm' ],
			[ "Winston’s Wish", '/winstons.htm']
		]],
		[ 'Shipment Tracking', '/shipment.htm' ],
		[ 'Contact Us', [
			[ 'Contact details', '/contact_us.htm' ],
			[ 'Contact the team', '/contact_the_team.htm' ],
			[ 'Brochure request', '/brochure_request.htm' ],
			[ 'Location Maps', '/location_maps.htm' ],
			[ 'Careers', '/careers.htm' ]
		]]
	];

	var baseURL = document.location.protocol + '//' + document.location.host;

	function buildMenu(elem, menuLiteral, menuItem) {
		if(menuLiteral == 'undefined')
			alert('undefined menuLiteral');

		if(typeof menuItem == 'string') {
			var div = $('<div />').appendTo(elem);
			$('<a />').attr('href', baseURL + menuItem).appendTo(div).text(menuLiteral);
		}
		else if(typeof menuItem == 'object') {
			var div = $('<div />').appendTo(elem);
			$('<span>' + menuLiteral + '</span>').appendTo(div);
//			div.text(menuLiteral);
			var ul = $('<ul />').appendTo(div);
			for(var i = 0; i < menuItem.length; i++) {
				var li = $('<li />').appendTo(ul);
				var a = $('<a />').attr('href', baseURL + menuItem[i][1]).text(menuItem[i][0]);
				a.appendTo(li);
			}
		}
		else {
			alert(menuLiteral + ' is unknown');
		}
	}

	var menubarPanel = $('div#menubarPanel').empty();
	for(var i = 0; i < menubar.length; i++) {
		buildMenu(menubarPanel, menubar[i][0], menubar[i][1]);
	}

	/*
	 * Set the offset of last submenu
	 */

	/*
	var lastSubmenu = $('div#menubarPanel div:last-child');
	var aList = $('ul li a', lastSubmenu);
	var maxWidth = localGetWH(lastSubmenu.get(0), 'width');
	aList.each(function(){
		var width = localGetWH(this, 'width');
		if(width > maxWidth)
			maxWidth = width;
	});
	var marginLeft = localGetWH(lastSubmenu.get(0), 'width') - maxWidth;
	marginLeft -= 22;
	$('ul', lastSubmenu).css('margin-left', marginLeft + 'px');
	*/

	/*
	 * Hover to open/close submenu
	 */

	$('#menubarPanel div').hover(function(){
		$('ul', this).css('visibility', 'visible');
		$(this).addClass('hover');
	},function(){
		$('ul', this).css('visibility', 'hidden');
		$(this).removeClass('hover');
	});

	/*
	 * Hover to change colors of submenu items
	 */

	$('#menubarPanel ul li').hover(function(){
		$(this).addClass('hover');
	},function(){
		$(this).removeClass('hover');
	});

	/*
	 * Set the width of the submenu items.
	 * Notes:
	 *		This is mostly for IE7 since it doesn't set a consistant
	 *		width on the li elements.
	 *		The width() function built in to jquery doesn't work.  There is
	 *		a function in page.js, localGetWH, to deal with this.
	 *		IE and all others use different a different function,
	 *		setMenubarwidth, to set the width.
	 */

	setMenubarwidth();
});

