/** 

Functions
------------
reconfigurePage()
setupMenu()
linkSetup ()
rolloverSetup()
startAnimation()
checkContentHeight()
checkQueue()

**/
$('#loadingImg, #loadingBg, #loadingBg img').ready(function () {
	$('#loadingImg, #loadingBg').show();										 
});
$(document).ready(function(){
	setupMenu();
		
	contentWrap = $('#mainContent');
	mainContent = $('.content');
	newContent = $('.newContent');
	
	$('#loadingImg, #loadingBg').fadeOut();
	
});

/** reconfigurePage() - Reconfigures the globals once all animations are complete
******************************************************************************/
function reconfigurePage() {
	mainContent = $('.content');
	newContent = $('.newContent');
	innerNav = $('#menu');
	portfolioLinks = $('.portSlideMenu a');
	setExternalLinks();
}

/** setupMenu() - Sets up the top rollover menu
******************************************************************************/
function setupMenu() {
	key = $('#mainContent .content').attr('class').match(/[0-9]+/)[0];
	
	homePageLinks = $('.homePageHeader a');
	topMenu = $('#mainNav');
	menuLinks = $('#mainNav #mainLinks a').not('#mainLinks .subLinks a');
	subMenu = $('.subLinks');
	subLinks = $('.subLinks a');
	footerLinks = $('#footer a:not(.precEmail, .icons)');
	innerNav = $('#menu');
	portTabBar = $('.newContent .portfolioNav');
	carLinks = $('.sectorList a');
	linkSetup();
	rolloverSetup();
	checkMenuStatus();
	
	/** linkSetup() - Sets up the calls to other site content
	******************************************************************************/
	function linkSetup () {
		//Home page links binded event
		homePageLinks.click( function () {
			menuClick('homePage', $(this));				   
			$("#mainLinks a[rel!='"+key+"']").parents('li').removeClass('active');
			$("#mainLinks a[rel='"+key+"']").parents('li').addClass('active');
			return false;
		});
		
		//Main-nav binded event
		menuLinks.click( function (event) {
		    event.preventDefault();
			if ($(this).attr('rel') != 8 || $('.portSlideMenu').length == 0) {
				menuClick('mainMenu', $(this));
				$(this).parents('li').siblings().removeClass('active');
				$('.subLinks li').removeClass('active');
				$(this).parents('li').addClass('active');
			}
		});
		
		//Sub-nav binded event - not sure we even need this bit anymore
		subLinks.click( function () {
			if (!$(this).parents('ul').hasClass('SL8') || $('.portSlideMenu').length == 0) {
				menuClick('subMenu', $(this));
			}
			return false;
		});
		
		//Footer nav binded event
		footerLinks.click( function () {
			menuClick('footer', $(this));
			return false;
		});
		
	}
	
	/** rolloverSetup() - Configures the rollover states
	******************************************************************************/
	function rolloverSetup() {
		menuLinks.hover(
			function () {
				subLinkActiveID = $(this).attr('rel');
				if ($('.SL'+subLinkActiveID).html() != null)
					showSubMenu(subLinkActiveID, true);
				else
					hideSubMenu(this);
			}, 
			function () {
				subLinkLastHoverID = $(this).attr('rel');
			}
		);
		topMenu.hover(
			function () {
			//do nothing on hover over
			}, 
			function () {
				if (!subLinksOpen) {
					hideSubMenu(this);
				} else {
					subLinkActiveID = '';
					showSubMenu(subLinkOpenID, false);
					subLinkLastHoverID = subLinkOpenID;
				}
			}
		);
	}
	
	/** checkMenuStatus() - Shows a submenu if its active
	******************************************************************************/
	function checkMenuStatus() {
		var active = $('.subLinks .active');
		
		if (active.length != 0) {
			active.parent('ul').show();
			active.parent('ul').parent('li').addClass('active');
			menuId = (active.parent('ul').attr('class')).split(' ');
			menuId = menuId[0].substr(2, menuId[0].length);
			
			subLinksOpen = true;
			subLinkOpenID = menuId;
			subLinkLastHoverID = menuId;
		} else {
			topMenu.height(48);
		}
	}
}

//Triggered when a link on the site is clicked
function menuClick(region, menuItem) {
	
	portMenuDisabled = true;
	requestKey = menuItem.attr('rel');
	
	carLinks.unbind('click', animateCarousel);
	$('.left .down, .left .up').die('click');
	if (key == 1)
		clearTimeout(timeOut);
	portTabBar.stopTime("tabTimer");
	$('.precImage1').stopTime("precTimer");

	//check that we're not on the same page
	
	if (requestKey != key) {
		
		key = requestKey;
		checkSubLinks(region, menuItem);
		//set the queue if a link has already been clicked
		
		if (linkClicked == true) {
			queueKey = key;
		} else {
			linkClicked = true;

			newContent.addClass('page'+key);
			var newHTML = '/getPage?title=' + key;
			$('#loadingImg, #loadingBg').fadeIn();
			newContent.load(newHTML, function () {
				$('#loadingImg').fadeOut('fast');
				$('#loadingBg').fadeOut('fast', function () {
					if (region == 'homePage')
						topMenu.fadeIn();
					
					startAnimation();										   
				});
			});
		}
	}
}

/** checkSubLinks() - Checks the sublinks are correct
******************************************************************************/
function checkSubLinks(menu, menuItem) {
	switch (menu) {
		case 'homePage':	
			if ($('.SL'+key).html() != null) {
				subLinksOpen = true;
				subLinkOpenID = key;
				showSubMenu(key, false);
				subLinkLastHoverID = key;
			} else {
				subLinksOpen = false;
				subLinkOpenID = '';
			}
			break;
		case 'mainMenu':
			if ($('.SL'+key).html() != null) {
				subLinksOpen = true;
				subLinkOpenID = key;
			} else {
				subLinksOpen = false;
				subLinkOpenID = '';
			}
			break;
		case 'subMenu':
			menuId = (menuItem.parents('ul').attr('class')).split(' ');
			menuId = menuId[0].substr(2, menuId[0].length);
			menuItem.parents('li').siblings().removeClass('active');
			menuItem.parent('li').addClass('active');
			$("#mainLinks a[rel!='"+menuId+"']").not('#mainLinks .subLinks a').parents('li').removeClass('active');
			$("#mainLinks a[rel='"+menuId+"']").parent('li').addClass('active');
			subLinksOpen = true;
			subLinkOpenID = menuId;
			break;
		case 'footer':
			subLinksOpen = false;
			subLinkOpenID = '';
			hideSubMenu();
			menuLinks.parents('li').removeClass('active');
			subLinks.parents('li').removeClass('active');
			break;		
	}
}

function showSubMenu(linkID, inMenu){
	topMenu.stop();
	if (subLinkLastHoverID != '') {
		if (subLinkActiveID == subLinkLastHoverID || (!inMenu && subLinkOpenID == subLinkLastHoverID)) {
			//do nothing
			topMenu.animate({height:'68px'}, 'normal');
			return;
		}
	}
	showSubLinks(linkID);
	topMenu.animate({height:'68px'}, 'normal');
}
function hideSubMenu(){
	topMenu.stop();
	topMenu.animate({height:'48px'}, 'normal', function() {
		subMenu.hide();
		subLinkLastHoverID = '';
	});
}
function showSubLinks(linkID) {
	hideCount = 1;
	subMenu.fadeOut(200, function() {
		if (subMenu.length == hideCount)
			$('.SL'+linkID).fadeIn(200);
		hideCount++;
	});	
}

/** startAnimation() - Animates the window
******************************************************************************/
function startAnimation() {
	mainContent.animate({
		left: "-980px"
	}, 1400,
	function () {
		mainContent.remove();
	});
	newContent.animate({
		left: "0px"
	}, 1400,
	function () {
		newContent.removeClass('newContent').addClass('content').after('<div class="newContent"></div>');
		reconfigurePage();
		checkContentHeight();
	});
	animationWaiting = false;
}

/** checkContentHeight() - Configures the height of the new window
******************************************************************************/
function checkContentHeight() {
	newContentHeight = mainContent.innerHeight();
	if (mainContent.height() != contentHeight) {
		if (newContentHeight < 440)
			newContentHeight = 440;
		contentWrap.animate({
							height: newContentHeight+"px"
							}, 'slow',
							function () {
								finishAnimation = false;
								linkClicked = false;
								checkQueue();		
							}
							);
		/*$('#footer').animate({
							top: (newContentHeight+67)+"px"
							}, 'slow',
							function () {
								
							});*/
		contentHeight = newContentHeight;
	} else {
		finishAnimation = false;
		linkClicked = false;
		checkQueue();
	}
}

/** checkQueue() - Checks to see if a page is waiting in the queue
******************************************************************************/
function checkQueue() {
	if (queueKey) {
		key = queueKey;
		queueKey = null;
		linkClicked = true;
		newContent.addClass('page'+key);
		var newHTML = '/getPage?title=' + key;
		$('#loadingImg, #loadingBg').fadeIn();
		newContent.load(newHTML, function () {
		   	$('#loadingImg').fadeOut('fast');
			$('#loadingBg').fadeOut('fast', function () {
				startAnimation();										   
			});
		});
	}
}

function animateCarousel() {
	index = $('.sectorList a').index(this);
	$('.portSlideMenu').animate({left: '-' + (index * 950) + 'px'}, 1200, 'easeInOutCubic');
	checkSubLinks('subMenu', $(this));
	$('.portMenu h3').html($(this).html() + ' case studies');
	
	return false;	
}

