/*

Copyright 2008 Tor-Einar Jarnbjo

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

*/

var OMTK_P = {completeListeners: [], metadataUpdateListeners: []};

/**
 * Initializes the OMTK player and makes it use the specified backend ("flash" or "java")
 */
function OMTK_P_init(backend) {
	
	OMTK_P.isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
	OMTK_P.backend = backend;
	
	if(OMTK_P.backend == "flash") {
		var div = document.createElement("div");
		div.id = "omtk_player";
		div.dummy = true;
		document.body.appendChild(div);

		var swfVersionStr = "0.0.0";
		<!-- To use express install, set to playerProductInstall.swf, otherwise the empty string. -->
		var xiSwfUrlStr = "";
		var flashvars = {};
		var params = {};
		params.quality = "high";
		params.bgcolor = "#FFFFFF";
		params.allowscriptaccess = "sameDomain";
		var attributes = {};
		attributes.id = "omtk_player";
		attributes.name = "omtk_player";
		attributes.align = "middle";
		swfobject.embedSWF(
			"omtkp.swf", "omtk_player", 
			"1", "1", 
			swfVersionStr, xiSwfUrlStr, 
			flashvars, params, attributes);
	}
	else if(OMTK_P.backend == "java") {
		document.write("<applet id='omtk_player' code='org.omtk.js.PlayerApplet' archive='omtkp.jar' style='width: 1px; height: 1px;' MAYSCRIPT></applet>");

		var player = OMTK_P_getPlayer();

		var initialized = false;
		while(!initialized) {
			try {
				initialized = player.isInitialized();
			}
			catch(e) {
			}
		}

	}
	
}

/**
 * Makes the player play the specified URL. Curently playing media streams
 * are stopped.
 */
function OMTK_P_play(url) {
	OMTK_P_getPlayer().play(url);
}

/**
 * Returns the Vorbis comment for the specified key.
 */
function OMTK_P_getMetaData(key) {
	return OMTK_P_getPlayer().getMetaData(key);
}

/**
 * Returns the media stream position in milliseconds since start.
 */
function OMTK_P_getPosition() {
	return OMTK_P_getPlayer().getPosition();
}

/**
 * Adds a callback function, which is called when the currently
 * playing stream is finished.
 */
function OMTK_P_addCompleteListener(f) {
	OMTK_P.completeListeners.push(f);	
}

/**
 * Adds a callback function, which is called when the metadata
 * of the currently playing stream changes (used with chained
 * Ogg streams).
 */
function OMTK_P_addMetadataUpdateListener(f) {
	OMTK_P.metadataUpdateListeners.push(f);	
}

// "private"
function OMTK_P_getPlayer() {
	return document.getElementById("omtk_player");
}

// "private" callback for the player backend
function OMTK_P_complete() {
	for(var i=0; i < OMTK_P.completeListeners.length; i++) {
		OMTK_P.completeListeners[i]();	
	}
}

// "private" callback for the player backend
function OMTK_P_metadataUpdate() {
	for(var i=0; i < OMTK_P.metadataUpdateListeners.length; i++) {
		OMTK_P.metadataUpdateListeners[i]();	
	}	
}
