document.observe('dom:loaded', function () {
		
		new PeriodicalExecuter(function(pe) {
  			var tnLink = $$('#slide-images .li');

  			len = $('slide-images').childElements().length;
  			var countOK = 0;
  			tnLink.each(function(s, index) {
	            var tnImg = tnLink[index].firstDescendant();
	            var obj = $(s).getElementsByTagName('img');

	            if ( imgOK(obj[0]) ) {
	            	countOK++;
	            }
	        });
	        
	        if (countOK == len) {
	        	new Effect.Fade($('loader-ind'),{ duration:1});

	        	startgallery();
	        	pe.stop();
	        }
	        
  				
		}, 1);
	
 		
 		

		function imgOK(img) {
	    	if (!img.complete) {
	    	    return false;
	    	}
	    	if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) {
	    	    return false;
			}
			return true;
		}
		
		
		function startgallery() {
			var slider = new Slider('slide-images'); 
			new PeriodicalExecuter(function(pe) {
  				slider.slide();
			}, 5);
 		}	

});


var Slider = Class.create({
	initialize: function (element, duration, n) {
		var children, l, dimensions_w, dimensions_h, w = 0, h = 0, wrap, nav;
		element = $(element);
		element.style.visibility = 'visible';
		
		startowe = 3;
		
		children = element.childElements();
		l = children.length;
		if (n == undefined || isNaN(n)) {
			n = startowe
		}
		else if (n < startowe) {
			n = l + n;
			if (n < startowe) {
				n = startowe
			}
		}
		else if (n >= l) {
			n = startowe
		}
		children.each(function (child) {
			child.setStyle({
				display: 'block'

			});
			maxobj_w = child.select('img');
			maxobj_h = child.select('img');
			dimensions_w = maxobj_w[0].getWidth();
			dimensions_h = maxobj_w[0].getHeight();
			if (dimensions_w > w) {
				w = dimensions_w
			}
			if (dimensions_h > h) {
				h = dimensions_h
			}
			
		});
		wrap = new Element('div');
		
		var szer = 0;
		var naddatek = w*2; //++
		
		var tabs_w = new Array();
		var tabs_h = new Array();
		
		var wczesniejsze = children[0].getWidth() + children[1].getWidth() + children[2].getWidth() +60;
		
		var frst = children[startowe].getWidth();
		var startfrom = 840 - wczesniejsze -  (frst-12)*2 /2; //++
		var sz = (w+20) * l;
		
		wrap.setStyle({
			//marginLeft: n * -w + (840 - 0.5 * w) + 'px',
			marginLeft: startfrom +  'px',
			//width: children.length * (w+20 )+ 'px',
			width: sz + 'px',
			height: h + 'px'
			
		});
		

		

		for( i=0; i < children.length; i++){
			szer = szer + 20 +naddatek + children[i].getWidth();  //naddatek
			tabs_w[i] = children[i].getWidth() - 12;
			tabs_h[i] = children[i].getHeight() - 12;
			if(i!=3){	
				children[i].style.opacity = 0.6;
			}
		}
		for( i=children.length; i < children.length * 2; i++){
			tabs_w[i] = children[i-children.length].getWidth() - 12;
			tabs_h[i] = children[i-children.length].getHeight() - 12;
		}
		
		//alert(children.length + "  " + tabs_w);
		
		
		var zdj = children[3];
		var szer_zdj = zdj.getWidth();
		var wys_zdj = zdj.getHeight();
		
		zdj_img = zdj.select('img');
		zdj_img[0].setStyle({
			width: (szer_zdj-12) *2 + 'px', //++
			height:  (wys_zdj-12) *2 + 'px', //++
			//marginLeft: (-(szer_zdj/8)) +'px',
			//marginRight: (-(szer_zdj/8)) +'px',
			marginTop: (-(wys_zdj/8)) + 'px'
		});

		var kt = 0;
		children.each(function (child) {
			
			child.setStyle({
				cssFloat: 'left'

			});
			child.addClassName('n'+kt);
			kt++;
			wrap.appendChild(child)
		});

		element.appendChild(wrap);

		this.duration = (duration == undefined ? 1.0 : duration);
		this.wrap = wrap;
		this.nav = nav;
		this.w = w; // width of each slide
		this.h = h; // height of each slide
		this.l = l;
		this.lchild = l; // number of slides in slide show
		this.n = n; // number of the active slide
		this.children = children;
		this.ile = startfrom;
		this.startfrom = startfrom;
		this.tabs_w = tabs_w;
		this.tabs_h = tabs_h;
		this.minusMargin = 0;
	},

	isFirst: function () {
		return (this.n == 0)
	},

	isLast: function () {
		return (this.n == this.l - 1)
	},
	
	current: function () {
		return this.n;
	},
	
	currentLeftMargin: function (t) {
		
	},

	slide: function () {
		
		
		//alert(this.tabs_w + "\n" + this.tabs_w[this.l-this.lchild]);
		
		//this.tabs_w[this.l] = this.tabs_w[this.l-this.lchild];
		//this.tabs_h[this.l] = this.tabs_h[this.l-this.lchild];
		this.l++;
		var pierwszy = this.children[this.n-3].cloneNode(true);
		this.wrap.insert(pierwszy,{ position: 'after' });
		
		this.children = this.wrap.childElements();
		//alert(this.children);
		
		var szerwrap = this.wrap.getWidth() + pierwszy.getWidth() + 20;
		
		
		
		this.wrap.setStyle({
			//marginLeft: szerwrap +  'px',
			width: szerwrap + 'px'
		});
		
		var max_margin = 32000;
		if (szerwrap >= max_margin) {
			window.location.replace("http://sawp.pl");
		}
		
		//alert(this.l + " " + this.tabs_w[this.l] + "\n" + this.n + " " + this.tabs_w[this.n]);
		
		//if (this.current() != this.l-1) {

			var ten = this.children[this.n];
			var tamten = this.children[this.n+1]; //nastepny

			var ten_width = ten.getWidth(); //szerokosc aktualnego
			var tamten_width = tamten.getWidth(); //szerokosc nastepnego
			
			//var ten_width = this.tabs[this.n]; //szerokosc aktualnego
			//var tamten_width = this.tabs[this.n+1]; //szerokosc nastepnego
		
			var wpol_ten = ten_width/2;
			var wpol_tamten = tamten_width/2;
		
			this.ile = this.ile - Math.round((((this.tabs_w[this.n]/2)/5)+6 + wpol_tamten*2)); //++
			//this.ile = this.ile - Math.round((((this.tabs_w[this.n]/2)/1.8)+6 + wpol_tamten*1.8));
			//this.ile = this.ile - (wpol_ten + wpol_tamten*1.2);
			
			//alert(this.ile);
			
			new Effect.Morph(this.wrap, {
				style: 'margin-left: ' + this.ile + 'px;',
				duration: this.duration
			})

			new Effect.Morph(ten, {
				style: 'opacity: 0.6',
				duration: this.duration
			})
			new Effect.Morph(tamten, {
				style: 'opacity: 1',
				duration: this.duration
			})
		
			var szer_zdj_ten = ten.getWidth();
			var wys_zdj_ten = ten.getHeight();
			var szer_zdj_tamten = tamten.getWidth();
			var wys_zdj_tamten = tamten.getHeight();
			var zdj_ten_img = ten.select('img');
			var zdj_tamten_img = tamten.select('img');
	
			var szer_zdj_ten_offset = ten.offsetWidth;
			var wys_zdj_ten_offset = ten.offsetHeight;
		
			new Effect.Morph(zdj_tamten_img[0], {
				style: 'margin-top: -' + wys_zdj_tamten/8 + 'px; width: ' + (szer_zdj_tamten-12) *2 + 'px; height: ' + (wys_zdj_tamten-12) *2 + 'px;' , //++
				//style: 'margin-top: -' + wys_zdj_tamten/8 + 'px; width: ' + (szer_zdj_tamten-12) *1.8 + 'px; height: ' + (wys_zdj_tamten-12) *1.8 + 'px;' ,
				duration: this.duration
			})

			//var szer_okr = Math.round((szer_zdj_ten-12)/1.5);
			//var wys_okr = Math.round((wys_zdj_ten+8)/1.5);
			new Effect.Morph(zdj_ten_img[0], {
				style: 'margin-top: 0px; width: ' + this.tabs_w[this.n] + 'px; height: ' + this.tabs_h[this.n] + 'px;',
				duration: this.duration
			})

			//var szerwrap = this.wrap.getWidth() + pierwszy.getWidth() + 20;
			/*this.ile = this.ile + pierwszy.getWidth() + 20;
			this.wrap.setStyle({
				marginLeft: - this.ile +  'px',
			});
			this.children[0].remove();
			*/
			this.n++;
			
			if (this.n > this.lchild+4) {
				
				for(i=0;i<=this.lchild-1;i++) {
					this.minusMargin += this.children[i].getWidth() + 20;
					this.children[i].remove();
					this.l--;
					this.n--;
				}
				//this.ile = this.ile + minusMargin;
				this.wrap.setStyle({
					paddingLeft: this.minusMargin + 'px'
				});
				//this.n = this.lchild;
				//this.l = this.l - 1;
			}
			
			
			
		

		
	},

	prev: function (e) {
		this.n--;
		this.slide();
	},

	next: function (e) {
		this.n++;
		this.slide();
	}
});

