﻿// Documentation
// http://meseo.com/examples/dhtml/wmplayer/
// http://www.aspnet-video.com/ASPNetVideo/documentation/?help=ASPNetVideo_JavaScript_API
// http://www.mioplanet.com/rsc/embed_mediaplayer.htm
// http://www.adobe.com/support/flash/publishexport/scriptingwithflash/scriptingwithflash_04.html#69558
/*

	Windows Media Player plugin for Firefox Javascript Support File

*/

// Browser Detection
var agt = navigator.userAgent.toLowerCase();

var is_ie = (agt.indexOf("msie") != -1);


// Create Windows Media Object
function setupWindowsMedia(videofile,autostart,videoPlayerWidth,videoPlayerHeight) {
	// create the windows media player with no video playing by default
	document.getElementById(VideoContainerId).innerHTML = wmpCreate(videofile,autostart,videoPlayerWidth,videoPlayerHeight);
}

function wmpCreate(url,autostart,videoPlayerWidth,videoPlayerHeight) {

	var str = "";
	
	if(!autostart){autostart = "true";};
	
	if (is_ie) {
	
		// create the WMP for IE

		str = '<object id="'+wmvPlayerId+'" classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6" width="'+videoPlayerWidth+'" height="'+videoPlayerHeight+'">';

	} else {
	
		// create it for FF.
		
		str = '<object id="'+wmvPlayerId+'" type="application/x-ms-wmp" data="'+url+'" width="'+videoPlayerWidth+'" height="'+videoPlayerHeight+'">';
		
	}

	str += '<param name="URL" value="'+url+'" />';
	str += '<param name="uiMode" value="none">';
	str += '<param name="enableContextMenu" value="true">';
	str += '<PARAM name="autostart" VALUE="'+autostart+'">'

	str += '</object>';

return str;
	
}



/*
	Windows Media Intervals
*/

function createWmpInterval()
{
	wmpInterval = window.setInterval(function () {
		
		UpdateScruber();
		
	},500);
}

function UpdateScruber()
{
	var controlBarWidth = document.getElementById(FlashScruberId).TGetPropertyAsNumber("/mcBar",8);
	var FlashScruberMargin = document.getElementById(FlashScruberId).TGetPropertyAsNumber("/mcBar",0);
	var objNetStats = checkNetStats();
	document.getElementById(FlashScruberId).TSetProperty("/mcScruber", 0, parseInt((controlInterfaceWidth*objNetStats.playheadPercent/100) * controlBarWidth / controlInterfaceWidth)+FlashScruberMargin);
	document.getElementById(FlashScruberId).TSetProperty("/mcPlayed", 8, parseInt(controlBarWidth * objNetStats.playheadPercent / 100));
	document.getElementById(FlashScruberId).SetVariable("/:txtCurrentPlayHead", objNetStats.playheadText);

}

function clearWmpInterval()
{
	window.clearInterval(wmpInterval);
	showBuffering(0);
}

function showBuffering(nShow)
{
	document.getElementById(FlashScruberId).TSetProperty("/mcBuffering", 7, nShow);
}

function showPlay(nShow)
{
	if(nShow == 1)
	{
		document.getElementById(FlashScruberId).TSetProperty("/btnPlay", 7, 1);
		document.getElementById(FlashScruberId).TSetProperty("/btnPlay", 7, 1);
		document.getElementById(FlashScruberId).TSetProperty("/btnTransparent", 7, 1);
		//document.getElementById(FlashScruberId).TSetProperty("/FullScreen", 6, 30);
	}else{
		document.getElementById(FlashScruberId).TSetProperty("/btnPlay", 7, 0);
		document.getElementById(FlashScruberId).TSetProperty("/btnPause", 7, 1);
		document.getElementById(FlashScruberId).TSetProperty("/btnTransparent", 7, 0);
		//document.getElementById(FlashScruberId).TSetProperty("/FullScreen", 6, 100);
	}
}


/*
	Windows Media Player event notifiers.
*/

// This function will run every time the Media changes in Windows Media Player
function wmpMediaChange(item) {

	// get a handle to the Windows Media Player
	
	var wmp = document.getElementById(wmvPlayerId);


	// send the media change info (name and url) into flash as plain text
	try {
		document.getElementById(FlashScruberId).mediaChange(wmp.currentMedia.name, wmp.currentMedia.sourceURL);
	} catch(e) {}
	
}

/* all available playstate values
   0 Undefined
   1 Stopped
   2 Paused
   3 Playing
   4 ScanForward
   5 ScanReverse
   6 Buffering
   7 Waiting
   8 MediaEnded
   9 Transitioning
   10 Ready
   11 Reconnecting
*/
var playstateValues = new Array("Undefined","Stopped","Paused","Playing","Scan Forward","Scan Reverse","Buffering","Waiting","Media Ended","Transitioning","Ready","Reconnecting");

// This function will run every time the Play State changes in Windows Media Player
function wmpPlayStateChange(newState)  {
	
	// send the play state change as plain text to flash
	try {
		document.getElementById(FlashScruberId).playStateChange(playstateValues[newState]);
	} catch(e) {}
	
	// manage interval
	switch (playstateValues[newState]) {
	case "Playing":
		createWmpInterval();
		showBuffering(0);
		showPlay(0);
		showDuration();
		break;
	case "Stopped":
		clearWmpInterval();
		showPlay(1);
		break;
	case "Paused":
		clearWmpInterval();
		showPlay(1);
		break;
	case "Media Ended":
		clearWmpInterval();
		showBuffering(0);
		showPlay(1);
		scrubertEnd();
		goToNextvideo();
		break;
	case "Buffering":
		showBuffering(1);
		showPlay(0);
		break;
	}
}


/*

	Utility Functions

*/

function showDuration() {
	var wmp = document.getElementById(wmvPlayerId);
	document.getElementById(FlashScruberId).SetVariable("/:txtDuration", wmp.currentMedia.durationString);
}

function playVideo(url) {

	var wmp = document.getElementById(wmvPlayerId);

	wmp.controls.stop();
	
	wmp.URL = url;
	
	wmp.controls.play();
}

function controlAction(action) {
	
	switch (action) {
		case "play":
			document.getElementById(wmvPlayerId).controls.play();
			break;
		case "pause":
			document.getElementById(wmvPlayerId).controls.pause();
			break;
		case "stop":
			scrubertStart()
			document.getElementById(wmvPlayerId).controls.stop();
			break;
	}
	
}

function checkWMPVer() {

	var wmpInfo;

	try {
		wmpInfo = document.getElementById(wmvPlayerId).versionInfo;
	} catch (e) {
		wmpInfo = "error!";
	}
	
	return wmpInfo;

}

function checkNetStats() {

	var wmp = document.getElementById(wmvPlayerId);

	var dataObject = new Object();
	
	dataObject.bandWidth = wmp.network.bandWidth;
	dataObject.bitRate = wmp.network.bitRate;
	dataObject.bufferingProgress = wmp.network.bufferingProgress;
	dataObject.status = wmp.status;
	dataObject.playheadPercent = (wmp.controls.currentPosition / wmp.controls.currentItem.duration)*100;
	dataObject.playheadText = (wmp.controls.currentPositionString == "" ? "00:00" : wmp.controls.currentPositionString);
	
	return dataObject;
		
}

function scrubertEnd()
{
	var FlashScruberMargin = document.getElementById(FlashScruberId).TGetPropertyAsNumber("/mcBar",0);
	var wmp = document.getElementById(wmvPlayerId);
	document.getElementById(FlashScruberId).SetVariable("/:txtCurrentPlayHead", ("00:00"));
	document.getElementById(FlashScruberId).TSetProperty("/mcScruber", 0, FlashScruberMargin);
}

function scrubertStart()
{

	var wmp = document.getElementById(wmvPlayerId);
	var FlashScruberMargin = document.getElementById(FlashScruberId).TGetPropertyAsNumber("/mcBar",0);
	document.getElementById(FlashScruberId).SetVariable("/:txtTimer", ("00:00/" + wmp.currentMedia.durationString));	
	document.getElementById(FlashScruberId).TSetProperty("/mcScruber", 0, FlashScruberMargin);
	//document.getElementById(FlashScruberId).TSetProperty("/mcPlayed", 8, 0);

}


function setPosition(nPosition,bRelative) {

	var wmp = document.getElementById(wmvPlayerId);
	wmp.controls.play();
	wmp.controls.currentPosition = (bRelative ? wmp.controls.currentItem.duration * nPosition : nPosition);
	UpdateScruber();
	
}

function setMarkerIndex(nIndex) {

	var wmp = document.getElementById(wmvPlayerId);
	wmp.controls.play();
	
	wmp.controls.currentMarker = nIndex;
	UpdateScruber();
	
}

function toggleMute() {

	var wmp = document.getElementById(wmvPlayerId);
	
	if (wmp.settings.mute) {
		wmp.settings.mute = false;
		document.getElementById(FlashScruberId).TSetProperty("/muteButton", 7, 1);
		return wmp.settings.volume + "%";
	} else {
		wmp.settings.mute = true;
		document.getElementById(FlashScruberId).TSetProperty("/muteButton", 7, 0);
		return "Mute";
	}
	
}

function setVolume(dir) {

	var wmp = document.getElementById(wmvPlayerId);

	wmp.settings.mute = false;

	switch (dir) {
		case "up":
			if (wmp.settings.volume < 100) {
				wmp.settings.volume += 10;
			}
			break;
		case "down":
			if (wmp.settings.volume > 0) {
				wmp.settings.volume -= 10;
			}
			break;
	}
	
	return wmp.settings.volume + "%"; 

}

function fullScreen() {
	var player=document.getElementById(wmvPlayerId); 
	player.fullScreen='true';
}

function goToNextvideo() {
	if(nextvideolink) {
		window.location = nextvideolink;
	}
}
