jQuery.fn.accordion = function(options) {
    var options = jQuery.extend({
        minWidth: 65,
        maxWidth: false,
        onShow: false,
        autoSlide: 0
    },options);
	
    var open = function(current, fast) {
    	var curLeft = 0;
        if(typeof(fast) == 'undefined')
            fast = false;
    	items.each(
            function(index) {
                if(fast)
                    jQuery(this).css('left', curLeft + 'px');
                else
                    jQuery(this).animate({left: curLeft + 'px'}, 800, 'swing');
                
                if(current != this) {
                    curLeft += options.minWidth;
                    jQuery(this).css('cursor', 'pointer');
                } else {
                    curLeft += options.maxWidth;
                    jQuery(this).css('cursor', 'default');
                    accordion.find("li").addClass('close');
                    jQuery(this).removeClass('close').addClass('open');
                    if(options.onShow && !fast)
                        options.onShow(this);
                    openBlockId = index;
                }
            }
    	);
    };

    var openByIndex = function(index, fast) {
    	return open(items.get(index), fast);
    };
    
    var next = function() {
    	openByIndex(++openBlockId%items.length);
    };
    
    var timerHandler = function() {
    	if(slideStop)
            return clearInterval(intervalId);
    	next();
    };
    
    function calcMaxWidth() {
        return jQuery(window).width() - (items.length - 1) * options.minWidth;
    }

    function adjustOptionsMaxWidth(maxWidth) {
        if(typeof(maxWidth) == 'undefined' || !maxWidth)
            maxWidth = calcMaxWidth();
        options.maxWidth = maxWidth;
    }

    function adjustMaxWidth() {
        adjustOptionsMaxWidth();
        items.width(options.maxWidth);
    }

    function onItemClick() {
    	slideStop = true;
    	open(this);
    }

    function onWindowResize() {
        adjustMaxWidth();
        openByIndex(openBlockId, true);
    }

    var accordion = this;
    var items = this.children();
    var openBlock = items.get(0);
    var openBlockId = 0;
    var slideStop = false;

    adjustMaxWidth(options.maxWidth);
    open(openBlock);
    
    jQuery(this).children().click(onItemClick);
    jQuery(window).resize(onWindowResize);

    
    if(options.autoSlide > 0)
    	var intervalId = setInterval(timerHandler, options.autoSlide);
    
}
