;(function($) {
    $.fn.bannerSlideshow = function(settings) {
        var config = {
            slideEase: $.easing ? "easeOutExpo" : "swing",
            slideSpeed: 1000,
            captionEase: $.easing ? "easeOutExpo" : "swing",
            activeArrowHideEase: $.easing ? "swing" : "swing",
            activeArrowRevealEase: $.easing ? "swing" : "swing",
            maskOpacity: 0.73
        };
 
        if (settings) $.extend(config, settings);
 
        return this.each(function() {
        	var slideshow = $(this);
            slideshow.addClass("banner-slideshow-jquery-building");
            var slides = $("ul.banner-slideshow-slides li", slideshow);
            var numSlides = slides.length;
            if (!numSlides) {
            	$(this).removeClass("banner-slideshow-jquery-building");
            	return;
            }
            //Load up the thumbs and main image.  When they're ready
            //reveal them.
            var firstSlide = slides.filter(":first");
            var mainImage = $("img.full", firstSlide);
            var thumbs = $("img.thumb", slides);
            var totalToComplete = numSlides + 1;
            var numCompleted = 0;
            
            $(mainImage).add(thumbs)
            	.one("load", function() {
            		if (++numCompleted == totalToComplete) {
            			$(document).ready(function() {
            				StartIntro(slideshow, slides, firstSlide);
            			});
            		}
            	})
            	.each(function(){
            		if(this.complete || (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6))
            			$(this).trigger("load");
            	});
        });
        
        function StartIntro(slideshow, slides, firstSlide) {
        	var mainImageURL = $("img.full", firstSlide).attr("src");
        	var activeArrow = $(".banner-slideshow-slide-arrow", firstSlide);
        	var slideThumbs = $("img.thumb", slides);
        	var mainSlide = $(".banner-slideshow-current", slideshow);
        	var caption = $(".banner-slideshow-caption", slideshow);
        	
        	mainSlide
        		.hide()
        		.css({
        			backgroundImage: "url('" + mainImageURL + "')",
        			top: 0,
        			left: 0
        		});
        		
        	caption
        		.css({
        			bottom: caption.outerHeight() * -1
        		});

        	activeArrow
        		.hide()
        		.css({
        			left: 0
        		});
        	
        	slideThumbs
        		.hide()
        		.css({
        			top: 0,
        			left: 0
        		});
        	
        	slideshow.addClass("banner-slideshow-jquery-intro").removeClass("banner-slideshow-jquery-building");
        	
        	var idx = 0;
        	
        	window.setTimeout(function() {
        		mainSlide.fadeIn("slow", function() { slideshow.removeClass("banner-slideshow-jquery-intro"); });
        		if (!($.browser.msie && $.browser.version == 6)) {
        			activeArrow.fadeIn("normal");
        		} else {
        			activeArrow.delay(100).show();
        		}
        		slides.each(function(idx) {
        			var img = $("img.thumb", this);
        			window.setTimeout(function() {
        				var opacity = img.css("opacity");
        				var fadeTo = idx ? 1 : 0.2;
        				
        				img.fadeTo(
        					"normal",
        					fadeTo,
        					function() {
        						var li = $(this).closest("li");
        						li
        							.hover(
        								function() {
        									if ($(this).hasClass("active")) return;
        									if ($(this).data("switching")) return;
        									$("img.thumb", this).stop().fadeTo("fast", 0.5);
        								},
        								function() {
        									if ($(this).hasClass("active")) return;
        									if ($(this).data("switching")) return;
        									$("img.thumb", this).stop().fadeTo("fast", 1);
        								}
        							)
        							.click(
        								function() {
        									var li = $(this).closest("li");
        									if (li.hasClass("active")) return;
        									
        									var activeLi = li.closest("ul").children("li.active");
        									var activeImg = $("img.thumb", activeLi);
        									
        									var img = $("img.thumb", li);
        									var largeImgURL = $("img.full", li).attr("src");
        									var currentImg = $(".banner-slideshow-current", slideshow);
        									var next = $(".banner-slideshow-next", slideshow);
        									var nextCaption = $("h3, p", li).clone(true);
        									
        									next.css("background-image", "url('" + largeImgURL + "')");
        									
        									li.data("switching", true);
        									
        									li
        										.stop()
        										.animate( { backgroundColor: "black" }, 250, "swing" );
        									activeLi
    											.stop()
    											.animate( { backgroundColor: "white" }, 250, "swing" );
        									img
    											.stop()
    											.animate( { opacity: 0.2 }, 200, "swing" );
        									activeImg
												.stop()
												.animate( { opacity: 1 }, 200, "swing" );
        									$(".banner-slideshow-slide-arrow", li)
        										.stop()
        										.animate( { left: 0 }, 250, config.activeArrowRevealEase, function() { li.addClass("active"); } );
        									$(".banner-slideshow-slide-arrow", activeLi)
        										.stop()
        										.animate( { left: -16 }, 275, config.activeArrowHideEase, function() { activeLi.removeClass("active"); } );
        									currentImg
        										.stop(true)
        										.fadeOut( 300, function() { $(this).css("background-image", "url('" + largeImgURL + "')").show(); } );
        									$(".banner-slideshow-caption-ie6", slideshow).remove();
        									caption
        										.stop(true)
        										.css("bottom", caption.outerHeight() * -1)
        										.children()
        											.remove()
        										.end()
        										.append(nextCaption)
        										.animate(
        											{ bottom: -1 },
        											300,
        											config.captionEase,
        											function() {
        												li.removeData("switching");
        												// Ugh..
        												FixCaption(slideshow, $(this));
        											}
        										);
        								}
        							);

        					}
        				);
        				
        			}, idx * 50);
        		});
        		firstSlide.animate(
        			{
        				backgroundColor: 'black'
        			},
        			100,
        			"linear"
        		);
        		caption.animate(
        			{
        				bottom: -1
        			},
        			"slow",
        			config.captionEase,
        			function() {
        				FixCaption(slideshow, $(this));
        			}
        		);
        	}, 250);
        }
        function FixCaption(slideshow, caption) {
        	if (!($.browser.msie && $.browser.version == 6)) return;
			var height = caption.height();
			var tempCaption = $('<div class="banner-slideshow-caption-ie6" />');
			tempCaption.appendTo(slideshow);
			caption.css("height", height);
			var content = $("h3, p", caption);
			content.appendTo(tempCaption);
        }
    };
})(jQuery);
