﻿;(function ($) {

    $.fn.CollapsibleSection = function( options ) {
        
        var settings = {};
        
        return this.each(function() {
            if( options )
                $.extend( settings, options );

            var $this = $(this);

            var toggleSwitch = $('<a href="javascript:void(0)" class="toggle-switch">Show</a>');
            var title = $('h3', this);
            if (title)
                title.append(toggleSwitch);

            title.click(CollapsibleSectionOnClick);
            toggleSwitch.click(CollapsibleSectionOnClick);
        });

        function CollapsibleSectionOnClick(e) {
            e.preventDefault();
            var container = $(this).parent();
            var content = $(".content", container);

            var isHidden = content.css('display') == 'none';

            content.slideToggle();
            $('.toggle-switch', container).text((isHidden ? 'hide' : 'show'));
        }

    };


    $.fn.ItemFader = function( options ) {
        
        var settings = {
            speed: '2000',
            hold: 4000
        };

        return this.each(function() {

            var $this = $(this);

            var $items = $this.find('li');

            $items.filter(':not(:first)').hide();

            $items.filter(':first').addClass('item-active');

            setInterval( function() { Transition($this, settings) }, settings.hold );
        });

        function Transition(item, settings)
        {
            var $active = item.find('.item-active');
            var $next = $active.next();

            if(!$next.length)
                $next = item.find('li:first');

            $active.fadeOut(settings.speed, function() { 
                $next.fadeIn(settings.speed, function() {
                    $active.removeClass('item-active');
                    $next.addClass('item-active');
                });
            });
        }

    };

})(jQuery);
