// po načtení stránky spustí inicializaci
window.addEventListener?window.addEventListener("load",inicialization,false):window.attachEvent("onload",inicialization);

// globální proměnné
var imgArray = new Array();
var current = 0;
var speedEffect = 50; // rychlost prolínání obrázků
var speedImageDuration = 3000; // jak dlouho má obrázek zůstat zobrazený

function inicialization() {
	if(!document.getElementById || !document.createElement)return;

        // přidání dalšího souboru se stylama do hlavičky stránky
	css = document.createElement("link");
	css.setAttribute("href","css/stylesJS.css");
	css.setAttribute("rel","stylesheet");
	css.setAttribute("type","text/css");
	document.getElementsByTagName("head")[0].appendChild(css);


        // načte všechny obrázky, které jou vnořené v div s id="imageContainer""
        imgArray = document.getElementById("imageContainer").getElementsByTagName("img");
	for(i = 0; i < imgArray.length; i++) imgArray[i].xOpacity = 0;
        imgArray[0].style.display = "block";
	imgArray[0].xOpacity = .99;
        
        // první obrázek zviditelněn a po x vteřinách předá správu funkci slideShow
	setTimeout(slideShow, speedImageDuration);
}

function slideShow() {
	currentOpacity = imgArray[current].xOpacity;
	nextIndex = imgArray[current + 1]? current + 1 : 0;

	nextOpacity = imgArray[nextIndex].xOpacity;

        // simulace prolínání obrázků
	currentOpacity -= .05;
	nextOpacity += .05;

	imgArray[nextIndex].style.display = "block";
	imgArray[current].xOpacity = currentOpacity;
	imgArray[nextIndex].xOpacity = nextOpacity;

	setOpacity(imgArray[current]);
	setOpacity(imgArray[nextIndex]);

        // klesne-li opacity na nulu, spouští se rekurzivně slideShow
        // před tím ale zneviditelní stávající obrázek
	if(currentOpacity <= 0) {
            imgArray[current].style.display = "none";
            current = nextIndex;
            setTimeout(slideShow, speedImageDuration);
	} else {
            setTimeout(slideShow, speedEffect); // rychlost prolínání
	}

        // nastavení opacity pro všechny prohlížeče
	function setOpacity(obj) {

                if(obj.xOpacity > .99) {
                    obj.xOpacity = .99;
                    return;
		}

                obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity * 100) + ")";
	}
}
