/* Author: 
Tom Judd
*/
var currentSelection = 0;

var topGap = 0;
var titleY = 170;
var titleTextX = 270;
var footerTextX = 267;
var itemHeight = 550;

var stickyX = 210;
var stickyY = 10;

var newsSel = 0;
var numNews = 0;

var galleryPageSel = 0;
var numPagesGallery = 0;
var galleryPageHeight = 212;

var videosSel = 0;
var numVideos = 0;

// set up the pages array
 var pages = new Array(6);
 pages[0] = "home";
 pages[1] = "about";
 pages[2] = "news";
 pages[3] = "bio";
 pages[4] = "gallery";
 pages[5] = "videos";
 pages[6] = "contact";
 pages[7] = "links";

// On DOCUMENT LOAD
jQuery(document).load(function(){
	
	
});


jQuery(document).ready(function(){
	
	
	
	$('.slideshow').cycle({
		fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});
	
	flowplayer("player", "assets/flash/flowplayer-3.2.6.swf");
	
	/* FANCY BOX */
	
	/* Apply fancybox to multiple items */
	
	$("a.group").fancybox({
		//'transitionIn'	:	'elastic',
		//'transitionOut'	:	'elastic',
		'speedIn'		:	400, 
		'speedOut'		:	200, 
		'overlayShow'	:	true,
		'titlePosition'	: 'over'
	});
	


	$('body').css("visibility", "visible");

	$('body').fadeIn( 1000 );
	
	// CLICKING FUNCTIONS
	$("#titleBar").click(function(){
		// clicking header scrolls to top if below home page
		if($(document).scrollTop() >= $(window).height()){
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: 0}, 1000 );
		}
	});
	
	$("a.video").fancybox({
		//'transitionIn'	:	'elastic',
		//'transitionOut'	:	'elastic',
		'speedIn'		:	400, 
		'speedOut'		:	200, 
		'overlayShow'	:	true,
		'type'			: 'iframe'
		
	});
	
	$("a.videoInline").fancybox({
		'speedIn':400,
		'speedOut':200,
		'overlayShow':true,
		'width':720,
		'height':410,
		'autoDimensions':false 
	});

	$('body').animate({ opacity: 1}, 1000 );
	
	// CLICKING FUNCTIONS
	$("#titleBar").click(function(){
		// clicking header scrolls to top if below home page
		if($(document).scrollTop() >= $(window).height()){
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: 0}, 1000 );
		}
	});	
	
	$(".downArrow").click(function(){	
		// var to hold scroll position
		var scrollPos = 0; 
		// get the number of children (items) in main div
		var numItems = $("#main > div").size();
		// get the window height
		var windowH = $(window).height();
		
		var i = 0;
		// work out current pos and calulate the nest step
		for (i=0; i < numItems; i++) {
			// are we on the last page?
			if($(document).scrollTop() < (i * windowH)){
				scrollPos = i * windowH;
				break;
			}else{
				//scrollPos = windowH * numItems;
			}
		} 
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: scrollPos}, 800 );
	});
	
	$(".upArrow").click(function(){	
		// var to hold scroll position
		var scrollPos = 0; 
		// get the number of children (items) in main div
		var numItems = $("#main > div").size();
		// get the window height
		var windowH = $(window).height();
		
		var i = 0;
		// work out current pos and calulate the nest step
		for (i=0; i < numItems; i++) {
			// are we on the last page?
			scrollPos = windowH * (numItems - 2);
			
			if(i === numItems ){
				scrollPos = 300;
				break;
			} else if($(document).scrollTop() < (i * windowH)){
				scrollPos = (i-2) * windowH;
				break;
			}
		} 
		
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: scrollPos}, 800 );
	});
	
	$(".footNav").click(function(){
		var scrollPos = $(window).height()*parseFloat($(this).attr("linkId"));
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: scrollPos}, 1000 );
	});
	
	function positionTitleBar(){
		
		//('#titleBar').css("top", titleY + $(window).height() / 2 );
		
		if($(document).scrollTop() <= titleY + $(window).height() / 2){
			// position for home page
			//$('.upArrow').css({ opacity: 0});
			$('.upArrow').fadeOut('fast');
			$('#titleBar').css({'top' : titleY + $(window).height() / 2 , 'position' : 'absolute'});
		}else{
			// set to fixed
			//$('.upArrow').css({ opacity: 1});
			$('.upArrow').fadeIn('fast');
			$('#titleBar').css( {'top' : '0px', 'position' : 'fixed'});
		}
		
		$('#titleBar .titleText').css("padding-left", $(window).width() / 2 - titleTextX);	
		
	}
	
	function positionFooterBar(){
		
		//('#titleBar').css("top", titleY + $(window).height() / 2 );
		
		if($(document).scrollTop() <= $(window).height()){
			// position for home page
			$('#footerBar').css({'top' : ($(window).height() * 2) - $('#footerBar').height() , 'position' : 'absolute'});
		}else{
			// set to fixed
			$('#footerBar').css( {'top' : $(window).height() - $('#footerBar').height(), 'position' : 'fixed'});
		}
		$('#footerBar .footerText').css("padding-left", $(window).width() / 2 - footerTextX);	
				
	}
	
	function centerItems(){
		var leftOffset = $(window).width() / 2;
		
		var count=0;
		// loop through all items (children of main div) and position to window
		$('#main').children(".item").each(
		    function(){
		        //access to form element via $(this)
				var pad =  ($(window).height()/2) - (itemHeight/2);
		
				$(this).css({'left':leftOffset - ($(this).outerWidth()/2),'padding-top':pad, 'padding-bottom':pad});
				count++;
			}
		);	
		
		$('#main').css("width",$(window).width());
		
		$('#sticky').css("top", titleY + stickyY + $(window).height() / 2 );
		$('#sticky').css("left", $(window).width() / 2 + stickyX );
		$('#sticky').css("position","fixed" );
		
		$('.divider').css("left",$(window).width() /2 - ($(".divider").width() /2 ) );
		
		
		$('#shadowLeft').css({ 'display' :'inline','height' : $(window).height()* pages.length, 'top' : '0px', 'left' : -300 });
		$('#shadowRight').css({ 'display' :'inline','height' : $(window).height()* pages.length, 'top' : '0px', 'left' : $(window).width() -300 });
		
		
	}
	
	function apex(){
		
		
		var dist = 0;
		var count = 0;
		var count2 = 0;
		

		
		// loop thoough all items
		$('#main').children().each(
		    function(){
				// get the distance from apex
				// dist = (parseFloat($(this).css("top")) + ($(this).height() / 2)) - ($(document).scrollTop() + ($(window).height() / 2));
				dist = $(document).scrollTop() - ($(window).height() * count2);
			
		    //access to form element via $(this)
				// loop thoough all children
				count = 0;
					$(this).children(".l, .m ,.r").each(
						function(){
							switch(count)
							{
							case 0:
							$(this).css("margin-top", - dist/10);
							
							break;
							case 1:
							$(this).css("margin-top", - dist/5);
							break;
							case 2:
							break;
							default:
							}						
							count ++;
						}
				);
				count2++;
				
			}
		);	
	}
	
	function setHash(){
		var i=0;
		for (i=0; i < pages.length; i++) {
			if(i === currentSelection){
				window.location.hash = pages[i];
			}
		}
	}
	
	function setNavSelected(){
		var count = 0;
		var sT = $(document).scrollTop();
		var wH = $(window).height();
		// work out the selection depending on scroll
		var oldSelection = currentSelection;
		
		currentSelection = Math.round(sT / wH);
		
		if(oldSelection!==currentSelection){
			
			// loop thoough all items
			$('.footerText').children('.footNav').each(
				function(){	
					if ( currentSelection === count+1 ){
						$(this).addClass('selected');
					}else{
						$(this).removeClass('selected');
					}

					count ++;
				}
			);
			
			setHash();
		}
		
	}
	

	
	function goToSelected(){
		var pos = currentSelection * $(window).height();
		$("html:not(:animated),body:not(:animated)").animate({ scrollTop: pos}, 0 );
		
	}
	

	
	
	// NEWS SORT AND NAVIGATE
	function setNews(sel){
		numNews = $("#newsSection").children(".newsHolder").size() -1;
		var count = 0;
		// loop thoough all news items
		$("#newsSection").children(".newsHolder").each(
			function(){	
				if(count === sel){
					$(this).fadeIn('fast');
					newsSel = sel;
				}else{
					$(this).fadeOut('fast');
				}
				count++;
			}
		);
	}
	
	function newsNav(dir){
		var sel = 0;
		switch(dir){
			case 'next':
				sel = newsSel+1;
				if(newsSel === numNews){
					sel = 0;
				}	
			break;
			case 'prev':
				sel = newsSel-1;
				if(newsSel === 0){
					sel = numNews;
				}	
			break;
		}
		setNews(sel);
	}
	$("#newsNext").click(function(){
		newsNav('next');
	});	
	$("#newsPrev").click(function(){
		newsNav('prev');
	});

	
	// GALLERY NAVIGATE
	function setGallery(page){
		numPagesGallery = Math.ceil($("#gallerySection .gallery").children("a").size() / 6) - 1;
		var newTop = galleryPageHeight * page;
		$("#gallerySection .gallery").animate({ top: -newTop}, 400 );
		galleryPageSel = page;
	}
	function galleryNav(dir){
		var sel = 0;
		switch(dir){
			case 'next':
				sel = galleryPageSel+1;
				if(galleryPageSel === numPagesGallery){
					sel = 0;
				}	
			break;
			case 'prev':
				sel = galleryPageSel-1;
				if(galleryPageSel === 0){
					sel = numPagesGallery;
				}	
			break;
		}
		setGallery(sel);
	}
	$("#galleryNext").click(function(){
		galleryNav('next');
	});	
	$("#galleryPrev").click(function(){
		galleryNav('prev');
	});

		
	
	// VIDEOS SORT AND NAVIGATE
	function setVideos(sel){
		numVideos = $("#videosSection").children(".newsHolder").size() -1;
		var count = 0;
		// loop thoough all news items
		$("#videosSection").children(".newsHolder").each(
			function(){	
				if(count === sel){
					$(this).fadeIn('fast');
					videosSel = sel;
				}else{
					$(this).fadeOut('fast');
				}
				count++;
			}
		);
	}
	
	function videosNav(dir){
		var sel = 0;
		switch(dir){
			case 'next':
				sel = videosSel+1;
				if(videosSel === numVideos){
					sel = 0;
				}	
			break;
			case 'prev':
				sel = videosSel-1;
				if(videosSel === 0){
					sel = numVideos;
				}	
			break;
		}
		setVideos(sel);
	}
	
	$("#videosNext").click(function(){
		videosNav('next');
	});	
	$("#videosPrev").click(function(){
		videosNav('prev');
	});

	
	// URL NAVIGATION WITH HASH TAGS
	// check to see if there's a start page in the url
	function hashNav(){

		var startPage = (window.location.hash) ? window.location.hash.replace('#', '') : 'home';
		var i=0;
		if(startPage !== "home"){
			for (i=0; i < pages.length; i++) {
				if(pages[i] === startPage){
					currentSelection = i;
				}
			}
		}
		// move page to selected page
		var pos = currentSelection * $(window).height();
		$("body").animate({ scrollTop: pos}, 1000 );
	}	
		
		
	
	// EVENTS _______________________________
	$(window).scroll(function() {
    
		positionTitleBar();
		positionFooterBar();
		apex();
		setNavSelected();
		//setHash();
	});
	
	// Reposition and scale on window resize
	$(window).resize(function() {
		centerItems();
		apex();
		positionTitleBar();
		positionFooterBar();
		goToSelected();
		
	});
	
	$(window).mousemove(function(e){
		$('#sticky').animate({ opacity: 1}, 600 );
	});
	//________________________________________
	centerItems();
	positionTitleBar();
	apex();
	setNews(0);
	setGallery(0);
	setVideos(0);
	hashNav();

});





















