﻿(function($) {
    /* Create jQuery Plugin */
    $.fn.slideShow = function(options) {
        return this.each(function() {
            new Slideshow(this, options);
        });
    }
    /*remove whitespace from inline-block nodes */
    jQuery.fn.cleanWhitespace = function() {
        textNodes = this.contents().filter(function() {
            return (this.nodeType == 3 && !/\S/.test(this.nodeValue));
        })
        .remove();
    }

    $.slideShow = {};
    /* default options */
    $.slideShow.defaultOptions = {
        slideContainerCls: "slideshow-content",
        navigationCls: "slideshow-navigation",
        slideSelector: "a",
        navigationSelector: "a",
        selectedCls: "selected",
        slideCls: "slide",
        fadeInDuration: 500,
        fadeOutDuration: 1,
        fadeOutEasing: "linear",
        fadeInEasing: "linear",
        timeout: 5000,
        hashPrefix: "slide",
        autoplay: true
    }                  


    var Slideshow = function(el, options) {
        var _slideshow, _slidesContainer, _slides, _navigation,
            _navigationItems, _currentSlide, _currentIndex, _count, _timeout, _options = {};

        var _init = function() {
            _options = $.extend(_options, $.slideShow.defaultOptions, options);
            _slideshow = $(this);
            _slidesContainer = _slideshow.find("." + _options.slideContainerCls);
            _slides = _slidesContainer.find(_options.slideSelector);
            _navigation = _slideshow.find("." + _options.navigationCls);
            _navigationItems = _navigation.find(_options.navigationSelector);
            _navigation.cleanWhitespace();
            _bindEvents();
            _setupSlides();
            _setupNavigation();
            _showSlide(_getHash() || 0);
        }

        var _getHash = function() {
            var regex = RegExp(["#", _options.hashPrefix, "-"].join("|"));
            return window.location.hash.replace(regex, "");
        }

        var _setHash = function(hash) {
            //window.location.hash = [_options.hashPrefix, hash].join("-");
        }

        var _bindEvents = function() {
            _navigationItems.click(_onNavigationItemClick);
        }

        var _setupNavigation = function() {
            _navigationItems.each(function(i) {
                var $item = $(this);
                $item.attr("index", i);
            });
            _navigationItems.first().addClass("first");
            _navigationItems.last().addClass("last");
        }

        var _setupSlides = function() {
            _slides.css({
                display: "none",
                opacity: 0
            });
            _count = _slides.length;
        }

        var _showSlide = function(index) {
            if (isNaN(index) || index == "" || !index) {
                index = 0;
            }
            _navigationItems.removeClass(_options.selectedCls);
            _slides.removeClass(_options.selectedCls);
            _currentIndex = parseInt(index);
            if (_currentIndex > _count - 1 || _currentIndex < 0) _currentIndex = 0;
            _setHash(_currentIndex);
            _currentSlide = _slides.eq(_currentIndex);
            _navigationItems.eq(_currentIndex).addClass(_options.selectedCls);
            /* start fade out */
            _slides.filter(":visible").stop().animate({
                opacity: 0
            }, _options.fadeOutDuration, _options.fadeOutEasing, _onFadeOutComplete);
            /* start fade in */
            _currentSlide
                .css("display", "block")
                .stop()
                .animate({
                    opacity: 1
                }, _options.fadeInDuration, _options.fadeOutEasing, _onFadeInComplete);
            /* go to next slide after timeout duration */
            if (_options.autoplay) {
                if (_timeout) clearTimeout(_timeout);
                _timeout = setTimeout(function() {
                    _showSlide(_currentIndex + 1);
                }, _options.timeout);
            }
        }

        var _getInstance = function() {

        }
        var _onFadeInComplete = function() {
            $(this).addClass(_options.selectedCls);
        }
        var _onFadeOutComplete = function() {
            $(this).css("display", "none");
        }
        var _onNavigationItemClick = function(ev) {
            ev.preventDefault();
            var $this = $(this);
            if (!$this.is("." + _options.selectedCls)) {
                var index = $this.attr("index");
                _showSlide(index);
            }
        }
        $(_init);
    }
})(jQuery);
