var	jt_background_images = {
	home: 'system/images/default-home-bg.jpg',
	menu: 'system/images/default-menu-bg.jpg',
	about: 'system/images/default-about-bg.jpg',
	bookings: 'system/images/default-bookings-bg.jpg',
	contact: 'system/images/default-contact-bg.jpg'
};

(function($) {
	$(document).ready(function() {
		var		jt_nav_busy = 0;
		var		jt_menu_busy = 0;
		var		jt_about_timer = 0;

		/**
		 *	Preload backgrounds
		 */
		$('body').preload([
			jt_background_images.home,
			jt_background_images.menu,
			jt_background_images.about,
			jt_background_images.bookings,
			jt_background_images.contact
		]);

		/**
		 *	Initial background image
		 */
		$.backstretch(jt_background_images.home);
		
		/**
		 *	Logo
		 */
		$('#logo-container').click(function(event) {
			if($(this).hasClass('selected')) { return; }
			jt_nav_close();
		});
		
		/**
		 *	Navigation
		 */
		$('.nav-item-button .button').hover(
			function() {
				if(!$(this).hasClass('selected')) {
					$(this).addClass('hover', 'fast');
				}
			}, function() {
				$(this).removeClass('hover', 'fast');
			}
		);
		$('.nav-item-button .button').click(function(event) {
			var		name, selected;
			
			// get item name
			name = $(this).attr('id');
			if(!name.length) { return; }
			name = name.substring(0, name.length - 7);
			
			// back to mobile site
			if(name == 'mobile-site') {
				$.cookie('huxtable', false);
				location.reload(true);
				return;
			}
			
			// change nav (or close nav)
			selected = $('.nav-item-button .button.selected');
			if((selected.length > 0) && (selected[0] == this)) {
				jt_nav_close();
			} else{
				jt_nav_open(name);
			}
		});
		function jt_nav_open(name) {
			// do nothing if nav is animating
			if(jt_nav_busy == 1) { return; }
			jt_nav_busy = 1;
			
			// animate logo
			if($('#logo-container').hasClass('selected')) {
				$('#logo-container').animate({
					paddingTop: '20px', paddingBottom: '5px'
				}, 500, function() {
					$(this).removeClass('selected');
				});
			}
			
			// close old nav
			$('.nav-item-button .button.selected').removeClass('selected', 'fast');
			$('.nav-item-content.selected').animate({
				height: 'toggle', marginBottom: '0px', paddingTop: '0px', paddingBottom: '0px'
			}, 1000, 'easeOutBounce', function() {
				$(this).removeClass('selected');
			});
			
			// set background image
			$.backstretch(jt_background_images[name], { speed: 500 });

			// open new nav
			$('#'+name+'-button').addClass('selected');
			$('#'+name+'-content').animate({
				height: 'toggle', marginBottom: '10px', paddingTop: '20px', paddingBottom: '20px'
			}, 1000, 'easeOutBounce', function() {
				$(this).addClass('selected');
				jt_nav_busy = 0;
			});
		}
		function jt_nav_close() {
			// do nothing if nav is animating
			if(jt_nav_busy == 1) { return; }
			jt_nav_busy = 1;
			
			// animate logo
			if(!$('#logo-container').hasClass('selected')) {
				$('#logo-container').animate({
					paddingTop: '92px', paddingBottom: '20px'
				}, 500, function() {
					$(this).addClass('selected');
				});
			}
			
			// set background image
			$.backstretch(jt_background_images.home, { speed: 500 });
			
			// close old nav
			$('.nav-item-button .button.selected').removeClass('selected', 'fast');
			$('.nav-item-content.selected').animate({
				height: 'toggle', marginBottom: '0px', paddingTop: '0px', paddingBottom: '0px'
			}, 1000, 'easeOutBounce', function() {
				$(this).removeClass('selected');
				jt_nav_busy = 0;
			});
		}
		
		/**
		 *	Menu
		 */
		$('#menu-left a').click(function(event) {
			var		name, selected;
			
			// prevent anchor action
			event.preventDefault();
			
			// get item name
			name = $(this).attr('id');
			if(!name.length) { return; }
			name = name.substring(0, name.length - 7);
			
			// check item isn't already selected
			selected = $('#menu-left a.selected');
			if((selected.length > 0) && (selected[0] == this)) { return; }
			
			// do nothing is menu is animating
			if(jt_menu_busy == 1) { return; }
			jt_menu_busy = 1;
			
			// update menu items
			$(selected).removeClass('selected', 'fast');
			$(this).addClass('selected', 'fast');
			
			// close old content
			$('#menu-right .menu-content.selected').animate({
				height: 'toggle'
			}, 350, 'swing', function() {
				$(this).removeClass('selected');
				$('#'+name+'-content').animate({
					height: 'toggle'
				}, 350, 'swing', function() {
					$(this).addClass('selected');
					jt_menu_busy = 0;
				});
			});
		});
		
		/**
		 *	About
		 */
		jt_about_timer = setInterval(jt_about_slideshow, 5000);
		function jt_about_slideshow() {
			var		slides, index, next;
			
			if($('#about-content').css('display') == 'none') { return; }
			
			slides = $('#about-right').children();
			if(slides.length <= 0) { return; }
			
			index = $('#about-right .about-slide.selected').index();
			next = index + 1;
			if(next >= slides.length) { next = 0; }
			
			$(slides[index]).fadeOut(1500, function() {
				$(this).removeClass('selected');
			});
			$(slides[next]).fadeIn(1500, function() {
				$(this).addClass('selected');
			});
		}
		
		/**
		 *	Footer
		 */
		$('#twitter').hover(
			function() {
				$(this).stop().animate({paddingTop: '56px'}, 300, 'easeOutBounce');
			}, function() {
				$(this).stop().animate({paddingTop: '0px'}, 500, 'easeOutBounce');
			}
		);
		$('#facebook').hover(
			function() {
				$(this).stop().animate({paddingTop: '56px'}, 300, 'easeOutBounce');
			}, function() {
				$(this).stop().animate({paddingTop: '0px'}, 500, 'easeOutBounce');
			}
		);
	});
})(jQuery);

