function showAdvancedSearch() {
    var advancedForm = document.getElementById('advancedForm');
    var simpleForm = document.getElementById('simpleForm');
    advancedForm.CISOBOX1.value = simpleForm.CISOBOX1.value;
    advancedForm.CISOFIELD1.selectedIndex = 0;
    advancedForm.CISOOP1.selectedIndex = 0;
    for (var i=2; i<=4; i++) {
        document.getElementById('CISO'+i).style.display = 'none';
        advancedForm['CISOBOX'+i].value = '';
        advancedForm['CISOFIELD'+i].selectedIndex = 0;
        advancedForm['CISOOP'+i].selectedIndex = 0;
    }
    if (simpleForm.CISOBOX2.selectedIndex > 0) {
        document.getElementById('CISO2').style.display = 'block';
        advancedForm.CISOBOX2.value = simpleForm.CISOBOX2.options[simpleForm.CISOBOX2.selectedIndex].value;
        for (var i=0; i<advancedForm.CISOFIELD2.options.length; i++) {
            if (advancedForm.CISOFIELD2.options[i].value == simpleForm.CISOFIELD2.value) {
                advancedForm.CISOFIELD2.selectedIndex = i;
                break;
            }
        }
        for (var i=0; i<advancedForm.CISOOP2.options.length; i++) {
            if (advancedForm.CISOOP2.options[i].value == simpleForm.CISOOP2.value) {
                advancedForm.CISOOP2.selectedIndex = i;
                break;
            }
        }
    }
    document.getElementById('simpleSearch').style.display = 'none';
    document.getElementById('advancedSearch').style.display = 'block';
}

function showSimpleSearch(confirmed) {
    var advancedForm = document.getElementById('advancedForm');
    var simpleForm = document.getElementById('simpleForm');
    var stringTerm = null;
    var formatTerm = null;
    var isSimple = true;
    for (var i=1; i<=4; i++) {
        var term = advancedForm['CISOBOX'+i].value;
        if (term == '') {
            continue;
        }
        var field = advancedForm['CISOFIELD'+i].options[advancedForm['CISOFIELD'+i].selectedIndex].value;
        var mode = advancedForm['CISOOP'+i].options[advancedForm['CISOOP'+i].selectedIndex].value;
        if (field == 'CISOSEARCHALL' && mode == 'all') {
            if (stringTerm != null) {
                isSimple = false;
                break;
            } else {
                stringTerm = i;
            }
        } else if (field == 'format' && mode == 'exact') {
            if (formatTerm != null) {
                isSimple = false;
                break;
            } else {
                formatTerm = i;
            }
        }
    }
    simpleForm.CISOBOX1.value = '';
    simpleForm.CISOBOX2.selectedIndex = 0;
    if (stringTerm != null) {
        simpleForm.CISOBOX1.value = advancedForm['CISOBOX'+stringTerm].value;
    }
    if (formatTerm != null) {
        for (var i=0; i<simpleForm.CISOBOX2.options.length; i++) {
            if (simpleForm.CISOBOX2.options[i].value == advancedForm['CISOBOX'+formatTerm].value) {
                simpleForm.CISOBOX2.selectedIndex = i;
                break;
            }
        }
        if (simpleForm.CISOBOX2.selectedIndex == 0) {
            isSimple = false;
        }
    }
    if (!(isSimple || confirm("Your search cannot be converted to a simple form without losing data. Do you want to continue?"))) {
        return;
    }
    document.getElementById('simpleSearch').style.display = 'block';
    document.getElementById('advancedSearch').style.display = 'none';
}

function addSearchTerm() {
    for (var i=1; i<=4; i++) {
        var element = document.getElementById("CISO" + i);
        if (element.style.display == 'none') {
            element.style.display = 'block';
            return;
        }
    }
}

function removeSearchTerm(input) {
    var element = input.parentNode;
    element.style.display = 'none';
    var elements = element.getElementsByTagName('select');
    for (var i=0; i<elements.length; i++) {
        elements[i].selectedIndex = 0;
    }
    elements = element.getElementsByTagName('input');
    for (var i=0; i<elements.length; i++) {
        if (elements[i].name.indexOf('CISO') >= 0) {
            elements[i].value = '';
        }
    }
}

function searchCatalog(button) {
    var form = button.form;
    document.location.href = 'http://waldo.library.nashville.org/search/a?searchtype=d&searcharg=' + escape(button.form.CISOBOX1.value);
}

function setOpacity(element, opacity) {
    opacity = (opacity >= 100) ? 99.999 : opacity;
    // IE
    element.style.filter = "alpha(opacity:" + opacity + ")";
    // KHTML
    element.style.KHTMLOpacity = opacity/100;
    // Old mozilla
    element.style.MozOpacity = opacity/100;
    // CSS3
    element.style.opacity = opacity/100;
}

function fadeIn(id, opacity) {
    var element = document.getElementById(id);
    setOpacity(element, opacity);
    if (opacity <= 100) {
        opacity += 10;
        window.setTimeout("fadeIn('" + id + "'," + opacity + ")", 100);
    }
}

function SlideshowSet() {
    this.slideshows = new Array();
    this.offset = -1;
}

SlideshowSet.prototype.addSlideshow = function(slideshow) {
    this.slideshows[this.slideshows.length] = slideshow;
}

SlideshowSet.prototype.getNextSlideshow = function() {
    if (this.slideshows.length == 0) {
        return null;
    }
    this.offset++;
    if (this.offset >= this.slideshows.length) {
        this.offset -= this.slideshows.length;
    }
    return this.slideshows[this.offset];
}

SlideshowSet.prototype.showNextSlide = function(fade) {
    var slideshow = this.getNextSlideshow();
    var slide;
    var match = false;
    while (!match && (slide = slideshow.getNextSlide()) != null) {
        match = true;
        for (var i=0; i<this.slideshows.length; i++) {
            if (this.slideshows[i] == slideshow) {
                continue;
            }
            if (this.slideshows[i].getSlide().src == slide.src) {
                match = false;
                break;
            }
        }
    }
    slideshow.show(slide, fade);
}

function Slideshow(id) {
    this.id = id;
    this.imgElement = document.getElementById(id + "-image");
    this.slides = new Array();
    this.offset = 0;
    this.loadAhead = 3;
    this.loadingSlides = new Array();
}

Slideshow.prototype.show = function(slide, fade) {
    if (slide == null) {
        return;
    }
    if (fade) {
        fadeIn(this.imgElement.id, 0);
    }
    this.imgElement.src = slide.src;
    // we used to check the slide's image's pixels, but "auto" seems better
    this.imgElement.style.width = "auto";
    this.imgElement.style.height = "auto";
    if (slide.title != null && slide.title != '') {
        this.imgElement.setAttribute('title', slide.title);
    }
    if (this.imgElement.parentNode.nodeName == 'A' && slide.href != null && slide.href != '') {
        this.imgElement.parentNode.href = slide.href;
    }
}

Slideshow.prototype.showNextSlide = function(fade) {
    this.show(this.getNextSlide(), fade);
}

Slideshow.prototype.showPrevSlide = function(fade) {
    this.show(this.getPrevSlide(), fade);
}

Slideshow.prototype.getNextSlide = function() {
    if (this.slides.length == 0) {
        return null;
    }
    this.offset += 1;
    if (this.offset >= this.slides.length) {
        this.offset -= this.slides.length;
    }
    for (var i=1; i<=this.loadAhead; i++) {
        var slide = this.getSlide(this.offset + i);
        if (slide == null) {
            break;
        }
        slide.load();
    }
    return this.getSlide();
}

Slideshow.prototype.getPrevSlide = function() {
    if (this.slides.length == 0) {
        return null;
    }
    this.offset -= 1;
    if (this.offset < 0) {
        this.offset = this.slides.length - 1;
    }
    return this.getSlide();
}

Slideshow.prototype.getSlide = function(offset) {
    if (offset == null) {
        offset = this.offset;
    }
    if (offset >= 0 && offset < this.slides.length) {
        return this.slides[offset];
    }
    return null;
}

Slideshow.prototype.addSlide = function(src, title, href) {
    var offset = this.slides.length;
    var slide = new Slide(this, src, title, href);
    this.slides[offset] = slide;
    slide.offset = offset;
    if (offset < (this.offset + this.loadAhead)) {
        slide.load();
    }
}

function Slide(slideshow, src, title, href) {
    this.slideshow = slideshow;
    this.src = src;
    this.title = title;
    this.href = href;
    this.loading = false;
    this.loaded = false;
}

Slide.prototype.onload = function() {
    // this ends up being the Image and not the Slide
    this.slide.loading = false;
    this.slide.loaded = true;
}

Slide.prototype.load = function() {
    if (this.loading || this.loaded) {
        return;
    }
    this.loading = true;
    this.image = new Image();
    this.image.slide = this;
    this.image.onload = this.onload;
    //this.image.onerror
    //this.image.onabort
    this.image.src = this.src;
}

var itemTimeout;
var nextItemHref;
var prevItemHref;

function prevItem() {
    if (prevItemHref != null) {
        document.location.href = prevItemHref;
    }
}

function nextItem() {
    if (nextItemHref != null) {
        document.location.href = nextItemHref;
    }
}

function pauseItem() {
    clearTimeout(itemTimeout);
    itemTimeout = null;
    var element = document.getElementById('pauseItem');
    element.innerHTML = 'play';
    element.onclick = nextItem;
    return false;
}

function setItemTimeout() {
    itemTimeout = setTimeout("nextItem()", 5000);
}

function goBack() {
    if (history != null && history.back != null && history.length != null && history.length > 0) {
        history.back();
        return false;
    }
    return true;
}
