(function($) {
	
	$window = $(window)
	
	$(document).ready(function(){
		
		// Parallax stuff 
		if(window.innerWidth > 1024) { 
			$window.scroll(function() {
				var newY = Math.floor($window.scrollTop() / 4) * -1
				$('#board').css('backgroundPosition', "50% " + newY + "px");
			});
    	}
		
		// Reservation Date stuff
		var d = new Date();
		var month = d.getMonth() + 1, day = d.getDate(), year = d.getFullYear();
		var pad = function(num) { return (num < 10) ? '0'+num : num }
		var reserve = pad(month) + '/' + pad(day) + '/' + year;
		$('#startDate').attr('value', reserve); 
		
		// Mobile Nav
		$('#nav_mobile').change(function () {
		    window.location = $(this).val();
		});
		
	});
	
	// The image gallery
	// Joshua Inkenbrandt takes 0 responsibility for this code.
	$.fn.slideGallery = function(options) {
		
		var options = options || {};
		var imageIndex = 0;
		var currentPos = 0;
		var images = this.find('img');
		var holder = this.find('.holder');
		var sections = this.find('.holder > div');
		var duration = options.duration || 50;
		var controls = options.controls || false;
		var widths = [];
		var offset = [];
		var loading = images.length;

		if (controls) {
			controls.each(function(i, a) {
				$(a).click(function(e) {
					e.preventDefault();
					gotoSection(i);
					return false;
				}).attr('href', 'javascript:void(0);');
			});
		}

		holder.css({'position':'absolute', 'top':0, 'left':0})

		var gotoImage = function(nextIndex) {
			var inc = nextIndex - imageIndex;
			if (nextIndex >= 0 && nextIndex < images.length) {
				var current = $(images[imageIndex]);
				current.removeClass('selected');
				if (loading) {
					// Get what we can until we actually
					// have all the images loaded.
					getWidths();
				}
				currentPos = widths[nextIndex] * -1;
				holder.clearQueue().animate({'left':currentPos}, duration);
				imageIndex = nextIndex;
				$(images[imageIndex]).addClass('selected');
				highlightControl(nextIndex);
			}
		}

		var getWidths = function() {
			var total = 0;
			widths = [];
			images.each(function(i, img) {
				widths.push(total);
				total += $(img).outerWidth() + 5;
			});
		}
		
		images.each(function(i, el) {
			widths.push(0);
			$(el).load(function(e) {
				loading--;
				if (!loading) {
					getWidths();
					gotoImage(imageIndex);
				}
			});
		});

		var gotoSection = function(index) {
			var section = sections[index];
			if (!section) return;
			nextIndex = 0;
			for (var i = 0, l = images.length; i < l; i++) {
				if ($(images[i]).parent()[0] == section) {
					return gotoImage(i);                  
			  }
			}
		}
		
		var highlightControl = function(index) {
			if (!controls) return;
			var section = $(images[index]).parent()[0];
			if (!section) return;
			var sectionIndex = 0;
			$(section).parent().find('> div').each(function(i, el) {
				if (el == section) {
					sectionIndex = i;
				}
			});
			controls.removeClass('selected');
			$(controls[sectionIndex]).addClass('selected');
		}

		$(document).keydown(function(e) {
			var inc = ({37:-1, 39:1}[e.keyCode] || 0);
			gotoImage(imageIndex + inc);
		});

		$('.prev').click(function() {
			gotoImage(imageIndex - 1);
		});

		$('.next').click(function() {
			gotoImage(imageIndex + 1);
		});

		images.each(function(i, img) {
			$(img).click(function() {
				gotoImage(i);
			});
		});

		$(document).focus();
	}
	
})(jQuery);

// Subnavs ////////////////////////////////

function toggleMenu(item,bodyImage)
{
	if (!$('#subnav_'+item).hasClass('selected'))
	{
		$('.toggle_section').hide();
		$('#section_'+item).fadeIn(300);
		$('ul.subnav a').removeClass('selected');
		$('#subnav_'+item).addClass('selected');
		if (bodyImage)
		{
			$('body').removeClass();
			$('body').addClass(bodyImage+' '+item);
		}
	}
}
