(function($) {  
    $.fn.flashPanel = function(options) {
        
        // Set some default options
        var defaults = {
            height: 250,
            width: '100%',
            duration: 500,
            interval: 5000
        };  
        
        // Extend function with options
        var options = $.extend(defaults,options);
        
        // Apply function to all matched objects
        return this.each(function() {
            
            // Get promoPanel object and style
            var flashPanel = $(this);
            flashPanel.css({
                position: 'relative',
                margin: 0,
                padding: 0,
                width: options.width,
                height: options.height+'px',
                overflow: 'hidden'
            });
            
            // Get slides and style
            var slides = flashPanel.children('li');
            slides.css({
                position: 'absolute',
                top: 0,
                left: 0,
                width: '100%',
                height: '100%',
                overflow: 'hidden'
            });
            slides.hide();
            
            // Style contents of slides
            slides.children('.slide-image').css({
                position: 'absolute',
                top: 0,
                left: 0,
                border: 'none',
                margin: 0,
                padding: 0
            });
            slides.children('.slide-link').css({
                display: 'block',
                width: '100%',
                height: '100%',
                position: 'absolute',
                top: 0,
                left: 0,
                'text-indent': '-9999px'
            });
            slides.children('.slide-text').css({ position: 'absolute' });
            
            var totalSlides = slides.length;
            var currentSlide = flashPanel.children('li:first-child');
            currentSlide.show();
            
            // Set timer
            var timer;
            var setTimer = function() {
                if(timer != null) { clearInterval(timer); }
                timer = setInterval(function() { nextSlide(); },options.interval);
            }
            if(options.interval > 1) { setTimer(); }
            
            // Move to next slide
            var nextSlide = function() {
                if(totalSlides > 1) {
                    var outgoing = currentSlide;
                    var incoming = flashPanel.children('li:nth-child(2)');
                    incoming.fadeIn(options.duration,function() {
                        outgoing.hide();
                        outgoing.appendTo(flashPanel);
                    });
                    currentSlide = incoming;
                }
            }
            
        });
        
    };
})(jQuery);
