var banners, banner_dots, sidebar_quotes, lucky_quote;
jQuery.noConflict();
jQuery(document).ready(function(){ 
// Quotes
	sidebar_quotes = jQuery('.sidebar-quote');
	lucky_quote = sidebar_quotes.eq(Math.floor(Math.random() * sidebar_quotes.length));
	lucky_quote.show();
	document.getElementById('outbound-links').style.top = lucky_quote.height() + 20 + 'px';

	// sidebar_quotes.bind('click', function(e){
	// 	var next_quote = jQuery(this).next('.sidebar-quote').length > 0 ? jQuery(this).next('.sidebar-quote') : sidebar_quotes.first();
	// 	jQuery(this).fadeOut();
	// 	next_quote.fadeIn();
	// });

// Banners
	banners = jQuery('#banners li');
	banner_dots = jQuery('#banner-dots li');

	banners.bind('cycle', function(e){
		jQuery(this).fadeOut(800);

		var next_banner = (jQuery(this).next('li').length > 0) ? jQuery(this).next('li') : jQuery(this).siblings('li').first();
		next_banner.fadeIn(800);

		banner_dots.eq( banners.index(jQuery(this)) ).removeClass('active');
		banner_dots.eq( banners.index(next_banner) ).addClass('active');

	});
	
	banner_dots.bind('click', function(e){
		e.preventDefault();
		clearInterval(banner_cycle);

		if (!jQuery(this).hasClass('active'))
		{
			jQuery(this).siblings('.active').removeClass('active');
			jQuery(this).addClass('active');

			var target = banners.eq( banner_dots.index(jQuery(this)) );
			banners.filter(':visible').fadeOut(400);
			target.fadeIn(400);
		}
	});

	banners.hide(); // Hide all banners via JS
	banners.first().show(); // Show first one only
	banner_dots.first().addClass('active'); // Activate the first dot

	// Only activate cycling if we have more than one banner
	if (banners.length > 1)
		banner_cycle = setInterval(function(){banners.filter(':visible').trigger('cycle');}, 4000);

// Collapsable lists
	jQuery('.collapsable dd').hide();
	jQuery('.collapsable dt').bind('click', function(e){
		var collapsable_target = jQuery(this).next('dd');
		if (collapsable_target.is(':visible'))
			collapsable_target.slideUp();
		else
			collapsable_target.slideDown();
	});

// Add latest newsletter link to Latest News box
	var recent_posts_box = document.getElementById('recent-posts-3');
	if (recent_posts_box)
	{
		var newsletter_link = document.createElement('A');
		newsletter_link.setAttribute('href', '/newsletter/latest');
		newsletter_link.setAttribute('id', 'latest-newsletter');
		newsletter_link.innerHTML = 'Download latest newsletter';
		recent_posts_box.appendChild(newsletter_link);
	}

});

