var
//  host           = "http://www.firstchurchboxford.org/1cc/",
    host           = "",
    num_visible    = 0,
    sel            = {},
    cur_mdy        = "",
    visible_series = "",

    // Set this variable to 'true' to allow only one series to be expanded at a time
    // The first video in the data file found to be expanded will be the series
    // expanded initially.  If this is 'false' then the visitor may expand and
    // collapse at their will.
    only1 = true;

//-----------------------------------------------------------------------------

function populateSermons() {
    // populate the selector panel
    var elem, u, l, a, mdy, dt, today, init;

    var d = document.getElementById( "select" );

    if (d) {
        today = new Date();

        // top rule
        elem = document.createElement( "hr" );
        d.appendChild( elem );
        for (var i = 0; i < videos.length; i++) {
            // create the title span
            a = document.createElement( "a" );
            a.className = "series vidsel";
            a.href = "";
            a.setAttribute( "series", i );
            a.onclick = selectSeries;
            a.appendChild( document.createTextNode( '"' + videos[i].title + '"' ) );
            d.appendChild( a );

            // list of dates
            elem = document.createElement( "div" );
            elem.id = "dates" + i;
            if ((only1 && num_visible > 0) || (!videos[i].expanded && (i < videos.length - 1 || num_visible > 0))) {
                elem.style.display = "none";
            }
            else {
                num_visible++;
                if (only1) {
                    visible_series = elem.id;
                }
            }
            u = document.createElement( "ul" );
            for (var j = 0; j < videos[i].dates.length; j++) {
                mdy = videos[i].dates[j].split( "/" );
                dt  = new Date( Number(mdy[2]) + 2000, Number(mdy[0]) - 1, mdy[1] );

                l = document.createElement( "li" );
                a = document.createElement( "a" );
                a.className = "vidsel";
                a.href = "";
                a.setAttribute( "mdy", mdy[2] + mdy[0] + mdy[1] );
                a.onclick = selectVideo;
                a.appendChild( document.createTextNode( videos[i].dates[j] + " - Part " + (j + 1) ) );
                l.appendChild( a );
                u.appendChild( l );
                if (today.getTime() > dt.getTime()) {
                    var diff = today.getTime() - dt.getTime();
                    if (diff / (1000 * 60 * 60 * 24) < 7) {
                        showVideo( dt, today, mdy[2] + mdy[0] + mdy[1] );
                        sel = l;
                        sel.className = "cur";
                    }
                }
            }
            elem.appendChild( u );
            d.appendChild( elem );

            // rule after section
            elem = document.createElement( "hr" );
            d.appendChild( elem );
        }
    }
}

//-----------------------------------------------------------------------------

function selectSeries() {
    var
        d, s = this.getAttribute( "series" );

    if (only1 && visible_series) {
        if (visible_series == "dates" + s) return false;

        d = document.getElementById( visible_series );
        d.style.display = "none";
        num_visible--;
        visible_series = "dates" + s;
    }

    d = document.getElementById( "dates" + s );
    if (d) {
        if (d.style.display == "none") {
            d.style.display = "block";
            num_visible++;
         } else {
            d.style.display = "none";
            num_visible--;
         }
    }
    return false;
}

//-----------------------------------------------------------------------------

function selectVideo() {
    var
        s = this.getAttribute( "mdy" );

    if (s == cur_mdy) {return false;}
    cur_mdy = s;

    var
        v,
        n,
        m  = Number(s.substring( 2, 4 )) - 1,
        d  = Number(s.substring( 4, 6 )),
        y  = 2000 + Number(s.substring( 0, 2 )),
        dt = new Date( y, m, d );

    if (sel.className) {
        sel.className = "";
    }
    sel = this.parentNode;
    sel.className = "cur";

    showVideo( dt, new Date(), s );

    // v = document.getElementById( "vid_title" );
    // n = document.createElement( "div" );
    // n.setAttribute( "id", "vid_title" );
    // n.className = "series";
    // n.appendChild( document.createTextNode( sel.parentNode.parentNode.previousSibling.firstChild.data ) );
    // n.appendChild( document.createElement( "br" ) );
    // n.appendChild( document.createTextNode( this.firstChild.data + " - " + prettyMonth( dt.getMonth() ) + " " + dt.getDate() + ", " + dt.getFullYear() ) );
    // v.parentNode.replaceChild( n, v );
    return false;
}

//-----------------------------------------------------------------------------

function showVideo( dt, today, s ) {
    var
        pic,
        v   = document.getElementById( "vid" ),
        n   = document.createElement( "embed" );

    n.setAttribute( "id", "vid" );
    n.setAttribute( "src", host + "flvplayer.swf" );
    n.setAttribute( "width", "640" );
    n.setAttribute( "height", "480" );
    n.setAttribute( "bgcolor", "#000000" );
    n.setAttribute( "align", "center" );
    n.setAttribute( "type", "application/x-shockwave-flash" );
    n.setAttribute( "pluginspage", "http://www.macromedia.com/go/getflashplayer" );

    //Make file available at 11:00pm
    if (today.getTime() > dt.getTime() + 1000*60*60*23) {
        pic = "eveningclickhere2.jpg";
    } else {
        pic = "notavailable.jpg";
    }
    n.setAttribute( "flashvars", "image=" + host + "img/" + pic + "&file=" + host + "video/ews" + s + ".flv&autostart=false" );
    v.parentNode.replaceChild( n, v );
}

//-----------------------------------------------------------------------------
