var _pvpPlaylist = [{'index': 0, 'episode': 'Cuba - The Accidental Eden', 'description': "This small island's varied landscape, its location and its place at the center of Cold War politics have all combined to preserve some of the richest and most unusual natural environments of the hemisphere.", 'link_title': 'Go to Nature site', 'title': 'Cuba - The Accidental Eden', 'url': 'http://www.pbs.org/media/nights/100926-naat002711_320.mov', 'link_url': 'http://www.pbs.org/nature', 'nola': 'NAAT', 'program': 'Nature', 'program_logo': 'http://www.pbs.org/images/programlogos/nature_98px.gif', 'duration': 30, 'clip_image': 'http://www.pbs.org/images/features/nature/nature-cubatheaccidentaleden.jpg'}, {'index': 1, 'episode': 'Inspector Lewis - Falling Darkness', 'description': 'When two murders share a link with their own Doctor Hobson, Lewis and Hathaway try to clear her name. ', 'link_title': 'Go to Masterpiece Mystery! site', 'title': 'Inspector Lewis - Falling Darknes', 'url': 'http://www.pbs.org/media/nights/100926-mast004025_320.mov', 'link_url': 'http://www.pbs.org/masterpiece', 'nola': 'MAST', 'program': 'Masterpiece Mystery!', 'program_logo': 'http://www.pbs.org/images/programlogos/mystery_98px.gif', 'duration': 30, 'clip_image': 'http://www.pbs.org/images/features/masterpiece/masterpiece-mystery-inspectorlewis-fallingdarkness.jpg'}, {'index': 2, 'episode': 'The Tenth Inning', 'description': 'Thousands of bats, three home run records and one "curse" have been broken since Ken Burns last explored the history of America\'s national pastime with his landmark 1994 PBS series BASEBALL. Now, Burns and co-director update the series with THE TENTH INNING.', 'link_title': 'Go to The Tenth Inning site', 'title': 'The Tenth Inning', 'url': 'http://www.pbs.org/media/nights/100928-btin000000_320.mov', 'link_url': 'http://www.pbs.org/thetenthinning', 'nola': 'BTIN', 'program': 'Ken Burns', 'program_logo': 'http://www.pbs.org/images/programlogos/be-more.gif', 'duration': 30, 'clip_image': 'http://www.pbs.org/images/features/1offs/thetenthinning.jpg'}, {'index': 3, 'episode': 'Philadelphia - Hour 2', 'description': "The Roadshow visits the Philadelphia shop and museum of appraiser Fred Oster to view his impressive collection of instruments and learn more about the city's long history as a center for musical instrument makers.", 'link_title': 'Go to Antiques Roadshow site', 'title': 'Philadelphia - Hour 2', 'url': 'http://www.pbs.org/media/nights/101004-anro001105_320.mov', 'link_url': 'http://www.pbs.org/antiquesroadshow', 'nola': 'ANRO', 'program': 'Antiques Roadshow', 'program_logo': 'http://www.pbs.org/images/programlogos/antiquesroadshow_98px.gif', 'duration': 30, 'clip_image': 'http://www.pbs.org/images/features/roadshow/antiquesroadshow-philadelphia-hour2.jpg'}, {'index': 4, 'episode': 'Preview - Circus', 'description': 'Hit the road with CIRCUS, a new six-hour series from PBS that takes you on an unforgettable trip with the legendary Big Apple Circus. ', 'link_title': 'Go to PBS.org', 'title': 'Preview - Circus', 'url': 'http://www.pbs.org/media/nights/100808-ppci000000_320.mov', 'link_url': 'http://www.pbs.org', 'nola': 'PPCI', 'program': 'PBS Presents', 'program_logo': 'http://www.pbs.org/images/programlogos/be-more.gif', 'duration': 30, 'clip_image': 'http://www.pbs.org/images/features/1offs/circus-preview.jpg'}];
var _pvpCurrent = 0;
var _pvpStation;

function _pvpLoadVideo(obj) {
	_pvpCurrent = obj.index;
	_pvpSwapVideo(obj);
	document.getElementById("id_video_headline").innerHTML = obj.title;
	document.getElementById("id_duration").innerHTML = _pvpFormatDuration(obj.duration);
	document.getElementById("id_short_video_info").innerHTML = obj.description;
	document.getElementById("id_program_logo_link").href = _pvpLocalLink(obj.link_url);
	document.getElementById("id_program_logo_image").src = obj.program_logo;
	document.getElementById("id_program_logo_image").alt = obj.program;
	document.getElementById("id_episode_title").innerHTML = obj.episode;
	document.getElementById("id_link_title").href = _pvpLocalLink(obj.link_url);
	document.getElementById("id_link_title").innerHTML = obj.link_title;
	document.getElementById("id_episode_airdate").innerHTML = _pvpBroadcastLink(obj.nola);
	document.getElementById("id_program_logo").style.visibility = obj.program_logo ? "visible" : "hidden";

	for( var i=0; i<_pvpPlaylist.length; i++ ) {
		if( document.getElementById("id_navigation_"+i) ) {
			document.getElementById("id_navigation_"+i).className = (_pvpCurrent==i ? 'current' : 'notcurrent');
		}
	}
}
function _pvpSwapVideo(obj) {
    try {
        document.pvpMovieObj.SetURL(obj.url);
        document.pvpMovieObj.Play();
    } catch(e) {
	document.getElementById("video_player").innerHTML = _pvpGetVideoTag(obj);
    }
}
function _pvpGetVideoTag(obj) {
	return '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" width="320" height="256" id="pvpMovieObj"><param name="src" value="' + obj.url + '" /><embed width="320" height="256" src="' + obj.url + '" type="video/quicktime" pluginspage="www.apple.com/quicktime/download" name="pvpMovieObj" enablejavascript="true" /></object>';
}
function _pvpGetPrevIndex() {
	var prev = _pvpCurrent-1;
	if( prev < 0 )
		prev = _pvpPlaylist.length-1;
	return prev;
}
function _pvpLocalLink(url) {
	try {
		if( _pvpStation )
			return 'http://www.pbs.org/cgi-registry/golocal?station='+_pvpStation.toUpperCase()+'&site='+encodeURIComponent(url);
	} catch(e) {}
	return url;
}
function _pvpFormatDuration(duration) {
	var s = duration%60;
	var m = (duration-s)/60;
	if( m < 10 )
                m = '0' + m;
	if( s < 10 )
		s = '0' + s;
	return m + ':' + s;
}
function _pvpBroadcastLink(nola) {
	try {
        if( _pvpStation && nola ){
            var popup_str;
            if(nola.substr(0,4).toUpperCase() == 'ANRO'){
                popup_str = 'program_id=899704';
            }else if(nola.substr(0,4).toUpperCase() == 'MAST'){
                popup_str = 'program_id=9421';
            }else{
                popup_str = 'title_id=' + nola.substr(0,4);
            }
            return '<a href="' + _pvpLocalLink('http://www.pbs.org/modules/tvschedules/includes/programinfopopup.html?' + popup_str + '&station=' + _pvpStation) + '" onclick="window.open(this.href,\'ProgramInformation\',\'scrollbars=yes,resizable=yes,width=605,height=450\');return false;">Get broadcast time</a>';
         }
    } catch(e) {}
    return "";
}
function pvpWritePlayer(station) {
	_pvpStation = station;
	var output = '';
	output += 	'<div id="pbs_pvp_container">';
	output +=       '\n<!-- Container start -->';
	output +=       '\n	<div class="bsgutter10">';
	
	//Start navigation
	output +=       '\n	<div id="video_number_navigation">';
	output +=       '\n		<ul>';

	output +=       '\n			<li class="previous_navigation"><a href="javascript:_pvpLoadVideo(_pvpPlaylist[_pvpGetPrevIndex()])" id="id_previous_navigation">&lt; Previous video</a></li>';

	for( var i=0; i<_pvpPlaylist.length; i++ ) {
		output +=       '\n			<li><a href="javascript:_pvpLoadVideo(_pvpPlaylist[' + i + '])" id="id_navigation_' + i + '" class="' + (i==0?'current':'notcurrent') + '">' + (i+1) + '</a></li>';
	}

	output +=       '\n			<li class="next_navigation"><a href="javascript:_pvpLoadVideo(_pvpPlaylist[(_pvpCurrent+1)%_pvpPlaylist.length])" id="id_next_navigation">Next video &gt;</a></li>';
	output +=       '\n		</ul>';
	output +=       '\n	</div>';

	output +=       '\n	<div id="main_area">';
	output +=       '\n	<!-- Main area start -->';
	output += 	'\n		<div class="bsgutter5">';

        output +=       '\n             <div id="video_player_container">';

	output +=       '\n			<div class="video_player" id="video_player">';
	output +=	'\n				' +  _pvpGetVideoTag(_pvpPlaylist[_pvpCurrent]);

	output +=       '\n			</div>';

	output +=       '\n			<div id="video_info">';
	output +=       '\n				<h1 class="video_headline" id="id_video_headline">' + _pvpPlaylist[_pvpCurrent].title + '</h1>';
	output +=       '\n				<p class="duration" id="id_duration">' + _pvpFormatDuration(_pvpPlaylist[_pvpCurrent].duration) + '</p>';
	output +=       '\n				<p class="short_video_info" id="id_short_video_info">' + _pvpPlaylist[_pvpCurrent].description + '</p>';
	output +=       '\n			</div>';

	output +=       '\n  		  </div>'; //video_player_container

	output +=       '\n			<div id="episode_info">';
	output +=       '\n  	  		<div class="bsgutter7">';
	output +=       '\n					<div class="program_logo" id="id_program_logo" style="visibility:' + (_pvpPlaylist[_pvpCurrent].program_logo ? "visible" : "hidden") +  '">';
	output +=       '\n						<a href="' + _pvpLocalLink(_pvpPlaylist[_pvpCurrent].link_url) + '" id="id_program_logo_link"><img src="' + _pvpPlaylist[_pvpCurrent].program_logo + '" alt="' + _pvpPlaylist[_pvpCurrent].program + '" id="id_program_logo_image" /></a>';
	output +=       '\n					</div>';

	output += 	'\n						<div id="episode_text">';
	output += 	'\n							<div id="pbs_logo">';
	output +=       '\n							<a href="' + _pvpLocalLink('http://www.pbs.org/') + '"><img id="pbslogo_advanced" src="http://www.pbs.org/modules/pvp/css/pbslogo_advanced.gif" width="55" height="28" alt="PBS" /><img id="pbslogo_basic" src="http://www.pbs.org/modules/pvp/css/pbslogo_basic.gif" width="55" height="28" alt="PBS" /></a>';
	output +=       '\n						</div>';
	output +=       '\n						<div>';
	output +=       '\n							<span class="episode_info_header">Episode:</span>';
	output +=       '\n							<span class="episode_title" id="id_episode_title">' + _pvpPlaylist[_pvpCurrent].episode + '</span>';
	output +=       '\n						</div>';

	output +=       '\n						<div id="airdate_details">';
	output +=       '\n     	                		        <span class="episode_airdate" id="id_episode_airdate">' + _pvpBroadcastLink(_pvpPlaylist[_pvpCurrent].nola) + '</span>';
	output +=       '\n						</div>';

	output +=       '\n						<a href="' + _pvpLocalLink(_pvpPlaylist[_pvpCurrent].link_url) + '" id="id_link_title">' + _pvpPlaylist[_pvpCurrent].link_title + '</a>';
	output +=       '\n             	</div>';

	output +=       '\n					<div id="floathack"><!-- This is a hack to make the floated logo and episode info box depths work --></div>';
	output +=       '\n				</div>'; //end bsgutter7
	output +=       '\n			</div>';
	output +=       '\n		</div>'; //end bsgutter5

	output +=       '\n	<!-- Main area end -->';
	output +=       '\n	</div>';

	//More video links
	output +=       '\n	<div id="video_links">';
	output +=       '\n		<dl>';
	output +=       '\n			<dt class="morevideos_headline">More videos</dt>';

	for( var i=0; i < _pvpPlaylist.length; i++ ) {
		output +=       '\n				<dd class="promo150x80" id="id_promo150x80_' + i + '"><a href="javascript:_pvpLoadVideo(_pvpPlaylist[' + i + ']);"><img src="' + _pvpPlaylist[i].clip_image + '" width="150" height="80" alt="' + _pvpPlaylist[i].program + '" class="video_promo_art" /></a></dd>';
		output +=       '\n				<dd class="promoset" id="id_promoset_' + i + '">';
		output +=       '\n      			        <div class="bsgutter5">';
		output +=       '\n						<dl>';
		output +=       '\n							<dt class="promoset_link"><a href="javascript:_pvpLoadVideo(_pvpPlaylist[' + i + ']);">' + _pvpPlaylist[i].program + ': ' + _pvpPlaylist[i].title + '</a></dt>';
		output +=       '\n							<dd class="promoset_text">' + _pvpPlaylist[i].description + '</dd>';
		output +=       '\n						</dl>';
		output +=       '\n    		        	        </div>';  //end bsgutter5
		output +=       '\n				</dd>';
	}
	
	output +=       '\n		</dl>';
	output +=       '\n	</div>';

        output +=       '\n     <!-- Quicktime link start -->';
        output +=       '\n    	<div id="software_link">';
        output +=       '\n    		<p><a href="http://www.apple.com/quicktime/download/" target="_blank"><img src="http://www.pbs.org/modules/pvp/css/quicktimelogo.gif" width="42" height="42" alt="Get QuickTime" /></a>';
        output +=       '\n    		<a href="http://www.apple.com/quicktime/download/" target="_blank">Download free QuickTime plug-in</a></p>';
        output +=       '\n    	</div>';
        output +=       '\n    	<!-- Quicktime link end -->';

        output +=       '\n</div>'; //end bsgutter10

	output +=       '\n<!-- Container end -->';
	output +=       '\n</div>';
	document.write(output);
    cOnUnload = window.onunload;
    window.onunload = function() {
        var player = document.getElementById('pbs_pvp_container');
        player.parentNode.removeChild(player);
        if (cOnUnload != null && typeof(cOnUnload) == 'function') {
            cOnUnload();
        }
    }
}

