$(document).ready(function(){
	//External links.
	$('a[rel="external"]').click( function() {
		window.open( $(this).attr('href') );
		return false;
	});	
	
	// Insert the sophie image if on the homepage
	if ($('#div_twitter_home').size() > 0) {
		$('#div_content').append('<div id="sophie_image"></div>');
		// Here we work out the height of sophie dependant on the size of the content.
		var contentHeight = $('#div_content').height() - 165;
		$("#sophie_image").css('height', contentHeight + 'px');	
		
		// On the homepage set the fading tweets going.
		revolveTweets();
	};	
	
	//Make carousel images work 
	jQuery('#mycarousel').jcarousel({
		scroll: 4
    });
    
	
	shareLink($('.sharelink'));
	
});

function shareLink(button){
	button.mouseover(function(e){
		var link = 'http://' + location.host + '/' + $(this).attr('rel');
		var sharehtml = '<span id="share_links"><a href="http://www.facebook.com/share.php?u=' + link + '" rel="external">Facebook</a> / <a href="http://twitter.com/home?status=Currently reading ' + link + '" rel="external">Twitter</a> / <a href="http://digg.com/submit?phase=2&url=' + link + '" rel="external">Digg</a> / <a href="http://del.icio.us/post?url=' + link + '" rel="external">Delicious</a><br /></span>';
		
		if ($('#share_links').size() < 1) {
			//No share links are already on screen so lets show em.
			$('#div_content').append(sharehtml);
			$('#share_links').css('top' , e.pageY - 30);
			$('#share_links').css('left' , e.pageX - 85);
		} else {
			// Share link on screen lets fade them out and then show the others.
			$('#share_links').remove();
			$('#div_content').append(sharehtml);
			$('#share_links').css('top' , e.pageY - 30);
			$('#share_links').css('left' , e.pageX - 85);
		}
		
	}).mouseout(function(){
		$('#share_links').animate({opacity: 1.0}, 1500).fadeOut('slow', function() {
			$('#share_links').remove();
		});
    });
}

// Function to revolve throught a list of tweets.
function revolveTweets(){
	// First fade out the active_tweet div.
	$('.active_tweet').show();
	$('.active_tweet').animate({opacity: 1.0}, 5000).fadeOut('slow', function() {
		$(this).removeClass('active_tweet');		
		// Now check if there is a next div. If there isn't add the active_tweet class to the first div in the list.
		if ($(this).next('div').size() > 0) {
			$(this).next('div').addClass('active_tweet');
		} else {
			$('.div_tweet:first').addClass('active_tweet');
		}		
		revolveTweets();
	});
};