//
// player.js -- Copyright 2011 One More Story, Inc.
//
// A set of javascript library routines for manipulating the Library Player popup window
//
// getPlayerHandle -- returns true if the player window is found by name.  Will save the
// showPlayer -- shows the player on top of the browser.  Opens the player if needed.  Displays the Play button.
// hidePlayer -- shows the main window on top of the browser, hiding the player.  Displays the Play button.
// closePlayer -- closes the player
// playerClosing -- triggers main window callback when player is closed
// playerClosed -- callback for playerClosing
// writeMessage -- writes a message to the messages div
// writeMainMessage -- writes a message to the messages div in the mainHandle window
// setPlayerType -- takes in a window name and sets the cookie and HTML file names based on it
//

var debug	= false;
if ( debug ) {
	alert( "Loading player.js" );
}


//
// getPlayerHandle -- returns true if the player window is found by name.  Will save the
// handle found in playerHandle.
//
function getPlayerHandle( curPlayerWinName ) {
	var debug	= false;
	var retVal	= false;

	if ( debug ) {
		writeMainMessage( "In getPlayerHandle, curPlayerWinName = [" + curPlayerWinName + "]" );
	}

		// Get the player type

	setPlayerType( curPlayerWinName );
	if ( debug ) {
		writeMainMessage( "back from setPlayerType" );
	}

		// Try to get the handle

	if ( debug ) {
		writeMainMessage( "About to check cookie, pCookie = [" + pCookie + "]" );
	}
	if ( $.cookie( pCookie )) {
		if ( debug ) {
			writeMainMessage( "found playerOpen cookie" );
		}

		var tempHandle	= window.open( "", pWinName );
		if ( debug ) {
			writeMainMessage( "tempHandle = [" + tempHandle + "]" );
		}

		if ( tempHandle.location.href.toString().indexOf( pFile ) > -1 ) {
			playerHandle	= tempHandle;
			retVal			= true;
		} else {
			tempHandle.close();
		}
	} else {
		if ( debug ) {
			writeMainMessage( "pCookie not found, = [" + pCookie + "]" );
		}
	}

	if ( debug ) {
		writeMainMessage( "Done getPlayerHandle" );
	}

	return retVal;
}


//
// showPlayer -- shows the player on top of the browser.  Opens the player if needed.  Displays the Play button.
//

function showPlayer( curPlayerWinName ) {
	var debug	= false;
	if ( debug ) {
		writeMainMessage( "In showPlayer" );
	}

		// Set the player type

	setPlayerType( curPlayerWinName );
	if (( pFile == "" )
			|| ( pCookie == "" )
			|| ( pWinName == "" )) {
		if ( debug ) {
			writeMainMessage( "Player Type not set, exiting" );
		}
		return;
	}

		// Try to bring any existing window back to the top

	var wasOpened	= false;
	if (( playerHandle != null ) && ( playerHandle.closed == false )) {
		playerHandle.focus();
		wasOpened	= true;

	} else if ( getPlayerHandle( curPlayerWinName )) {
		if ( debug ) {
			writeMainMessage( "Trying to use existing window" );
		}

		playerHandle.focus();
		wasOpened	= true;

	}


	if ( !wasOpened ) {

		var wincontrols	= "";

			// Get screen size

		if ( window.screen ) {
			var scrnWidth	= screen.availWidth;
			var scrnHeight	= screen.availHeight;
		} else {
			var scrnHeight = 600;
			var scrnWidth = 800;
		}

			// Open a new window

		if ( debug ) {

			wincontrols	= "toolbar=yes," +
							"location=yes," +
							"status=yes," +
							"menubar=yes," +
							"scrollbars=yes," +
							"resizable=yes," +
							"left=0," +
							"top=0," +
							"width=" + scrnWidth + "," +
							"height=" + scrnHeight;

		} else {

			wincontrols	= "toolbar=no," +
								"location=no," +
								"status=no," +
								"menubar=no," +
								"scrollbars=no," +
								"resizable=no," +
								"left=0," +
								"top=0," +
								"width=" + scrnWidth + "," +
								"height=" + scrnHeight;
		}

		if ( debug ) {
			writeMainMessage( "wincontrols = [" + wincontrols + "]" );
		}

		playerHandle = window.open( pFile, pWinName, wincontrols );
		if ( debug ) {
			writeMainMessage( "playerHandle = [" + playerHandle + "]" );
			writeMainMessage( "playerHandle.toSource() = [" + playerHandle.toSource() + "]" );
		}

/*		!! - playerHandle was coming in as undefined and causing a break in Safari

		if (( !debug )) {
			playerHandle.focus();
		}
*/

		if (( !debug )
				&& ( playerHandle != undefined )) {
			playerHandle.focus();
		}

		wasOpened	= true;
	}


	if ( wasOpened ) {
		wasClosed	= false;

			// Set the cookie

		$.cookie( pCookie, "true", { path: '/', domain: DOMAIN_NAME });
	}

	if ( debug ) {
		writeMainMessage( "Done showPlayer" );
	}
}


//
// hidePlayer -- shows the main window on top of the browser, hiding the player.  Displays the Play button.
//
function hidePlayer() {
	var debug	= false;
	if ( debug ) {
		writeMainMessage( "In hidePlayer" );
	}

	if ( mainHandle ) {
		if ( debug ) {
			writeMainMessage( "Trying to give mainHandle the focus" );
			writeMainMessage( "mainHandle.name = [" + mainHandle.name + "]" );
			writeMainMessage( "navigator.userAgent = [" + navigator.userAgent + "]" );
			writeMainMessage( "$.browser = [" + $.browser + "]" );
			writeMainMessage( "$.browswer.version = [" + $.browser.version + "]" );
		}

			// Check to see if we must use window.open for newer FF browsers

		if ( navigator.userAgent.indexOf( "Gecko" ) > -1 ) {
			if ( debug ) {
				writeMainMessage( "Using window.open to give mainHandle the focus" );
			}
			window.open( "", mainHandle.name, "" );
		}

			// Always call mainHandle.focus()

		if ( debug ) {
			writeMainMessage( "mainHandle.focus() to give mainHandle the focus" );
		}
		mainHandle.focus();

	}


	if ( debug ) {
		writeMainMessage( "Done hidePlayer" );
	}
}


//
// closePlayer -- closes the player
//
function closePlayer( curPlayerWinName ) {
	var debug	= false;
	if ( debug ) {
		writeMessage( "In closePlayer" );
		writeMessage( "curPlayerWinName = [" + curPlayerWinName + "]" );
	}

		// Get the player type

	setPlayerType( curPlayerWinName );

		// Close the window

	if (( playerHandle != null ) && ( playerHandle == mainHandle )) {

			// parent and child are the same, this should not happen

		writeMessage( "main window an player window are the same, doing nothing" );


	} else if ( playerHandle ) {

			// In parent, call child.closePlayer

		writeMessage( "In main window, calling playerHandle.closePlayer()" );
		playerHandle.closePlayer( pWinName );

	} else if ( mainHandle ) {

			// In child, call parent.playerClosed, then close

		writeMessage( "in player window, calling mainHandle.playerClosed()" );
		mainHandle.playerClosed( pWinName );
		callDB( "unlockLibrary", "" );

		self.close();


	} else {

			// Error, shouldn't get here, just close

		writeMessage( "WARNING: Both mainHandle and playerHandle are null" );
		self.close();
	}

	if ( debug ) {
		writeMessage( "Done closePlayer" );
	}
}


//
// playerClosing -- triggers main window callback when player is closed
//
function playerClosing( curPlayerWinName ) {
	var debug	= false;
	if ( debug ) {
		writeMainMessage( "In playerClosing" );
	}

		// Get the player type

	setPlayerType( curPlayerWinName );

	if ( !wasClosed ) {
		wasClosed	= true;

		if ( mainHandle ) {
			writeMainMessage( "Calling mainHandle.playerClosed()" );
			try {
				mainHandle.playerClosed( pWinName );
			} catch ( ex ) {
				writeMainMessage( "Exception caught: " + ex.description );
			}
		}
	}

	if ( debug ) {
		writeMainMessage( "Done playerClosing" );
	}
}


//
// playerClosed -- callback for playerClosing
//
function playerClosed( curPlayerWinName ) {
	var debug	= false;
	if ( debug ) {
		writeMessage( "In playerClosed" );
	}

		// Get the player type

	setPlayerType( curPlayerWinName );

		// Toggle the closed setting

	if ( !wasClosed ) {
		wasClosed	= true;

		if ( debug ) {
			writeMessage( "mainHandle = [" + mainHandle + "]" );
			writeMessage( "playerHandle = [" + playerHandle + "]" );
		}
	}

		// Unlock the library and remove the player cookie.  Note that this may end up
		// calling unlockLibrary twice, once on the player close and once on the call to
		// here.  However, some browsers (safari, opera) block the first call

	unlockLibrary();
	$.cookie( pCookie, null, { path: '/', domain: DOMAIN_NAME });

	if ( debug ) {
		writeMessage( "Done playerClosed" );
	}
}


//
// writeMessage -- writes a message to the messages div
//
function writeMessage( msg ) {
	var debug	= false;

	if ( $( "#messages" ).length ) {
		$( "#messages" ).html( $( "#messages" ).html() + "<br>" + msg );
	} else if ( debug ) {
		alert( msg );
	}
}


//
// writeMainMessage -- writes a message to the messages div in the mainHandle window
//
function writeMainMessage( msg, indent ) {
	var debug	= false;

	try {
		mainHandle.writeMessage( msg );
	} catch ( ex ) {
		if ( debug ) {
			alert( "Exception caught: " + ex.description );
		}
		if ( indent != "undefined" ) {
			for ( curSpace = 0; curSpace < indent; curSpace++ );
				msg	= "&nbsp;" + msg;
			}
		writeMessage( msg );
	}
}


//
// setPlayerType -- takes in a window name and sets the cookie and HTML file names based on it
//
function setPlayerType( curPlayerWinName ) {
	var debug	= false;
	if ( debug ) {
		writeMainMessage( "In setPlayerType" );
		writeMainMessage( "curPlayerWinName = [" + curPlayerWinName + "]" );
	}

		// Get the correct path to the library

	var curPath	= document.location.pathname;
	if ( debug ) {
		writeMessage( "curPath = [" + curPath + "]" );
	}

		// Check for missing or empty curPlayerWinName

	if (( curPlayerWinName == "undefined" )
			|| ( curPlayerWinName == null )
			|| ( curPlayerWinName == "" )) {
		if ( debug ) {
			writeMainMessage( "curPlayerWinName is empty, trying window name" );
		}

			// Try to use the window name

		curPlayerWinName	= window.name;
		if ( debug ) {
			writeMainMessage( "curPlayerWinName = [" + curPlayerWinName +"]" );
		}

			// Try to use the HTML file name

		if ( curPlayerWinName == "" ) {
			if ( debug ) {
				writeMainMessage( "window.name is empty, trying curPath" );
			}

			if ( curPath.indexOf( TAB_PLAYER_WIN_NAME ) > -1 ) {
				curPlayerWinName	= TAB_PLAYER_WIN_NAME;
			} else if ( curPath.indexOf( ITS_TAB_PLAYER_WIN_NAME ) > -1 ) {
				curPlayerWinName	= ITS_TAB_PLAYER_WIN_NAME;
			} else if (( curPath.indexOf( SAB_PLAYER_WIN_NAME ) > -1 )
					|| ( curPath.indexOf( SAB_DONE_FILE ) > -1 )) {
				curPlayerWinName	= SAB_PLAYER_WIN_NAME;
			}
		}
		if ( debug ) {
			writeMainMessage( "curPlayerWinName ends = [" + curPlayerWinName +"]" );
		}
	}

		// Fix path to player files

	if ( curPath.indexOf( "index.html" ) > -1 ) {
		curPath	= LIBRARY_FOLDER;
	} else if ( curPath.indexOf( LIBRARY_FOLDER ) < 0 ) {
		curPath	= ".." + LIBRARY_FOLDER;
	} else {
		curPath	= "";
	}
	if ( debug ) {
		writeMainMessage( "curPath ends = [" + curPath +"]" );
	}

		// Set the player global variables

	switch ( curPlayerWinName ) {

		case PLAYER_WIN_NAME:
			pCookie		= PLAYER_COOKIE;
			pFile		= curPath + PLAYER_FILE;
			pWinName	= PLAYER_WIN_NAME;
			break;

		case SAB_PLAYER_WIN_NAME:
			pCookie		= SAB_PLAYER_COOKIE;
			pFile		= curPath + SAB_PLAYER_FILE;
			pWinName	= SAB_PLAYER_WIN_NAME;
			break;

		case TAB_PLAYER_WIN_NAME:
			pCookie		= TAB_PLAYER_COOKIE;
			pFile		= curPath + TAB_PLAYER_FILE;
			pWinName	= TAB_PLAYER_WIN_NAME;
			break;

		case ITS_TAB_PLAYER_WIN_NAME:
			pCookie		= ITS_TAB_PLAYER_COOKIE;
			pFile		= curPath + ITS_TAB_PLAYER_FILE;
			pWinName	= ITS_TAB_PLAYER_WIN_NAME;
			break;

		default:
			pCookie		= PLAYER_COOKIE;
			pFile		= curPath + PLAYER_FILE;
			pWinName	= PLAYER_WIN_NAME;
			break;
	}

	if ( debug ) {
		writeMainMessage( "pCookie = [" + pCookie + "]" );
		writeMainMessage( "pFile = [" + pFile + "]" );
		writeMainMessage( "pWinName = [" + pWinName + "]" );
		writeMainMessage( "Done setPlayerType" );
	}

}


//
// closeAllPlayers -- attempts to close all players based on existing cookies
//
function closeAllPlayers() {
	var debug	= false;
	if ( debug ) {
		writeMainMessage( "In closeAllPlayers" );
	}

	if ( $.cookie( PLAYER_COOKIE )) {
		closePlayerByCookie( PLAYER_COOKIE );
	}

	if ( $.cookie( SAB_PLAYER_COOKIE )) {
		closePlayerByCookie( SAB_PLAYER_COOKIE );
	}

	if ( $.cookie( TAB_PLAYER_COOKIE )) {
		closePlayerByCookie( TAB_PLAYER_COOKIE );
	}

	if ( $.cookie( ITS_TAB_PLAYER_COOKIE )) {
		closePlayerByCookie( TAB_PLAYER_COOKIE );
	}

	if ( debug ) {
		writeMainMessage( "Done closeAllPlayers" );
	}


}


//
// closePlayerByCookie -- closes a player for a given cookie
//
function closePlayerByCookie( cookieName ) {
	var debug	= false;
	if ( debug ) {
		writeMainMessage( "In closePlayerByCookie, cookieName = [" + cookieName + "]" );
	}

		// get the handle to that window

	var	playerWinName	= "";
	switch ( cookieName ) {
		case PLAYER_COOKIE:
			playerWinName	= PLAYER_WIN_NAME;
			break;

		case SAB_PLAYER_COOKIE:
			playerWinName	= SAB_PLAYER_WIN_NAME;
			break;

		case TAB_PLAYER_COOKIE:
			playerWinName	= TAB_PLAYER_WIN_NAME;
			break;

		case ITS_TAB_PLAYER_COOKIE:
			playerWinName	= ITS_TAB_PLAYER_WIN_NAME;
			break;

		default:
			cookieName		= PLAYER_COOKIE;
			playerWinName	= PLAYER_WIN_NAME;
			break;
	}
	if ( debug ) {
		writeMainMessage( "playerWinName = [" + playerWinName + "]" );
	}

		// close it

	playerHandle	= null;
	getPlayerHandle( playerWinName );
	if ( playerHandle ) {
		playerHandle.closePlayer( playerWinName );

	}

		// remove the cookie

	$.cookie( cookieName, null, { path: '/', domain: DOMAIN_NAME });

	if ( debug ) {
		writeMainMessage( "Done closePlayerByCookie" );
	}
}


if ( debug ) {
	alert( "Done loading player.js" );
	debug	= false;
}

