// Global vars

// Interval Object used in polling of players
var oInterval = null;
var oIntervalAuthRec = null;
var pollSrc = "poll.php";
var pollSrcTmp = "";
var eventID = -1;
var slideID = -1;

// Object Used to access the player
var player = null;
var player2 = null;
var playerType = null;
var bufferTime = 0;
var bufferingFlag = false;
var qtimePos = 0;
var qtLoaded = false;
var playerStopped = false;
var finishedFlag = false;
var finishedPosition = 0;
var duration = 0;
var fudgeFactor = 30;

// Flags and vars for Call Viewer
var httpAuthFlag = false;
var authenticated = false;
var chatJoinFlag = false;
var chatJoinInterval = null;
var currentTab = 0;

// Image vars
var loadNum = 0;
var currentSlide = 0;
var currentSelection = 0;

// frameLoaded vars
var numFramesLoaded = 0;
var formatString = "";
var allLoaded = false;
var presentorFlag = false;
var pollDone = false;
var recordedFlag = false;

// event struct
var events = null;
var slideEvents = null;
var currentEvent = 0;
var lastPosition = 0;

// frame objects
var leftFrame = null;
var bottomFrame = null;
var bottomFrame2 = null;
var topFrame = null;
var pollFrame = null;

// chat vars
var chatInput = null;
var messageVar = "";
var chatContent = null;

// flash vars
var flashFlag = true;
var flashSlide = null;
var noFlash = null;
var flashIE = false;

function OnLoadCV( panelName )
{	
	window.parent.numFramesLoaded++;
	var numberLoaded = window.parent.numFramesLoaded;
	var formatString = window.parent.formatString;
	
	//initalize vars for panels
	switch( panelName )
	{
		case "bottom":
			chatContent = GetObj( "chatContent" );
			break;
		case "bottom2":
			chatInput = GetObj( "chatInput" );
			break;
		default:
			break;
	}
		
	if( ( formatString == "video,chat,slides" && numberLoaded == 4 ) || ( formatString == "video,slides" && numberLoaded == 2 ) || ( formatString == "video,chat" && numberLoaded == 3 ) || ( formatString == "video" && numberLoaded == 1 ) )
	{
		// All the frames have loaded start polling
		window.parent.allLoaded = true;
		ChangeLocation();
	}
}

function InitFrameObj()
{
	leftFrame = GetObj( "leftPanel" );
	topFrame = GetObj( "topPanel" );
	bottomFrame = GetObj( "bottomPanel" );
	bottomFrame2 = GetObj( "bottomPanel2" );
	pollFrame = GetObj( "pollFrame" );
	
	if( leftFrame != null && leftFrame.contentWindow != null )
		leftFrame.contentWindow.OutputEntireChat();
}

function InitFlash()
{
	// Determine if the client has flash installed and set up the appropriate vars.
	flashSlide = GetObj( "flashSlide" );
	noFlash = GetObj( "noFlash" );
	
	if( ( flashSlide == null && !isIE ) || ( (flashSlide == null || noFlash != null) && isIE ) )
	{
		flashFlag = false;
	}
}

function ChangeLocation()
{
	if( !window.parent.recordedFlag && window.parent.allLoaded && ( window.parent.presentorFlag || window.parent.formatString != "video" ) )
	{
		// Live case
		//make sure frames have been initialized
		if( window.parent.pollFrame == null )
			window.parent.InitFrameObj();
		
		if( window.parent.pollFrame != null && window.parent.pollFrame.contentWindow != null && ( !window.parent.httpAuthFlag || ( window.parent.httpAuthFlag && window.parent.authenticated ) ) )
		{
			window.parent.pollFrame.contentWindow.src = window.parent.pollSrcTmp;
			window.parent.pollFrame.contentWindow.location = window.parent.pollFrame.contentWindow.src;
			
			if( !window.parent.chatJoinFlag )
			{
				//Chat just connected
				var chatConnection = null;
				
				if( window.parent.bottomFrame != null && window.parent.bottomFrame.contentWindow != null)
					chatConnection = window.parent.bottomFrame.contentWindow.GetObj( "chatConnection" );
				
				if( chatConnection != null )
					chatConnection.innerHTML = "Connected to Chat";
					
				window.parent.chatJoinFlag = true;
					
				//Inform the server that you have joined.
				if( window.parent.bottomFrame2 != null && window.parent.bottomFrame2.contentWindow != null)
					window.parent.bottomFrame2.contentWindow.sendSystemMessages( "join" );
					
				// Flip to slide 0 for httpAuth with a non-presentor
				if( window.parent.httpAuthFlag && !window.parent.presentorFlag && window.parent.topFrame != null && window.parent.topFrame.contentWindow != null )
				{
					window.parent.topFrame.contentWindow.LoadFirstSlide( false, false );
				}					
			}
		}
	}
	else if( window.parent.recordedFlag )
	{
		// Recorded case
		if( !window.parent.chatJoinFlag )
		{
			window.parent.chatJoinFlag = true;
			
			// Flip to slide 0 for httpAuth with a non-presentor
			if( window.parent.httpAuthFlag && !window.parent.presentorFlag && window.parent.topFrame != null && window.parent.topFrame.contentWindow != null )
			{
				window.parent.topFrame.contentWindow.LoadFirstSlide( false, false );
			}
		}
	}
}

function CallFalied( errorType, presentor )
{
	// Function called when call has ended.
	// Redirects client to a call ended notification page.
	StopPolling();
	if( window.parent.leftFrame != null && window.parent.leftFrame.contentWindow != null )
	{
		var info = window.parent.leftFrame.contentWindow.GetObj( "infoContent" );
		// Set error text based on the type of failure.
		var errorText = "This call has ended.<br><a href=\"javascript:window.parent.close()\">Click here to close the window</a>";
		
		if( errorType == "Transcode" )
			errorText = "There was an error transcoding the call.<br>Please <a href=\"javascript:window.parent.close()\">close this call viewer</a> " + ( presentor ? "and re-open this call from the call center." : "then refresh the catalog and re-open this call." );
			
		if( info != null )
		{		
			info.innerHTML += errorText;
		}
	}
}

function StopPolling()
{	
	//Inform the server that you have quit.
	if( !pollDone )
	{
		if( bottomFrame2 != null && bottomFrame2.contentWindow != null)
			bottomFrame2.contentWindow.sendSystemMessages( "quit" );
			
		if( leftFrame != null && leftFrame.contentWindow != null )
			leftFrame.contentWindow.ClearIntervals();	
		
		if( pollFrame != null && pollFrame.contentWindow != null )
		{
			StopPollReload();
			pollFrame.contentWindow.src = "about:blank";
			pollFrame.contentWindow.location = "about:blank";
		}
	}
}

function StopPollReload()
{
	// update the onload for the poll page so that chat is not repeated.
	pollDone = true;
}

function SetPollSrc( format, loadCount, presentor, httpAuth, username, source, callid, password, chatname, live )
{
	if( live )
	{
		window.parent.pollSrcTmp = window.parent.pollSrc = "poll.php?username=" + username + "&source=" + source + "&callid=" + callid + "&password=" + password + "&chatname=" + chatname + "&loadcount=0";
		window.parent.pollSrcTmp = window.parent.pollSrc + "&eventid=-1";
	}
	
	window.parent.formatString = format;
	window.parent.presentorFlag = presentor;
	window.parent.httpAuthFlag = httpAuth;
}

function BrowserSupported()
{
	window.parent.BrowserDetection();
	
	// Tells the user whether their browser is supported
	if( !window.parent.isSupported )
	{
		var info = GetObj( "infoContent" );
		
		if( info != null )
		{
			var infoTemp = "You are not using a supported browser.  We recommend Internet Explorer v. 5.5 or newer or Netscape v. 7.1 or newer.  If you continue to use this browser you may experience some problems.<br>";
			
			if( navigator.platform != "Win32" )
				infoTemp = "You are not using a supported browser.  We recommend Netscape v. 7.1 or newer.  If you continue to use this browser you may experience some problems.<br>";
				
			info.innerHTML += infoTemp;
		}
	}
}

function PlayerLoad( type, live, format, presentor )
{
	// continously check out the time and state of the player
	// Let the applet know the current time and let it update slides and chat accordingly
	
	if( type != "" )
	{
		syncTime = 1000;
		InitPlayer( type );
		
		// Don't poll the quicktime player on Mac platform
		if( live != "live" && (type != "windows media" || !window.parent.isMac) )
		{
			// recorded sync
			ClearIntervals();
			oInterval = window.setInterval( "UpdateTime()", 1000 );
			if( type != "windows media" )
				oIntervalAuthRec = window.setInterval( "AuthTimer()", syncTime );
		}
		else if( !presentor && format != "video" && window.parent.httpAuthFlag && !window.parent.authenticated && type != "windows media" )
		{
			// auth polling
			ClearIntervals();
			oInterval = window.setInterval( "AuthTimer()", syncTime );	
		}
		else if( presentor && window.parent.httpAuthFlag )
		{
			// Auth the presentor
			SetAuthPresentor();
		}
	}
}

function ClearIntervals( type)
{
	// clean up intervals when page is closed, reloaded, or unloaded
	if( oIntervalAuthRec != null )
		window.clearInterval( oIntervalAuthRec );
	else if( oInterval != null )
		window.clearInterval( oInterval );
}

function ClearChatInterval()
{	
	if( window.parent.bottomFrame != null && window.parent.bottomFrame.contentWindow != null && window.parent.bottomFrame.contentWindow.chatJoinInterval != null )
		window.parent.bottomFrame.contentWindow.clearInterval( chatJoinInterval );
}

function SetAuth()
{
	if( window.parent.httpAuthFlag && !window.parent.authenticated )
		window.parent.authenticated = true;
}

function SetAuthPresentor()
{
	SetAuth();
	ChangeLocation();
	ClearIntervals();
}

function PlayerStartBufTime()
{
	if( !window.parent.pollDone )
	{
		// alert the poll page that the player has started to buffer
		window.parent.pollSrcTmp = window.parent.pollSrc + "&eventid=" + window.parent.eventID;
		
		SetAuth();
		ChangeLocation();
	}
}

function PlayerStartPlaying()
{
	PlayerStartBufTime();
}

function PlayerStop()
{
	if( !window.parent.pollDone )
	{
		// alert the poll page that the player has started to buffer
		window.parent.pollSrcTmp = window.parent.pollSrc;
		
		ChangeLocation();
	}
}

// Windows Media Player 9 events
function WM9_playStateChange(NewState)
{
	var playerTmp = GetObj( "Player" );
	
	// Get player for non IE browsers
	if( !window.parent.isIE )
	{
		playerTmp = document.Player;
	}
	
	if( NewState == 6 )
	{
		// Buffering has started
		// Set Auth
		PlayerStartBufTime();
	}
	else if( NewState == 3 )
	{
		// Buffering has started
		// Set Auth
		PlayerStartPlaying();
	}
	else if( NewState == 1 )
	{
		// Set player stop
		PlayerStop();
	}
}

function WM6_playStateChange( lOldState, lNewState )
{
	var playerTmp = GetObj( "Player2" );
	
	// Get player for non IE browsers
	if( !window.parent.isIE )
	{
		playerTmp = document.Player2;
	}
	
	if( lOldState != lNewState && lNewState == 0 )
	{
		PlayerStop();
	}
}

function WM6_Buffering( bStart )
{
	var playerTmp = GetObj( "Player2" );
	
	// Get player for non IE browsers
	if( !window.parent.isIE )
	{
		playerTmp = document.Player2;
	}
	
	if( bStart )
	{
		// Buffering has started
		// Set Auth
		PlayerStartBufTime();
	}
}

function WM6_ReadyState( lReadyState )
{	
	if( window.parent.isIE && lReadyState == 1 )
	{
		WM6_Buffering( true );
	}
}

function LoadFirstSlide( presentor, httpAuth )
{
	var slide = GetObj( "slide" );
	var slideSpacer = GetObj( "slideSpacer" );
	var webpage = GetObj( "webpage" );
	var slidelength = GetObj( "slidelength" );

	if( thumbnails != null && webpage != null && webpage.contentWindow != null && thumbnails.length > 0 )
	{
		if( httpAuth && !presentor )
		{
			slide.className = slideSpacer.className = "hidden";
			webpage.className = "hidden";
			webpage.src = "";
			if( flashSlide != null )
				flashSlide.className = "hidden";
		}
		else if( thumbnails[0].image && flashFlag && ( thumbnails[0].src.match( /\.jpg$/i ) || thumbnails[0].src.match( /\.jpeg$/i ) ) )
		{
			webpage.className = "hidden";
			webpage.src = "";
			if( flashSlide != null )
				flashSlide.className = "visible";
			slide.className = "hidden";
			
		}
		else if( thumbnails[0].image )
		{
			slide.className = "slide";
			slideSpacer.className = "visible";
			slide.style.height = "0px";
			slide.style.width = "0px";
			slide.src = thumbnails[0].src;
			if( flashSlide != null )
				flashSlide.className = "hidden";
			webpage.className = "hidden";
			webpage.src = "";
		}
		else
		{
			slide.className = slideSpacer.className = "hidden";
			webpage.className = "visible";
			webpage.src = thumbnails[0].url;
			webpage.contentWindow.location = webpage.src;
			if( flashSlide != null )
				flashSlide.className = "hidden";
		}
		
		if( presentor && slidelength != null )
		{
			slidelength.innerHTML = thumbnails.length;
			SelectThumbViewer( 0, true );
		}
		
		ResizeSlide( (window.parent.recordedFlag? true : presentor ) );
	}
	else
	{
		slide.className = slideSpacer.className = "hidden";
		webpage.className = "hidden";
		webpage.src = "";
		if( flashSlide != null )
			flashSlide.className = "hidden";
	}
}

function getDimensions()
{
	// determine the height and width of the client area
	var width = 0;
	var height = 0;
	
	if (typeof(window.innerWidth) == 'number')
	{
		// Not Internet Explorer
		width = window.innerWidth ;
		height = window.innerHeight;
	}
	else if ((document.documentElement) && ((document.documentElement.clientWidth) || (document.documentElement.clientHeight)))
	{
		// IE 6+ in standards-compliant-mode
		width = document.documentElement.clientWidth;
		height = document.documentElement.clientHeight;
	}
	else if ((document.body) && ((document.body.clientWidth) || (document.body.clientHeight)))
	{
		//IE 4 compatible
		width = document.body.clientWidth;
		height = document.body.clientHeight;
	}
	
	return new Array(width, height);
}

function ResizeSlideWrapper( presentor )
{	
	if( window.parent.isIE && window.parent.topFrame != null && window.parent.topFrame.contentWindow != null )
	{
		window.parent.topFrame.contentWindow.ResizeSlide( presentor );
	}
}

function ResizeSection( width, height, sectionID, staticSectionWidth, staticSectionHeight )
{
	// Resizes the hidden thumbnails
	// This will allow the thumbnails div to have a scrollbar
	var section = GetObj( sectionID );
	
	if( section != null && width != null && (width - staticSectionWidth) > 0)
	{
		section.style.width = ( width - staticSectionWidth ) + "px";
	}
	
	if( section != null && height != null && (height - staticSectionHeight) > 0  )
	{
		section.style.height = ( height - staticSectionHeight ) + "px";
	}
}

function ResizeSlide( presentor )
{
	if( thumbnails != null )
	{
		// determine the height and width of the client area
		var dimensionsArray = getDimensions();
		var width = dimensionsArray[0];
		var height = dimensionsArray[1];

		ResizeSection( null, height, "thumnailsDiv", 0, (isIE?91:102) );
		
		var slide = GetObj( "slide" );
		var slideSpacer = GetObj( "slideSpacer" );
		var webpage = GetObj( "webpage" );
		var slideTabHeight = 100;
		var spacer = 20;
		
		if( !presentor )
			slideTabHeight = 20;
			
		slideTabHeight += spacer;
		
		var divHeight = height - slideTabHeight;
		var divWidth = width;
		var slideHeight = thumbnails[currentSlide].height;
		var slideWidth = thumbnails[currentSlide].width;
		
		if( divHeight < 200 )
		{
			divHeight = 200;
		}
		
		if( divWidth < 200 )
		{
			divWidth = 200;
		}
		
		if( thumbnails[currentSlide].image && ( !flashFlag || ( !thumbnails[currentSlide].src.match( /\.jpg$/i ) && !thumbnails[currentSlide].src.match( /\.jpeg$/i ) ) ) )
		{
			var tempWidth;
			var tempHeight;
			var slideTop = null;
			var slidePadding = 10;
			
			if( slideWidth > slideHeight )
			{
				tempWidth = divWidth;
				tempHeight = tempWidth * ( slideHeight / slideWidth );
				
				if( tempHeight > divHeight )
				{
					tempHeight = divHeight;
					tempWidth = tempHeight * ( slideWidth / slideHeight );
				}
				
				// need to center the slide vertcally
				slideTop = 0.5*(divHeight - tempHeight);
			}
			else
			{
				tempHeight = divHeight;
				tempWidth = tempHeight * ( slideWidth / slideHeight );
				
				if( tempWidth > divWidth )
				{
					tempWidth = divWidth;
					tempHeight = tempWidth * ( slideHeight / slideWidth );
				}
			}
			
			slide.style.width = tempWidth - slidePadding;
			slide.style.height = tempHeight - slidePadding;
			
			if( slideTop != null && slideTop > 0 )
				slideSpacer.style.height = slideTop + "px";
			else
				slideSpacer.style.height = "0px";
		}
		else if( thumbnails[currentSlide].image )
		{
			if( flashSlide != null )
			{
				flashSlide.style.width = divWidth - 18;
				flashSlide.style.height = divHeight - 10;
			}
		}
		else
		{	
			webpage.style.width = divWidth - 18;
			webpage.style.height = divHeight - 10;
		}
	}
}

function ResizeBranding()
{
	var brand = GetObj( "brand" );
	
	if( brand != null )
	{
		var brandHeight = brand.height;
		var brandWidth = brand.width;
		var divWidth = 181;
		
		if( brandWidth > divWidth )
		{
			var tempWidth;
			var tempHeight;
			
			tempWidth = divWidth;
				
			tempHeight = tempWidth * ( brandHeight / brandWidth );
			
			brand.style.width = tempWidth;
			brand.style.height = tempHeight;
		}
	}
}

function SelectThumbnailViewer( i, presentor, recorded )
{	
	SelectThumbViewer( i, presentor );
	
	if( recorded )
	{
		SlideFlipRec( i );
	}
	else if( presentor)
	{
		SlideFlipSend( i );
	}
	else
	{
		// code for both non presentor
	}
	
	TabClick(0);
}

function SelectThumbViewer( i, presentor )
{	
	var oldThumbID = "thumbnail" + currentSelection;
	var thumbID = "thumbnail" + i;
	
	GetObj( oldThumbID ).className = "thumb";
	
	GetObj( thumbID ).className = "thumbSelected";
	
	currentSelection = i;
}

function updateEventId( eventid )
{
	// update the onload for the poll page so that chat is not repeated.
	window.parent.eventID = eventid;
}

function pollOnLoad()
{
	if( !window.parent.pollDone )
	{		
		eval( "window.location='/call_viewer/" + window.parent.pollSrc + "&eventid=" + window.parent.eventID + "'");
	}
}

function chatFocus()
{
    chatInput = GetObj("chatInput");
    chatInput.select();
    chatInput.focus();
}

function chatOnLoad()
{
	chatJoinInterval = window.setInterval( "joiningChat()", 1000 );
}

function joiningChat()
{
	if( !window.parent.chatJoinFlag )
	{
		var chatConnection = null;
		
		if( window.parent.bottomFrame != null && window.parent.bottomFrame.contentWindow != null )
			chatConnection = window.parent.bottomFrame.contentWindow.GetObj( "chatConnection" );
		
		if( chatConnection != null )
		{
			if( chatConnection.innerHTML == "Joining Chat...." )
			{
				chatConnection.innerHTML = "Joining Chat.";
			}
			else
			{
				chatConnection.innerHTML += ".";
			}
		}
	}
	else
	{
		ClearChatInterval();
	}
}

function sendIRCMessages( event, sendButton )
{
	if( ( event.keyCode == 13 || sendButton ) && ( !window.parent.httpAuthFlag || ( window.parent.httpAuthFlag && window.parent.authenticated ) ) )
	{
		messageVar = chatInput.value;
		
		// escape ' and "
		messageVar.replace( /'/g, "\'" );
		messageVar.replace( /\"/g, "\"" );
		ValueSet( "message", messageVar );
		
		FormSubmit( "slidesChatForm" );
		ValueSet( "message", "" );
		chatInput.value = "";
	}
}

function sendSystemMessages( messageType )
{
	//Send messages related to join, quit, etc.
	if( !window.parent.httpAuthFlag || ( window.parent.httpAuthFlag && window.parent.authenticated ) )
	{
		// escape ' and "
		messageType.replace( /'/g, "\'" );
		messageType.replace( /\"/g, "\"" );
		ValueSet( "sysMessage", messageType );
		
		FormSubmit( "slidesChatForm" );
		ValueSet( "sysMessage", "" );
	}
}

function printIRCMessages( message, live )
{	
	if( bottomFrame != null && bottomFrame.contentWindow != null )
	{	
		// unescape ' and "
		bottomFrame.contentWindow.chatContent.innerHTML += message;
		
		chatLines = bottomFrame.contentWindow.chatContent.innerHTML.match( /<br>/gi ).length;
		
		// There are 46 lines of text per a page of text.
		// It's not a very scientific unit, but chat will be limited 2 pages or 92 lines.
		
		if(  chatLines > 92 && live )
			bottomFrame.contentWindow.chatContent.innerHTML = trimIRCMessages( chatLines - 92, bottomFrame.contentWindow.chatContent.innerHTML );
			
		bottomFrame.contentWindow.scrollIRC();
	}
}


function clearIRCMessages()
{	
	if( bottomFrame != null && bottomFrame.contentWindow != null )
	{	
		bottomFrame.contentWindow.chatContent.innerHTML = "<span style=\"font-weight:bold;\">Connected to Chat</span><br>";
		bottomFrame.contentWindow.scrollIRC();
	}
}

function trimIRCMessages( numberLines, chat )
{	
	// This function trims off so many lines from the begining of thet chat.
	// It's used so the browser does not get bogged down.
	if( chat != null )
	{			
		var chatArray = chat.split( /\s*<br>\s*/i )
		
		chatArray = chatArray.slice( numberLines );
		
		// IE and Netscape implemented join diffrently.
		// add <br> to the end for IE
		if( window.parent.isIE )
		{
			chat = chatArray.join("<br>") + "<br>";
		}
		else
		{
			chat = chatArray.join("<br>");
		}
			
	}
	return chat;
}

function scrollIRC()
{
	if( window.parent.bottomFrame != null && window.parent.bottomFrame.contentWindow != null )
		window.parent.bottomFrame.contentWindow.document.body.scrollTop = window.parent.bottomFrame.contentWindow.document.body.scrollHeight;
}

// Slide Control Functions

// Client Side

function SlideFlipRec( slideNum, presentor )
{
	// Called when user selects a slide from a call viewer during replay
	// Change player position
	if( ( !window.parent.httpAuthFlag || ( window.parent.httpAuthFlag && window.parent.authenticated ) ) && window.parent.leftFrame != null && window.parent.leftFrame.contentWindow != null && window.parent.leftFrame.contentWindow.slideEvents.length > 0 )
	{
		var leftFrameTemp = window.parent.leftFrame.contentWindow;
		// we have the time in seconds
		// change the player position to that time
		if( slideNum >= 0 && leftFrameTemp.slideEvents.length > slideNum )
		{
			if( leftFrameTemp.playerType == "windows media9" )
			{
				leftFrameTemp.player.controls.currentPosition = leftFrameTemp.slideEvents[slideNum].ts;
			}
			else if( leftFrameTemp.playerType == "windows media6" )
			{
				leftFrameTemp.player.currentPosition = leftFrameTemp.slideEvents[slideNum].ts;
			}
			else if( leftFrameTemp.playerType == "quicktime" )
			{
				// get currentPosition and convert to seconds.
				
				leftFrameTemp.player.Stop();
				
				// check to see player is not at the end
				if( leftFrameTemp.slideEvents[slideNum].ts * leftFrameTemp.player.GetTimeScale() > leftFrameTemp.player.GetDuration() )
					leftFrameTemp.player.SetTime( leftFrameTemp.player.GetDuration() + leftFrameTemp.player.GetStartTime() - leftFrameTemp.player.GetTimeScale() );
				else
					leftFrameTemp.player.SetTime( leftFrameTemp.slideEvents[slideNum].ts * leftFrameTemp.player.GetTimeScale() + leftFrameTemp.player.GetStartTime() );
				
				leftFrameTemp.player.Play();
			}
		}
	}
}

function HandleScriptEvent( type, param )
{
	// Function to handle url events for windows media player
	if( type == "URL" )
	{
		//flip the slide
		var paramString = new String(param);
		var index = paramString.match(/index=\d+/)[0].slice(6);
		window.parent.sb_slideflip( index );
	}
}
function sb_slideflip( slideNum )
{
	// Fired when slide flips are embedded in the stream.
	if( !presentorFlag && ( !recordedFlag || ( isMac && transcode ) ) )
	{
		SlideFlip( slideNum, presentorFlag, false );
	}
}

function SlideFlip( slideNum, presentor, preview )
{	
	var domObj = window;

	if( !preview )
	{
		if( topFrame != null )
			domObj = topFrame.contentWindow;
		else
			domObj = null;
	}
	else
	{
		// Check that the slideNum is ok
		if( slideNum < 0 || slideNum >= domObj.thumbnails.length )
		{
			domObj = null;
		}
	}

	if( domObj != null && ( preview || domObj.currentSlide != ( slideNum - 1 ) ) )
	{
		var slide = domObj.GetObj( "slide" );
		var slideSpacer = domObj.GetObj( "slideSpacer" );
		var webpage = domObj.GetObj( "webpage" );
		var slidenumber = domObj.GetObj( "slidenumber" );
		
		if( !preview )
			slideNum--;
		domObj.currentSlide = slideNum;
		
		if( domObj.thumbnails != null && webpage != null && webpage.contentWindow != null )
		{
			if( domObj.thumbnails[slideNum].image && domObj.flashFlag && ( domObj.thumbnails[slideNum].src.match( /\.jpg$/i ) || domObj.thumbnails[slideNum].src.match( /\.jpeg$/i ) ) )
			{
				if( ( ( domObj.noFlash == null && isIE ) || ( domObj.flashSlide != null && !isIE ) ) && domObj.flashSlide )
				{
					domObj.flashSlide.SetVariable("_global.currentIndex", slideNum); 
					domObj.flashSlide.TCallFrame("/", 1);
					domObj.flashSlide.className = "visible";
				}
				
				slide.className = slideSpacer.className = "hidden";
				webpage.className = "hidden";
				webpage.src = "";
			}
			else if( domObj.thumbnails[slideNum].image )
			{
				slide.className = "slide";
				slideSpacer.className = "visible";
				webpage.className = "hidden";
				webpage.src = "";
				slide.style.height = "0px";
				slide.style.width = "0px";
				slide.src = domObj.thumbnails[slideNum].src;
				if( domObj.flashSlide )
					domObj.flashSlide.className = "hidden";
			}
			else
			{
				slide.className = slideSpacer.className = "hidden";
				webpage.className = "visible";
				webpage.src = domObj.thumbnails[slideNum].url;
				webpage.contentWindow.location = webpage.src;
				if( domObj.flashSlide )
					domObj.flashSlide.className = "hidden";
			}
			
			if( presentor && slidenumber != null )
			{
				slidenumber.innerHTML = slideNum + 1;
				domObj.SelectThumbViewer( slideNum, true );
			}
			
			if( domObj.currentTab != 0 && presentor )
			{
				webpage.className = "hidden";
				if( domObj.flashSlide )
					domObj.flashSlide.className = "hidden";
			}
			
			domObj.ResizeSlide( (window.parent.recordedFlag? true : presentor ) );
		}
	}
}

// Server Side
function SlideFlipSend( slideNum )
{	
	if( window.parent.topFrame != null && window.parent.topFrame.contentWindow != null && window.parent.topFrame.contentWindow.thumbnails != null && ( !window.parent.httpAuthFlag || ( window.parent.httpAuthFlag && window.parent.authenticated ) ) )
	{
		if( slideNum < 0 )
		{
			slideNum = 0;
		}
		else if( slideNum >= window.parent.topFrame.contentWindow.thumbnails.length )
		{
			slideNum = window.parent.topFrame.contentWindow.thumbnails.length - 1;
		}
		slideNum++;
		
		ValueSet( "slideid", slideNum );
		FormSubmit( "slidesChatForm" );
	}
}

function NextSlideSend( )
{	
	if( window.parent.topFrame != null && window.parent.topFrame.contentWindow != null )
		SlideFlipSend( window.parent.topFrame.contentWindow.currentSlide + 1 );
}

function PreviousSlideSend( )
{	
	if( window.parent.topFrame != null && window.parent.topFrame.contentWindow != null )
		SlideFlipSend( window.parent.topFrame.contentWindow.currentSlide - 1 );
}

function NextSlideRec( )
{	
	if( window.parent.topFrame != null && window.parent.topFrame.contentWindow != null )
		SlideFlipRec( window.parent.topFrame.contentWindow.currentSlide + 1 );
}

function PreviousSlideRec( )
{	
	if( window.parent.topFrame != null && window.parent.topFrame.contentWindow != null )
		SlideFlipRec( window.parent.topFrame.contentWindow.currentSlide - 1 );
}

function ControlsButton( eventType )
{
	if( !window.parent.httpAuthFlag || ( window.parent.httpAuthFlag && window.parent.authenticated ) )
	{
		ValueSet( "eventtype", eventType );
		FormSubmit( "slidesChatForm" );
	}
}

function OverQuota( userName )
{
	var info = leftFrame.contentWindow.GetObj( "infoContent" );

	if( info != null )
	{		
		info.innerHTML = "User " + userName + " has exceeded their quota limit.  Recording will be disabled for that user.";
	}
}

function UpdateRecordState( recording )
{
	if( leftFrame != null && leftFrame.contentWindow != null )
	{
		var startrec = leftFrame.contentWindow.GetObj( "swapcvButton1" );
		var stoprec = leftFrame.contentWindow.GetObj( "swapcvButton2" );
		
		if( startrec != null && stoprec != null )
		{
			if( recording )
			{
				startrec.className = "hidden";
				stoprec.className = "visibleInline";
			}
			else
			{	
				startrec.className = "visibleInline";
				stoprec.className = "hidden";
			}
		}
	}
}

// Player Functions
// Init for each player
function InitPlayer( type )
{
	playerType = type;
	player = GetObj( "Player" );
	player2 = GetObj( "Player2" );
	var version = /^9./i;
	
	// Get player for non IE browsers
	if( !window.parent.isIE )
	{
		player = document.Player;
		player2 = document.Player2;
	}
	
	if( type == "windows media" )
	{
		if( window.parent.isMac )
		{
			playerMac = document.PlayerMac;
			playerDiv = GetObj( "playerDiv" );
			playerDiv.style.height = (window.parent.recordedFlag?430:396);
			
			playerType = "windows media6";
			player.className = "hiddenPlayer";
			player2.className = "hiddenPlayer";
			player = playerMac;
			
			if( !audioOnly )
				player.style.height = (window.parent.recordedFlag?430:396);
			else
				player.style.height = (window.parent.recordedFlag?72:52);
			player.className = "visiblePlayer";
		}
		else if( player.playState == null )
		{
			playerType = "windows media6";
			player.className = "hiddenPlayer";
			
			player = player2;
			if( !audioOnly )
				player.style.height = 365;
			else
				player.style.height = 97;
			player.className = "visiblePlayer";
			
			if( window.parent.isIE )
				player.style.border = "1px solid black";
			
			player.FileName = playerSrc;
		}
		else if( player.versionInfo.search( version ) == -1 && !window.parent.transcode )
		{
			// Windows media player 7 fix
			player.URL = "";
			player.controls.stop();
			player.className = "hiddenPlayer";
			
			playerType = "windows media6";
			player = player2;
			if( !audioOnly )
				player.style.height = 365;
			else
				player.style.height = 97;
			player.className = "visiblePlayer";
			
			if( window.parent.isIE )
				player.style.border = "1px solid black";
			
			player.FileName = playerSrc;
		}
		else
		{
			player2.className = "hiddenPlayer";
			
			if( !audioOnly )
				player.style.height = 365;
			else
				player.style.height = 64;
			playerType = "windows media9";
			
			if( window.parent.isIE )
				player.style.border = "1px solid black";
			
			player.URL = playerSrc;
		}
	}
	else if( type == "quicktime" && window.parent.isIE && window.parent.isSupported && player != null  )
	{
		// Fix for QuickTime not showing up until mouse over
		// Netscape does not support events for embed elements
		player.fireEvent("onmouseover");
	}
}
		
// Player events
function UpdateTime()
{	
	// Handle events for the players
	if( player != null )
	{
		var currentPosition = 0;
		// seconds to add to get rest of events
		
		if( playerType == "windows media9" )
		{
			currentPosition = player.controls.currentPosition;
			
			if( player.currentMedia.duration - 1.0 <= currentPosition && currentPosition != 0 )
			{
				//MediaEnded
				finishedFlag = true;
				finishedPosition = currentPosition + fudgeFactor;
				duration = currentPosition - 1;
			}
		}
		else if( playerType == "windows media6" )
		{
			// get currentPosition
			currentPosition = player.currentPosition;
			
			if( player.Duration - 1.0 <= currentPosition && currentPosition != 0)
			{
				//MediaEnded
				finishedFlag = true;
				finishedPosition = currentPosition + fudgeFactor;
				duration = currentPosition - 1;
			}
		}
		else if( playerType == "quicktime" )
		{
			status = player.GetPluginStatus();
			if( status != "Waiting" )
			{
				// get currentPosition and convert to seconds.
				currentPosition = ( player.GetTime() - player.GetStartTime() ) / player.GetTimeScale();				
				
				if( player.GetDuration() - player.GetTimeScale() <= player.GetTime() && currentPosition != 0 )
				{
					//MediaEnded
					finishedFlag = true;
					finishedPosition = currentPosition + fudgeFactor;
					duration = currentPosition - 1;
				}
			}
			else
			{
				currentPosition = -1;
			}
		}
		
		finishedFlag = TimeUpdate( currentPosition, finishedFlag, finishedPosition, duration );
	}
}

function TimeUpdate( currentPosition, finishedFlag, finishedPosition, duration )
{
	var reset = false;
	var sumChatMsg = "";
	var sumSlideFlips = null;
	// update chat and slides based on the currentPosition of the player
	if( finishedFlag && ( currentPosition <= duration && currentPosition != 0 ) )
	{
		finishedFlag = false;
	}
	
	if( ( currentPosition < lastPosition && !finishedFlag ) || ( lastPosition == 0 && lastPosition != currentPosition ) )
	{		
		// Player has seeked backwards
		// clear chat and reset currentEvent
		currentEvent = 0;
		window.parent.clearIRCMessages();
		
		if( currentPosition != -1 )
			window.parent.SlideFlip( 1, true, false );
	}
	
	for( i = currentEvent; i < events.length; i++ )
	{
		// Update chat and slides
		if( currentPosition >= events[i].ts || ( finishedFlag && finishedPosition >= events[i].ts ) )
		{
			if( events[i].slideID != null )
			{
				sumSlideFlips = events[i].slideID;
			}
			else if( events[i].chatMsg != null )
			{
				sumChatMsg += events[i].chatMsg;
			}
			
			currentEvent++;
		}
		else
		{
			break;
		}
	}
	
	if( sumSlideFlips != null )
		window.parent.SlideFlip( sumSlideFlips, true, false );
	
	if( sumChatMsg != "" )
		window.parent.printIRCMessages( sumChatMsg, false );
	
	lastPosition = currentPosition;
	
	return finishedFlag;
}

function OutputEntireChat()
{
	// Output the entire chat log.
	// This is used for players that can't be scripted.
	if( window.parent.isMac && window.parent.transcode && window.parent.recordedFlag )
	{
		var sumChatMsg = "";
		
		for( i = 0; i < events.length; i++ )
		{
			if( events[i].chatMsg != null )
			{
				sumChatMsg += events[i].chatMsg;
			}
		}

		if( sumChatMsg != "" )
			window.parent.printIRCMessages( sumChatMsg, false );
	}
}

function AuthTimer()
{	
	// Check to see if owner of the call has gotten http Auth
	
	if( player != null )
	{	
		if( playerType == "quicktime" && ( player.GetTime() == qtimePos ) )
		{
			if( !bufferingFlag )
			{
				// Buffering has started
				bufferingFlag = true;
			}
		}
		else if( bufferingFlag )
		{
			// done buffering
			// Hack for quicktime and httpAuth
			if( playerType == "quicktime" )
				SetAuthPresentor();
			
			// reset bufferingFlag
			bufferingFlag = false;
			bufferTime = 0;
		}
		
		if( playerType == "quicktime" )
		{
			qtimePos = player.GetTime();
		}
	}
}

// NEW FUNCTIONS
function TabClick( tabNum )
{
	if( !window.parent.httpAuthFlag || ( window.parent.httpAuthFlag && window.parent.authenticated ) )
	{
		var presentation = GetObj( "presentation" );
		var selector = GetObj( "slideSelector" );
		var slideSelectorDiv = GetObj( "slideSelectorDiv" );
		var slide = GetObj( "slide" );
		var webpage = GetObj( "webpage" );
		
		if( tabNum == 0 )
		{
			presentation.style.zIndex = 2;
			selector.style.zIndex = 1;
			slideSelectorDiv.className = "hiddenSlides";
			
			if( thumbnails != null && !thumbnails[currentSlide].image )
			{
				webpage.className = "visible";
			}
			else if( thumbnails != null && thumbnails[currentSlide].image && flashFlag && ( thumbnails[currentSlide].src.match( /\.jpg$/i ) || thumbnails[currentSlide].src.match( /\.jpeg$/i ) ) )
			{		
				flashSlide.className = "visible";
			}
			
			ResizeSlide( true );
		}
		else
		{
			webpage.className = "hidden";
			if( flashSlide )
				flashSlide.className = "hidden";
			
			slideSelectorDiv.className = "visibleSlides";
			presentation.style.zIndex = 1;
			selector.style.zIndex = 2;
		}
		
		currentTab = tabNum;
	}
}

// Events Struct Functions
function InitEvents()
{
	// create the array that will hold all the Events for recorded and export
	events = new Array();
	slideEvents = new Array();
	currentEvent = 0;
}

function Event( slideID, chatUser, chatMsg, ts )
{
	// Creates a Thumbnail object.
	this.slideID = slideID;
	this.chatUser = chatUser;
	this.chatMsg = chatMsg;
	this.ts = ts;
}
