var detectableWithVB = false;
var mediaType = "";
var qtonly = 0;
var qtdir = "";
var rpdir = "";
var newissue = "";  //remove newissue
var anamorphic = false;
var anamorphiclarger = false;

function handleclick(targ){

	opener.location.href = targ;
	window.close(self);
	
}

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

function checkForOldBrowser() {
	var version = parseInt(navigator.appVersion);
	var agt = navigator.userAgent.toLowerCase();
	var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1) && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1) && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
	if ((is_ie || is_nav) && (version < 4)) {
		alert("We strongly recommend you update your browser.  This feature may not work correctly with versions of Netscape or Internet Explorer earlier than 4.0");
	}
}

function detectPlugin(plugin) {
	var pluginFound = false;
	if (navigator.plugins && (navigator.plugins.length > 0)) {
		for (var pluginsCounter = 0; pluginsCounter < navigator.plugins.length ; pluginsCounter++) {
			if ((navigator.plugins[pluginsCounter].name.indexOf(plugin) >= 0) || (navigator.plugins[pluginsCounter].description.indexOf(plugin) >= 0)) {
			pluginFound = true;
			break;
			}
		}
	}
	return pluginFound;
}

function detectQuickTime() {
    var pluginFound = detectPlugin('QuickTime');
    if(!pluginFound && detectableWithVB) {
	pluginFound = detectQuickTimeActiveXControl();
    }
    return pluginFound;
}

function detectRealPlayer() {
    var pluginFound = detectPlugin('RealPlayer');
    if(!pluginFound && detectableWithVB) {
	pluginFound = (detectActiveXControl('rmocx.RealPlayer G2 Control') ||
		       detectActiveXControl('RealPlayer.RealPlayer(tm) ActiveX Control (32-bit)') ||
		       detectActiveXControl('RealVideo.RealVideo(tm) ActiveX Control (32-bit)'));
    }	
    return pluginFound;
}

function detectWindowsMedia() {
    pluginFound = detectPlugin('Windows Media');
    if(!pluginFound && detectableWithVB) {
	pluginFound = detectActiveXControl('MediaPlayer.MediaPlayer.1');
    }
    return pluginFound;
}

if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1)) {
    document.writeln('<script language="VBscript">');

    document.writeln('\'do a one-time test for a version of VBScript that can handle this code');
    document.writeln('detectableWithVB = False');
    document.writeln('If ScriptEngineMajorVersion >= 2 then');
    document.writeln('  detectableWithVB = True');
    document.writeln('End If');

    document.writeln('\'this next function will detect most plugins');
    document.writeln('Function detectActiveXControl(activeXControlName)');
    document.writeln('  on error resume next');
    document.writeln('  detectActiveXControl = False');
    document.writeln('  If detectableWithVB Then');
    document.writeln('     detectActiveXControl = IsObject(CreateObject(activeXControlName))');
    document.writeln('  End If');
    document.writeln('End Function');

    document.writeln('\'and the following function handles QuickTime');
    document.writeln('Function detectQuickTimeActiveXControl()');
    document.writeln('  on error resume next');
    document.writeln('  detectQuickTimeActiveXControl = False');
    document.writeln('  If detectableWithVB Then');
    document.writeln('    detectQuickTimeActiveXControl = False');
    document.writeln('    hasQuickTimeChecker = false');
    document.writeln('    Set hasQuickTimeChecker = CreateObject("QuickTimeCheckObject.QuickTimeCheck.1")');
    document.writeln('    If IsObject(hasQuickTimeChecker) Then');
    document.writeln('      If hasQuickTimeChecker.IsQuickTimeAvailable(0) Then ');
    document.writeln('        detectQuickTimeActiveXControl = True');
    document.writeln('      End If');
    document.writeln('    End If');
    document.writeln('  End If');
    document.writeln('End Function');

    document.writeln('</scr' + 'ipt>');
}

function real_high(target) {
	report(1,2);
	realheight = (anamorphic)? 180:240;
	if(anamorphiclarger == true){
		realheight = 216;
	}
	realwidth = (anamorphiclarger)? 384:320;
	document.write('<object id="video" classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="320" height="' + realheight + '">');
	var videopath = (rpdir == "")? ("http://media.pbs.org/ramgen/wgbh/pages/frontline/" + target):(rpdir)
	document.write('<param name="autostart" value="true"><param name="controls" value="imagewindow"><param name="CONSOLE" value="video"><param name="SRC" value="' + videopath + '">');
	document.write('<embed name="video" console="video" type="audio/x-pn-realaudio-plugin" src="' + videopath + '?embed" width="' + realwidth + '" height="' + realheight + '" hspace="0" vspace="0" autostart="true" loop="false" controls="imagewindow">');
	document.write('<\/embed><\/object><object id="video" classid="CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA" width="' + realwidth + '" height="25">');
	document.write('<param name="SRC" value="' + videopath + '"><param name="CONTROLS" value="ControlPanel"><param name="CONSOLE" value="video">');
	document.write('<embed name="video" src="' + videopath + '?embed" type="audio/x-pn-realaudio-plugin" width="' + realwidth + '" height="25" hspace="0" vspace="0" border="0" controls="ControlPanel" console="video">');
	document.write('<\/embed> <\/object><object id="video" classid="CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA" width="' + realwidth + '" height="30"><param name="SRC" value="' + videopath + '">');
	document.write('<param name="CONTROLS" value="StatusPanel"><param name="CONSOLE" value="video"> <embed name="video" src="' + videopath + '?embed" type="audio/x-pn-realaudio-plugin" width="' + realwidth + '" height="30" hspace="0" vspace="0" border="0" controls="StatusPanel" console="video"> <\/embed> <\/object>');
}

function real_medium(target) {
	report(1,1);
	document.write('<object id="video" classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="208" height="156">');
	var videopath = (rpdir == "")? ("http://media.pbs.org/ramgen/wgbh/pages/frontline/" + target):(rpdir)
	document.write('<param name="autostart" value="true"><param name="controls" value="imagewindow"><param name="CONSOLE" value="video"><param name="SRC" value="' + videopath + '">');
	document.write('<embed name="video" console="video" type="audio/x-pn-realaudio-plugin" src="' + videopath + '?embed" width="208" height="156" hspace="0" vspace="0" autostart="true" loop="false" controls="imagewindow">');
	document.write('<\/embed><\/object><object id="video" classid="CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA" width="208" height="25">');
	document.write('<param name="SRC" value="' + videopath + '"><param name="CONTROLS" value="ControlPanel"><param name="CONSOLE" value="video">');
	document.write('<embed name="video" src="' + videopath + '?embed" type="audio/x-pn-realaudio-plugin" width="208" height="25" hspace="0" vspace="0" border="0" controls="ControlPanel" console="video">');
	document.write('<\/embed> <\/object><object id="video" classid="CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA" width="208" height="30"><param name="SRC" value="' + videopath + '">');
	document.write('<param name="CONTROLS" value="StatusPanel"><param name="CONSOLE" value="video"> <embed name="video" src="' + videopath + '?embed" type="audio/x-pn-realaudio-plugin" width="208" height="30" hspace="0" vspace="0" border="0" controls="StatusPanel" console="video"> <\/embed> <\/object>');
}

function real_low(target) {
	report(1,1);
	realheight = (anamorphic)? 90:120;
	if(anamorphiclarger == true){
		realheight = 108;
	}
	realwidth = (anamorphiclarger)? 192:160;
	document.write('<object id="video" classid="clsid:CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA" width="' + realwidth + '" height="' + realheight + '">');
	var videopath = (rpdir == "")? ("http://media.pbs.org/ramgen/wgbh/pages/frontline/" + target):(rpdir)
	document.write('<param name="autostart" value="true"><param name="controls" value="imagewindow"><param name="CONSOLE" value="video"><param name="SRC" value="' + videopath + '">');
	document.write('<embed name="video" console="video" type="audio/x-pn-realaudio-plugin" src="' + videopath + '?embed" width="' + realwidth + '" height="' + realheight + '" hspace="0" vspace="0" autostart="true" loop="false" controls="imagewindow">');
	document.write('<\/embed><\/object><object id="video" classid="CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA" width="' + realwidth + '" height="25">');
	document.write('<param name="SRC" value="' + videopath + '"><param name="CONTROLS" value="ControlPanel"><param name="CONSOLE" value="video">');
	document.write('<embed name="video" src="' + videopath + '?embed" type="audio/x-pn-realaudio-plugin" width="' + realwidth + '" height="25" hspace="0" vspace="0" border="0" controls="ControlPanel" console="video">');
	document.write('<\/embed> <\/object><object id="video" classid="CLSID:CFCDAA03-8BE4-11CF-B84B-0020AFBBCCFA" width="' + realwidth + '" height="30"><param name="SRC" value="' + videopath + '">');
	document.write('<param name="CONTROLS" value="StatusPanel"><param name="CONSOLE" value="video"> <embed name="video" src="' + videopath + '?embed" type="audio/x-pn-realaudio-plugin" width="' + realwidth + '" height="30" hspace="0" vspace="0" border="0" controls="StatusPanel" console="video"> <\/embed> <\/object>');
}

function windows_high(target) {
	report(2,2);
	winheight = (anamorphic)? 252:312;
	if(anamorphiclarger == true){
		winheight = 278;
	}
	winwidth = (anamorphiclarger)? 384:320;
	document.write('<object id="MediaPlayer" classid="CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6" height="' + winheight + '" width="' + winwidth + '" 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="http://media.pbs.org/asxgen/general/windows/media4/frontline/' + target + '.asx"><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="http://media.pbs.org/asxgen/general/windows/media4/frontline/' + target + '.asx" type="application/x-mplayer2" pluginspage="http://www.microsoft.com/windows95/downloads/contents/wurecommended/s_wufeatured/mediaplayer/default.asp" name=MediaPlayer animationatstart="0" transparentatstart="0" showcontrols="1" showtracker="1" showdisplay="0" showstatusbar="1" width="' + winwidth + '" height="' + winheight + '"><\/embed><\/object>');
}

function windows_low(target) {
	report(2,1);
	winheight = (anamorphic)? 161:191;
	if(anamorphiclarger == true){
		winheight = 181;
	}
	winwidth = (anamorphiclarger)? 192:160;
	document.write('<object id="MediaPlayer" classid="CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6" height="' + winheight + '" width="' + winwidth + '" 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="http://media.pbs.org/asxgen/general/windows/media4/frontline/' + target + '.asx"><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="http://media.pbs.org/asxgen/general/windows/media4/frontline/' + target + '.asx" type="application/x-mplayer2" pluginspage="http://www.microsoft.com/windows95/downloads/contents/wurecommended/s_wufeatured/mediaplayer/default.asp" name=MediaPlayer animationatstart="0" transparentatstart="0" showcontrols="1" showtracker="1" showdisplay="0" showstatusbar="1" width="' + winwidth + '" height="' + winheight + '"><\/embed><\/object>');
}

function qt_high(target){
	report(3,2);
	document.write('<OBJECT CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" WIDTH="320" HEIGHT="254" CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab">');
	document.write('<PARAM name="SRC" VALUE="' + target + '">');
	document.write('<PARAM name="AUTOPLAY" VALUE="true">');
	document.write('<PARAM name="CONTROLLER" VALUE="true"><EMBED SRC="' + target + '" WIDTH="320" HEIGHT="254" AUTOPLAY="true" CONTROLLER="true" PLUGINSPAGE="http://www.apple.com/quicktime/download/"><\/EMBED><\/OBJECT>');
}

function qt_low(target){
	report(3,1);
	document.write('<OBJECT CLASSID="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" WIDTH="160" HEIGHT="136" CODEBASE="http://www.apple.com/qtactivex/qtplugin.cab">');
	document.write('<PARAM name="SRC" VALUE="' + target + '">');
	document.write('<PARAM name="AUTOPLAY" VALUE="true">');
	document.write('<PARAM name="CONTROLLER" VALUE="true"><EMBED SRC="' + target + '" WIDTH="160" HEIGHT="136" AUTOPLAY="true" CONTROLLER="true" PLUGINSPAGE="http://www.apple.com/quicktime/download/"><\/EMBED><\/OBJECT>');
}

function complain(){
	if ((mediaStatus[2] == "wm") && (mediaStatus[1] == "rp")) document.write('<p><b>You\'ll need</b> either <a href="javascript:handleclick(\'http://www.microsoft.com/windowsmedia/\')";>windows media</a> or <a href="javascript:handleclick(\'http://www.real.com/\');">realplayer</a> to watch this video clip.</p>');
	else if (mediaStatus[1] == "rp") document.write('<p><b>You\'ll need</b> <a href="javascript:handleclick(\'http://www.real.com/\');">realplayer</a> to watch this video clip.</p>');
	else if (mediaStatus[0] == "qt") document.write('<p><b>You\'ll need</b> <a href="javascript:handleclick(\'http://quitcktime.apple.com/\');">quicktime</a> to watch this video clip.</p>');
}

function nomedia(){
	document.write('<p><b>We\'re sorry.</b>  Your preferred media choice is not available for this clip due to hosting restrictions.  We apologize for the inconvenience.</p>');
}

function setCookie(type,value) {
	document.cookie = type + "=" + value + ";path=/wgbh/pages/frontline/;expires=Friday, 31-Dec-2006 23:59:59 GMT";
}

function readCookie() {
	var pos = document.cookie.indexOf("frol_media");
	var value = "";
	if (pos != -1) {
		value = document.cookie.substring(pos+11, pos+13);
	}
	return value;
}

function checkLocalized() {
	var pos = document.cookie.indexOf("pbsol.station=");
	var value = "";
	if (pos != -1) {
		value = document.cookie.substring(pos+14, pos+18);
	}
	return value;
}

function checkMasterCookie() {
	var pos = document.cookie.indexOf("frol_master");
	if (pos >= 0) {
		return true;
	} else {
		return false;
	}
}

function detectMedia() {
	if ((mediaStatus[2] == "wm") && detectWindowsMedia()) { return "wm" }
	else if ((mediaStatus[1] == "rp") && detectRealPlayer()) { return "rp" }
	else if ((mediaStatus[0] == "qt") && detectQuickTime()) { return "qt" }
}

function checkExceptions(media) {
	var until = mediaStatus.length;
	for (i = 0; i < until; i++) {
		if (mediaStatus[i] == media) {
			return false;
		}
	}
	return true;
}

function detectSettings() {
	var cookieSays = readCookie();
	var urlthing = window.location.search.substring(1);
	if (urlthing == ""){
		if (cookieSays == "") {
			mediaType = detectMedia();
			setCookie("frol_media",mediaType);
		} else {
			if (!checkExceptions(cookieSays)){
				mediaType = cookieSays;
			} else {
				mediaType = detectMedia();
			}		
		}
	} else {
		mediaType = urlthing;
		setCookie("frol_media",mediaType);
	}

}

function setMedia(type) {
	if (checkMasterCookie()) {
		setCookie("frol_media",type);
		location.reload();
	}
	else {
		var loc = location.href;
		var pos = loc.indexOf("?");
		if (pos != -1) {
			loc = loc.substring(0,pos);
		}
		location.href = (loc + "?" + type);
	}
}

function buildBar() {
	checkForOldBrowser();
	if (!checkMasterCookie()) { setCookie("frol_master","master"); }
	detectSettings();
	var bar = "";
	var qtsize = (level == "hi")? ("width=94 height=20"):("width=82 height=18");
	var wmsize = (level == "hi")? ("width=130 height=20"):("width=109 height=18");
	var rpsize = (level == "hi")? ("width=102 height=20"):("width=84 height=18");
	var ifLo = (level == "hi")? (""):("_lo");
	var currenturl = location.href;
	var temppos = currenturl.indexOf("?");
	if(temppos != -1){
		currenturl = currenturl.substring(0,temppos);
	}
	if (mediaType == "qt") {
		bar += '<img src="http://www.pbs.org/wgbh/pages/frontline/art/video/qt' + ifLo + 's.gif" ' + qtsize + ' alt="quicktime" border=0>';
	} else if (mediaStatus[0] == "qt") {
		bar += '<a href="' + currenturl + '?qt"><img src="http://www.pbs.org/wgbh/pages/frontline/art/video/qt' + ifLo + '.gif" ' + qtsize + ' alt="quicktime" border=0></a>';
	}
	if (mediaType == "wm") {
		bar += '<img src="http://www.pbs.org/wgbh/pages/frontline/art/video/wm' + ifLo + 's.gif" ' + wmsize + ' alt="windows media" border=0>';
	} else if (mediaStatus[2] == "wm") {
		bar += '<a href="' + currenturl + '?wm"><img src="http://www.pbs.org/wgbh/pages/frontline/art/video/wm' + ifLo + '.gif" ' + wmsize + ' alt="windows media" border=0></a>';
	}
	if (mediaType == "rp") {
		bar += '<img src="http://www.pbs.org/wgbh/pages/frontline/art/video/rp' + ifLo + 's.gif" ' + rpsize + ' alt="real player" border=0>';
	} else if (mediaStatus[1] == "rp") {
		bar += '<a href="' + currenturl + '?rp"><img src="http://www.pbs.org/wgbh/pages/frontline/art/video/rp' + ifLo + '.gif" ' + rpsize + ' alt="real player" border=0></a>';
	}
	document.write(bar);
}

function buildViewer() {
	var levelexp = (level == "hi")? ("high"):("low")
	if (level == "md") { levelexp = "medium" }
	if (mediaType == "qt") {
		if (qtonly == 1){
			eval("qt_" + levelexp + "('http://raspail.entropymedia.com/qt/" + program + "_" + name + "/" + title + ".mov')");
		} else {
			var dir = (qtdir == "")? ("view"):("etc");
			eval("qt_" + levelexp + "('http://www.pbs.org/wgbh/pages/frontline/shows/" + name + "/" + dir + "/" + title + "_" + level + ".mov')");
		}
	} else if (mediaType =="wm") {
		eval("windows_" + levelexp + "('" + program + "/windows/" + title + "_" + level + newissue + ".wmv')");  // remove newissue
	} else if (mediaType == "rp") {
		eval("real_" + levelexp + "('" + program + "/real/" + title + "_" + level + ".rm')");
	} else if (qtonly == 1) {
		nomedia();
	} else {
		complain();
	}
}

function goTo(direction) {
	var goTo;
	var ifLo = (level == "hi")? (""):("_lo");
	var arsize = (level == "hi")? ("width=23 height=19"):("width=19 height=19");
	if ((direction == "prev") && (num == 1)) {
		document.write('<img src="../art/blank.gif" width=1 height=19 alt="" border=0>');
	}
	else {
		var extra = (checkMasterCookie())? (""):("?" + mediaType);
		if (direction == "next" && num != total) {
			goTo = (num == total)? (1):(num + 1);
			document.write('<a href="' + chprefix + goTo + '_' + level + '.html' + extra + '" class="chapter' + level + '"><font color="#ffffff">Chapter ' + goTo + '</font> <font color="#aaaaaa">(of ' + total + ')</font></span><img src="http://www.pbs.org/wgbh/pages/frontline/art/video/ar_fwd' + ifLo + '.gif" ' + arsize + ' alt="" border=0 align="middle"><\/a>');
		} else if (direction == "prev"){
			goTo = (num - 1);
			document.write('<a href="' + chprefix + goTo + '_' + level + '.html' + extra + '" class="chapter' + level + '"><img src="http://www.pbs.org/wgbh/pages/frontline/art/video/ar_back' +  ifLo + '.gif" ' + arsize + ' alt="" border=0 align="middle"><font color="#ffffff">Chapter ' + goTo + '</font> <font color="#aaaaaa">(of ' + total + ')</font></span><\/a>');
		}
	}
}
