function slide(slide, selectable,auto){
    var $ = jQuery,
    current = 0,
    itens = $(".reel .item", slide),
    itensWidth = itens.width(),
    itenHeight = $(itens[0]).height(),
    itemSum = itens.size(),
    reelWidth = itensWidth * itemSum,
    _slide = $(slide),
    currentHeight = 0,
    
    reel = $(".reel", slide);


    _slide.height(itenHeight);

    $(".reel .item:first", slide).load(function(e){
        currentHeight = this.height;
        reel.css({
            'height' : this.height
            });
        _slide.css({
            'height' : this.height
            });
    });

    if(itemSum <= 1){
        return;
    }
    
    
    reel.css({
        'width' : reelWidth
    });
   
    var rotate = function(c){
        var _height = 0;
            
        if(c == -itemSum){
            c = 0;
            current = 0;
        }
        if(c > 0){
            c = 0;
            current = 0;
        }
        if(selectable){
            $("." + slide.id + "_ref a.on").removeClass("on");
            $("." + slide.id + "_ref a:eq(" + (c * -1)+")").addClass("on");
        }

        _height = $(".reel .item:eq(" + (c * -1)+")", slide).height();

        var reelPosition = c * itensWidth;

        if(_height > currentHeight) {
            reel.animate({
                left: reelPosition,
                height: _height
            }, 700);
            setTimeout(function(){
                _slide.animate({
                    height: _height
                }, 700);
            }, 700)
            
        } else {
            _slide.animate({
                height: _height
            }, 700);
            setTimeout(function(){
                reel.animate({
                    left: reelPosition,
                    height: _height
                }, 700);
            }, 700)
        }

        currentHeight = _height;
        
    };

    if(selectable){
        $("." + slide.id + "_ref a:eq(0)").addClass("on");
        $("." + slide.id + "_ref a").each(function(i,e){
            $(e).click(function(){
                var index = i;
                current = (index * -1);
                rotate(current);
                $(this).parent().find('.on').removeClass('on');
                $(this).addClass('on');
            })
        });
        $("." + slide.id + "_next").each(function(i,e){
            $(e).click(function(){
                rotate(--current);
            })
        });
    }
    
    if(auto && itemSum){
        setInterval(next, 4000);
    }
}

(function($){

    $(document).ready(function(){
        $(".slide").each(function(i,e){
            slide(e,true,false);
        });
    });
    
})(jQuery);
