var mediastring = 'qt';
if(navigator.userAgent.indexOf('Win') != -1){
	mediastring = 'wm';
}
var mediasize = 3;
var pickedown = 0;
var flipfix = 0;
window.name = 'player';
if (navigator.plugins) { 
	var np = navigator.plugins; 
	var npl = navigator.plugins.length; 
	for (i=0; i<npl; i++) { 
 	if (np[i].name.indexOf("Flip4Mac") != -1) { 
		flipfix = 1;
 	} 
	}
}
function setcookie(name,value) {
 	var today = new Date();
	 var expire = new Date();
	 var nDays = 360;
 	 expire.setTime(today.getTime() + 3600000*24*nDays);
	 document.cookie = name+"="+escape(value) + ";path=/frontlineworld/;expires="+expire.toGMTString();
}

function report(rf,rs){
	snitch = new Image;
	station = getcookie('pbsol.station');
	snitch.src = "http://videocheck.entropymedia.com/videotrack/blank.gif?p=" + programcode + "&c=" + title + "&f=" + rf + "&s=" + rs + "&l=" + station;
}

function getcookie(name) {
   var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function selector() {
	var destination = title;
	if(num == 0){
		destination = title;
	} else {
		destination = num;
	}
	var cl = '<p><span class="title">Please select a media player</span></p>';
	if(mediastatus[0] == 'qt'){
		var count = 0;
		cl += '<p>Quicktime 6: ';
		if(sizes[0] == 'xl'){
			cl += '<a href="'+destination+'.html?&c=4qt">Large (350k, 2x size)</a>';
			cl += ' | <a href="'+destination+'.html?&c=3qt">Medium (350k)</a>';
			count++;
		}
		if(sizes[1] == 'hi'){
			if(count > 0) cl += ' | ';
			cl += '<a href="'+destination+'.html?&c=2qt">Small (220k)</a>';
			count++;
		}
		if(sizes[2] == 'lo'){
			if(count > 0) cl += ' | ';
			cl += '<a href="'+destination+'.html?&c=2qt">Dial-up (56k)</a>';
			count++;
		}
		cl += '</p>';
	}	
	if(mediastatus[2] == 'rp'){
		var count = 0;
		cl += '<p>RealPlayer 10: ';
		if(sizes[1] == 'hi'){
			cl += '<a href="'+destination+'.html?&c=2rp">Small (220k)</a>';
			count++;
		}
		if(sizes[2] == 'lo'){
			if(count > 0) cl += ' | ';
			cl += '<a href="'+destination+'.html?&c=1rp">Dial-up (56k)</a>';
			count++;
		}
		cl += '</p>';
	}
	if(mediastatus[1] == 'wm'){
		var count = 0;
		cl += '<p>Windows Media 9: ';
		if(sizes[0] == 'xl'){
			cl += '<a href="'+destination+'.html?&c=3wm">Medium (350k)</a>';
			count++;
		}
		if(sizes[1] == 'hi'){
			if(count > 0) cl += ' | ';
			cl += '<a href="'+destination+'.html?&c=2wm">Small (220k)</a>';
			count++;
		}
		if(sizes[2] == 'lo'){
			if(count > 0) cl += ' | ';
			cl += '<a href="'+destination+'.html?&c=1wm">Dial-up (220k)</a>';
			count++;
		}
		cl += '</p>';
	}	
	document.write(cl);
}


function vidhandleclick(url,target){
	var win = window.open(url, target);
}

function displaychapter() {
	var cl = '<table class="chnav"><tr>';
	var numrows = 1;
	for (var cc = 1; cc < (total + 1); cc++) {
		if(num == cc){
			cl += '<td class="sel"><p>' + cc + '</p></td><td class="line"></td>';
		} else {
			cl += '<td class="norm" onclick="location=\'' + cc + '.html?c=' + mediasize + mediastring + '\'"><p>' + cc + '</p></td><td class="line"></td>';
		}
		numrows++;
	}
	cl += '</tr><tr><td colspan="' + (numrows * 2) + '"><p class="ch" align="center">CHAPTERS</p></td></tr></table>';
	document.write(cl);
}

function setup() {
	var cstart = window.location.search.indexOf("c=");
	if(cstart != -1){
		setmediastatus(window.location.search.substring(cstart+2,cstart+5));
		pickedown = 1;
	} else {
		if(getmediastatus() == false){
			pickedown = 0;
		} else {
			pickedown = 1;
		}
	}
}

function displayvideo () {
	if(pickedown == 1 && mediastring != null){
		document.write('<div class="' +mediastring + '">');
		var hilo = '_xl';
		var mediapath = '';
		var mediaheight = 0;
		var mediawidth = 0;
		if(mediasize == 1) hilo = '_lo';
		if(mediasize == 3 || mediasize == 4) hilo = '_xl';

		if(mediastring == 'wm'){
		
			var fsembed = '';
			var fsobj = '';
			report(2,mediasize);
			if(mediasize == 1){
				mediaheight = (anamorphic)? 152:182;
				mediawidth = (anamorphic)? 192:160;
			} else if (mediasize == 2) {
				mediaheight = (anamorphic)? 278:312;
				mediawidth = (anamorphic)? 384:320;
				if(flipfix == 1){
					mediaheight -= 46;
				}
			} else if (mediasize == 3) {
				mediaheight = (anamorphic)? 352:448;
				mediawidth = 512;
				if(flipfix == 1){
					mediaheight -= 48;
				}
			}
 			else if (mediasize == 4) {
 				fsembed = 'fullScreen = "1"';
				fsobj = '<param name="fullScreen" value="1">';
				mediaheight = (anamorphic)? 352:448;
				mediawidth = 512;
			}
			
			if(programcode == 0){
				if(name == "promos"){
					mediapath = 'http://media.pbs.org/asxgen/general/windows/media4/frontline/' + name + '/' + title + '/windows/' + title + hilo + '.wmv.asx';
				} else {
					mediapath = 'http://media.pbs.org/asxgen/general/windows/media4/frontline/' + name + '/windows/' + title + hilo + '.wmv.asx';
				}
			} else {
				mediapath = 'http://media.pbs.org/asxgen/general/windows/media4/frontline/' + programcode + '/windows/' + title + hilo + '.wmv.asx';
			}
			
			document.write('<object id="MediaPlayer" classid="CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6" height="' + mediaheight + '" width="' + mediawidth + '" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701" standby="Loading Microsoft Windows Media Player components..." type="application/x-oleobject">');
			document.write('<param name="URL" value="' + mediapath + '">' + fsobj + '<param name="AnimationAtStart" value="0"><param name="TransparentAtStart" value="0"><param name="ShowControls" value="1"><param name="ShowTracker" value="1"><param name="ShowDisplay" value="0"><param name="ShowStatusBar" value="1">');
			document.write('<embed src="' + mediapath + '" type="application/x-mplayer2" pluginspage="http://www.microsoft.com/windows95/downloads/contents/wurecommended/s_wufeatured/mediaplayer/default.asp" name=MediaPlayer animationatstart="0" transparentatstart="0" ' + fsembed + ' showcontrols="1" showtracker="1" showdisplay="0" showstatusbar="1" width="' + mediawidth + '" height="' + mediaheight + '"><\/embed><\/object>');
		
		} else if (mediastring == 'rp'){
			report(1,mediasize);
			if(mediasize == 2){
				mediaheight = (anamorphic)? 216:240;
				mediawidth = (anamorphic)? 384:320;
			} else if (mediasize == 1) {
				mediaheight = (anamorphic)? 88:120;
				mediawidth = 160;
			}

			if(program == 0){
				mediapath = 'http://media.pbs.org/ramgen/wgbh/pages/frontline/' + name + '/real/' + title + hilo + '.rm';
			} else {
				mediapath = 'http://media.pbs.org/ramgen/wgbh/pages/frontline/' + program + '/real/' + title + hilo + '.rm';
			}
			document.write('<object id="video" classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="' + mediawidth + '" height="' + mediaheight + '">');
			document.write('<param name="autostart" value="true"><param name="controls" value="imagewindow"><param name="CONSOLE" value="video"><param name="SRC" value="' + mediapath + '">');
			document.write('<embed name="video" console="video" type="audio/x-pn-realaudio-plugin" src="' + mediapath + '?embed" width="' + mediawidth + '" height="' + mediaheight + '" hspace="0" vspace="0" autostart="true" loop="false" controls="imagewindow">');
			document.write('<\/embed><\/object><br /><object id="video" classid="CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA" width="' + mediawidth + '" height="25">');
			document.write('<param name="SRC" value="' + mediapath + '"><param name="CONTROLS" value="ControlPanel"><param name="CONSOLE" value="video">');
			document.write('<embed name="video" src="' + mediapath + '?embed" type="audio/x-pn-realaudio-plugin" width="' + mediawidth + '" height="25" hspace="0" vspace="0" border="0" controls="ControlPanel" console="video">');
			document.write('<\/embed><\/object><br /><object id="video" classid="CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA" width="' + mediawidth + '" height="30"><param name="SRC" value="' + mediapath + '">');
			document.write('<param name="CONTROLS" value="StatusPanel"><param name="CONSOLE" value="video"><embed name="video" src="' + mediapath + '?embed" type="audio/x-pn-realaudio-plugin" width="' + mediawidth + '" height="30" hspace="0" vspace="0" border="0" controls="StatusPanel" console="video"><\/embed><\/object>');
		} else {
			var movfile = 'http://www.pbs.org/media/frontline/' + programcode + '/' + title;
			if(programcode == ''){
				movfile = 'http://www.pbs.org/media/frontline/' + name + '/' + title;
			}
			report(3,mediasize);
			var scaleobject = '';
			var scaleembed = '';
			if(mediasize == 1){
				mediaheight = (anamorphic)? 152:182;
				mediawidth = (anamorphic)? 192:160;
			} else if (mediasize == 2) {
				mediaheight = (anamorphic)? 278:312;
				movfile += "_hi";
				mediawidth = (anamorphic)? 384:320;
			} else if (mediasize == 3) {
				mediaheight = (anamorphic)? 304:400;
				mediawidth = 512;
			} else if (mediasize == 4) {
				mediaheight = (anamorphic)? 477:481;
				mediawidth = (anamorphic)? 820:620;
				window.resizeTo(mediawidth+50, mediaheight+280);
				scaleobject = 'scale="tofit"';
				scaleembed = '<param name="scale" value="tofit" />';
			}
			
			movfile += '.mov';
			
			document.write('<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="'+mediawidth+'" height="'+mediaheight+'" codebase="http://www.apple.com/qtactivex/qtplugin.cab" id="qt1"><param name="src" value="'+movfile+'" /><param name="AUTOPLAY" value="true" /><param name="KIOSKMODE" value="true" /><param name="CONTROLLER" value="true" /><param name="cache" value="true" />'+scaleembed+'<param name="bgcolor" value="#000000" /><embed src="'+movfile+'" kioskmode="true" width="'+mediawidth+'" '+scaleobject+' height="'+mediaheight+'" alt="" cache="true" autoplay="true" controller="true" bgcolor="#000000" pluginspage="http://www.apple.com/quicktime/download/" name="qt1" type="video/quicktime" EnableJavaScript="true"></embed></object>');
			document.qt1.focus();
		
		} 
		
		reselector();
		document.write('</div>');
	} else {
		document.write('<div class="selector">');
		selector();
		document.write('</div>');
	}
}

function buttonnext (){
	if(num > 0 && num < total){	
		document.write('<div class="bnext" onclick="location=\'' + (num + 1) + '.html?c=' + mediasize + mediastring + '\'"><p><span class="big">&#187;</span><br />NEXT</p></div>');
	}
}


function buttonpast (){
	if(num > 1){	
		document.write('<div class="bpast" onclick="location=\'' + (num - 1) + '.html?c=' + mediasize + mediastring + '\'"><p><span class="big">&#171;</span><br />BACK</p></div>');
	}
}


function setmediastatus (mstr){
	switch (mstr) {
		case '4wm':
			if(navigator.userAgent.indexOf('Win') != -1){
				mediastring = 'wm';
				mediasize = 4;
			} else {
				mediastring = 'wm';
				mediasize = 3;
			}			
			break;
		case '3wm':
			mediastring = 'wm';
			mediasize = 3;
			break;
		case '2wm':
			mediastring = 'wm';
			mediasize = 2;
			break;
		case '1wm':
			mediastring = 'wm';
			mediasize = 1;
			break;
		case '2rp':
			mediastring = 'rp';
			mediasize = 2;
			break;
		case '1rp':
			mediastring = 'rp';
			mediasize = 1;
			break;
		case '4qt':
			mediastring = 'qt';
			mediasize = 4;
			break;
		case '3qt':
			mediastring = 'qt';
			mediasize = 3;
			break;
		case '2qt':
			mediastring = 'qt';
			mediasize = 2;
			break;
		case '1qt':
			mediastring = 'qt';
			mediasize = 1;
			break;
	}
	setcookie('frol_media',mediastring);
	setcookie('frol_mediasize',mediasize);
}

function getmediastatus (){
	var outp = false;
	tgtmedia = getcookie('frol_media');
	tgtsize = getcookie('frol_mediasize');
	if(tgtmedia != '' && tgtmedia != 'undefined' && tgtmedia != null){
		mediastring = tgtmedia;
		if(tgtsize != '' && tgtsize != 'undefined' && tgtsize != null){
			mediasize = tgtsize;
			outp = true;
		}
	}
	return outp;
}

function reselector (){
	var ret = '';
	var destination = 1;
	if(num == 0){
		destination = title;
	} else {
		destination = num;
	}
	ret += '<div class="reselect"><p>';

	if(sizes[2] == 'lo'){
		if(mediasize == 1){
			ret += '<span class="sel">&nbsp;DIAL UP&nbsp;</span>';
		} else {
			ret += '<a class="unsel" href="' + destination + '.html?c=1' + mediastring + '">&nbsp;DIAL UP&nbsp;</a>';
		}
	}
	if(sizes[1] == 'hi'){
		if(mediasize == 2){
			ret += '<span class="sel">&nbsp;SMALL&nbsp;</span>';
		} else {
			ret += '<a class="unsel" href="' + destination + '.html?c=2' + mediastring + '">&nbsp;SMALL&nbsp;</a>';
		}
	}
	if(sizes[0] == 'xl'){
		if(mediasize == 3){
			ret += '<span class="sel">&nbsp;MEDIUM&nbsp;</span>';
		} else {
			if(mediastring == 'wm'){
				ret += '<a class="unsel" href="' + destination + '.html?c=3wm">&nbsp;MEDIUM&nbsp;</a>';
			} else if(mediastring == 'qt'){
				ret += '<a class="unsel" href="' + destination + '.html?c=3qt">&nbsp;MEDIUM&nbsp;</a>';
			} else {
				ret += '<span class="unseldis">&nbsp;MEDIUM&nbsp;</span>';
			}
		}
		if(mediasize == 4){
			ret += '<span class="sel">&nbsp;LARGE&nbsp;</span>';
		} else {
			if(mediastring == 'qt'){
				ret += '<a class="unsel" href="' + destination + '.html?c=4qt">&nbsp;LARGE&nbsp;</a>';
			} else {
				if(navigator.userAgent.indexOf('Win') != -1){
					ret += '<a class="unsel" href="' + destination + '.html?c=4wm">&nbsp;FULL SCREEN&nbsp;</a>';
				}
			}
		}
	}

	ret += "&nbsp;&nbsp;&nbsp;&nbsp;";

	if(mediastatus[0] == 'qt'){
		if(mediastring == 'qt'){
			ret += '<span class="sel">&nbsp;QUICKTIME&nbsp;</span>';
		} else {
			ret += '<a class="unsel" href="' + destination + '.html?c=' + mediasize + 'qt">&nbsp;QUICKTIME&nbsp;</a>';
		}
	}
	if(mediastatus[2] == 'rp'){
		if(mediastring == 'rp'){
			ret += '<span class="sel">&nbsp;REALPLAYER&nbsp;</span>';
		} else {
			ret += '<a class="unsel" href="' + destination + '.html?c=' + mediasize + 'rp\">&nbsp;REALPLAYER&nbsp;</a>';
		}
	}
	if(mediastatus[1] == 'wm'){
		if(mediastring == 'wm'){
			ret += '<span class="sel">&nbsp;WINDOWS MEDIA&nbsp;</span>';
		} else {
			ret += '<a class="unsel" href="' + destination + '.html?c=' + mediasize + 'wm\">&nbsp;WINDOWS MEDIA&nbsp;</a>';
		}
	}

	ret += "</p>";

	document.write(ret);
}
