﻿(function($) {
    $.fn.carousel = function(options) {
        var defaults = {
            flipDelay: 5000,
            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.InitPrevNextButtons();

            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();
                    });
                    var as = 'container_' + $(this).attr('id').split('_')[1];
                    $('#' + as).css({ opacity: 1 }).show();
                    $(this).addClass('selected');
                    Cufon.replace(['.flipbuttoninner a'], {
                        hover: true
                    });
                    return false;
                });
            });
        };

        this.InitPrevNextButtons = function() {
            $('.nextbutton', $('.flipbuttons')).click(
                function() {
                    currentId = currentId + 1;
                    if (currentId > options.buttons.length) {
                        currentId = 1;
                    }
                    var selectedbutton = $($('.' + options.buttonContainer).children('div')[currentId - 1]);
                    selectedbutton.click();
                }
            );


            $('.prevbutton', $('.flipbuttons')).click(
                function() {
                    currentId = currentId - 1;
                    if (currentId <=  0) {
                        currentId = options.buttons.length ;
                    }
                    var selectedbutton = $($('.' + options.buttonContainer).children('div')[currentId - 1]);
                    selectedbutton.click();
                }
            );
        };

        this.StartFlip = function() {
            intervalId = setInterval(function() {
                $('.' + options.tabContainer).children().each(function() {
                    var as = $(this).attr('id').split('_')[1];

                    var containerId = as;
                    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(); });
                        Cufon.replace(['.flipbuttoninner a'], {
                            hover: true
                        });


                        return false;
                    }
                });
            },
			options.flipDelay
		);

        };
        return this.initialize();
    };
})(jQuery);
