var timeoutSlideshow;
var slideshowIsPlaying = false;

function slide_it(next_i){
	var change_slide = true;
	if(next_i == $(document).data('actual_slide')){
		change_slide = false;
	} else if($(document).data('pause_slideshow')){
		change_slide = false;
	}
	
	if(change_slide){
		var old_slide = $($(document).data('slides')[$(document).data('actual_slide')]);
		var next_slide = $($(document).data('slides')[next_i]);
		
		old_slide.removeClass('one_slide_actual');
		$(document).data('actual_slide',next_i);
		next_slide.addClass('one_slide_actual');
		
		$('#actual_slide_description').slideUp(500, function(){
			$('#actual_slide_image').fadeOut(500, function(){
				//tady se zmeni obrazek, popis a tridy
				$('#actual_slide_image').attr('src',next_slide.find('.slide_image').attr('src'));
				$('#actual_slide_image').css('left','0px');
				var new_description = next_slide.find('.slide_description');
					  
				if (new_description.hasClass('left_description')){
					var w = new_description.css('width');
					$('#actual_slide_description').css({left:'0px', right:'auto', top:'auto', bottom:'0px', width:w,height:'100%'});
				}else if (new_description.hasClass('right_description')){
					var w = new_description.css('width');
					$('#actual_slide_description').css({right:'0px',left:'auto',top:'auto',bottom:'0px',width:w,height:'100%'});
				}else if (new_description.hasClass('top_description')){
					var h = new_description.css('height');
					$('#actual_slide_description').css({left:'0px',right:'auto',bottom:'auto',top:'0px',width:'100%',height:h});
				}else if (new_description.hasClass('bottom_description')){
					var h = new_description.css('height');
					$('#actual_slide_description').css({left:'0px',bottom:'0px',right:'auto',top:'auto',width:'100%',height:h});
				}
				
				$('#actual_slide_description').html(new_description.html());
				$('#actual_slide_image').fadeIn(500, function(){
					$('#actual_slide_description').slideDown(500, function(){
						$.fx.interval = 180;
						$('#actual_slide_image').animate({left:'-100px'},6000, function(){
							$.fx.interval = 13;
							if (slideshowIsPlaying==true){
								timeoutSlideshow = window.setTimeout('slideshow()',2000);
							}
						});
					});
				});
			});
		});	
	}
}

function slideshow(){
	var next_i = $(document).data('actual_slide') +1 < $(document).data('slides').length ? $(document).data('actual_slide') + 1 : 0;
	slideshowIsPlaying = true;
	slide_it(next_i);
}

function slide_by_click(i){
	$('#actual_slide_image').stop();
	if (slideshowIsPlaying){
		clearTimeout(timeoutSlideshow);
	}
	slide_it(i);
}

$(document).ready(function() {		
	var slides = $("#all_slides > li");
	$(document).data('pause_slideshow',false);
	$(document).data('actual_slide',-1); //promenna, ktera obsahuje index aktualniho slideu
	$(document).data('slides',slides);  //pole vsech slideu (<li>)
	
	slides.each(function(i) {
		//var next_i = (i+1)<slides.length ? i+1 : 0;
		var next_i = i;
		$(slides[i]).mousedown(function() {
    			slide_by_click(next_i);
        	});
        });
        
	//slideshow();
	slideshowIsPlaying = true;
	slide_it(Math.floor(Math.random()*4)); //nahodny slide
})

