
// Start up Shadowbox.
var Shadowbox = Shadowbox || '';
if (Shadowbox)
{
	Shadowbox.init({
		players: ['img', 'swf']
	});
}

if (typeof google != 'undefined' && google.load)
{
	// Get the Google Ajax Feeds API.
	google.load("feeds", "1"); // For Google Ajax Feed API documentation read http://code.google.com/apis/ajaxfeeds/documentation/
}
$(function()
{
	// Indicate whether jQuery is on or not.
	$('body').removeClass('nj').addClass('j');
	
	// Get the Library Blog Feed via the Google Ajax Feed API, wrap in some friendly markup and append to a placeholder.
	if ($('#feed').length && typeof google != 'undefined' && google.load) // Only contact Google if the placeholder exists.
	{
		function initialize() // For Google Ajax Feed API documentation read http://code.google.com/apis/ajaxfeeds/documentation/
		{
			var feed = new google.feeds.Feed("http://tcld.wordpress.com/feed/");
			feed.setNumEntries(20);
			feed.load(function(result)
			{
				if (!result.error)
				{
					var months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
					var html = '<ul>';
					for (var i = 0; i < result.feed.entries.length; i++)
					{
						var entry = result.feed.entries[i];
						var pdate = new Date(entry.publishedDate);
						html += '<li>';
						if (entry.link != '')
						{
							html += '<a href="'+entry.link+'">'+entry.title+'</a>';
						}
						else
						{
							html += entry.title;
						}
						html += ' <br />' + months[pdate.getMonth()] + ' ' + pdate.getDate() + ' ' + pdate.getFullYear() + '</li>';
					}
					html += '</ul>';
					$('#feed').html(html);
					$('#feed').before('<div class="tcd-carousel-control tcd-carousel-control-pgup tcd-carousel-resting"></div>');
					$('#feed').after('<div class="tcd-carousel-control tcd-carousel-control-pgdn"></div>');
					$('#feed-wrap').find('div.tcd-carousel-control')
					.bind('mouseenter', function(e)
					{
						$(this).closest('#feed-wrap').trigger('refocus');
					});
					$('#feed-wrap')
					.bind('click', function(e)
					{
						if ($(e.target).hasClass('tcd-carousel-control-pgup'))
						{
							$(this).trigger('nextup');
						}
						else if($(e.target).hasClass('tcd-carousel-control-pgdn'))
						{
							$(this).trigger('nextdown');
						}
					})
					.bind('refocus', function(e) // Determine the current item in view.
					{
						var feed = $(this).find('#feed');
						var items = $(feed).find('ul:first').find('li');
						var items_length = $(items).length;
						for (var i = 0; i < items_length; i++)
						{
							if ($(items).eq(i).offset().top - $(feed).offset().top >= 0)
							{
								$(feed).data('tcd-carousel.item', i);
								break;
							}
						}
					})
					.bind('nextup', function(e)
					{
						var new_scroll_top = 0; // The value to determine and then scroll to.
						var feed = $(this).find('#feed');
						var items = $(feed).find('ul:first').find('li');
						var items_length = $(items).length;
						var scroll_page_height = $(feed).height();
						var scroll_item = $(feed).data('tcd-carousel.item') || 0;
						scroll_item = (scroll_item < 1) ? 1 : scroll_item;
						// Determine which items can be shown.
						for (var i = 0; i < items_length - scroll_item; i++)
						{
							scroll_item = scroll_item - 1;
							if ($(feed).offset().top - $(items).eq(scroll_item).offset().top >= scroll_page_height)
							{
								scroll_item = scroll_item + 1;
								break;
							}
						}
						new_scroll_top = Math.floor($(items).eq(scroll_item).offset().top - $(feed).offset().top + $(feed).scrollTop() - (-1 + parseInt($(items).eq(scroll_item).css('marginTop'), 10)/2));
						$(feed).data('tcd-carousel.item', scroll_item);
						$(feed).stop().animate({scrollTop: new_scroll_top+'px'}, {duration: 500, queue: false});
					})
					.bind('nextdown', function(e)
					{
						var new_scroll_top = 0; // The value to determine and then scroll to.
						var feed = $(this).find('#feed');
						var items = $(feed).find('ul:first').find('li');
						var items_length = $(items).length;
						var scroll_page_height = $(feed).height();						
						var scroll_item = $(feed).data('tcd-carousel.item') || 0;
						var old_scroll_item = scroll_item;
						// Determine which is the next item. It's the first one with its end below the fold.
						for (var i = 0; i < items_length - scroll_item; i++)
						{
							scroll_item = scroll_item + 1;
							if (0 < ($(items).eq(scroll_item).offset().top + $(items).eq(scroll_item).height() - $(feed).offset().top - scroll_page_height))
							{
								break;
							}
						}
						scroll_item = (old_scroll_item == scroll_item) ? scroll_item + 1 : scroll_item;
						scroll_item = (scroll_item > items_length - 1) ? items_length - 1 : scroll_item;
						new_scroll_top = Math.floor($(items).eq(scroll_item).offset().top - $(feed).offset().top + $(feed).scrollTop() - (-1 + parseInt($(items).eq(scroll_item).css('marginTop'), 10)/2));
						$(feed).data('tcd-carousel.item', scroll_item);
						$(feed).stop().animate({scrollTop: new_scroll_top+'px'}, {duration: 500, queue: false});
					});
				}
			});
		}
		google.setOnLoadCallback(initialize);
	}
	
	// Javascript/jQuery for all pages.
	$('div.h div.tabs').accessibleTabs({
		tabhead:'h3',
		fx:"show",
		fxspeed: 0
	});
	// Javascript/jQuery for all pages.
	$('div.c div.tabs').accessibleTabs({
		tabhead:'h2',
		fx:"show",
		fxspeed: 0
	});
	if ($.fn.link_thumbnailer)
	{
		$('.collection-images ul').link_thumbnailer();
	}

	$('form.print').submit(function()
	{
		print(document);
	});
	$('form.popular-links').submit(function()
	{
		var list = $(this).find('select')[0];
		location.href = list.options[list.selectedIndex].value;
		return false;
	});
	
	$('#slidebottom button').click(function()
	{
		$(this).next().slideToggle();
	});
});




