(function($) {  
    $.fn.flashPanel = function(options) {
        
        // Set some default options
        var defaults = {
            height: 250,
            duration: 250,
            interval: 2000,
            controls: false,
            tabs: false
        };  
        
        // Extend function with options
        var options = $.extend(defaults,options);
        
        // Apply function to all matched objects
        return this.each(function() {
        
            // Set timer
            var timer;
            var setTimer = function() {
                if(timer != null) { clearInterval(timer); }
                timer = setInterval(function() { nextSlide(); },options.interval);
            }
            if(options.interval > 1) { setTimer(); }
            
            // Get tabPanel object and style
            var tabPanel = $(this);
            var tabPanelWidth = tabPanel.width();
            
            // Create slide wrapper to hide edges of slide container
            var slideWrapper = $('.slides', tabPanel);
            slideWrapper.css({
                'position':'relative',
                'top':0,
                'left':0,
                'width':tabPanelWidth+'px',
                'height':tabPanel.height()+'px',
                'overflow':'hidden'
            });
            
            
            // Get slide objects and style
            var slides = slideWrapper.children('li');
            slides.each(function(i) {
                var xPos = i*tabPanelWidth;
                $(this).css({
                    'position':'absolute',
                    'top':0,
                    'left':0,
                    'width':tabPanelWidth+'px',
                    'height':options.height+'px'
                });
            });
            slides.hide();
            slideWrapper.children('li:first-child').show();
            
            var currentSlide = 0;
            
                        
            // Function to move slides
            var moveTo = function(slideNumber) {
                var slideSelected = slideWrapper.children('li:nth-child('+(slideNumber+1)+')');
                if (slideSelected.is(':not(:visible)') ) {
                    slides.fadeOut();
                    slideSelected.fadeIn();
                    if (options.tabs == true) {
                        tabContainer.children('li').removeClass('selected');                
                        var button = tabContainer.children('li:nth-child('+(slideNumber+1)+')');
                        button.addClass('selected');
                    }
                    currentSlide = slideNumber;
                    setTimer();
                }
            }
            
            // Check slideshow length
            if (slides.length == 1) {
                options.tabs = false;
                options.controls = false;
            }
            
            // Create tab control functionality
            if (options.tabs == true) {
                var tabContainer = $('<ul>');
                tabContainer.css({
                    'position':'absolute',
                    'left':0,
                    'bottom':0,
                    'margin':0,
                    'padding':0,
                    'list-style':'none'
                }).addClass('tabs');
                for (var i=0; i < slides.length; i++) {
                    var j = i + 1;
                    var tabButton = $('<li>');
                    tabButton.html('<a href="#" title="Slide'+j+'">'+j+'</a>');
                    tabButton.addClass('item'+j);
                    tabButton.appendTo(tabContainer);
                }
                tabContainer.children('li:first-child').addClass('selected');
                tabContainer.appendTo(tabPanel);
                
                var tabItems = tabContainer.children('li');
                tabItems.each(function(i) {
                    $(this).children('a').click(function() {
                        moveTo(i);
                        return false;
                    });
                });
            }
            
            // Create Controls
            if (options.controls == true) {
                var slideControls = $('<ul>');
                slideControls.addClass('controls');
                slideControls.html('<li class="next"><a href="#" title="Next">Next</a></li><li class="previous"><a href="#" title="Previous">Previous</a></li>');
                slideControls.appendTo(tabPanel);
                
                slideControls.children('.previous').children('a').click(function() {
                    previousSlide();
                    return false;
                });
                slideControls.children('.next').children('a').click(function() {
                    nextSlide();
                    return false;
                });
            }
            
            // Previous Slide
            var previousSlide = function() {
                var targetSlideNumber = ((currentSlide) == 0) ? slides.length-1 : currentSlide-1;
                moveTo(targetSlideNumber);
            }
            
            // Next slide
            var nextSlide = function() {
                var targetSlideNumber = ((currentSlide+1) == slides.length) ? 0 : currentSlide+1;
                moveTo(targetSlideNumber);                    
            }
            
            // Make sure slides and tabs are visible again
            slideWrapper.show();
            if (options.tabs == true) { tabContainer.show(); }
        });
        
    };
})(jQuery);
