function SlideshowController(smallPics, largePics, xlargePics, headshotNames) {

var log = log4javascript.getDefaultLogger("dd_utility");

    // This refers to the index in the UI.
    var selectedIndex = 0;
    // This index points to an index in the image
    // array that is the first image displayed.
    var firstDisplayedImageIndex = 0;
    var timerId = null;
    var timerRunning = false;
    var delay = 2000;
    var totalImages = smallPics.length;
    var totalDisplayedImages = smallPics.length;//(smallPics.length < 10) ? smallPics.length : 10;

    // arrays with image URL's
    var smallHeadshots = smallPics;
    var largeHeadshots = largePics;
    var xlargeHeadshots = xlargePics;
    var names = headshotNames;
    
    this.init = function() {
        _updateImageDisplay(true);        
        this.display(selectedIndex);
        this.auto();
    }

	this.showHeadshots = function(){
		document.getElementById('video_gallery').style.display = 'none';
		document.getElementById('sound_gallery').style.display = 'none';
		document.getElementById('headshot_gallery').style.display = '';
	}
	
	this.showSoundClips = function(){
		document.getElementById('video_gallery').style.display = 'none';
		document.getElementById('headshot_gallery').style.display = 'none';
		document.getElementById('sound_gallery').style.display = '';
	}
	
	this.showVideoClips = function(){
		document.getElementById('headshot_gallery').style.display = 'none';
		document.getElementById('sound_gallery').style.display = 'none';
		document.getElementById('video_gallery').style.display = '';
	}

    this.previous = function() {
        this.display(selectedIndex - 1);
    }

    this.next = function() {
        this.display(selectedIndex + 1);
        if (timerRunning) {
            clearTimeout(timerId);
            timerId = setTimeout('slideshowController.next()', delay);
        }
    }

    this.displayFull = function() {
        var url = xlargeHeadshots[_normalize(selectedIndex + firstDisplayedImageIndex)];
        openWindow(url, 'width=700,height=850');
    }

    this.display = function(idx) {
    log.info('selectedIndex='+selectedIndex);
        var currentDisplayElement;
        if (idx < 0) {
            //selectedIndex = 0;
            selectedIndex = totalDisplayedImages - 1;
            //firstDisplayedImageIndex = _normalize(idx + firstDisplayedImageIndex);
            _updateImageDisplay(false);
        } else if (idx > totalDisplayedImages - 1) {
            //selectedIndex = totalDisplayedImages - 1;
            selectedIndex = 0;
            //firstDisplayedImageIndex = _normalize(idx + firstDisplayedImageIndex - selectedIndex);
            _updateImageDisplay(false);
        } else {
            // Unselect current image
            currentDisplayElement  = document.getElementById("anchor_" + selectedIndex);
            if (currentDisplayElement != null) {
                currentDisplayElement.className = null;
            }
            
            selectedIndex = idx;
        }
        // Set selected image(box), update main image and update name of person
        currentDisplayElement  = document.getElementById("position_selected");
        currentDisplayElement.src = largeHeadshots[_normalize(firstDisplayedImageIndex + selectedIndex)];
        currentDisplayElement  = document.getElementById("anchor_" + selectedIndex);
        if (currentDisplayElement != null) {
            currentDisplayElement.className = "selected";
            currentDisplayElement = null;
        }
        var title = currentDisplayElement  = document.getElementById("nameTitle");
        if (title != null) {
            title.innerHTML = names[_normalize(firstDisplayedImageIndex + selectedIndex)];
        }
    }

    this.auto = function() {
        if (!timerRunning) {
            _startTimer();
        }
    }

    this.manual = function() {
        if (timerRunning) {
            _stopTimer();
        }
    }

    this.toggleAutoplay = function () {
        if (timerRunning) {
            _stopTimer();
        } else {
            _startTimer();
        }
    }

    function _startTimer() {
        timerId = setTimeout('slideshowController.next()', delay);
        timerRunning = true;
        document.getElementById('autoplayControlImage').src = document.getElementById('autoplayImagePause').src;
    }

    function _stopTimer() {
        clearTimeout(timerId);
        timerRunning = false;
        document.getElementById('autoplayControlImage').src = document.getElementById('autoplayImagePlay').src;
    }

    function _updateImageDisplay(redisplay) {
        var currentDisplayElement;
        for (var displayIndex = 0; displayIndex < totalDisplayedImages; displayIndex++) {
            currentDisplayElement  = document.getElementById("position_" + displayIndex);
            if (currentDisplayElement != null) {
            	if ( redisplay )
                	currentDisplayElement.src = smallHeadshots[_normalize(firstDisplayedImageIndex + displayIndex)];
            }
            currentDisplayElement  = document.getElementById("anchor_" + displayIndex);
            if (currentDisplayElement != null) {
                currentDisplayElement.className = null;
            }
        }
    }

    function _normalize(idx) {
        return (idx + totalImages) % totalImages;
    }

    return this;
}

