
$(document).ready(function(){

	$("#mask div").hide();
	$("#mask p").fadeIn("slow").fadeOut("slow").fadeIn("slow").fadeOut("slow").fadeIn("slow").fadeOut("slow").fadeIn("slow");

	var imgH = 0;
	var windH = 0;
	var openH = 0;
	var imgPosY = 0;
	var leftPos = 0;
	var gall = "";

	if (window.location.hash) {
		if (window.location.hash=="#work") { gall = "open"; }
		if (window.location.hash=="#past") { gall = "open"; }
		if (window.location.hash=="#bio") { gall = "closed"; }
		if (window.location.hash=="#studio") { gall = "closed"; }
		if (window.location.hash=="#contact") { gall = "closed"; }
		if (window.location.hash=="#news") { gall = "closed"; }
	}

	$("#mask").animate({ opacity: 1 },1600,function(){
		windH = $(window).height();
		imgH = $("div.slideshow img").height();
		openH = windH - 60;
		imgPosY = (openH/2) - (imgH/2);
		$("div.slideshow").animate({ top: imgPosY });
		$("#content,#galleryWrap,div.slideshow-container").height(openH);
		if (gall=="closed") {
			$("#overlayWrap").height(windH).css({ top: "0px", overflow: "auto" });
			$("div.navigation,div.controls").hide();
			$(".caption").fadeOut(1000);
		}
		else {
			$("#overlayWrap").height(windH-openH).css({ top: openH, overflow: "hidden" });
			$("div.navigation,div.controls").show();
			$(".caption").fadeIn(1000);
			if (window.location.hash=="#work") {
				$("#workNav").addClass("On");
				$("#selectedWorks").fadeIn("slow",function(){
					leftPos = ($(window).width() - $("#selectedWorks").width()) / 2;
					$('div.navigation').animate({ left: leftPos });
				});
			}
			if (window.location.hash=="#past") {
				$("#pastNav").addClass("On");
				$("#pastWorks").fadeIn("slow",function(){
					leftPos = ($(window).width() - $("#pastWorks").width()) / 2;
					$('div.navigation').animate({ left: leftPos },600);
					$('#pastNav').click();
				});
			}
		}
	}).fadeOut("slow",function(){
		// Firefox Fix
		if (window.location.hash) {
			$('a[href$="'+window.location.hash+'"]').click();
		} else {
			$('a[href$="#work"]').click();
		}
	});

	$(window).resize(function() {
		windH = $(window).height();
		openH = windH - 60;
		imgH = $("div.slideshow img").height();
		imgPosY = openH/2 - imgH/2;
		leftPos = ($(window).width() - $("ul.thumbs").width()) / 2;
		$('div.navigation').css({ left: leftPos });
		if (gall=="open") { $("#overlayWrap").css({ top: openH }); }
		else { $("#overlayWrap").height(windH); }
		$("#content,#galleryWrap,div.slideshow-container").height(openH);
		$("div.slideshow").stop().animate({ top: imgPosY },100);
		$panels.css({ width: $(window).width() },function(){});
		$container.css('width', $panels[0].offsetWidth * $panels.length);
	});

	$(".logo,#workNav").click(function(){;
		window.location.hash = "#work";
		$("#header .nav a,#pastNav").removeClass("On");
		if ($(this).attr("id")=="workNav"||$(this).hasClass("logo")) { $("#workNav").addClass("On"); }
		if (gall=="closed") {
			$("div.navigation,div.controls,#selectedWorks").fadeIn("slow");
			$("#overlayWrap").animate({ height: windH-openH, top: openH, overflow: "hidden !important" });
			$(".caption").fadeIn(1000);
		}
		$("#pastWorks").fadeOut("slow",function(){
			$("#selectedWorks").fadeIn("slow");
			leftPos = ($(window).width() - $("#selectedWorks").width()) / 2;
			$('div.navigation').animate({ left: leftPos },600);
		});
		$("#selectedWorks a.thumb:first").click();
		gall = "open";
	});
	$("#pastNav").click(function(){;
		window.location.hash = "#past";
		$("#header .nav a,#workNav").removeClass("On");
		$("#pastNav").addClass("On");
		if (gall=="closed") {
			$("div.navigation,div.controls,#pastWorks").fadeIn("slow");
			$("#overlayWrap").animate({ height: windH-openH, top: openH, overflow: "hidden !important" });
			$(".caption").fadeIn(1000);
		}
		$("#selectedWorks").fadeOut("slow",function(){
			$("#pastWorks").fadeIn("slow");
			leftPos = ($(window).width() - $("#pastWorks").width()) / 2;
			$('div.navigation').animate({ left: leftPos },600);
		});
		$("#pastWorks a.thumb:first").click();
		gall = "open";
	});
	$("#header .nav a").click(function(){
		$("#header .nav a,#workNav,#pastNav").removeClass("On");
		$(this).addClass("On");
		if (gall=="open") {
			$("div.navigation,div.controls,#pastWorks,#selectedWorks").fadeOut("slow");
			$("#overlayWrap").animate({ height: windH, top: "0", overflow: "auto" });
			$(".caption").fadeOut(1000);
		}
		gall = "closed";
	});

	$("#contactForm").validate();

	$("#newsbit .close").click(function(){
		$("#newsbit").fadeOut(500);
	});

	$("#studioGallery a[rel=group]").fancybox({
		'titlePosition'	: 'inside',
		'titleFormat'	: function(title, currentArray, currentIndex, currentOpts) {
//			return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
			return '<span id="fancybox-title-over">' + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});


/********** Page Slider **********/
	var $panels = $('#overlay .scrollContainer > div');
	var $container = $('#overlay .scrollContainer');
	var horizontal = true;
	if (horizontal) {
		$panels.css({
			'float' : 'left',
			'width' : $(window).width(),
			'position' : 'relative' // IE fix to ensure overflow is hidden
		});
		$container.css('width', $panels[0].offsetWidth * $panels.length);
	}
	var $scroll = $('#overlay #content').css('overflow-x', 'hidden');
	function selectNav() {
		$(this)
			.parents('table:first')
				.find('a')
					.removeClass('selected')
	      		.end()
	    	.end()
		.addClass('selected On');
	}
	$('#overlay .nav').find('a').click(selectNav);
	function trigger(data) {
		var el = $('#overlay .nav').find('a[href$="' + data.id + '"]').get(0);
		selectNav.call(el);
		if (data.id) {
			window.location.hash = '#' + data.id;
		}
		if (data.id=="studio") {
			$("a#studio1").click();
		}
		window.scrollTo(0,0);
	}
	var offset = parseInt((horizontal ? $container.css('paddingTop') : $container.css('paddingLeft')) || 0) * -1;
	var scrollOptions = {
		target: $scroll, // the element that has the overflow
		items: $panels,
		navigation: '.nav a',
		axis: 'x',
		onAfter: trigger, // our final callback
		offset: offset,
		duration: 500,
		easing: 'swing'
	};
	$('#overlay').serialScroll(scrollOptions);
	$.localScroll(scrollOptions);
	scrollOptions.duration = 1;
	$.localScroll.hash(scrollOptions);


});


$(window).load(function(){

			// get window dimensions to position thumb block
			var leftPos = ($(window).width() - $("ul.thumbs").width()) / 2;
			// We only want these styles applied when javascript is enabled
			$('div.navigation').css({ position: 'absolute', top: '0px', left: leftPos });
			$('div.content').css({ display: 'block' });

			// Initially set opacity on thumbs and add
			// additional styling for hover effect on thumbs
			var onMouseOutOpacity = 1.0;
			$('#thumbs ul.thumbs li').opacityrollover({
				mouseOutOpacity:   onMouseOutOpacity,
				mouseOverOpacity:  1.0,
				fadeSpeed:         'fast',
				exemptionSelector: '.selected'
			});

			// Initialize Advanced Galleriffic Gallery
			var gallery = $('#thumbs').galleriffic({
				delay:                     2500,
				numThumbs:                 90,
				preloadAhead:              10,
				enableTopPager:            false,
				enableBottomPager:         false,
				maxPagesToShow:            7,
				imageContainerSel:         '#slideshow',
				controlsContainerSel:      '#controls',
				captionContainerSel:       '#caption',
				loadingContainerSel:       '#loading',
				renderSSControls:          false,
				renderNavControls:         true,
				playLinkText:              'Play Slideshow',
				pauseLinkText:             'Pause Slideshow',
				prevLinkText:              '<img id="arrowL" src="images/arrow-left.png" />',
				nextLinkText:              '<img id="arrowR" src="images/arrow-right.png" />',
				nextPageLinkText:          'Next &rsaquo;',
				prevPageLinkText:          '&lsaquo; Prev',
				enableHistory:             false,
				autoStart:                 false,
				syncTransitions:           true,
				defaultTransitionDuration: 900,
				enableKeyboardNavigation:  false,
				onSlideChange:             function(prevIndex, nextIndex) {
					// 'this' refers to the gallery, which is an extension of $('#thumbs')
					this.find('ul.thumbs').children()
						.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
						.eq(nextIndex).fadeTo('fast', 1.0);
				},
				onPageTransitionOut:       function(callback) {
					this.fadeTo('fast', 1.0, callback);
				},
				onPageTransitionIn:        function() {
					this.fadeTo('fast', 1.0);
				}
			});

});

