$(document).ready(function(){

	//insert overlay box
	$('#inner-content').before('<div id="overlaycontainer"><div id="overlaycontainer-border"></div></div>');
	
	var autoPlay = setTimeout('openOverlay()', 3000);

	// open section on page load
	var loc = window.location.href;
	if(/\?page=/g.test(loc)) {
		var page = loc.split('?page=')[1];
		switch(page) {
		case 'about':
			openSection(page + '.html', 244);
			clearTimeout(autoPlay);
			break;
		case 'photos':
			openSection(page + '.html', 244);
			clearTimeout(autoPlay);
			break;
		case 'buy-seasonone':
			openSection(page + '.html', 288);
		}
	} else {
		openSection('buy-seasontwo.html', 288);		
	}

	// open sections via nav
	$('#justified-nav li a').bind('click', function(){	
		clearTimeout(autoPlay);
		if(!$('#overlaycontainer').is(':animated')) {
			if($('span', this).length > 0) {
				window.open($('span', this).text());
			}
			$('#overlaycontainer-border').empty();
			var rel = $(this).attr('rel'),
				href = $(this).attr('href'),
				height = $(this).attr('rev').split('-')[1];		
			if(rel=='overlay'){
				if ($('#overlaycontainer').is(':hidden')) {
					openSection(href, height);
					return false;
				} else {
					transitionSection();	
					switchSection(href, height);
					return false;
				};	
			};
		}
	});
});

function openSection(href, height) {		
		$('#overlaycontainer').fadeIn('fast', function() {								   
			$("#overlaycontainer-border").append('<iframe id="overlaycontainer-iframe" src="' + href + '" scrolling="no" width="502" height="' + height + '" frameborder="0"></iframe>');
		});		
};

function switchSection(href, height) {
		$('#overlaycontainer').fadeIn('fast', function() {								   					   
			$("#overlaycontainer-border").append('<iframe id="overlaycontainer-iframe" src="' + href + '" scrolling="no" width="502" height="' + height + '" frameborder="0"></iframe>');
		});
};

function closeSection() {
	if ($("#overlaycontainer").is(":visible")) {
		$('#overlaycontainer').hide();
		$('#overlaycontainer-iframe').remove();
		$('#index-about').fadeTo("fast", 1.0);
	};
};

function transitionSection() {
	if ($("#overlaycontainer").is(":visible")) {	
		$('#overlaycontainer').fadeOut(200);
		$('#overlaycontainer-iframe').delay(200).remove();	
	};
};
