var slide_show;
var counter = 0;

$(document).ready(function(){
    // setInterval( "slideSwitch()", 3000 );
	init();
    
	
    $("#startSlideshow").click(function(){startSlideshow()});
    $("#stopSlideshow").click(function(){stopSlideshow()});
   
	$('#content_scroll').jScrollPane();

	$("#showmap").click(function(){
		$("#map").toggle();
		return false;
	});
	$("#shownews").click(function(){
		$("#news").toggle();
		return false;
	});
	
	$("#slideshow_controll .slide_loader").click(function(){
        stopSlideshow();
        var $activate = $("#" + $(this).attr('rel'));
		$('#slideshow img').each(function(){
			$(this).removeClass('active last-active');
		});
		var rel = '#' + $(this).attr('rel');
		var $activate = $(rel);
		//var activate = $("#" + $(this).attr('rel'));
		$activate.addClass('active');
		$('.slide_loader').each(function(){
			$(this).removeClass('active');
		});
		$(this).addClass('active');
		//$($(this).attr('rel')).addClass('active');
		$("#startSlideshow").show();
    	$("#stopSlideshow").hide();
		return true;
	});
	
	
	$('.photogallery a').click(function(){
					$('.photogallery a').removeClass('active');					
										var src = $(this).attr('rel');
										$('#slideshowp_img').attr('src', src);
										$(this).addClass('active');
										});
});

function init() {
	
	$("#main").append('<div id="slideshow_controll"></div>');
	$('#slideshow img').each(function(){
		counter++;
		$("#slideshow_controll").append('<span class="slide_loader" rel="image_' + counter + '" id="slide_loader_image_' + counter + '">' + counter + '</span>');
		$(this).attr('id','image_' + counter);
		$('#slide_loader_image_1').addClass('active');
	});
	$("#slideshow_controll").append('<span id="stopSlideshow">Stop</span>');
	$("#slideshow_controll").append('<span id="startSlideshow">Start</span>');
	$('#slide_loader_image_1').addClass('active');
	//$("#startSlideshow").hide();
	startSlideshow();
}
function slideshow() {
	var $active = $('#slideshow IMG.active');

    if ( $active.length == 0 ) $active = $('#slideshow IMG:last');

    var $next =  $active.next().length ? $active.next() : $('#slideshow IMG:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
		
	$('.slide_loader').each(function(){
		$(this).removeClass('active');
	});
	var $cont_ativate = $('#slide_loader_' + $next.attr('id'));
	$cont_ativate.addClass('active');
}

function startSlideshow() {
	//alert('startSlideshow');
	$("#startSlideshow").hide();
   	$("#stopSlideshow").show();
	slide_show = setInterval( "slideshow()", 3000);
}

function stopSlideshow() {
	//alert('stopSlideshow');
	$("#startSlideshow").show();
   	$("#stopSlideshow").hide();
	clearInterval(slide_show); // zruší nacasování
}
