function external_init() {
	$('.external').click(function() {
		window.open($(this).attr('href'));
		return false;
	});
}

function print_init() {
	$('.print').click(function() {
		window.print();
		return false;
	});
}

function get_youtube_id(a_selector) {
	return $(a_selector).attr('href').split('v=')[1];
}

function get_youtube_embed(youtube_id,width,height) {
	return '<object width="' + width + '" height="' + height + '"><param name="movie" value="http://www.youtube.com/v/' + youtube_id + '?fs=1&amp;hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="wmode" value="transparent"><embed src="http://www.youtube.com/v/' + youtube_id + '?fs=1&amp;hl=en_US" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" wmode="transparent" width="' + width + '" height="' + height + '"></embed></object>';
}

function cancel_link(selector) {
	$(selector).unbind('click');
	$(selector).click(function() { return false; });
}

function video_init() {
	$('.sofa-widget-preview .video-trigger').click(function() {/*Sidebar*/
		cancel_link(this);
		/*$('.sofa-widget-preview .video').animate({
			borderWidth:'4px',
			left:'-444px',
			top:'30px',
			width:'480px',
			height:'330px'
		},700,function() {
			$(this).html(get_youtube_embed(get_youtube_id($(this).children('a')),480,330));
		});*/
		$('.sofa-widget-preview .video').html(get_youtube_embed(get_youtube_id(this),230,158));
		return false;
	});
	$('.entry-content .video-trigger').click(function() {/*Keynotes*/
		cancel_link(this);
		var $thumb = $('.entry-content .video[href=' + $(this).attr('href') + ']');
		$thumb.replaceWith(get_youtube_embed(get_youtube_id(this),230,158));
		return false;
	});
	$('.video-list a').click(function() {/*Videos*/
		var is_first = ($('#video-holder').length === 0);
		if (is_first) {
			$('.entry-content h1').after('<div id="video-holder"></div>');
		}
		window.location.hash = 'video-title';
		$('#video-holder').html(get_youtube_embed(get_youtube_id(this),480,385)).slideDown(1000);
		return false;
	});
}

$(function() {
	external_init();
	print_init();
	video_init();
});
