$(document).ready(function(){
	// Full Screen BG
	var win = $(window);
		var bg = $('#bg img');
		var bgW = bg.width();
		var bgH = bg.height();
		var bgRatio = bg.width()/bg.height();	
		
		var bgResizer = function(){
			bgW = bg.width();
			bgH = bg.height();
			bgRatio = bg.width()/bg.height();	
			winW = win.width();
			winH = win.height();
			winRatio = winW/winH;
			if(winRatio >= bgRatio){
				bg.css({'width': win.width(), 'height': win.width()/bgRatio});
				bgH = bg.height();
				bg.css({'margin-top': (winH-bgH)/2+'px', 'margin-left': '0'});
			} else{
				bg.css({'width': 'auto', 'height': '100%'});
				bgW = bg.width();
				bg.css({'margin-top': '0', 'margin-left': (winW-bgW)/2+'px'});
			}
			//console.log('bgResize');
		}		
		
		win.resize(function(){
			bgResizer();
		});
		
		bgResizer(); 
		
		$(window).load(function(){
			bgResizer();
			$('#bg-loading').fadeOut(0);
			bg.fadeIn(600)
		});
		
		setTimeout(bgResizer, 1000);
		
	$('h1 a').append('<span></span>'); 
	$('h1 a span').fadeTo(0, 0);
	$('h1 a').hover(function(){
		$(this).find('span').stop().fadeTo(400, 1);
	}, function(){
		$(this).find('span').stop().fadeTo(400, 0);
	});
	
	$('#social a').append('<span></span>'); 
	$('#social a span').fadeTo(0, 0);
	$('#social a').hover(function(){
		$(this).find('span').stop().fadeTo(400, 1);
	}, function(){
		$(this).find('span').stop().fadeTo(400, 0);
	});
	
	// Shadowbox
	function vimeoSetup() {	
		Shadowbox.setup('a[title="FoxGlove - Take Me Away"]', {
			height:     453,
			width:      799,
			flashVars:  {
				clip_id:    "17225101",
				autoplay:   "1"
			}
		});
	}
	
	Shadowbox.init({
		overlayOpacity: .85,
		autoplayMovies: true,
		fadeDuration: .5
	}, vimeoSetup);
	
	/*Shadowbox.init({
		overlayOpacity: .85,
		autoplayMovies: true,
		fadeDuration: .5
	});*/
	
	// Custom Scrollbar 
	$('.scroller').jScrollPane({
		verticalGutter: 20,
		verticalDragMaxHeight: 31
	});
	
	// Nivo slider 
	$(window).load(function() {
        $('#slider').nivoSlider({
			effect: 'fade',
			directionNav: false	
		});
    });
	
	// Navigation
	$('nav li a').append('<span></span>');		
	
	// Current Navigation
	var currentPage = $('html').attr('id');	
	if(currentPage === 'page-home'){
		$('#home').addClass("current");
	}
	else if(currentPage === 'page-talent' || currentPage === 'page-talent-projects'){
		$('#talent').addClass("current");
	}
	else if(currentPage === 'page-projects'){
		$('#projects').addClass("current");
	}
	else if(currentPage === 'page-production'){
		$('#production').addClass("current");
	}
	else if(currentPage === 'page-archive'){
		$('#archive').addClass("current");
	}
	else if(currentPage === 'page-news'){
		$('#news').addClass("current");
	}
	else if(currentPage === 'page-contact'){
		$('#contact').addClass("current");
	}
	
	var navNot = $('nav a.current').parent(); 
	$('nav li').not(navNot).prepend('<span class="bg"></span>');
	
	$('nav li').hover(function(){
		$(this).find('.bg').stop().animate({'left': '0'}, 400, 'easeOutExpo');
	}, function(){
		$(this).find('.bg').stop().animate({'left': '-320px'}, 400, 'easeOutExpo');
	});
	
	var hoverTimeout;
	$('li').has('ul').hover(function(){		
		clearTimeout(hoverTimeout);
		$(this).find('ul').stop(false,true).slideDown({duration: 400, queue: false}).fadeTo(400,1);
	}, function(){
		var obj = $(this).find('ul');
		hoverTimeout = setTimeout(function(){
			obj.stop(false,true).slideUp({duration: 400, queue: false}).fadeTo(400, 0, function(){obj.css('display', 'none')});
		}, 400);
	});
	
	// Projects
	
	if(currentPage === 'page-projects' || currentPage === 'page-archive'){
	
		// Project Navigation
		
		var projectNavNot = $('#project-nav .project-list a.current').parent(); 
		$('#project-nav .project-list li').not(projectNavNot).prepend('<span class="bg"></span>');
		
		$('#project-nav .project-list li').hover(function(){
			$(this).find('.bg').stop().animate({'right': '0'}, 400, 'easeOutExpo');
		}, function(){
			$(this).find('.bg').stop().animate({'right': '-230px'}, 400, 'easeOutExpo');
		});
		
		// Project Pages
		
		var currentPage = parseInt((location.hash).replace('#', ''));
		if(currentPage == '' || isNaN(currentPage) || currentPage > $('.project-list').length || currentPage < 1){
			currentPage = 1;			
		}
		
		location.hash = currentPage;
		$('.project-list:eq('+(currentPage-1)+')').addClass('current').css('display', 'block');
		
		if($.browser.msie){
			$('.project-list').not('.project-list:eq('+(currentPage-1)+')').css({'top': '-500px'});
			//$('.project-list').not('.project-list:eq('+(currentPage-1)+')').find('.bg').hide();
		} else {
			$('.project-list').not('.project-list:eq('+(currentPage-1)+')').css({'top': '-400px', 'opacity': 0});
			$('.project-list').not('.project-list:eq('+(currentPage-1)+')').find('.bg').hide();
		}
		
		$('.project-list').each(function(){
			var currentClass;
			var pageIndex = $(this).index()+1;
			if(pageIndex == currentPage){
				currentClass = 'current';
			} else {
				currentClass = '';	
			}
			$('#project-pages ul').append('<li><a href="#'+(pageIndex)+'" class="'+currentClass+'">'+(pageIndex)+'</a></li>');
		});	
		
		$('.project-list').not('.project-list:eq(0)').each(function(){
			var page = $(this).index()+1;
			$(this).find('a').each(function(){
				var origLink = $(this).attr('href');
				$(this).attr('href', origLink+'#'+page);
			});
		});	
		
		if($.browser.msie){
			$('#project-pages li a').live('click',function(){
				if(!($('.project-list').is(':animated'))){
					var target = $('.project-list:eq('+parseInt($(this).html()-1)+')');
					location.hash = $(this).html()-1;
					$('.project-list.current').find('.bg').hide();
					$('.project-list.current').animate({'top': '-500px'}, 500, function(){
						$('.project-list.current').removeClass('current').css('top','-500px');
						target.css('display', 'block').addClass('current').animate({'top': '0px'},{ duration:500, queue: false, callback: function(){target.find('.bg').show();}});
					});		
					$(this).addClass('current');
					$(this).parent().siblings().find('a').removeClass('current');
				}
			});
		} 
		else {
			
			$('#project-pages li a').live('click',function(){
				if(!($('.project-list').is(':animated'))){
					var target = $('.project-list:eq('+parseInt($(this).html()-1)+')');
					location.hash = $(this).html()-1;
					$('.project-list.current').find('.bg').hide();
					$('.project-list.current').animate({'top': '150px', 'opacity': 0}, 500, function(){
						$('.project-list.current').removeClass('current').css('top','-400px');
						target.css('display', 'block').addClass('current').animate({'top': '0px', 'opacity': 1}, 500, function(){target.find('.bg').show();});
						});		
					$(this).addClass('current');
					$(this).parent().siblings().find('a').removeClass('current');
				}
			});
			}
	}
	
	// Talent Projects
	
	if(currentPage === 'page-talent-projects'){
	
		// Project Navigation
		
		var projectNavNot = $('#talent-project-nav .project-list a.current').parent(); 
		$('#talent-project-nav .project-list li').not(projectNavNot).prepend('<span class="bg"></span>');
		
		$('#talent-project-nav .project-list li').hover(function(){
			$(this).find('.bg').stop().animate({'right': '0'}, 400, 'easeOutExpo');
		}, function(){
			$(this).find('.bg').stop().animate({'right': '-230px'}, 400, 'easeOutExpo');
		});
		
		// Project Pages
		
		var currentPage = parseInt((location.hash).replace('#', ''));
		if(currentPage == '' || isNaN(currentPage) || currentPage > $('.project-list').length || currentPage < 1){
			currentPage = 1;			
		}
		
		location.hash = currentPage;
		$('.project-list:eq('+(currentPage-1)+')').addClass('current').css('display', 'block');		
		if($.browser.msie){
			$('.project-list').not('.project-list:eq('+(currentPage-1)+')').css({'top': '-500px'});
			//$('.project-list').not('.project-list:eq('+(currentPage-1)+')').find('.bg').hide();
		} else {
			$('.project-list').not('.project-list:eq('+(currentPage-1)+')').css({'top': '-400px', 'opacity': 0});
			$('.project-list').not('.project-list:eq('+(currentPage-1)+')').find('.bg').hide();
		}
		
		$('.project-list').each(function(){
			var currentClass;
			var pageIndex = $(this).index()+1;
			if(pageIndex == currentPage){
				currentClass = 'current';
			} else {
				currentClass = '';	
			}
			$('#talent-project-pages ul').append('<li><a href="#'+(pageIndex)+'" class="'+currentClass+'">'+(pageIndex)+'</a></li>');
		});	
		
		$('.project-list').not('.project-list:eq(0)').each(function(){
			var page = $(this).index()+1;
			$(this).find('a').each(function(){
				var origLink = $(this).attr('href');
				$(this).attr('href', origLink+'#'+page);
			});
		});
		
		if($.browser.msie){
			$('#talent-project-pages li a').live('click',function(){
				if(!($('.project-list').is(':animated'))){
					var target = $('.project-list:eq('+parseInt($(this).html()-1)+')');
					location.hash = $(this).html()-1;
					$('.project-list.current').find('.bg').hide();
					$('.project-list.current').animate({'top': '-500px'}, 500, function(){
						$('.project-list.current').removeClass('current').css('top','-400px');
						target.css('display', 'block').addClass('current').animate({'top': '0px'},{ duration:500, queue: false, callback: function(){target.find('.bg').show();}});
					});		
					$(this).addClass('current');
					$(this).parent().siblings().find('a').removeClass('current');
				}
			});
		}
		else {
			$('#talent-project-pages li a').live('click',function(){
				if(!($('.project-list').is(':animated'))){
					var target = $('.project-list:eq('+parseInt($(this).html()-1)+')');
					location.hash = $(this).html()-1;
					$('.project-list.current').find('.bg').hide();
					$('.project-list.current').animate({'top': '150px', 'opacity': 0}, 500, function(){
						$('.project-list.current').removeClass('current').css('top','-400px');
						target.css('display', 'block').addClass('current').animate({'top': '0px', 'opacity': 1}, 500, function(){target.find('.bg').show();});
						});		
					$(this).addClass('current');
					$(this).parent().siblings().find('a').removeClass('current');
				}
			});
		}
	}
	
});
