/* Flash Video transcript v0.2 */
/**
 * Created by Andreas Levers, 2008 - www.96dpi.de
 * JavaScript licensed under Creative Commons Attribution-NonCommercial.
 * Requires the fantastic prototype.js framework and the JW FLV-Player.
 * A big thank you to Ole Begemann for his feedback and support along the way.
 **/

var player = null;
var ts_sync = null;
var ts_current = 0;
var ts_seek_enabled = false;
var ts_currentstate = null;

function playerReady(thePlayer) {
	player = window.document[thePlayer.id];
	ts_addVideoListeners();
}

function ts_addVideoListeners() {
	if (player) { 
		player.addModelListener("TIME", "ts_positionListener");
		player.addModelListener('LOADED', 'ts_loadedListener');
		player.addModelListener("STATE", "ts_stateListener");
	} else {
		setTimeout("ts_addVideoListeners()",100);
	}
}

function ts_positionListener(obj) { 
	ts_current = obj.position;
}

function ts_loadedListener(obj) {
	if (obj.loaded == obj.total && !ts_seek_enabled) {
		ts_seek_enabled = true;
	} 
}

function ts_stateListener(obj) {
	ts_currentstate = obj.newstate;
}

function ts_syncTranscript () {
	transcript_parts = $$('#transcript p');
	var parts_played = new Array();
	$$('#transcript p').each(function(el) {
		el_ts = ts_to_s(el.id.split('_')[1]);
		el_obj = $('ts_'+el_ts);
		if (el_ts <= ts_current) {
			parts_played.push(el);
		} else {
			el.removeClassName('tsactive');
			el.removeClassName('tsplayed');
		}
	});
	for (var p = 0; p < parts_played.length; p++) {
		if (p == parts_played.length-1) {
			parts_played[p].addClassName('tsactive');
		} else {
			parts_played[p].addClassName('tsplayed');
		}
	}
	if ($('autoscroll').checked && ts_currentstate == 'PLAYING') {
		el_last = $$('#transcript p.tsactive');
		if (el_last.length) {
			el_last[el_last.length-1].scrollIntoView(false)
		}

	}
}

function ts_showMovie(theFile) {
	var flashvars = {
		file:theFile, 
		autostart:"true", 
		shuffle:"false"
	}
	var params = {
		allowfullscreen:"true", 
		allowscriptaccess:"always"
	}
	var attributes = {
		id:"player1",  
		name:"player1"
	}

	swfobject.embedSWF("/wp-content/plugins/flash-transcript/mediaplayer/player.swf", "container", "500", $('container').offsetHeight, "9.0.115", false, flashvars, params, attributes);
	ts_sync = setInterval('ts_syncTranscript()',250);
}

function ts_to_s(ts) {
	var el_parts = ts.split('-');
	return parseInt(el_parts[0]*3600) + parseInt(el_parts[1]*60) + parseFloat(el_parts[2]);
}



Event.observe(window, 'load', function() {
	if (!$('transcribed-video')) {
		return false;
	}
	ts_showMovie($('transcribed-video').href);
	$$('#transcript p').each(function(el) {	
		el.style.cursor = 'pointer'
		Event.observe(el, 'click', function(ev) {
			if (!ts_seek_enabled) {
				alert('Bitte warte bis der Film vollständig geladen wurde.')
			} else {
				$$('.tsseek').each(function(oldseek) {
					oldseek.removeClassName('tsseek');
				});
				var ts_paragraph = Event.element(ev);
				ts_paragraph.style.cursor = 'pointer';
				ts_paragraph.addClassName('tsseek');
				seek_pos = ts_to_s(ts_paragraph.id.split('_')[1]);
				player.sendEvent('SEEK', seek_pos);
			}
		})
	});

});