jQuery.extend (jQuery.easing, {
    easeInOutExpo: function (x, t, b, c, d) {
        if (t==0) return b;
        if (t==d) return b+c;
        if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
        return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
    }
});

jQuery(function () {

    var animateHeight = function ($obj, height, duration) {
        $obj.animate({
                height: height
            }, duration);
    };

    jQuery('li.sliding-postlist').each(function () {

        var $widget = jQuery(this),
            $sliders = jQuery('ul.slider:first > li', $widget),
            $originSlider = $sliders.first().parent(),
            $buttons = jQuery('ul.navigator:first > li', $widget),
            $pagers = jQuery('ul.pagination > li', $widget),
            animDurationMs = 300, vis = 'visible', sel = 'selected';

        $buttons.click(function () {
                var $this = jQuery(this),
                $activeSlider = $sliders.eq($buttons.index($this));

                if ($this.hasClass(sel)) {
                    return;
                }
                $buttons.removeClass(sel);
                $this.addClass(sel);

                $sliders.removeClass(vis);
                $activeSlider.addClass(vis);

               
                $originSlider.animate({
                    'margin-left': ($activeSlider.index() * -269 ) + 'px'
                }, {
                    duration: animDurationMs,
                    easing: 'easeInOutExpo'
                });

                animateHeight($sliders.parent(), $activeSlider.height(), animDurationMs);

        });

        $pagers.click(function () {

            var $this = jQuery(this),
                $activeSlider = $this.parent().parent(),
                $items = $activeSlider.children('ul.links:first').children('li'),
				$currentPageItems;

            if ($this.hasClass('selected')) {
                return;
            }
            $this.parent().children('li').removeClass('selected');
            //$pagers.removeClass('selected');
            $items.hide();
            $this.addClass('selected');
			$currentPageItems = $items.filter('.page-' + ($this.index() + 1) + '-item');
			$currentPageItems.last().addClass('page-last');
			$currentPageItems.fadeIn(animDurationMs);


            animateHeight($sliders.parent(), $activeSlider.height(), animDurationMs);

        });

        $sliders.find('ul.links > li:not(.page-1-item)').hide();
		$sliders.find('ul.links').each(function () {
			$(this).children('li:visible').last().addClass('page-last');
		});
        $sliders.parent().height($sliders.filter('.visible').height());


    });

});

