/*
 * jQuery plugin expanding functionality
 *
 * @author Petter Svensson-Silfver
 * @date 2010-10-14
 * @param expandFirst:  if the information of the first object should be visible or not on render
 * @param expandAll:    if the information of all objects should be visible or not on render
 * @param items:        the type of element if it's a collection of objects that are going to expand
 * @param expander:     the element class of the expander
 * @param information:  the element which is to be expanded
 * @param expanded:     the class thats appended to the expander element for sake of styling
 * @param duration:     the duration of the toggle animation
 */

(function() {
    $.fn.expand = function(options) {
        var
        defaults = {
            expandFirst: false,
            expandAll: false,
            items: 'li',
            expander: '.expander',
            information: '.information',
            expandedClass: 'expanded',
            duration: 150,
            isWrapping:true
        },
        settings = $.extend({}, defaults, options);

        if (!this) return false;

        this.each(function() {
            var $this = $(this),
            $items = ($this.children(settings.items).size() > 0) ? $this.children(settings.items) : $this;

            $.each($items, function(i) {
                var $expander, $information;
     		
                if (settings.isWrapping) {
                    $expander = $(this).children(settings.expander);
                    $information = $(this).children(settings.information);
                } else {
                    $expander = $(this);
                    $information = $(settings.information);
                }
				
                if((settings.expandFirst && i == 0) || settings.expandAll) {
                    $expander.addClass(settings.expandedClass);
                } else {
                    $information.hide();
                }

                $expander.click(function(){
                    if($expander.hasClass(settings.expandedClass)) {
                        $expander.removeClass(settings.expandedClass);
                        $information.hide();
                    } else {
                        $expander.addClass(settings.expandedClass);
                        $('html, body').animate({
                            scrollTop:$expander.offset().top-10
                            }, 300);
                        $information.delay(300).fadeIn();
                        
                    }
                    //$information.slideToggle(settings.duration);
                    
                    return false;
                });
            });
        });
        return this;
    };
})(jQuery);
