
(function($){

	var winW = $(window).width();
	var winH = $(window).height();
	var $content = $('#content');
	var navving = false;

	function closePage() {
		navving = true;
		$('ul#nav a').removeClass('Open');
		$content.fadeOut(300,function(){
			$(this).find('.overview').html('');
			navving = false;
		});
		$('#underlay').hide();
		$('#overlay').animate({ height: '115px' },400);
		$('#nextslide img,#prevslide img').animate({ top: ( (winH/2) - 31 + ($('#header').height()/2) - 100 )+"px" },400);
		window.location.hash = '#Gallery';
	}

	function openPage(href) {
		navving = true;
		var href = href.substring(1);
		$('ul#nav a').removeClass('Open');
		$('a#'+href).addClass('Open');
		$('#underlay').show();
		$('#overlay').animate({ height: (winH-130)+'px' },400,function(){
			var thisH = $(this).height()-180;
			var thisW = $(this).width()-300;
			$('#content, #content .viewport').height(thisH);
			$content.fadeOut(200,function(){
				$('#content .overview').load('content/'+href+'.php',function(){
					$('iframe.page').width(thisW).load(function(){
//						this.style.height = (this.contentWindow.document.body.offsetHeight + 20) + 'px';
						$content.tinyscrollbar_update();
					});
					$('.viewport').width(winW-300);
					$content.fadeIn(300,function(){
						$content.tinyscrollbar_update();
						$(".fancybox").fancybox({
							overlayColor: '#000000',
							overlayOpacity: 0.8,
							padding: 0
						});
						navving = false;
					});
				});
			});
		});
		$('#nextslide img,#prevslide img').stop(true,true).animate({ top: "48px" },400);
	}

	var storedHash = window.location.hash;
	window.setInterval(function() {
		if (window.location.hash != storedHash) {
			storedHash = window.location.hash;
			if (storedHash == '#Gallery') closePage();
			else openPage(storedHash);
		}
		if (!window.location.hash) closePage();
	}, 100);


	$(function() {
	
		$.supersized({
		
			//Functionality
			slideshow               :   1,		//Slideshow on/off
			autoplay				:	0,		//Slideshow starts playing automatically
			start_slide             :   1,		//Start slide (0 is random)
			slide_interval          :   3000,	//Length between transitions
			transition              :   1, 		//0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left
			transition_speed		:	500,	//Speed of transition
			new_window				:	0,		//Image links open in new window/tab
			pause_hover             :   0,		//Pause slideshow on hover
			keyboard_nav            :   1,		//Keyboard navigation on/off
			performance				:	1,		//0-Normal, 1-Hybrid speed/quality, 2-Optimizes image quality, 3-Optimizes transition speed // (Only works for Firefox/IE, not Webkit)
			image_protect			:	1,		//Disables image dragging and right click with Javascript
			image_path				:	'img/gallery/', //Default image path
			image_click_next		:	1,		//Clicking the image goes to next image instead of URL
	
			//Size & Position
			min_width		        :   0,		//Min width allowed (in pixels)
			min_height		        :   0,		//Min height allowed (in pixels)
			vertical_center         :   0,		//Vertically center background
			horizontal_center       :   1,		//Horizontally center background
			fit_portrait         	:   0,		//Portrait images will not exceed browser height
			fit_landscape			:   0,		//Landscape images will not exceed browser width
			
			//Components
			navigation              :   1,		//Slideshow controls on/off
			thumbnail_navigation    :   1,		//Thumbnail navigation
			slide_counter           :   1,		//Display slide numbers
			slide_captions          :   1,		//Slide caption (Pull from "title" in slides array)
			slides 					:  	[		//Slideshow Images
				{ image : 'img/gallery/01.jpg' },
				{ image : 'img/gallery/02.jpg' },
				{ image : 'img/gallery/03.jpg' },
				{ image : 'img/gallery/04.jpg' },
				{ image : 'img/gallery/05.jpg' },
				{ image : 'img/gallery/06.jpg' },
				{ image : 'img/gallery/07.jpg' },
				{ image : 'img/gallery/08.jpg' },
				{ image : 'img/gallery/09.jpg' },
				{ image : 'img/gallery/10.jpg' },
				{ image : 'img/gallery/11.jpg' },
				{ image : 'img/gallery/12.jpg' },
				{ image : 'img/gallery/13.jpg' }
			]
										
		}); 
	
		$('#underlay').width(winW-200).hide().click(function(e){
			e.preventDefault();
			if ($('ul#nav a').hasClass('Open') && !navving) {
				window.location.hash = '#Gallery';
			}
		});

		$('ul#nav a').click(function(e){
			if (!navving) {
//				e.preventDefault();
				var $thisID = $(this).attr('id');
				if ($thisID == window.location.hash.substring(1)) closePage();
				else window.location.hash = $thisID;
			}
		});
		
		$content = $('#content');
		$content.tinyscrollbar();

		if (window.location.hash) {
			if (window.location.hash == '#Gallery') closePage();
			else openPage(window.location.hash);
		}

	});


	$(window).bind("load", function() {
		
	});
	

	$(window).resize(function() {
		
		winW = $(window).width();
		winH = $(window).height();

		if ($('ul#nav a').hasClass('Open')) {
			$('#overlay').height(winH-130);
			var thisH = $('#overlay').height()-180;
			var thisW = $('#overlay').width()-300;
			$('.viewport').width(winW-300);
			$('#content, #content .viewport').height(thisH);
			$('iframe.page').width(thisW).load(function(){
				this.style.height = (this.contentWindow.document.body.offsetHeight + 20) + 'px';
				$content.tinyscrollbar_update();
			});
			$content.tinyscrollbar_update();
		}
		else {
			$('#nextslide,#prevslide').height(winH);
			$('#nextslide img,#prevslide img').css({ top: ( (winH/2) - 31 + ($('#header').height()/2) - 100 )+"px" });
		}
		$('#underlay').width(winW-200);
	
	});
		

})(window.jQuery);

