(function ($) {
    //
    // HTML slideshow for jQuery 1.3.2
    //
    // "Member functions" for the plugin, called in the style of jQuery UI's member functions


    function showSlide(index) {
        var index = index % $(this).data('numberOfSlides');
        return this.each(function () {
            if ($(this).data('slideIndex') != index) {
                $(this).data('slideIndex', index).children('.html-slideshow-slide').fadeOut('slow').eq(index).fadeIn('slow');
                $(this).find('.html-slideshow-control' + (index + 1)).addClass('selected').siblings().removeClass('selected');
            }
        });
    }

    function advance() {
        return this.each(function () {
            $(this).htmlslides('showSlide', $(this).data('slideIndex') + 1);
        });
    }

    function timer() {
        var returnValue = ($(this).data('timer') == undefined) ? this : advance.apply(this, []);
        var me = this;
        $(this).data('timer', setTimeout(function () {
            return timer.apply(me, Array.prototype.splice.call(arguments, 1));
        }, $(this).data('interval')));
        return returnValue;
    }

    function stopTimer() {
        clearTimeout($(this).data('timer'));
    }
    $.fn.htmlslides = function (options) {
        // build main options before element iteration
        var opts = $.extend({}, $.fn.htmlslides.defaults, options);
        // The following line of code sets up functions the user of the plugin can call by re-using the plugin: e.g. $('#slideshow').htmlslideshow('advance');
        var functions = {
            'stopTimer': stopTimer,
            'showSlide': showSlide,
            'advance': advance
        };
        var $this = $(this);
        $this.addClass('html-slideshow');
        if (functions[options] != undefined) {
            return functions[options].apply(this, Array.prototype.slice.call(arguments, 1));
        }
        $this.children().addClass('html-slideshow-slide');
        $this.data('numberOfSlides', $(this).children('.html-slideshow-slide').size());
        $this.prepend($('<div class="html-slideshow-controls"></div>'));
        $this.children('.html-slideshow-slide').css('position', 'absolute').hide().each(function (index) {
            // if the slide has a data-href attribute set, go to the specified URL
            if ($(this).attr('data-href') != undefined) {
                if (opts.hrefExclusionSelector != undefined) {
                    $this.find(opts.hrefExclusionSelector).bind('mouseenter mouseleave click', function (event) {
                        $(this).css('cursor', 'auto');
                        event.stopPropagation();
                    });
                }
                $(this).hover(function () {
                    $(this).css('cursor', 'pointer');
                });
                $(this).click(function (event) {
                    location.href = $(this).attr('data-href');
                });
            }
            var currentControlText = ($(this).attr(opts.controlText) != undefined) ? $(this).attr(opts.controlText): (index+1);
            var currentControl = $('<div class="html-slideshow-control html-slideshow-control' + (index + 1) + '"><div class="control-arrow"></div><div class="container"><a href="#">' + currentControlText + '</a></div></div>');
            if (opts.controlOrientation == 'horizontal') {
                currentControl.css('float', 'left');
            }
            currentControl.data('index', index).click(function (event) {
                event.preventDefault();
                $this.htmlslides('showSlide', index).htmlslides('stopTimer');
            });
            currentControl.hover(
           		function () {
           		    $(this).addClass('hover'); //Add class "hover" on hover in
           		}, function () {
           		    $(this).removeClass('hover'); //Remove class "hover" on hover out
            });
            $this.find('div.html-slideshow-controls').append(currentControl);
        });
        $this.find('div.html-slideshow-controls').each(function () {
            $(this).css('position', 'absolute');
            $(this).css('z-index', '2');
            if (opts.controlVAlign == 'bottom') $(this).css('margin-top', $this.outerHeight() - $(this).outerHeight());
            if (opts.controlAlign == 'right') $(this).css('margin-left', $this.outerWidth() - $(this).outerWidth());
        });
        $this.data('interval', opts.interval);
        timer.apply(this, [opts.interval]);
        return showSlide.apply(this, [opts.startIndex]);
    };
    $.fn.htmlslides.defaults = {
        interval: 10000,
        startIndex: 0,
        controlAlign: 'right',
        controlVAlign: 'bottom',
        controlOrientation: 'horizontal'
    };
})(jQuery);
