//
// Set slideShowSpeed (milliseconds)
//
var slideShowSpeed = 20000;

//
// Duration of crossfade (seconds)
//
var crossFadeDuration = 3;

//
// Array to specify slides
//
var Slides        = new Array ();
var SlideTitle    = new Array ();
var SlideTimeout  = new Array ();
var SlideInactive = new Array ();


//
//  Debugging hooks
//
var debugSlideShow = 0;
var debugSlideShow_init_done = 0;
var SlideShowSpeedMultiplier = 1;

// ================================
// don't edit below this line
// ================================
var slideshow_timer;
var slide_no = 0;

function setSlideShowSpeed(x){
     slideShowSpeed = x;
}

function setSlideSpeed(url, timeout)
{
    if (timeout != undefined && timeout != "") {
        SlideTimeout[url] = timeout;
    }
}

function resetSlideSpeed(url)
{
    delete SlideTimeout[url];
}

function addSlide(url, title, timeout, inactive) {

    Slides.push(url);
    if (title != undefined && title != "") {
        SlideTitle[url]=title;
    }
    if (timeout != undefined && timeout != "") {
        setSlideSpeed(url,timeout);
    }
    if (inactive != undefined && inactive != 0) {
        SlideInactive[url] = 1;
    }
}

function nextSlide()
{
    var		looped_once = 0;

    /*
     *  Move to the next slide, wrapping around to the beginning as needed.
     */
    do {
        slide_no = slide_no + 1;
        if (slide_no > (Slides.length-1)) {
            slide_no=0;
            if (looped_once != 0) {
                alert("Excessive looping in nextSlide()");
                slide_no = 0;
                return;
            }
            looped_once = 1;
        }
    } while (SlideInactive[Slides[slide_no]] != undefined && SlideInactive[Slides[slide_no]] != 0);
}

function prevSlide()
{
    var		looped_once = 0;

    /*
     *  Move to the previous slide, wrapping around to the end as needed.
     */
    do {
        slide_no = slide_no - 1;
        if (slide_no < 0) {
            slide_no = Slides.length-1;
            if (looped_once != 0) {
                alert("Excessive looping in prevSlide()");
                slide_no = Slides.length-1;
                return;
            }
            looped_once = 1;
        }
    } while (SlideInactive[Slides[slide_no]] != undefined && SlideInactive[Slides[slide_no]] != 0);
}

function showSlide(url)
{
    /*
     *  Set the contents of the frame/iframe named main_frame to the
     *  correct URI.
     */
    slideshow_frame.location.href = url;

    /*
     *  Update the debug info.
     */
    updateSlideShowDebug();

//
//  The following code is for when we use frameset/frame and have the
//  optional titles being displayed by this routine.
//
//    if ( SlideTitle[url] != undefined ) {
//        parent.title_frame.document.open();
//	parent.title_frame.document.write("<head>\n");
//	parent.title_frame.document.write("<link href=\"dcodisplay.css\" rel=\"stylesheet\" type=\"text/css\">\n");
//	parent.title_frame.document.write("</head>\n");
//	parent.title_frame.document.write("<body>\n");
//        parent.title_frame.document.write("<div id=\"header_text\" align=\"center\">");
//        parent.title_frame.document.write(SlideTitle[url]);
//        parent.title_frame.document.write("</div>");
//	parent.title_frame.document.write("</body>\n");
//        parent.title_frame.document.close();
//        parent.document.getElementById("dco_frames").rows = "70, *, 35";
//    } else {
//        parent.title_frame.document.open();
//        parent.title_frame.document.write("<div align=\"center\"><h1>");
//        parent.title_frame.document.write("Blank title");
//        parent.title_frame.document.write("</h1></div>");
//        parent.title_frame.document.close();
//        parent.document.getElementById("dco_frames").rows = "0, *, 35";
//    }

}

function runSlideShow()
{
    var timeout = slideShowSpeed;
    var url     = Slides[slide_no];

    /*
     *  Compute the timeout.
     */
    if (SlideTimeout[url] != undefined && SlideTimeout[url] != "" && SlideTimeout[url] > 0) {
        timeout = SlideTimeout[url];
    }

    /*
     *  Show the slide, along with its title.
     */
    showSlide(url);

    /*
     *  Move to the next slide so that it displays next time.
     */
    nextSlide();

    /*
     *  Register a timeout to take us to the next slide.
     */
    slideshow_timer = setTimeout('runSlideShow()', timeout*SlideShowSpeedMultiplier);
}

function startSlideShow()
{
    /*
     *  Make sure we do not end up with two timeouts scheduled.
     */
    clearTimeout(slideshow_timer);

    /*
     *  If we are debugging...
     */
    if (debugSlideShow != 0) {

        /*
         *  ...make sure we have initialized debugging.
         */
        SlideShowDebugInit();
    }

    /*
     *  Check to make sure we have at least one active frame.
     */
    var num_active = 0;
    for (var i in Slides)
    {
        var url = Slides[i];

        if (SlideInactive[url] == undefined || SlideInactive[url] == 0) {
            num_active = num_active + 1;
        }
    }

    if (num_active==0) {
        alert("No slides active");
    }  else {

        if (debugSlideShow != 0) {

            /*
             *  Register a short timeout to start things, so that the debug window can come up.
             */
            slideshow_timer = setTimeout('runSlideShow()', 2000);
        } else {

            /*
             *  Run the slide show.
             */
            runSlideShow();
        }
    }
}

function stopSlideShow()
{
    clearTimeout(slideshow_timer);
}

function scrollSlideShow(evt)
{
    /*
     *  Avoid the multiple timeout issue.
     */
    clearTimeout(slideshow_timer);

    msg = 'on' + evt.type + ' event fired by ' + '"' + evt.keyCode + '"';

    if (evt.keyCode == 39) {		// Right
        nextSlide();
        msg = msg + "  scrolled Right";
    } else if (evt.keyCode == 37) {	// Left
        prevSlide();
        msg = msg + "  scrolled Right";
    }

    alert(msg);

    runSlideShow();
}
