/* ROTATOR */
/* milo ze chcesz zobaczyc jak to dziala :) */

var rotateTime = 4000;
var speed = 0.05; // im mniejsza liczba tym wolniej i płynniej
var rotateBannerOpacity = 1;
var banners = new Array();
var banners2 = new Array();
var zindex_start = 30;
var objInterval;

function rotateBgInit(){
	rotateBannerOpacity = 0;
	objInterval = setInterval("rotateOpacity()", 50); // wykonujemy ciagle
}

function rotateAdd(id){
	banners.push(id);
	banners2 = banners.slice(0); // tworzymy kopie tablicy
}

function rotateOpacity(){
	var opa = document.getElementById(banners[0]);
	if(opa !== null && opa.value != ""){
		if(rotateBannerOpacity == 0){
			opa.style.zIndex = ++zindex_start;
			//Gdy ostatni banner, to nalezy pokazac banner nr 1
			if(banners.length==1){
				opa2 = document.getElementById(banners2[0]);
				opa2.style.opacity = 1;
				opa2.style.filter = "alpha(opacity = "+1*100+")";
			}
			// Gdy mamy jeszcze bannery w wektorze, to pokazujemy nastepny
			if (banners.length > 1) {
				opa2 = document.getElementById(banners[1]);
				opa2.style.opacity = 1;
				opa2.style.filter = "alpha(opacity = "+1*100+")";
			}
			rotateBannerOpacity += speed;
		}
		if(rotateBannerOpacity < 1 && rotateBannerOpacity > 0){
			rotateBannerOpacity += speed;
			if (rotateBannerOpacity > 1) rotateBannerOpacity = 1;
			opa.style.opacity = rotateBannerOpacity;
			opa.style.filter = "alpha(opacity = "+rotateBannerOpacity*100+")";
		}
		else{
			clearInterval(objInterval);	// czyscimy handlera
			setTimeout("rotateChange()", 50);
		}
	}
	else{
		clearInterval(objInterval);	// czyscimy handlera
		setTimeout("rotateChange()", 50);
	}
}

function rotateChange(){
	last = banners.shift(); //usuwamy pierwszy banner z wektora
	if(banners.length==0){
		banners = banners2.slice(0); //jesli usunelismy wszystkie bannery to przywracamy kopie
	}
	setTimeout("rotateBgInit()", rotateTime);
}

jQuery(document).ready(function() {
	// oparte o jquery ale w zasadzie moznaby bez :)
	if(banners.length > 1) setTimeout("rotateChange()", 1);
});
