var
//  host           = "http://www.firstchurchboxford.org/1cc/",
//  host           = "C:/Church/Website/Live/video/",
    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;

//-----------------------------------------------------------------------------

var player= null;
function playerReady(obj)
{
player = document.getElementsByName(obj.id)[0];
};

//-----------------------------------------------------------------------------

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.setAttribute( "mdy", videos[i].dates[j] );
                a.onclick = selectVideo;
                a.appendChild( document.createTextNode( videos[i].dates[j] ) );
                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( videos[i].dates[j] );
//                      sel = l;
//                      sel.className = "cur";
//                  }
//              }
            }

            //Set redarrow to last video
            sel = l;
            sel.className = "cur";

            elem.appendChild( u );
            d.appendChild( elem );

            // rule after section
            elem = document.createElement( "hr" );
            d.appendChild( elem );

            // create the instructions
            a = document.createElement( "a" );
            //a.className = "none";
            //a.href = "";
            //a.setAttribute( "series", i );
            a.appendChild( document.createTextNode( "Click picture to start/pause" ) );
            d.appendChild( a );

        }
    }
}

//-----------------------------------------------------------------------------

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( 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( s ) {
    var
        pic,
        div = document.getElementById( "vid" );
        flash_thing = document.getElementById( "flash_thing" );
    
    //div.stop();
    //player.sendEvent('stop');

    // remove element if exists
    if( flash_thing ) {
        div.removeChild( flash_thing );
    };

    var n = document.createElement( "embed" );

    n.setAttribute( "id", "flash_thing" );
    //n.setAttribute( "name", "flash_thing" );
    n.setAttribute( "src", host + "player.swf" );
    n.setAttribute( "width", "610" );
    n.setAttribute( "height", "480" );
    n.setAttribute( "bgcolor", "#FFFFFF" );
    n.setAttribute( "allowfullscreen", "true" );
    n.setAttribute( "align", "center" );
    n.setAttribute( "type", "application/x-shockwave-flash" );
    n.setAttribute( "pluginspage", "http://www.macromedia.com/go/getflashplayer" );

    pic = "clickimage.jpg";

    n.setAttribute( "flashvars", "image=" + host + "img/" + pic + "&file=" + host + "video/" + s +
	 ".flv&autostart=false&screencolor=#FFFFFF");
    div.appendChild( n );
}


//-----------------------------------------------------------------------------

