var slider = null;


function class_timer(czas, func) {
	
	this.czas = czas;
	this.func = func;
	
	this.start();
	}

class_timer.prototype.start = function() {
	
	this.time = setInterval(this.func, this.czas);
	};

class_timer.prototype.reset = function() {
	
	this.stop();
	this.start();
	};

class_timer.prototype.stop = function() {
	
	clearInterval(this.time);
	};



function class_slider() {
	
	var self = this;
	
	
	
	this.animacja_value = false;
	
	
	this.wszystkich_slidow = wszystkich_slidow;
	
	this.showFast_zalegle = null;
	


																					//zerujemy pozycję początkową slidów
	var slideshow_scroll = FSite2.getElementsByClassName('slideshow_scroll');
	
	
	if (slideshow_scroll.length > 0) {
		
		slideshow_scroll = slideshow_scroll[0];
		
		slideshow_scroll.scrollLeft = 0;
		}
	
	
	
	var params = {};
	
	params.slideShowClass		= 'slideshow';
	params.slideShowTransTime	= 2000;						//2000ms opóźnienia
	params.slideShowTransType	= 'scroll';
	params.slideShowCurrent		= 0;						//pierwszy slid
	
	//params.slideShowWaitTime	= 10000;
	
	params.slideShowLoop		= false;
	params.slideShowOnShowSlide = self.odswiez;
		
	FSite2.extendForm(document.body, params);

	
	
	var zwrot = FSite2.getElementsByClassName('slideshow');
	
	
	if (zwrot.length > 0) {
		this.slider = zwrot[0]._slideShow;
	}
	
	else {
		console.error('niezainicjowano slidera');
		}
	
	

	
	this.timer = new class_timer(20000, function(){
		
		self.next(false);		
		});
	}



class_slider.prototype.anim = function(value) {
	
	
	if (value === true) {		
		this.animacja_value = true;
		}
	
	else if (value === false) {
		this.animacja_value = false;
		}
	
	else if (typeof(value) === 'undefined') {
		return this.animacja_value;
		}
	
	else {
		console.error('nieprawidłowe wywołanie funkcji anim');
		}
	};



class_slider.prototype.odswiez = function() {
	

	if (slider === null) {
		return;
		}
	
	if (slider.slider.currentSlide <= 0) {
		
		slider.showFast(slider.wszystkich_slidow - 2);
		}
	
	
	if (slider.slider.currentSlide >= slider.wszystkich_slidow - 1) {
		
		slider.showFast(1);
		}
	
	};



class_slider.prototype.show = function(numer) {

	var self = this;
	

	if (self.anim() === true) {
		
		console.warn('show: konflikt, trwa animacja');
		return;
		}

	var index = self.slider.currentSlide;
	
	self.anim(true);
	
	self.slider.showSlide(numer, function(){
		
		if (self.slider.currentSlide === index) {
			console.error('brak zmiany indexu');	
			}
		
		
		self.anim(false);
		
		if (self.showFast_zalegle === null) {
			self.showFast_zalegle = numer;
			}
		});
	};



class_slider.prototype.showFast = function(numer) {
	
	var self = this;
	
	
	
	
	var old_time = self.slider.transTime;
	self.slider.transTime = 0;
	
	
	self.slider.showSlide(numer, function(){
	
		self.slider.transTime = old_time;
		self.anim(false);
		});
	};




class_slider.prototype.next = function(reset) {
	
														//wartość domyślna
	if (typeof(reset) === 'undefined') {
		reset = true;
		}
	
	
	var self = this;
	
	var index = self.slider.currentSlide + 1;
	
	if (0 <= index && index <= self.wszystkich_slidow - 1) {	
		
		self.show(index);
		
		
		if (reset === true) {			
			this.timer.reset();
			}
		}
	
	else {
		console.error('next: nieprawidłowy index ' + index);
		}
	};



class_slider.prototype.prev = function() {
	
	var self = this;
	
	
	if (self.anim() === true) {
		
		console.error('left: konflikt, trwa animacja');
		return;
		}
	
	if (self.slider.currentSlide === 0) {
		
		console.error('prev niedozwolone dla pierwszego slajdu');
		return;
		}
	
	
	self.show(self.slider.currentSlide - 1);
	this.timer.reset();
	};




function load() {
	
	
	slider = new class_slider();
	
															//wywołujemy raz jeszcze funkcję odśwież po to żeby poprawnie zainicjować pierwszy slid
	slider.odswiez();
	}

	



