/*===========================================================
wmv.js
************************************************************
Created by Ming Chan(ming_chan@designreactor.com) & William Kwok (william_kwok@designreactor.com)
copyright © 2003 Design Reactor Inc.
************************************************************
Last Modified: 3/02/03
Version: 1.0
===========================================================*/

// create an array of events
// if the movie has no synchronized content. Set curTimeStamp[0] = -1
var curTimeStamp = new Array();
var curSyncContent = new Array();
var myWin;

var timer = -1;
var preTimer = -1;
var counter = 0
var g_bPaused = false;
var vid_init = false;
var currentTime = 0;
var duration  = 0;
var flashErrCode = false;

// ---------------------------------------------------------------------------------------
// NAME: sendtoColleague
// PARAMETERS: urlPath
// DESCRIPTION: Opens the send to a friend window
// LIMITATIONS: None
// ---------------------------------------------------------------------------------------



function openSyncPopup(arg){

	var h= syncWindowHeight;
	var w= syncWindowWidth;
	if (document.all || document.layers) {
  		 screenw = screen.availWidth;
  		 screenh = screen.availHeight;
	}
	var topPos= (screenw/2-h/2);
	var leftPos= (screenh/2+200-w/2);

	myWin = window.open(arg,'popup','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width=' + w + ',height=' + h + ',top=' + topPos + ',left=' + leftPos);
	myWin.focus();

}
function closeSyncPopup(){
	if(myWin){
		if(!myWin.closed)
		{
		myWin.close();
		}
	}

}
function syncPopupWin(arg){
	if (myWin)
		{
			if(!myWin.closed)
			{
				myWin.location.href=arg;
			
			}
		}
}
function externalLink(arg){
	//parse arg
	var temp= new Array();
	var begin=0;
	var end=0;
	//alert(arg);
	var firstSpace=arg.indexOf(",");
	var lastSpace=arg.lastIndexOf(",");
	for(var i=0;i<arg.length;i++){
		if(end!=lastSpace){
			end=arg.indexOf(",",begin+1)
			temp[i]=arg.substring(begin,end,end-begin);
			begin=arg.indexOf(",",end)+1;
		}
	}
	temp.push(arg.substring(lastSpace+1,arg.length));
	var urlPath= temp[0];
	var w= temp[1];
	var h= temp[2];
	var toolbar= temp[3];
	var location= temp[4];
	var directories= temp[5];
	var statusbar= temp[6];
	var menubar= temp[7];
	var scrollbars= temp[8];
	var resizable= temp[9];
	if (document.all || document.layers) {
  		 screenw = screen.availWidth;
  		 screenh = screen.availHeight;
	}
	var leftPos = (screenw-w)/2, topPos = (screenh-h)/2;
	newWindow=window.open(urlPath,'externalLink','toolbar=' + toolbar + ',location=' + location + ',directories=' + directories + ',status=' + statusbar + ',menubar=' + menubar + ',scrollbars=' + scrollbars + ',resizable=' + resizable + ',width=' + w + ',height=' + h + ',top=' + topPos + ',left=' + leftPos)
}
function popup(arg){
	var a= arg.split(",");
	var temp= new Array();
	for (var x= 0; x<a.length; x++) {
		temp.push(a[x]);
	}
	
	var urlPath= temp[0];
	var wName= temp[1];
	var temp_features= temp[2];
	for(i=3;i<temp.length;i++){
		temp_features= temp_features+ "," + temp[i];
	}
	var features= temp_features.split("'").join("");
	newWindow=window.open(urlPath,wName,features)
}
// ---------------------------------------------------------------------------------------
// NAME: hideLayer, showLayer
// PARAMETERS: none
// DESCRIPTION: Displays and Hides the video layer
// LIMITATIONS: None
// ---------------------------------------------------------------------------------------

function hideLayer() {
	document.all.item("videoLayer").style.top = 3000
}

function showLayer(){
	document.all.item("videoLayer").style.top = 94.7
	//resumeMe();
}

// ---------------------------------------------------------------------------------------
// NAME: fullScreen, exitFullScreen
// PARAMETERS: none
// DESCRIPTION: Sets video to fullscreen mode and exit from fullscreen mode
// LIMITATIONS: None
// ---------------------------------------------------------------------------------------

function fullScreen() {
	if (Player.playState == 3) {
        Player.uiMode = 'none';
        Player.fullScreen = 'true';
        if(myWin != undefined)
        	{
        		if(!myWin.closed ){myWin.close()};
	}
	}

}

function exitFullScreen(){
	if (Player.playState == 3) {
        Player.uiMode = 'none';
    	Player.fullScreen = 'false';

	}
}

// ---------------------------------------------------------------------------------------
// NAME: initVideo
// PARAMETERS: urlPath, videoID
// DESCRIPTION: Initializes the video and checks for synchronized content
// LIMITATIONS: None
// ---------------------------------------------------------------------------------------

function initVideo(urlPath, videoID)
{

	if(preTimer<1){
		var newPlayList;
		var newMedia;
		var plRet;
		var position = -1;
		counter=0;
		window.clearInterval(timer);
		// find the position of this video
		for(i=0;i<MovieIDs.length;i++) {
			if (MovieIDs[i] == videoID) {
				position = i;
				break;
			}
		}

		if (position != -1) {
			curTimeStamp = TimeStamp[position];
			curSyncContent = SyncContent[position];
		} else {
			curTimeStamp[0] = -1
			curSyncContent[0] = -1
		}

		// no UI controls for the video
		Player.uiMode = "none";
		Player.close();
		Player.settings.autoStart = false
		Player.enableContextMenu=false;
		Player.URL = urlPath;
		//Player.URL="C:\Documents and Settings\Ming\My Documents\My Work\onBusinessNetwork\newVersion\Flash\Young_Mellons_6_2.wmv";
		// set a timeout..to give time for the movie to load
		preTimer = window.setInterval("preStartCheck();",500);
		hideVideoTopInfo()
	}

}

// ---------------------------------------------------------------------------------------
// NAME: shutDown
// PARAMETERS: none
// DESCRIPTION: Clears the event timer
// LIMITATIONS: None
// ---------------------------------------------------------------------------------------

function shutDown(){
	window.clearInterval(timer);
}

// ---------------------------------------------------------------------------------------
// NAME: setVolume, getVolume
// PARAMETERS: none
// DESCRIPTION: Clears the event timer
// LIMITATIONS: None
// ---------------------------------------------------------------------------------------

function setVolume(level) {
	Player.settings.volume = level;
}

function getVolume() {
	return (Player.settings.volume)
}

// ---------------------------------------------------------------------------------------
// NAME: preStartCheck()
// PARAMETERS: none
// DESCRIPTION: Waits until movie can play and starts the movie
// LIMITATIONS: None
// ---------------------------------------------------------------------------------------

function preStartCheck()
{
	// Is playlist open and ready to play?
	if (Player.openState == 6) {
		window.clearInterval(preTimer);
		preTimer=-1;
		playVideo();
		hideLayer();
	}
}

// ---------------------------------------------------------------------------------------
// NAME: checkEvent
// PARAMETERS: none
// DESCRIPTION: checks if synchronized event is available and sends it to flash
//					 if available.
// LIMITATIONS: None
// ---------------------------------------------------------------------------------------

// event handler, check the time of the movie
function checkEvent() {

	if ((Player.playState == 3)) {
		duration = Player.currentMedia.duration;
		sendCurrentDuration("bodyFlash", '_level0.showVideoDuration,' + duration)

	}
	//sendCurrentRunTime("bodyFlash", '_level0.showVideoRunTime,'+ getCurrentTime())
	currentTime = Math.round(Player.controls.currentPosition);
	// send current playing time to flash
	sendCurrentRunTime("bodyFlash", '_level0.showVideoRunTime,'+ currentTime)
	// send current playstate to flash
	//sendCurrentPlayState("bodyFlash", '_level0.getVideoState,'+ Player.playState)

	if (curTimeStamp[0] > -1) {
		enableSyncEvent("bodyFlash", '_level0.enableSyncButton,' + curSyncContent[counter]);
		if (curTimeStamp[counter] <= currentTime)  {
			// send the event to flash
			syncEvent("bodyFlash",  '_level0.showSynchronization,' + curSyncContent[counter]);
			counter = counter + 1;
		}
	}
	else{
		//alert("disable");
		disableSyncEvent("bodyFlash",  '_level0.disableSyncButton,' + curSyncContent[counter]);
	}

}

// ---------------------------------------------------------------------------------------
// NAME: restartVideo, playVideo, pauseMe, resumeMe, stopMe
// PARAMETERS: none
// DESCRIPTION: Windows Media video controls
// LIMITATIONS: None
// ---------------------------------------------------------------------------------------

// restart current
function restartVideo(){
	showLayer();
	stopMe();
	Player.controls.play();
}


// Play a known video file from Media Library.
function playVideo ()
{
   // disable right click
    Player.controls.play();
}

// Pause the Player.
function pauseMe()
{
	Player.controls.pause();
}

// Resume the Player
function resumeMe(){
	Player.controls.play();
}

// Stop the Player.
function stopMe(){
    Player.controls.stop();
    counter=0;
}

// ---------------------------------------------------------------------------------------
// NAME: bodyFlash_DoFSCommand
// PARAMETERS: command, args
// DESCRIPTION: Handles FSCommand messages from flash
// LIMITATIONS: None
// ---------------------------------------------------------------------------------------

var InternetExplorer = navigator.appName.indexOf('Microsoft') != -1;

// Handle all the the FSCommand messages in a Flash movie
function bodyFlash_DoFSCommand(command, args) {
	if (command == "playVideo") {
		var stringToSplit = args
		var stringArray = stringToSplit.split(",");
		videoURL = stringArray[0]
		videoID = stringArray[1]
		stopMe();
		flashErrCode = false;
		initVideo(videoURL, videoID);

		sendBufferingPercent("bodyFlash", '_level0.showBuffering, 0');
	}
	else if (command == "pauseVideo"){
		pauseMe();
	}
	else if (command == "resumeVideo"){
		resumeMe();
	}
	else if (command == "stopVideo"){
		stopMe();
		hideLayer();
	}
	else if (command == "stopTimer"){
		shutDown();
	}
	else if (command == "fullScreen"){
		fullScreen();
		resumeMe();
	}
	else if (command == "exitFullScreen"){
		exitFullScreen();
	}
	else if (command == "restartVideo"){
		restartVideo();

	}
	else if (command == "setVolume"){
		setVolume(args);

	}
	else if (command == "hideLayer"){
		hideLayer()

	}
	else if (command == "showLayer"){
		showLayer();

	}
	else if (command == "launchSpecWin"){
			popup(args);

	}
	else if(command == "launchExternalLink"){
			externalLink(args);
	}
	else if(command == "openPopup"){
		openSyncPopup(args);
	}
	else if(command == "closePopup"){
		closeSyncPopup();
	}
	else if(command == "syncPopup"){
		syncPopupWin(args);
	}
	else if(command == "sendReporting"){
		sendReporting(args);
		//alert(args);
	}
	else if(command == "alert"){
	alert(args);	
	}
}

// Hook for Internet Explorer
if (navigator.appName && navigator.appName.indexOf('Microsoft') != -1 &&
	  navigator.userAgent.indexOf('Windows') != -1 && navigator.userAgent.indexOf('Windows 3.1') == -1) {
	document.write('<SCRIPT LANGUAGE=VBScript\> \n');
	document.write('on error resume next \n');
	document.write('Sub bodyFlash_FSCommand(ByVal command, ByVal args)\n');
	document.write('  call bodyFlash_DoFSCommand(command, args)\n');
	document.write('end sub\n');
	document.write('</SCRIPT\> \n');
}

// ---------------------------------------------------------------------------------------
// NAME: showVideoTopInfo, hideVideoTopInfo, syncEvent, sendCurrentRunTime, sendCurrentDuration
//			sendPlayState, sendBufferingPercent, sendVideoTopInfo
// PARAMETERS: none
// DESCRIPTION: Passes various variables from javascript to flash
// LIMITATIONS: None
// ---------------------------------------------------------------------------------------

// Send sync event to Flash
function syncEvent(swfId, event){
	if (curTimeStamp[0] != -1) {
		document.getElementById(swfId).SetVariable('System.externalFunction.call', event);
	}
}
// Send sync event to Flash
function disableSyncEvent(swfId, event){
		document.getElementById(swfId).SetVariable('System.externalFunction.call', event);
}
// Send sync event to Flash
function enableSyncEvent(swfId, event){
		document.getElementById(swfId).SetVariable('System.externalFunction.call', event);
}
// send current video run time to Flash
function sendCurrentRunTime(swfId, time){
	document.getElementById(swfId).SetVariable('System.externalFunction.call', time);

}

// send current video run time to Flash
function sendCurrentPlayState(swfId, state){
	document.getElementById(swfId).SetVariable('System.externalFunction.call', state);

}

// send current videoduration to Flash
function sendCurrentDuration(swfId, time){
	document.getElementById(swfId).SetVariable('System.externalFunction.call', time);

}

// send buffering % to Flash
function sendBufferingPercent(swfId, time){
	document.getElementById(swfId).SetVariable('System.externalFunction.call', time);
}

function sendVideoTopInfo(swfId, arg){
	document.getElementById(swfId).SetVariable('System.externalFunction.call', arg);
}

function sendVideoTopInfo(swfId, arg){
	document.getElementById(swfId).SetVariable('System.externalFunction.call', arg);
}

function sendVideoError (swfId, arg) {
	document.getElementById(swfId).SetVariable('System.externalFunction.call', arg);
}

function showBuffering(){
	// send buffering signal to flash
	sendBufferingPercent("bodyFlash", '_level0.showBuffering,' + Player.network.bufferingProgress);
}

function showVideoTopInfo(){
	sendVideoTopInfo("bodyFlash", '_level0.showTopVideoInfo, 1');
}

function hideVideoTopInfo(){
	sendVideoTopInfo("bodyFlash", '_level0.showTopVideoInfo, 0');

}
