var imageGroups=new Array();

function createImageGroup(imageNames, imageIds, groupName, secs){
    if (imageGroups[groupName] == undefined){
        imageGroups[groupName] = new Array();
        imageGroups[groupName].active=false;
        imageGroups[groupName].imageNames=imageNames;
        imageGroups[groupName].imageIds=imageIds;
        imageGroups[groupName].index=0;
        imageGroups[groupName].current=new Array();
        imageGroups[groupName].lastRandom=-1;
        imageGroups[groupName].onceOnlyList=new Array();
        imageGroups[groupName].timeTillNext=secs;
        imageGroups[groupName].repeatTime=secs;
        setAllImages(groupName);
        imageGroups[groupName].active=true;
    }
}

function processImageGroup(){
    for (var name in imageGroups){
        if (imageGroups[name] && imageGroups[name] != undefined){
            if (imageGroups[name].active==true){
                imageGroups[name].timeTillNext -= 1;
                if (imageGroups[name].timeTillNext<=0){
                    imageGroups[name].timeTillNext=imageGroups[name].repeatTime;
                    nextRandomImage(name);
                }
            }
        }
    }
    setTimeout("processImageGroup()", 1000);
}

processImageGroup();

function setAllImages(groupName){
    var grp = imageGroups[groupName];
    if (grp && grp.imageNames.length > 0){
        for (var i=0; i<grp.imageIds.length; i++){
            var id = grp.imageIds[i];
            var img = document.getElementById(id);
            if (img){
                var newSrc=nextImage(groupName);
                img.src=newSrc;
                grp.current[id]=newSrc;
            }
        }
    }
}
function setNextImage(groupName, imageId){
    var grp = imageGroups[groupName];
    if (grp){
        grp.current[imageId]="";
        var image = nextImage(groupName);
        if (image){
            blendimage(imageId+"_DIV", imageId, image, 2000);
            grp.current[imageId]=image;
        }
    }
}
function nextImage(groupName){
    var grp = imageGroups[groupName];
    var cleared = false;
    if (grp && grp.imageNames.length > 0){
        while (true){
            if (grp.index>=grp.imageNames.length){
                if (cleared) return null;
                grp.index=0;
                cleared = true;
            }
            var image = grp.imageNames[grp.index];
            grp.index += 1;
            var found = false;
            if (image.search("ONCE-ONLY")>=0){
                for (var onceOnlyIndx in grp.onceOnlyList){
                    if (image==grp.onceOnlyList[onceOnlyIndx]){
                        found = true;
                        break;
                    }
                }
                if (!found){
                    grp.onceOnlyList[grp.onceOnlyList.length]=image;
                }
            }
            if (!found){
                for (var currIndx in grp.current){
                    if (image==grp.current[currIndx]){
                        found = true;
                        break
                    }
                }
            }
            if (!found) break;
        }
        return image
    }
    return null;
}
function nextRandomImage(groupName){
    var grp = imageGroups[groupName];
    if (grp && grp.imageIds.length > 0){
        var ind = grp.lastRandom;
        var noImages = grp.imageIds.length;
        if (grp.imageIds.length>1){
            while (ind == grp.lastRandom){
                ind = Math.floor(Math.random()*noImages);
            }
        }else{
            ind = 0;
        }
        grp.lastRandom = ind;
        setNextImage(groupName, grp.imageIds[ind]);
    }
}
function opacity(id, opacStart, opacEnd, millisec) {
        //speed for each frame
        var speed = Math.round(millisec / 100);
        var timer = 0;

        //determine the direction for the blending, if start and end are the same nothing happens
        if(opacStart > opacEnd) {
          for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
          }
        }
        else if(opacStart < opacEnd) {
          for(i = opacStart; i <= opacEnd; i++) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
          }
        }
      }

      //change the opacity for different browsers
      function changeOpac(opacity, id) {
        var e = document.getElementById(id);
        if (e){
            var object = e.style;
            object.opacity = (opacity / 100);
            object.MozOpacity = (opacity / 100);
            object.KhtmlOpacity = (opacity / 100);
            if (navigator.appName == 'Microsoft Internet Explorer'){
                object.filter = "alpha(opacity=" + opacity + ")";
            }
        }
      }

      function shiftOpacity(id, millisec) {
        //if an element is invisible, make it visible, else make it ivisible
        if(document.getElementById(id).style.opacity == 0) {
          opacity(id, 0, 100, millisec);
        }
        else {
          opacity(id, 100, 0, millisec);
        }
      }

      function blendimage(divid, imageid, imagefile, millisec) {
        var speed = Math.round(millisec / 100);
        var timer = 0;

        //set the current image as background
        var ediv = document.getElementById(divid);
        var eimg = document.getElementById(imageid);
        if (ediv && eimg){
            ediv.style.backgroundImage = "url(" + eimg.src + ")";

            //make image transparent
            changeOpac(0, imageid);

            //make new image
            eimg.src = imagefile;


            //fade in image
            for(i = 0; i <= 100; i++) {
              setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
              timer++;
            }
        }

      }



function slideShowVersion(){
    return "$Revision: 1.3 $"+
          "$Date: 2011-08-12 17:16:00 $";
}

