$(document).ready( function () {
	/**** Bookmark Page Popup ****/
	$.bookmark.addSite('twitthis','Twitter', 45, 'http://twitthis.com/twit?url={u}');
	$.bookmark.addSite('print','Print','http://www.otago.ac.nz/prodcons/fragments/otg_assets/javascript/jquery.bookmark/printer.png','javascript:window.print();');

	$('#basicBookmark').bookmark({
		sites 		: ['print','delicious','digg','facebook','google','windows','myspace','stumbleupon','twitthis'],
		addEmail	: true,
		addFavorite	: true,
		icons		: 'http://www.otago.ac.nz/prodcons/fragments/otg_assets/javascript/jquery.bookmark/bookmarks.png',
		favoriteIcon: 'http://www.otago.ac.nz/prodcons/fragments/otg_assets/javascript/jquery.bookmark/favourites.gif',
		compact		: false,
		popup		: true,
		popupText	: '<img src="http://www.otago.ac.nz/prodcons/fragments/otg_assets/javascript/jquery.bookmark/share.jpg" alt="Share this page..." />'
	});
	/**** End Bookmark Popup ****/
	
	/**** Search Popup ****/
	var url = 'http://search.otago.ac.nz/search?site=otago&client=otago&output=xml_no_dtd&proxystylesheet=otago&q={term}', selectionImage;
	
	$("#content").bind('textselect', function(e) {
		var selection = e.text;
		if(selection && (selection = new String(selection).replace(/^\s+|\s+$/g,''))) {
			if(!selectionImage) {
				selectionImage = $('<a>').attr({
					href: url, 
					title: 'Click here to learn more about this term',
					id: 'selection-image'
				}).hide();
				$(document.body).append(selectionImage);
			}
			selectionImage.attr('href',url.replace('{term}',encodeURIComponent(selection))).css({
				top: e.pageY - 30,	//offsets
				left: e.pageX - 13 //offsets
			}).fadeIn();
		}
	});

	$("#content").bind('textunselect', function() {
		if(selectionImage) { selectionImage.hide(); }
	});
	/**** End Search Popup ****/
	
	/**** FancyBox Effect ****/
	$("a.player_480").fancybox({
		'overlayShow'			: true,
		'overlayOpacity'		: 0.9,
		'padding'				: 10,
		'hideOnContentClick'	: false,
		'frameWidth'			: 480,
		'frameHeight'			: 270,
		'callbackOnClose'		: function() {
			$("#fancy_content").empty();
			try {
				var myVideo = document.getElementsByTagName('video')[0];
				myVideo.pause();
			} catch (e) {
				notsupported = true;
			}
		}
	});

	$("a.player").fancybox({
		'overlayShow'			: true,
		'overlayOpacity'		: 0.9,
		'padding'				: 10,
		'hideOnContentClick'	: false,
		'frameWidth'			: 320,
		'frameHeight'			: 176,
		'callbackOnClose'		: function() {
			$("#fancy_content").empty();
			try {
				var myVideo = document.getElementsByTagName('video')[0];
				myVideo.pause();
			} catch (e) {
				notsupported = true;
			}
		}
	});
	/**** End FancyBox Effect ****/
});