var counter = 1;
var last_item = '';
var slideShowInterval = '';
var slideShowTimeout = '';

function slideSwitch() {
	
	//$('#debug').html('slideSwitch:counter:'+ counter);	
	

	if(!$('#slideshow_thumb'+counter).attr('id')){counter = 0;}	
	if(last_item){
		last_item.removeClass('active');		
		
	}
	var item = $('#slideshow_thumb'+counter);
	item.addClass('active');
	last_item = item;

	$('#slideshow_image').fadeOut(500,function(){
		//alert('aa');
	
		$(this).attr('src',item.attr('href'));
		$(this).load(function() {
			$(this).fadeIn(500);	
		});
			

	});
	

	//$('#debug').html(item.attr('href'));	
	
	counter++;
	/*
	var active = $('#thumbs li a.active');
    var next = active.next();    

    next.addClass('active');

    active.removeClass('active');
    */
		
	
}

function slideShowPause(image_number){
	
	counter = image_number;
	window.clearInterval(slideShowInterval);
	window.clearTimeout(slideShowTimeout);
	
	slideSwitch();
	
	
}


function slideShowPlay(){
	//$('#debug').html(image_number);	
	slideShowInterval = window.setInterval(slideSwitch, 3000);
}

$(document).ready(function(){

	last_item = $('#slideshow_thumb0');
	$("#photo_wrapper a").css({opacity: 0.7});
	$("#photo_wrapper a").hover(
	  function () {
	    $(this).stop().css({opacity: 1.0});
	  }, 
	  function () {
	    $(this).stop().css({opacity: 0.7});
	  }
	);
	$('#thumbs').children('li').children('a').click(function(){ return false;});
	
	$('#thumbs').children('li').children('a').hover(
	
		function(){
			//alert($(this).attr('id').replace('slideshow_thumb',''));
			slideShowPause($(this).attr('id').replace('slideshow_thumb',''));
		},
		function(){
			slideShowPlay();	
		}
	
	);
	
	
	slideShowTimeout = window.setTimeout(slideShowPlay, 9000);//+1000 trwanie animacji + 3000 delay = 13000 (13s)
	//slideShowTimeout = window.setTimeout(slideShowPlay, 500);
	//slideShowTimeout = window.setTimeout(slideShowPlay, 2000);//+1000 trwanie animacji + 3000 delay = 13000 (13s)


});  
