$(function(){
	// set up video lists
	// link action
	$('.video-thumb').children('a').click(function(){
		var vId = $(this).parent()[0].id.replace('vt','');
		// deactivate other links, activate this one
		$('.video-thumb.active').removeClass('active').each(function(){
			$(this).parent().eq(0).find('img')[0].src = videoData['v'+this.id.replace('vt','')].inactive;
		});
		$(this).parent().addClass('active');
		$(this).parent().eq(0).find('img')[0].src = videoData['v'+vId].thumbnail;
		
		// load description
		$('#player #description').text(videoData['v'+vId].description);
		// load download links
		var dlHtml = '';
		if (videoData['v'+vId].highresdl) {
			dlHtml += '<a href="'+videoData['v'+vId].highresdl+'">High Res</a><br/>';
		}
		if (videoData['v'+vId].lowresdl) {
			dlHtml += '<a href="'+videoData['v'+vId].lowresdl+'">Low Res</a><br/>';
		}
		if (dlHtml.length > 0) {
			dlHtml = '<strong>Download:</strong><br/>'+dlHtml;
			$('#download-links').html(dlHtml);
		} else {
			$('#download-links').empty();
		}
		// load flash file
		if (swfobject) {
			swfobject.embedSWF("swf/videoplayer.swf", "videoplayer", "637", "303", "9.0.28", "swf/expressInstall.swf", {'video':this.href}, {'swLiveConnect':'true','allowFullScreen':'true'}, {'swLiveConnect':'true','allowFullScreen':'true'});
		} else {
			$('#slideshow').append($('#slideshow').children('noscript').text());
		}
		return false;
	});
	// rollover effect
	$('.video-thumb').mouseover(function(){
		if ($(this).hasClass('active')) return;
		var vId = this.id.replace('vt','');
		$(this).find('img')[0].src = videoData['v'+vId].thumbnail;
		$(this).find('.img-link').addClass('active');
	}).mouseout(function(){
		if ($(this).hasClass('active')) return;
		var vId = this.id.replace('vt','');
		$(this).find('img')[0].src = videoData['v'+vId].inactive;
		$(this).find('.img-link').removeClass('active');
	});

	// add nav arrows to video list area
	$('#list-scroll').append('<a href="#" id="scroll-left">Left</a><a href="#" id="scroll-right">Right</a>');
	$('#scroll-left, #scroll-right').click(function(){
		var scrollAmt = 166 * 4;
		if (this.id == 'scroll-right') scrollAmt *= -1;
		var newPos = parseInt($('#list-scroll').data('activeList').css('left')) + scrollAmt;
		newPos = Math.max(Math.min(newPos, 0), $('#list-scroll').data('maxScroll'));
		$('#list-scroll').data('activeList').animate({'left':newPos+'px'}, 1000);
		return false;
	});
	// folder links
	$('#video-folders a').click(function(){
		var folderId = this.id.replace('va','');
		$('.video-list').hide();
		$('#vl'+folderId).css('left','0px'); // reset scroll
		$('#vl'+folderId).show();
		$('#list-scroll').data('activeList', $('#vl'+folderId));
		$('#list-scroll').data('maxScroll', $('#list-scroll').width() - $('#vl'+folderId).width());
		if ($('#list-scroll').data('maxScroll') >= 0) {
			// no point in having arrows, nowhere to scroll
			$('#scroll-left, #scroll-right').hide();
		} else {
			$('#scroll-left, #scroll-right').show();
		}
		return true;
	});
	// load from url hash or first folder
	if (location.hash) {
		var vId = location.hash.replace('#v','');
		$('#video-folders a#va'+vId).click();
	} else {
		$('#video-folders a:first').click();
	}
});
