currImgArray = new Array();
var imgNr = 10;
theImages = new Array();
var maxImgHeight = new Array();

function checkImages(loopIndex) {
        theImages[loopIndex] = new Array();
        maxImgHeight[loopIndex] = 0;
        for(n=1; n<=imgNr; n++) {
                if(document.getElementById("cimg"+loopIndex+"_"+n)) {
                        theImages[loopIndex].push(n);

                        imgHeight = document.getElementById("cimg"+loopIndex+"_"+n).height;
                        if(imgHeight > maxImgHeight[loopIndex]) {
                                maxImgHeight[loopIndex] = imgHeight;
                        }
                }
        }

        return(theImages[loopIndex]);
}

function setGallery(loopIndex) {
        imagesArray = new Array();
        imagesArray[loopIndex] = checkImages(loopIndex);

        if(isEditmode) {
                firstId = 1;
        }
        else {
                firstId = imagesArray[loopIndex][0];
        }

        showElements(firstId);
        currImgArray[loopIndex] = loopIndex+"_"+firstId;

        if(isEditmode)        {
                for(p=1; p< (imgNr+1); p++) {
                        document.getElementById("flipbox"+loopIndex).style.fontSize = "11px";
                        document.getElementById("flipbox"+loopIndex).style.lineHeight = "14px";
                        if(document.getElementById("link"+loopIndex+"_"+p)) {
                                document.getElementById("link"+loopIndex+"_"+p).style.display = "inline";
                        }
                }
        }
        else {                // flip links
                if(document.getElementById("link"+currImgArray[loopIndex])) {
                        document.getElementById("link"+currImgArray[loopIndex]).style.display = "inline";
                }
                lastId = imagesArray[loopIndex][imagesArray[loopIndex].length - 1];
                if(document.getElementById("totalnr"+loopIndex+"_"+lastId)) {
                        document.getElementById("totalnr"+loopIndex+"_"+lastId).style.display = "inline";
                }
        }

        /*
        if(maxImgHeight[loopIndex] > 0 && !isEditmode) {
                document.getElementById("imgdivscontainer"+loopIndex).style.height = maxImgHeight[loopIndex]+"px";
        }*/

        if(imagesArray[loopIndex].length < 2 && !isEditmode) {                        // flip controls ausblenden wenn nur 1 bild
                document.getElementById("flipbox"+loopIndex).style.display = "none";
        }
}

function flipBy(offset,loopIndex) {
        count = 0;

        if(isEditmode) {
                limit = imgNr;
        }
        else {
                limit = theImages[loopIndex][theImages[loopIndex].length - 1];
        }

        theCurrImg = currImgArray[loopIndex].split("_");
        currImgIndex = parseInt(theCurrImg[1]);

        if((currImgIndex + offset > 0) && ((currImgIndex + offset) <= limit)) {
                hideElements(currImgArray[loopIndex]);

                ///// blaettern
                currImgIndex += offset;
                currImgArray[loopIndex] = theCurrImg[0]+"_"+currImgIndex;

                if(!isEditmode) { // naechstes oder voriges existierendes bild suchen

                        while(!document.getElementById("cimg"+currImgArray[loopIndex])) {
                                count++;
                                if(count > imgNr) {
                                        break;
                                }

                                //currImgArray[loopIndex] += offset;                                // !!!! geht nicht mehr
                                theCurrImg = currImgArray[loopIndex].split("_");
                                currImgIndex = parseInt(theCurrImg[1]);
                                currImgIndex += offset;
                                currImgArray[loopIndex] = theCurrImg[0]+"_"+currImgIndex;
                        }
                }

                showElements(currImgArray[loopIndex]);
        }
}

function flipTo(theId,loopIndex) {
        hideElements(currImgArray[loopIndex]);
        currImgArray[loopIndex] = loopIndex+"_"+theId;
        showElements(currImgArray[loopIndex]);
}

function showElements(theId) {
        if(document.getElementById("caption"+theId)) {
                document.getElementById("caption"+theId).style.display = "block";
        }

        if(document.getElementById("imgdiv"+theId)) {
                document.getElementById("imgdiv"+theId).style.display = "block";
        }

        if(document.getElementById("link"+theId)) {
                document.getElementById("link"+theId).style.display = "inline";
        }
}

function hideElements(theId) {
        if(document.getElementById("caption"+theId)) {
                document.getElementById("caption"+theId).style.display = "none";
        }

        document.getElementById("imgdiv"+theId).style.display = "none";

        if(!isEditmode) {
                document.getElementById("link"+theId).style.display = "none";
        }
}


function setProjectsHeight() {
        correction = 380;
        if(window.innerHeight) {
                projectsHeight = window.innerHeight - correction;
        }
        else {
                projectsHeight = document.documentElement.clientHeight - correction;
        }

        if(projectsHeight < 150) {
                projectsHeight = 150;
        }

        return(projectsHeight);
}
