var $Y = YAHOO, $D = $Y.util.Dom, $E = $Y.util.Event, $L = $Y.lang,
    $ = $D.get, $$ = $D.getElementsByClassName;

$E.onAvailable ('controlbox', function() {
    var playerId = this.id;  // This gets called before player is initialized.
    var baseVideoURL = 'http://video.housedems.com/wrongful-denial/';
    var videoList = $('video-list');
    var videos = $D.getChildren (videoList);
    var selected = $$('selected', 'li', videoList);

    $E.on (videos, 'mouseover', function (e) {
	if (!$D.hasClass (this, 'selected'))
	    $D.addClass (this, 'hover');
    });
    $E.on (videos, 'mouseout', function (e) {
	$D.removeClass (this, 'hover');
    });
    $E.on (videos, 'click', function (e) {
	// If the user doesn't have flash, send them to get it.
	if (!swfobject.hasFlashPlayerVersion ('9')) {
	    open ('http://www.adobe.com/go/getflashplayer');
	    return;
	}

	var player = $(playerId);
	if (this == selected) {
	    player.sendEvent ('PLAY');
	    return;
	}

	// Update the list selection.
	$D.removeClass (selected, 'selected');
	selected = this;
	$D.removeClass (this, 'hover');
	$D.addClass (this, 'selected');

	// Update the video caption and the transcript link.
	$('video-info').getElementsByTagName ('p')[0].innerHTML
	    = this.innerHTML;
	$('alt-links').getElementsByTagName ('a')[0].href
	    = 'transcripts/' + this.id + '.php';

	// Update the flash player.
	var flashvars = {
	    file: baseVideoURL + this.id + '.f4v',
	    image: baseVideoURL + this.id + '.jpg',
	    link: '/wrongful-denial/petition/'
	};
	player.sendEvent ('LOAD', flashvars);
	if (!location.search.substring(1).match (/\bautostart=\b/))
	    player.sendEvent ('PLAY');
    });
});
