$(document).ready(function(){

	if(window.location.hash != '') {
		// add active to menu
	} else {
		$('#menu a:first').addClass('active');
	}

	$('#header').localScroll({ 'duration': 400 });
	$('h1 a').click(function(){
		$('#menu a').removeClass('active');
		$('#menu a:first').addClass('active');
	})
	$('#menu a').click(function(){
		$('#menu a').removeClass('active');
		$(this).addClass('active');
	});
	
	
	
	var scrolling = false;
	var scrollTimeOut = false;
	
	function randomStill() {
		var stills = $('#still').children('img');
		var randomStill = Math.floor(Math.random()*stills.length);
		var theStill = stills[randomStill];
		return theStill;
	}
	
	
	function showStill() {
		if(Math.random() < .6) {
			$(randomStill()).fadeIn(0).fadeOut(200, function(){
				clearTimeout(scrollTimeOut);
				setTimeout(function(){ scrolling = false; }, 1500);
			});
		} else setTimeout(function(){ scrolling = false; }, 500);
	}
	
	$(window).scroll(function(){
		if(scrolling == false) {
			scrolling = true;
			scrollTime = Math.floor(Math.random() * 800) + 800;		
			scrollTimeOut = setTimeout(showStill, scrollTime);
		}
	});
	
	
	function showPlayer(episode_no) {
		// overlay is outside window
		var oTop = $(window).height() * -1;
		$('#overlay').show();


		var stills = $('#still').children('img');
		var theStill = stills[episode_no-1];
		var still_url = $(theStill).attr('src'); 


		// media
		// var still_url = './stills/e' + episode_no + '_1.jpg';
		var movie_url = '../episodes/episode' + episode_no + '.f4v';
		
		// load still
		$('#overlay img').attr('src', still_url);
		
		// swfobject embed player
		var flashvars = {
			holder: still_url,
			bg: '000',
			file: movie_url,
			autoplay: 'false',
			width: '640', height: '360'
		}
		var params = { 
			wmode: 'transparent',
			allowFullScreen: 'true'
		}
		swfobject.embedSWF(
			"./flash/player_22.swf", 
			"clip", 
			"640", "360", 
			"9.0.0", "expressInstall.swf", 
			flashvars, params
		);

		$('#overlay').css('top', oTop).show();
		$('#overlay').animate({ top: 0 }, 400, function(){
			$('#player').fadeIn(200);
		});
	}

	function hidePlayer() {
		var oTop = $(window).height() * -1;
		$('#player').fadeOut(200, function() {
			$('#overlay').animate({ top: oTop }, 400, function() { $('#player').hide(); $('#overlay').hide(); });
		});
	}

	$('#episodes a').click(function(){ showPlayer($(this).attr('href').substr(1)); return false; });
	$('#close_player').click(function() { hidePlayer(); return false; });
	
	
	
	
	$('ul.episodes a').hover(
		function(){ 
			var stillno = parseInt($(this).attr('href').substr(1));
			var stills = $('#still').children('img');
			var theStill = stills[stillno-1];
			$(theStill).show(); 
		},
		function(){ $('#still img').hide(); }
	);
	
	

	

});
