(function($) {
    $.fn.tabs = function(options) {
        return this.each(function() {
            new $.tabs(this, options);
        });
    };

    $.fn.tabs.defaults = {
        auto: true,
        delay: 3000,
        index: 1,
    };

    $.tabs = function(element, options) {
        var base = this;

        base.element = element;
        base.$element = $(element);

        // Set up a few defaults
        base.index = -1;
        base.timer = null;

        // Add a reverse reference to the DOM object
        base.$element.data("tabs", this);

        base.$navigation = base.$element.find(".tabs_nav");
        base.$content = base.$element.find(".tabs_content");

        // Init function
        base.init = function() {
            base.options = $.extend({}, $.fn.tabs.defaults, options);

            // set variables
            base.count = base.$content.find("li").length;

            // set timer
            base.timer = null;
            base.playing = false;

            // bind navigation buttons
			$(base.$navigation).find("li > a").click(function() {
				base.show_tab($(this).parent().index(), false);														  
			});
			
			// show selected tab
			base.show_tab(base.options.index - 1, base.options.auto);

            // start auto scroll
            if (base.options.auto == true) {
                if (!base.options.delay) base.options.delay = 3000;
                if (base.options.delay < 1000)
                    base.options.delay *= 1000;

                $(document).ready(function() {
                    base.startStop(true);
                });
            }
        };

        // Go to slide
        base.show_tab = function(index, autoplay) {
            if ((autoplay == false) && (base.playing == true))
                base.startStop(false);

            if (index >= base.count) index = 0;
            if (index < 0) index = base.count - 1;

            if (base.index != index) {
				$(base.$navigation).find(".current").removeClass("current");
				$(base.$content).find(".selected").removeClass("selected");

                base.index = index;

				$(base.$navigation).find("li").eq(base.index).find("a").addClass("current");
				$(base.$content).find("li").eq(base.index).addClass("selected");
            }
        }

        // Timer
        base.startStop = function(playing) {
            if (playing !== true) playing = false; // Default if not supplied is false

            // Update variable
            base.playing = playing;

            if (playing) {
                base.clearTimer(); // Just in case this was triggered twice in a row

                base.timer = window.setInterval(function() {
                    base.show_tab(base.index + 1, true);
                }, base.options.delay);
            } else {
                base.clearTimer();
            };
        };

        base.clearTimer = function() {
            // Clear the timer only if it is set
            if (base.timer) window.clearInterval(base.timer);
        };

        // Trigger the initialization
        base.init();
    };
})(jQuery);
