/*
 * ciabVideoPlayerBox.js
 * functions specific to the video player box
 * copyrighted (c) christian hartmann 2009 (hartmann it-design)
 */

function CallParentPageCallback(clipNum) {
	window.parent.CiabOnClipStart(clipNum);
}

/*
 * flowplayer callback handlers
 */
function onFlowPlayerReady() {
	window.status = "FlowPlayer ready";
	InitFlowPlayerObject();
	stl = InitTrackSubTitelList();
	InitTrackSubtitel();
//	InitTrackAnnotation();
//	SetFlowPlayerConfig();
}

function InitTrackAnnotation() {
	CallBoxFunc("ciabTrackAnnotationBox", "InitTrackAnnotation", "");
}

function InitFlowPlayerObject() {
	if (document.getElementById) {
		fp = document.getElementById('FlowPlayer');
		window.status = 'CiaB Video Player Object Initialized';
	}
}

function onClipChanged(clip) {
	var _clipNum;
	_clipNum = fp.getCurrentClip();
	window.status = "Clip Changed: " + _clipNum;
	_UnshowAllTrackSubtitels();
	ShowTrackSubtitel(_clipNum); // local call
	ShowTrackAnnotation(_clipNum); // remote procedure call
//	HighlightCurrentClip(_clipNum); // remote procedure call
	fp.DoPlay(); // ensures that clip is played even if player is in paused state
	// call parent document callback if present
	CallParentCallback("CiabOnClipStart", _clipNum);
}

/*
function onLoadBegin(clip) {
	var _clipNum = fp.getCurrentClip();
	//alert("Clip Loading: " + _clipNum);
}
*/

function onClipDone(clip) {
	CallParentCallback("CiabOnClipDone");
}

function SelectClip(clipNum) {
    // jump to the clip if playing or not
	fp.ToClip(clipNum);
}

function Play() {
	fp.DoPlay();
}

function IsPlaying() {
	var playing = fp.getIsPlaying(); // API BUGGY?
	var paused = fp.getIsPaused();
	//alert('playing: ' + playing + ' paused: ' + paused);
	//return fp.getIsPlaying();
	return !paused;
}

function PlayClip(clipNum) {
	// jump to and start clip
	fp.ToClip(clipNum);
	fp.DoPlay();
}

function Pause() {
	fp.Pause();
}

function Stop() {
	fp.DoStop();
}

function _GetFlowplayerMainVersion() {
    //var _version_info = fp.getVersion(); // API BROKEN?
    //var _version_info = fp.getVersion().toString();
    //alert(_version_info);
    //var _main_version_number = USE A REGULAR EXPRESSION HERE
    //return version_info[0];
    return 2;
}

function Mute() {
    // mute is not supported in flowplayer older than version 3.0    var _ver = _GetFlowplayerMainVersion();
    if (_ver >= 3) {
    	fp.mute();
    } else {
	    fp.Pause();
    }}

function Unmute() {    // unmute is not supported in flowplayer older than version 3.0
    var _ver = _GetFlowplayerMainVersion();
    if (_ver >= 3) {
    	fp.unmute();
    } else {
    	fp.DoPlay();
    }}

function GetCurrentTrack() {
	try {
		var _cc = fp.getCurrentClip();
	} finally {
		return _cc > 0 ? _cc : 0;
	}
}

function ShowTrackSubtitel(clipNum) {
	//alert("Show Track Subtitel: " + clipNum);
	stl[clipNum - 1].style.display = "block";
}

function UnshowTrackSubtitel(clipNum) {
	stl[clipNum - 1].style.display = "none";
}

function _UnshowAllTrackSubtitels() {
	var _last = stl.length; // we loop here over our (logical) track list
	for (i = 1; i <= _last; i++) {
		UnshowTrackSubtitel(i);
	}
}

function InitTrackSubtitel() {
	var _cc = 0;
	_cc = GetCurrentTrack();
	if ( _cc > 0 ) {
		ShowTrackSubtitel(_cc);
	} else {
		// current track is zero if autoPlay is false. 
		// so enforce subtitel for track 1
		ShowTrackSubtitel(1);
	}
	window.status = "CiaB Subtitel List initialized";
}

function InitTrackSubTitelList() {
	// get the HTML <div> element containing all clip annotations
	var _ul = document.getElementById("ciabTrackSubtitelList");
	// get the individual subtitel elements as an array
	var _stl = _ul.getElementsByTagName("div");
	return _stl;
}



/*
 * local wrappers for remote procedures
 */
function ShowTrackAnnotation(trackNum) {
	CallBoxFunc("ciabTrackAnnotationBox", "ShowTrackAnnotation", trackNum);
}

function HighlightCurrentClip(clipNum)  {
	CallBoxFunc('ciabTrackListBox', 'HighlightCurrentClip', _clipNum);
}

