(function($){
    $.fn.slideshow = function(options) {
        var defaults = {
			imagePath:      '',
			imageType:		'.jpg',
			hoverClass:		'hover',
			nextId:			'nextLink',
			prevId:			'prevLink',
			hoverImage:		'hoverImage',
			animationSpeed: 500
        };

		var opts = $.extend(defaults, options),
			$lightbox,
			$theImage,
			$hoverNav,
			currentIndex = 0,
			$this = $(this);
			
		opts.imagePath = $this.find('a').attr('href').split('.')[0].substr(0, $this.find('a').attr('href').length-5); //this is shit - rewrite whole plugin using xml

		$this.find('a').each(function(i){
			$(this).hover(function(){
				$(this).prepend('<div class="' + opts.hoverImage + '"></div>');
			},
			function(){
				$(this).find('div').remove();
			});

			$(this).click(function(e){
				e.preventDefault();
				e.stopPropagation();
				
				$lightbox = $(this).lightbox({
					contents:	'<img src=""/>' +
								'<div class="hoverNav">' +
									'<div id="' + opts.prevId + '" href="#"/>' +
									'<div id="' + opts.nextId + '" href="#"/>' +
								'</div>'
				});
				$hoverNav = $lightbox.find('div.hoverNav');
				$theImage = $lightbox.find("img");
				displayImage(i);

				$hoverNav.find('div#' + opts.prevId).click(function(e){
					e.stopPropagation();
					if(currentIndex > 0){
						displayImage(currentIndex-1);
					}
				});
				$hoverNav.find('div#' + opts.nextId).click(function(e){
					e.stopPropagation();
					if(currentIndex != $this.find("a").size()-1){
						displayImage(currentIndex+1);
					}
				});

				$hoverNav.find('div#' + opts.prevId).hover(function(){
					if(currentIndex > 0){
						$(this).addClass(opts.hoverClass);
					}
				},
				function(){
					$(this).removeClass(opts.hoverClass);
				});

				$hoverNav.find('div#' + opts.nextId).hover(function(){
					if(currentIndex != $this.find("a").size()-1){
						$(this).addClass(opts.hoverClass);
					}
				},
				function(){
					$(this).removeClass(opts.hoverClass);
				});
		   });
		});

        function displayImage(index){
            var loadCount = 0;

            currentIndex = index;
            $theImage.hide();
            $theImage.attr('src', opts.imagePath + index + opts.imageType);
            $lightbox.prepend('<div class="loading"><img src="/img/loading.gif"/></div>');
            var $loadImage = $lightbox.find('div.loading').css({marginTop: $lightbox.height()/2 + 'px'});

            $theImage.load(function(e){
                if(loadCount == 0){
                    $loadImage.remove();
                    var imageHeight = $theImage.height(),
						imageWidth = $theImage.width();

                    if($lightbox.height() != imageHeight){
                        $lightbox.animate({
                            height:imageHeight,
                            top:($(window).height()-imageHeight)/2}, opts.animationSpeed
                        );
                    }
                    $lightbox.animate({
                        width:imageWidth,
                        left: ($(window).width()-imageWidth)/2}, opts.animationSpeed,
                        function(){
                            $theImage.fadeIn("slow");
                        }
                    );
                    $hoverNav.css({
                        height:imageHeight + 'px',
                        width:imageWidth + 'px'
                    });
                }
                loadCount++;
            }); 
        }
    }
})(jQuery)
