// Setup some global variables. Go memory savings!
var documentHeight, viewportHeight, documentWidth, documentHeightOnLoad;
var viewport, main, mainWidth, wrapperShadow, wrapperGradient, wrapperShadowWidth, wrapperShadowPadding;
var bigSplash;
var slideshow;
var mainPadding = 220; // This is already set in the CSS, but we re-set it here.

function repositionInterface() {
	// Don't cache documentHeight on document ready; Windows barfs on reload sometimes, so this is slower but more accurate.
	documentHeight = main.outerHeight();
	viewportHeight = viewport.height();
	viewportWidth = viewport.width();
	mainWidth = main.outerWidth();
	wrapperShadowWidth = wrapperShadow.outerWidth();

	// Increase #main's height to fill available window space
	if (viewportHeight > documentHeight || viewportHeight > documentHeightOnLoad) {
		main.css({height: viewportHeight - mainPadding + 'px'});
	}

	// Resize wrappers to fit window size to avoid scrollbars
	if (viewportWidth < mainWidth) {
		wrapperShadow.css({paddingLeft: '0', paddingRight: '0'});
		wrapperGradient.css({minWidth: mainWidth + 'px'});
	} else {
		wrapperShadowPadding = ((viewportWidth - mainWidth) % 2) ? ((viewportWidth - mainWidth) / 2) : ((viewportWidth - mainWidth - 1) / 2);
		wrapperShadow.css({paddingLeft: wrapperShadowPadding + 'px', paddingRight: wrapperShadowPadding + 'px'});
		wrapperGradient.css({minWidth: viewportWidth + 'px'});
	}

}

function positionSplash() {
	// Some of these declarations are via JS instead of native CSS so that JS-disabled browsers don't get a funky page render.
	bigSplash.css({left: '-850px'}); // Have to do this first so that offset().left in the next function calculates properly.
	bigSplash.css({
		width: $(window).width() - $(bigSplash[0]).offset().left + 'px',
		backgroundPosition: '0 0'
	});
	bigSplash.children('img').css({
		position: 'relative',
		left: '0'
	});
}

$(document).ready(function() {
	// Look for and setup validatable forms.
	$('form.validatable').validate();

	// Look for and setup lightboxes.
	slideshow = $('#slideshow');
	if (slideshow) {
		// Do it this way since jQuery's Lightbox doesn't natively support lightbox[array] like the real one does.
		slideshow.children('li').each(function(){
			$(this).find('a[rel*=lightbox]').lightBox();
		});
	}

	// If we're on a Big Splash page, work that magic baby.
	bigSplash = $('.big-splash');
	if (bigSplash.length > 0) {positionSplash();}

	// Grab a few elements so we don't have to over and over
	viewport = $(window);
	main = $('#main');
	documentHeightOnLoad = main.outerHeight();
	wrapperShadow = $('#wrapper-shadow');
	wrapperGradient = $('#wrapper-gradient');
	repositionInterface();
});

// These things gotta be redone when the window resizes, too. Also, Gecko is slow at this.
$(window).resize(function() {
	repositionInterface();
	if (bigSplash.length > 0) {positionSplash();}
});
