(function($){
	
	
	$(document).ready(function(){
    	
        // table
    	$('table')
    		.wrap('<div class="table-wrap"></div>')
    		.find('tbody tr:odd')
    			.addClass('even');
    		
    	$('hr')
    		.not('.hidden')
    		.wrap('<div class="separator"></div>')
    		.hide();
    	
    	$('#sub-intro').each(function(){
    		var $box = $(this),
    		 	$fragments = $('.slide', this),
    		 	$menu = $('<ul class="reset menu"></ul>');
    		 	
    		$fragments.each(function(i) {
    			$menu.append('<li><a href="#">'+ (i+1) +'</a></li>');
    		}); 	
    		
    		$box.find('.row-main').append($menu);
    	})
    			
    	// Home crossroad
	    $('#intro, #sub-intro').each(function(){
	        var $item = $('.menu 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')
	                    .eq(index)
	                        .addClass('active');
	                        
	                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){
	        	if( $box.is('#intro') )    
        		{ 
	                $(this)
	                    .hover(
	                        function(){
	                            rotate(i);
	                            timer.remove();
	                        },
	                        function(){
	                            timer.remove();
	                            timer.add(rotate, delay*1000);    
	                        }
	                    );  
	                    
	            }
				else{
				  	$(this).click(function() {
				  		rotate(i);
				  		timer.remove();
	                    timer.add(rotate, delay*1000);
				  		return false;
				  	});  	
				}  
	        });
		   
	        
	        timer.add(rotate, delay*1000);
	    })
	    .addClass('js-slideshow'); 

    	
    	/* IE */
    	$('.ie6ie7 a .img img, .ie6ie7 .filter-box img, .ie8 .filter-box img')
    		.click(function(){
    			this.parentNode.click()
    		});
    	
		
    });

})(this.jQuery);



