﻿(function ($) {
    $.fn.accordian = function (options) {
        options = jQuery.extend({
            titles: '.accordian_titles1',
            contents: '.accordian_contents',
            active: 'none',
            onClick: function () {},
            onShow: function () {},
            onHide: function () {},
            showSpeed: 'slow',
            hideSpeed: 'fast',
            auto: false,
            delay: 2000
        },
        options);

        var index = 1;
        var div = $(this);
        var titles = $(options.titles, div);
        var contents = $(options.contents, div);

        function activate(item) {
            titles.each(function (i) {
                if (item == i) {
                    $(this).next(options.contents).slideDown(options.showSpeed).each(options.onShow);
                } else {
                    $(this).next(options.contents).slideUp(options.hideSpeed).each(options.onHide);
                }
            });
        }

        function hidesParts() {
            contents.each(function () {
                $(this).hide();
            });
        }

        titles.each(function () {
            $(this).mouseover(function () {
                index = titles.index(this);
                activate(index);
            });
        });

        if (options.active == 'none') {
            hidesParts();
        } else {
            activate(options.active);
        }
    
        function $activate() {
            activate(index);
            index++;
            if (index == titles.length) {
                index = 0;
            }
        }

        if (options.auto) {
            var MyTime = setInterval($activate, options.delay);
            div.hover(
            function () {
                clearInterval(MyTime);
            },
            function () {
                MyTime = setInterval($activate, options.delay);
            });
        }
    };
})(jQuery);