$(document).ready(function(){
    // Home crossroad
    $('#intro').each(function(){
        var $item = $('li', this),
            $fragments = $('.slide', this),
            delay = 5, //sekundy
            len = $item.length,
            time = null,
            actual = 0,
            index = 0,
            $box = $(this);


        // funkce provádějící změnu obrázků a tříd
        var rotate = function(i, first){
            if(typeof i == 'undefined' || i!=index || first){
                index = typeof i == 'undefined' ? (index + 1) % len : i;
                /*$item
                    .removeClass('active')
                    .removeClass('next')
                    .eq(index)
                        .addClass('active')
                        .next()
                                .addClass('next');  */

                if(first){
                    $fragments
                        .css('z-index', '')
                        .eq(index)
                            .css('z-index', '2');
                }
                else{
                    $fragments
                        .css('z-index', '')
                        .eq(actual)
                            .css('z-index', '1')
                            .end()
                        .eq(index)
                            .stop()
                            .css({'z-index':'2', 'opacity': '0'})
                            .animate({'opacity': '1'}, 500);


                }
                actual = index;
            }
        };
        rotate(index, true);

        // objekt časovače
        var timer = function(){
            var time = null,
                w = window;
            return {
                add: function(callback, delay){
                    time = w.setTimeout(function(){
                        callback();
                        time = w.setTimeout(arguments.callee, delay)
                    }, delay)     
                }
                ,remove: function(){
                    time = w.clearTimeout(time);
                }
            }
        }();

        /*$item.each(function(i){
        $(this)
            .click(
                function(){
                    rotate(i);
                    timer.remove();
                    return false;
                }*//*,
                function(){
                    timer.remove();
                    timer.add(rotate, delay*1000);    
                }*//*
            );  

        });  */


        timer.add(rotate, delay*1000);
    })

});
