// --- Display a slideshow --- //
// --- Copyright by Przemyslaw Jaworski --- //

var slides_counter = 0;
var my_slides_nr = 0;
var actual_slides_nr = 0;

var slides_flag = 0;
var automat_flag = 0;
var automatic_slideshow_flag = 1;

var slideshow_time = 4000;
var slideshowTimer;
var automatTimer;


$(document).ready(
function() {
	slideshow();
})

function slideshow() {
	
	// startup values
	slides_counter = $(".slide").length;	
	slide_show();		
	setTimeout (function() {
		automatic_slideshow();
	},slideshow_time);	
	
	$(".slides_button").click(
	function() {		
		if ($(".slides_button").index(this) != my_slides_nr) {
			my_slides_nr = $(".slides_button").index(this);
			slide_show();			
		}
	});
	
	$(".slide").mouseover(
	function() {				
		slides_flag = 0;
		automat_flag = 0;
		clearTimeout(slideshowTimer);
	});
	
	$("#about_company").bind("mouseleave",
	function() {
		automat_flag = 0;
		slides_flag = 0;
		if (automatic_slideshow_flag == 0) {			
			automatic_slideshow_flag = 1;			
			automatic_slideshow();			
		}		
	});

	$("#about_company").bind("mouseenter",
	function() {
		clearTimeout(automatTimer);
		if (automatic_slideshow_flag == 1) {			
			automatic_slideshow_flag = 0;
		}
	});	
}

function slide_show() {	
	//clearTimeout(automatTimer);
	if (slides_flag == 0 || automatic_slideshow_flag == 1) {
		slides_flag = 1;		
		//slideshowTimer = setTimeout (function() {				
			$(".slide").fadeOut(500);
			$(".slide").eq(my_slides_nr).fadeIn(1200);	
			actual_slides_nr = my_slides_nr;
			change_button();									
			slides_flag = 0;			
		//},600);
	}
}

function automatic_slideshow() {
	clearTimeout(slideshowTimer);
	if (automatic_slideshow_flag == 1 && automat_flag == 0) {		
		automat_flag = 1;		
		automatTimer = setTimeout (function() {
			my_slides_nr = my_slides_nr + 1;
			if (my_slides_nr>=slides_counter) my_slides_nr=0;
			automat_flag = 0;	
			
			slide_show_automat();		
			automatic_slideshow();					
		},slideshow_time);
		
	}
}

function slide_show_automat() {	
	$(".slide").fadeOut(500);
	$(".slide").eq(my_slides_nr).fadeIn(1200);					
	change_button();								
}

function change_button() {
	if (automatic_slideshow_flag == 1) setTimeout (function() {
		$(".slides_button").css({background:'url("public/images/design/button_1.png") top left no-repeat'});
		$(".slides_button").eq(my_slides_nr).css({background:'url(public/images/design/button_2.png) top left no-repeat'});
	},500);
	else {
		$(".slides_button").css({background:'url("public/images/design/button_1.png") top left no-repeat'});
		$(".slides_button").eq(my_slides_nr).css({background:'url(public/images/design/button_2.png) top left no-repeat'});
	}
}

