/*

    jQuery Carousel plugin (created using version 1.3.2).
    
    This plugin creates a tabbed page where the selected tab changes automatically using a customizable delay.
    
    Usage:
        $('reference to the target').carousel({
            flipDelay: 1000, // Optional - defines the flip time.
            buttonContainer: '', // Required - class name of container that holds the tab buttons.
            buttons: [], // Required - array of tab button ids.
            buttonAttr: '', // Required - array of tab button to tab references.
            tabContainer: '', // Required - class name of container that holds the tab pages.
            tabs: [], // Required - array of tab ids.
            tabAttr: '', // Required - array of tab identifier (must be integers int).
            tabId: '' // Required - basename of the tabs id, e.g. tabId = 'container' where the ids are called containerx and x is an integer from 1 to number of tabs.
        });

*/


(function($) {
    $.fn.carousel = function(options) {
        var defaults = {
            flipDelay: 1000,
            buttonContainer: '',
            buttons: [],
            buttonAttr: '',
            tabContainer: '',
            tabs: [],
            tabAttr: '',
            tabId: ''
        };

        var options = $.extend({}, defaults, options);
        var intervalId = null;
        var $this = this;
        var currentId = 1;

        this.initialize = function() {
            if (options.buttons.length != options.tabs.length || options.tabs.length == 0 || options.buttons.length == 0) {
                alert('There must be at least on tab and one button\nand the number of tabs must be the same as the number of buttons!');
                return false;
            }
            // support MetaData plugin
            if ($.meta) {
                options = $.extend({}, options, this.data());
            }

            $('.' + options.tabContainer).children().each(function() {
                $(this).css({ opacity: 0 }).hide();
            });

            var firstTabContainer = $('.' + options.tabContainer).children()[0];
            $(firstTabContainer).css({ opacity: 1 }).show();
            var firstButton = $('.' + options.buttonContainer).children()[0];
            $(firstButton).addClass('selected');
            this.AddClickEvents();
            this.StartFlip();
        };

        this.AddClickEvents = function() {
            $('.' + options.buttonContainer).children('div').each(function() {
                $(this).click(function() {
                    $('.' + options.buttonContainer).children('div').removeClass('selected');
                    if (intervalId != null) {
                        clearInterval(intervalId);
                    }
                    $('.' + options.tabContainer).children('div').each(function() {
                        $(this).css({ opacity: 0 }).hide();
                    });
                    $('#' + $(this).attr(options.buttonAttr)).css({ opacity: 1 }).show();
                    $(this).addClass('selected');
                });
            });
        };

        this.StartFlip = function() {
            intervalId = setInterval(function() {
                $('.' + options.tabContainer).children().each(function() {
                    var containerId = $(this).attr(options.tabAttr);
                    if (containerId == currentId) {
                        var oldContainer = $('#' + options.tabId + currentId);
                        $('.' + options.buttonContainer).children('div').removeClass('selected');
                        currentId++;
                        if (currentId > options.buttons.length) {
                            currentId = 1;
                        }
                        $($('.' + options.buttonContainer).children('div')[currentId - 1]).addClass('selected');


                        $('.flipcontainertextbox').fadeOut();
                        $('#' + options.tabId + currentId).animate({ opacity: 1 }, 1500).show();
                        oldContainer.animate({ opacity: 0 }, 1500, function() { $(this).hide(); $('.flipcontainertextbox').fadeIn(); });


                        return false;
                    }
                });
            },
            options.flipDelay
        );

        };

        return this.initialize();
    };
})(jQuery);

