/**
 * Slides object
 */
function Slides (imgElemId, captionElemId)
{
    /*
     * Private propereties & methods
     */
    var __slides    = null;
    var __imgElem   = document.getElementById(imgElemId);
    var __capElem   = document.getElementById(captionElemId);
    var __delayInMS = null;
    var __stopped   = true;
    var __currSlide = 0;
    var __select    = null;

    function add (src, caption) { __slides.push( { src: src, caption:caption } ) };
    function clean () { __slides = [ {src:'', caption:''} ] };

    function showSlide (idx, doSlideShow) {
        if (idx == null || idx < 0 || idx >= __slides.length) idx = 0;
        if (__slides.length < 2) return;

        if (__imgElem) __imgElem.src = __slides[idx].src;
        if (__capElem) __capElem.innerHTML = __slides[idx].caption || '';
        __currSlide = idx;

        if (doSlideShow && !__stopped)
            this.setTimeout(function () { showSlide(idx+1, true) }, __delayInMS);
    };

    function start (delayInSecs) {
        __delayInMS = ((delayInSecs || 3) < 1 ? 3 : delayInSecs) * 1000;
        __stopped   = false;

        showSlide(0, true);
    };

    function stop ()     { __stopped = true };
    function show (idx)  { showSlide(idx, false) };

    function showPrevNext (d) {
        if (__select && __select.onchange) {
            var idx = __currSlide + d;
            if (idx == null || idx < 0 || idx >= __slides.length) idx = 0;
            __select.selectedIndex = idx;
            __select.onchange();
        }
        else
            showSlide(__currSlide + d)
    };

    function showNext () { showPrevNext(1) };
    function showPrev () { showPrevNext(-1) };

    function insertSelect (elemId, onChange) {
        var elem, sel, buf = [];
        var selId = elemId + '_SlideSelect';

        buf.push('<select id="', selId, '">');
        buf.push('<option value="0">Go to:</option>');

        for (var i = 1, cnt = __slides.length; i < cnt; i++)
            buf.push('<option value="', i, '">  ', i, ' of ', (cnt-1), '  </option>');

        buf.push('</select>');

        elem = document.getElementById(elemId);
        elem.innerHTML = buf.join('');
        __select = document.getElementById(selId);

        if (onChange) {
            __select.onchange = (function (ev) {
                var idx = __select.options[__select.selectedIndex].value * 1;
                if (idx > 0) this.show(idx)
                else __currSlide = 0;
                onChange(ev, __select.selectedIndex);
            }).bindAsEventListener(this);
        }
        else
            __select.onchange = selChgFunc;
    };

    /*
     * Class initialization
     */
    clean();

    /*
     * Public methods
     */
    this.add   = add;
    this.clean = clean;
    this.start = start;
    this.stop  = stop;
    this.show  = show;
    this.showNext = showNext;
    this.showPrev = showPrev;
    this.insertSelect = insertSelect;
    this.count = function () { return __slides.length - 1 };
    this.currentSlide = function () { return __currSlide };
};
