$(document).ready(function(){
	video_binds();
	tab_binds();

	if( $(document).getUrlParam('video_id') ){
		hard_link( $(document).getUrlParam('video_id') );
	} else if( $(document).getUrlParam('category') ){
		category_link( $(document).getUrlParam('category') );
	}
});

function category_link(category){
	// Click the corresponding tab:
	$('li#' + category + 'tab').click();

	return true;
}

function hard_link(video_id){
	// Click the corresponding tab:
	// var tab_name = $('.video_id').filter(function(){ return($(this)[0].textContent == video_id); }).parent().parent().parent().parent().attr('id') + 'tab';
	var tab_name = $('.video_link[data-video_id=' + video_id + ']').attr('data-tab_id');
	$("#" + tab_name).click();

	// Click the corresponding category:
	// $('.video_id').filter(function(){ return($(this)[0].textContent == video_id); }).parent().parent().parent().children('h3').children('a').click();
	$('.video_link[data-video_id=' + video_id + ']').parents('li.button').find('h3').eq(0).find('a').eq(0).click();

	// Click the corresponding video link:
	// $('.video_id').filter(function(){ return($(this)[0].textContent == video_id); }).parent().children('p.title').children('a.play_link').click();
	$('.video_link[data-video_id=' + video_id + ']').find('.play_link').eq(0).click();

	return true;
}

function play_video(video_id){
	// Retrieve the video information:
	$.getJSON("/ajax/video_json.php?video_id=" + video_id, function(entry){
		// Set the title:
		$('#cantaloupe-video-container-inner').children('h3:first').text(entry.title);

		// Replace the current content with the embedding code:
		$('#cantaloupe-video-container-inner #main').css('background-image', 'none');
		$('#cantaloupe-video-container-inner #main').html('<div style="text-align: center; padding-top: 8px;">' + entry.embed + '</div><p class="direct_link">Direct link: <input type="text" value="http://' + location.host + location.pathname + '?video_id=' + video_id + '" class="direct_link" /></p>');
	});

			
	return true;
}

function video_unbinds(){
	$('li.video_link a.play_link').unbind();
	$('.button h3 a').unbind();
}

function tab_binds(){
	$('#video_tabs li').click(function(){
		// Hide all tab content:
		$('.tab-content').hide();

		// Remove the active class from all tabs:
		$('#video_tabs li').removeClass('active');

		// Add the active class to the clicked tab:
		$(this).addClass('active');

		// Display the proper tab content:
		switch( $(this).attr('id') ){
			case 'testimonialstab':
				$('#testimonials').show();
				break;
			case 'scienceofairtab':
				$('#scienceofair').show();
				break;
			default:
				$('#productuse').show();
				break;
		}

		return false;
	});

	return true;
}

function video_binds(){
	video_unbinds();

	$('li.video_link a.play_link').click(function(){
		if( ! $(this).parent().parent().hasClass('playing') ){
			// Set all video links to their default state:
			$('li.video_link').removeClass('playing');
			$('li.video_link div.video_thumbnail a.play_link').html('Watch');

			// Set the applicable video as playing:
			$(this).parent().parent().addClass('playing');
			$(this).parent().parent().children('div.video_thumbnail').children('a.play_link').html('Playing');

			// Play the video:
			play_video($(this).parent().parent().children('.video_id').html());
		}

		// Return false so links aren't followed:
		return false;
	});

	$(".button h3 a:not('.down')").click(function(){
		var section_id = $(this).parent().parent().parent().attr('id');

		// Hide all sub navigations:
		$('#' + section_id + ' .button ul').slideUp();

		// Set all buttons to the inactive state:
		$('#' + section_id + ' .button.active').removeClass('active');
		$('#' + section_id + ' .button a.down img:first').attr('src', '/img/misc/arrow-up.gif');
		$('#' + section_id + ' .button a.down').removeClass('down').addClass('up');

		// Set the applicable button to the active state:
		$(this).parent().parent().addClass('active');
		$(this).children('img').attr('src', '/img/misc/arrow-down.gif');
		$(this).removeClass('up').addClass('down');

		// Show the applicable sub navigation:
		$(this).parent().parent().children('ul').removeClass('hidden');
		$(this).parent().parent().children('ul').slideDown();

		// Re-run the video binds to reset things:
		video_binds();

		// Return false so links aren't followed:
		return false;
	});

	$(".button h3 a:not('.up')").click(function(){
		return false;
	});
/*
	$('.button h3 a.down').click(function(){
		// Hide all sub navigations:
		$('.button ul').slideUp();

		// Set all buttons to the inactive state:
		$('.button.active').removeClass('active');
		$('.button a.down img:first').attr('src', '/img/misc/arrow-up.gif');
		$('.button a.down').removeClass('down').addClass('up');

		// Re-run the video binds to reset things:
		video_binds();

		// Return false so links aren't followed:
		return false;
	});
*/
}

