function slide_home(){
    var $ = jQuery,
    current = 0,
    slide = $("#imagem"),
    itens = $(".reel .item", slide),
    itensWidth = 680,
    itenHeight = 334,
    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){
        reel.css({
            'width' : itensWidth
        });
        return;
    }

    reel.css({
        'width' : reelWidth
    });

    $a = $('#imagens li a');

    var rotate = function(c){
        if(c == -itemSum){
            c = 0;
            current = 0;
        }
        if(c > 0){
            c = 0;
            current = 0;
        }

        $a.removeClass('on');
        $a.eq(c*-1).removeClass('on');

        var reelPosition = c * itensWidth;

        reel.animate({
            left: reelPosition
        }, 700);
    };

    function next(){
        rotate(--current);
        return false;
    }

    function preview(){
        rotate(++current);
        return false;
    }

    if(itemSum) {
        var ul = $('#imagens');
        for(var i = itemSum; i--;){
            var li = document.createElement('li');
            var a = document.createElement('a');

            $(a).data('index', i);

            a.onclick = function(){
                rotate($(this).data('index') * -1);
            }
            
            if(!i){
                a.className = 'on';
            }
            a.innerHTML = i+1;
            li.appendChild(a);
            ul.append(li);
        }
    }

    setInterval(function(){

        rotate(--current);

    }, 5000);


}

(function($){

    $(document).ready(function(){
        slide_home();
    });

})(jQuery);
