$.fn.slideshow = function(options)
{
	var defaults = {speed: 800};  	
	var images = [];
	var index = 0;
	var width;
	
	var options = $.extend(defaults, options);
	
	return this.each(function()
	{
		var $bullet, $currentImg, $nextImg, $prevImg;
		var $s = $(this);
		images = $('.slideshowContainer div img', $s);
		width = $s.width();

		$(images[0]).load(function()
		{
			$('.slideshowContainer', $s).height($(this).height());
			console.log('set height to: ' + $(this).height());
		});
	
		images.each(function(i)
		{
			$bullet = $('<li><a href="#">' + (i + 1) + '</a></li>').attr('title', $(this).attr('alt'));
			$('.slideshowNav', $s).append($bullet);
			
			$('a', $bullet).click(function(e)
			{
				e.preventDefault();
				$('.slideshowNav a', $s).removeClass('active');
				index = $('.slideshowNav li', $s).index($(this).addClass('active').parent().get(0));
				$('.slideshowContainer div', $s).animate({left: index * -width}, options.speed);
			});
		});
		
		$('.slideshowNav', $s).width(images.length * 16).children('li:first-child').children(0).addClass('active');
		$('.slideshowContainer div', $s).width(images.length * width);
		$('.slideshowTotal', $s).text(images.length);
		
		if(images.length < 2)
		{
			$('.slideshowNav', $s).hide();
		}
		
		$('.slideshowContainer', $s).click(function(e)
		{
			e.preventDefault();
			index++;
			index = (index < images.length) ? index : 0;
			$('.slideshowContainer div', $s).animate({left: index * -width}, options.speed);
			$('.slideshowNav a', $s).removeClass('active');
			$('.slideshowNav li:nth-child(' + (index + 1) + ') a', $s).addClass('active');
		});
	});
}

function align(e)
{
	$('#next, #prev').css({top: Math.round(($(window).height()) * 0.5) - 23});
	var offset = Math.round(($(document).width() - $('#wrapper').width()) * 0.25) - 14;
	$('#next').css({right: offset});
	$('#prev').css({left: offset});
}

$(document).ready(function()
{
	jQuery.easing.def = 'easeOutExpo';
	
	$(document).ready(function()
	{
		$('#next, #prev').fadeIn(500);
	});	

	$('a:not([href^=http://www.oscaralexander.com/])').attr('target', '_blank');
	$('.slideshow').slideshow();
	$(window).resize(align);
	align();
});
