var currentPage = 'home';
var currentIndex = 0;
var arrowTimeout = 0;

$(document).ready(function() {
	
	// rest menu scroll
	$('#menu_holder').scrollLeft(0);

	// display first page stuff
	$('.tagline:eq(0), .main_text:eq(0), #home_banner img:eq(0), .home_background:eq(0)').show();
	
	// trigger animation on nav tab click
	$('#menu_holder .menu li a').click(function() {
		
		var href = $(this).attr('href');
		var urlParts = href.split("/");
		var url = (urlParts[urlParts.length - 1].length > 0) ? urlParts[urlParts.length - 1] : 'home';
		
		slideToPage(url);
		
		return false;
	});
	
	// trigger animation on arrow click
	$('.arrow').click(function() {
		
		if($(this).attr('id') == "arrow_left" && currentIndex > 0) {
			
			currentIndex = 0;
			
		} else if(($(this).attr('id') == "arrow_right") && (currentIndex < ($('#menu_holder .menu li').length - 1))) {
			
			currentIndex = 3;
		}
			
		$('#menu_holder .menu li').eq(currentIndex).children('a').trigger('click');
	
	});

});


/*
 * Slide To Page
 * -------------------------
 *
 * Slide navigation to new page, change
 * background and banner images, slide
 * down sub menu and display tagline
 * and splash content
 *
 * @param String page name
 */
function slideToPage(page) {
	
	// if we're not on click tab
	if(currentPage !== page) {
		
		//slide up sub menu
		$('#dropdown').slideUp(500);
				
		// set current page
		currentPage = page;
		
		element = null;
		
		// loop round all nav items
		$('#menu_holder').find('li').each(function() {
	
			var iPage = (page == "home") ? "" : page;
			
			// find the page item we need 
			if($(this).children('a').attr('href') == HOME+'/'+iPage) {		
				
				element = $(this);
				
				// calcuate scroll amount dependent on index
				currentIndex = element.index();
				var index = currentIndex;
				var slide = ((202 * index) + (21 * index));
						
				// fade out background image and fade in new
				$('.home_background').fadeOut(500);
				$('.home_background:eq('+index+')').fadeIn(500);
						
				// fade out banner image and fade in new
				$('#home_banner img').fadeOut(500);
				$('#home_banner img').eq(index).fadeIn(500);
				
				// slide menu to active item		
				$('#menu_holder').animate({
						
						'scrollLeft' : slide
							
					}, 500, function() {
						
						// change text and tagline
						$('.tagline, .main_text').hide();
						$('.tagline:eq('+index+'), .main_text:eq('+index+')').show();
							
						// slide down drop down
						if(currentPage !== "home") {
								
							$('#dropdown ul').hide();
							$('#dropdown ul li ul').show();
							$('#dropdown ul#'+page).show();
							$('#dropdown').slideDown(500);
								
						}
							
					}
				);
				
			}
			
		});
							
		return false;
				
	}
	
}
