/**
 * @name okapion.okapionSauce
 * @desc Searches through the element's text content and replaces given text instances with
 *       spanned alternatives. These spans have a (given) classname. You can colorize the
 *       words if you like.
 *
 **/
$.fn.okapionSauce = function(options){

    var defaults = {
        words: ['Okapion ', ' Okapion'],
        className: 'sauce'
    }
    
    var options = $.extend(defaults, options);
    
    return this.each(function(){
        $(this).html($(this).html().replace(new RegExp('(' + options.words.join('|') + ')', 'gi'), '<span class="' + options.className + '">$1</span>'));
    });
};

/**
 * Initial tryout for a header type gallery
 */
(function($){

    $.fn.okapionSlider = function(options){
    
        // To avoid scope issues, use 'base' instead of 'this'
        // to reference this class from internal events and functions.
        var base = this;
        
        // The default options
        base.defaults = {
            page: 0,
            speed: 10000,
            fadeSpeed: 800,
            autoPlay: true
        }
        
        // Concat the options
        base.options = $.extend(base.defaults, options);
        
        // Define our needed settings
        base.$el = null; // The parent element all else is contained in
        base.$currentSlide = null; // The currently viewed slide
        base.$nextSlide = null; // The slide we are transitioning to 
        base.slides = null; // All slides
        base.interval = null; // The autoPlay interval
        // Define our needed functions
        base.setSlide = function(index){
        
            // Only allow 1 at a time
            if (base.$nextSlide) 
                return;
            
            // Store the slide we need to go to
            base.$nextSlide = $(base.slides[index]);
            
            // Is there currently a slide active?
            if (base.$currentSlide) {
                base.deactivateSlide(base.$currentSlide);
                base.$currentSlide = null;
                return;
            }
            
            // Stop interval
            base.stopInterval();
            
            // Activate the slide we want
            base.check();
        }
        
        base.check = function(){
            if (base.$nextSlide) {
                base.$el.stop().animate({
                    backgroundColor: base.$nextSlide.attr("background")
                }, 400, function(){
                    base.activateSlide(base.$nextSlide);
                });
            }
        }
        
        base.activateSlide = function($slide){
            $slide.stop().fadeIn(base.options.fadeSpeed, function(){
                base.$currentSlide = $slide;
                base.$nextSlide = null;
                base.$el.trigger("slideChangeComplete", [$(this), base.getSlideIndex($(this).get(0))]);
                base.resetInterval();
            });
        }
        
        base.deactivateSlide = function($slide){
            base.$el.trigger("slideChangeStart", [$slide, base.getSlideIndex($slide.get(0))]);
            $slide.stop().fadeOut(base.options.fadeSpeed, function(){
                base.check();
            });
        }
        
        base.next = function($slide){
            if (base.$currentSlide) {
                var index = base.getSlideIndex(base.$currentSlide.get(0));
                if (index > -1) {
                    index++;
                    if (index > base.slides.length - 1) 
                        index = 0;
                    base.setSlide(index);
                }
            }
        }
        
        base.getSlideIndex = function(slide){
            return jQuery.inArray(slide, base.slides);
        }
        
        base.stopInterval = function(){
            if (base.options.autoPlay) 
                clearInterval(base.interval);
        }
        
        base.resetInterval = function(){
            if (base.options.autoPlay) {
                clearInterval(base.interval);
                base.interval = setInterval(base.next, base.options.speed);
            }
        }
        
        // Loop through all found elements
        return this.each(function(){
            base.$el = $(this);
            
            // Add a reverse reference to the DOM object
            base.$el.data("OkapionSlider", base);
            
            // Get a reference to all inner slides
            base.slides = base.$el.find("aside");
            
            // Initially, hide them all in case our css didn't
            base.slides.each(function(index, element){
                $(element).hide();
            });
            
            // Default to first slide
            base.setSlide(base.options.page);
            
            // Set timer for autoPlay
            if (base.options.autoPlay) 
                base.resetInterval();
        });
    }
})(jQuery);

