function saveCookies(changeThis){
	if (changeThis == "player"){
		if (selMediaPref == "Quicktime"){
			selMediaPref = "Windows";
		}
		else {
			selMediaPref = "Quicktime";
		}
	}
	
	else {
		if (selAutoPla == "on"){
			selAutoPla = "off";
		}
		else {
			selAutoPla = "on";
		}
	}
	
	//save cookies
	var days = 100;
	var expires = new Date ();
	expires.setTime(expires.getTime() + days * (24 * 60 * 60 * 1000)); 
	document.cookie = "WUSTL-PA-video-player=" + escape(selMediaPref) + "; expires=" + expires.toGMTString();
	document.cookie = "WUSTL-PA-video-autoplay=" + escape(selAutoPla) + "; expires=" + expires.toGMTString();
	
	location.reload();
}


function choosePlayer(){	
	//check for cookies
	//player
	if (document.cookie.indexOf("WUSTL-PA-video-player=Windows") != -1){
		pref="win";
	}
	else if (document.cookie.indexOf("WUSTL-PA-video-player=Quicktime") != -1){
		pref="mac";
	}
	else if (navigator.userAgent.indexOf("Windows") != -1){
		pref="win";
	}
	else {
		pref="mac";
	}
	
	//autoplay
	if (document.cookie.indexOf("WUSTL-PA-video-autoplay=on") != -1){
		autoplay = "on";
	}
	else {
		autoplay = "off";
	}

	if (pref == "win"){
		selMediaPref = "Windows Media";
		selAutoPla = autoplay;
		writeWin(vid,autoplay);
	}
	else if (pref == "mac"){
		selMediaPref = "Quicktime";
		selAutoPla = autoplay;
		writeMac(vid,autoplay);
	}
}

function writeWin(movieName,autoP){
	if (autoP == "on"){
		ap = 1;
	}
	else {
		ap = 0;
	}
	if (navigator.userAgent.indexOf("Macintosh") != -1){
		ieH = 166;
	}
	else{
		ieH = 192;
	}
	
	//moviePath = "video/"+movieName+"/clip.asx";
	moviePath = "mms://mediaserver.wustl.edu/mpa/"+movieName+"/small.wmv";
	
	content.innerHTML = '<OBJECT CLASSID="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" id="videoClip" height="'+ieH+'" width="200" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,7,1112" type="application/x-oleobject">\n<PARAM NAME="FileName" VALUE="'+moviePath+'">\n<PARAM NAME="ShowControls" VALUE="1">\n<PARAM NAME="ShowDisplay" VALUE="0">\n<PARAM NAME="ShowStatusBar" VALUE="0">\n<PARAM NAME="AutoSize" VALUE="0">\n<PARAM NAME="TransparentAtStart" VALUE="1">\n<PARAM NAME="autoStart" VALUE="'+ap+'">\n<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/windows/windowsmedia/download/" filename="'+moviePath+'" src="'+moviePath+'" Name="videoClip" ShowControls=1 ShowDisplay=0 ShowStatusBar=0 autoSize=0 TransparentAtStart=1 autoStart='+ap+' width="200" height="'+ieH+'"></embed>\n</OBJECT>\n';
}

function writeMac(movieName,autoP){
	if (autoP == "on"){
		ap = true;
	}
	else {
		ap = false;
	}
	
	if (medical){
		posterPath = "http://medschool.wustl.edu/images/posters/"+movieName+"/poster.jpg";
	}
	else{
		posterPath = "http://news-info.wustl.edu/posterImages/"+movieName+"/poster.jpg";
	}
	
	moviePath = "rtsp://streamer-1.wustl.edu/~publications/"+movieName+"/small.mov";
	altMoviePath = "http://streamer-1.wustl.edu/~publications/"+movieName+"/small.mov";
	
	if (navigator.userAgent.indexOf("MSIE") != -1){
		if (ap == true){
			content.innerHTML = '<embed qtsrc="'+altMoviePath+'" width="200" height="166" autoplay="true" loop="false" controller="true" pluginspage="http://www.apple.com/quicktime/" type="video/quicktime"></embed>\n';
		}
		else{
			content.innerHTML = '<embed qtsrc="'+posterPath+'" href="'+moviePath+'" target="myself" width="200" height="166" autoplay="true" loop="false" controller="false" pluginspage="http://www.apple.com/quicktime/" type="video/quicktime"></embed>\n';
		}
	}
	else {
		if (ap == true){
			content.innerHTML = '<embed src="'+altMoviePath+'" width="200" height="166" autoplay="true" loop="false" controller="true" pluginspage="http://www.apple.com/quicktime/" type="video/quicktime"></embed>\n';
		}
		else {
		content.innerHTML = '<embed src="'+posterPath+'" href="'+moviePath+'" target="myself" width="200" height="166" autoplay="false" loop="false" controller="false" pluginspage="http://www.apple.com/quicktime/" type="video/quicktime"></embed>\n';
		}
	}
}

function writeTDstyle(){
	if (medical){
		contentTD.style.backgroundImage = 'url(http://medschool.wustl.edu/images/posters/'+vid+'/poster.jpg)';
	}
	else{
		contentTD.style.backgroundImage = 'url(http://news-info.wustl.edu/posterImages/'+vid+'/poster.jpg)';
	}
}

function writeChoices(){
	//if (navigator.userAgent.indexOf("Safari") != -1){
	//}
	
	//else {
	document.write("Player is "+selMediaPref+". <a href=\"javascript:saveCookies('player')\" style='color:#990000;text-decoration:none'>[change]</a><br>");
	//document.write("Autoplay is "+selAutoPla+". <a href=\"javascript:saveCookies('autoplay')\" style='color:#990000;text-decoration:none'>[change]</a><br>");
	//}
	/*
	if (selMediaPref == "Quicktime"){
		document.write("View in external player: <a href=\"http://news-info.wustl.edu/video/scripts/video.htm?name="+vid+"&size=large\" target=\"_blank\" style='color:#990000;text-decoration:none'>[large]</a> <a href=\"http://news-info.wustl.edu/video/scripts/video.htm?name="+vid+"&size=small\" target=\"_blank\" style='color:#990000;text-decoration:none'>[small]</a>");
	}
	
	else {
		document.write("View in external player: <a href=\"mms://mediaserver.wustl.edu/mpa/"+vid+"/large.wmv\" style='color:#990000;text-decoration:none'>[large]</a> <a href=\"mms://mediaserver.wustl.edu/mpa/"+vid+"/small.wmv\" style='color:#990000;text-decoration:none'>[small]</a>");
	}
	if (broadcastQuality){
		document.write("<br><a href=\"http://news-info.wustl.edu/video_help/\" style='color:#990000;text-decoration:none'>Request broadcast quality video</a>");
	}
	*/
}

selMediaPref = "";
selAutoPla = "";
var content = eval("document.getElementById('divMedia')");
var contentTD = eval("document.getElementById('divMediaTD')");
vid = videoSrc;

writeTDstyle();
choosePlayer();
writeChoices();