// when the DOM is ready...
$(document).ready(function () {

$(".panel h1").hide(); //Hide h1s since they aren't needed for the JS version of the site

$(".backToTop").click(function() {
	$(document).scrollTop(0);
});

//startTime = new Date().getTime();


$(".headerOther").click(function() { //click handler for header
	$('nav a:first').click();
});

$(".panel").each(function() { //add the prefix "page" to the ids of the panels to disable automatic hash scrolling
	var origID = this.id;
	if (origID.substr(0,4) !=="page") {
		this.id = "page"+origID;
	}
});

var animSpeed = 800;
var pagePosArr = ["pageHome","pageVideo","pageShow","pageFrank","pageBravo","pageFun_Stuff","pageContact"];

var $panels = $('.scrollContainer > .panel');
var $container = $('.scrollContainer');

// if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ animSpeed = 1; } //if Firefox, disable animation

// position the panels one after the other
$(".panel").each(function(index){
	var panelOffset = index*961;
	$(this).css({
		'position' : "absolute",
    	'left' : panelOffset+'px'
  	});
});

// calculate a new width for the container (so it holds all panels)
$container.css('width', $panels[0].offsetWidth * $panels.length);

// collect the scroll object, at the same time apply the hidden overflow
// to remove the default scrollbars that will appear
var $scroll = $('.scroll').css('overflow', 'hidden');

// handle nav selection (click)
function selectNav() {
  $('.mainNav').find('a').removeClass('selected');
  $(this).addClass('selected');
}

$('nav').find('a').click(function() {	
	animToPage( "page" + $(this).attr("href").split("#")[1] ); //start animateToPage, sending in the id (retrieved from the link's href)
});

function animToPage(pageID,first) {
	selectNav.call($("a[href=#"+pageID.split("page")[1]+"]").get()); //select nav button
	pageIndex = $.inArray(pageID,pagePosArr);
	pageLeftPosition = pageIndex*-961;
	var speed = animSpeed;
	if (first) {
		speed = 1;
		
		if (pageID !== "pageHome") {
			$.startSlideshow = false;
		} else {
			$.startSlideshow = true;
		}
	}
	
	$("#"+pageID+" .vid").not("#pageHome .vid").not(".optimizedYouTube .vid").show(); //show videos on page
	$(".optimizedYouTube .vid").addClass("optimizedYouTubeHide");
	var sectionHeight = $("#"+pageID).height()+50;
	var hackOffset=0;
	
	$(".scrollContainer").stop().animate({"left":pageLeftPosition+"px" , "height":sectionHeight+"px"},speed);
	if (pageID === "pageHome") {
		// $(".homeYouTube iframe").show(); //For the home page youtube video, since it has a bg fallback image
		$.startSlideshow = true;
		sectionHeight += 330;
		$(".headerOther").stop().fadeOut(speed);
		$("#slideshow").stop().fadeIn(speed);
		$("#slideshowCaption").stop().fadeIn(speed);
		$(".frankBig").stop().fadeIn(speed);
		$(".dynamic").stop().animate({"top":"336px"},speed);
		$(".socialButtons").stop().animate({"top":"305px","left":"6px"},speed);
		$(".header").stop().animate({"height":330+30+"px"},speed);
		$("#slideshow").crossSlideResume();
		hackOffset = 11;
	} else {
		// $(".homeYouTube iframe").hide(); //For the home page youtube video, since it has a bg fallback image
		$.startSlideshow = false;
		sectionHeight += 140;
		$(".headerOther").stop().fadeIn(speed);
		$("#slideshow").stop().fadeOut(speed);
		$("#slideshowCaption").stop().fadeOut(speed);
		$(".frankBig").stop().fadeOut(speed);
		$(".dynamic").stop().animate({"top":"134px"},speed);
		$(".socialButtons").stop().animate({"top":"100px","left":"470px"},speed);
		$(".header").stop().animate({"height":134+"px"},speed);
		$("#slideshow").crossSlidePause();
	}
	$(".wrapper").stop().animate({"height":sectionHeight+hackOffset+"px"},speed, function() {  //+11px for home (hackOffset)
		$(".vid").not("#"+pageID+" .vid").not(".optimizedYouTube .vid").hide(); //hide videos on other pages after the animation is finished
		$("#"+pageID+" .optimizedYouTube .vid").removeClass("optimizedYouTubeHide");
	});
	
	return true;
	
}


if (window.location.hash) {
	
	var myHash = window.location.hash;
	animToPage( "page" + myHash.split("#")[1], true); //start animateToPage, sending in the id (retrieved from the link's href)
	
	// if($("nav a[href^="+myHash+"]").length > 0) {
	// 	$("nav a[href^="+myHash+"]").click();
	// } else {
	// 	$('nav a:first').click();
	// }
	
} else {
  $('nav a:first').click();
}
document.gotoPage = animToPage;

});
