var subPlayerObject;
var subCaptionsObject;
var subCuepointTimes;
var subActiveCuetime = 0;

var listPlayerObjects = {};

function flowplayerList(elementId, ncaptions) {
	var captions = {};
	var ntime;
	for (var i in ncaptions) {
		ntime = Math.round(parseInt(i) / 100) * 100;

		captions[''+ntime] = ncaptions[i];
	}

	var cuepointTimes = [];
	for (var i in captions) {
		cuepointTimes.push(parseInt(i));
	}
	
	var thisObj = {
		elementId: elementId, 
		captions: captions, 
		cuepointTimes: cuepointTimes,
		activeCueTime: 0
	};

	listPlayerObjects[elementId] = $f(elementId, '/fileadmin/widgets/js/flowplayer/flowplayer.swf', {
		clip: {
			onCuepoint: [
				cuepointTimes, 
				onListCuepoint.bind( thisObj )
			],
			autoPlay: false,
			autoBuffering: true,
			scaling: 'fit',
			cuepointMultiplier: 1,
			onSeek: onListSeekClip.bind( thisObj )
		},
		plugins: {
			content: {
				url: '/fileadmin/widgets/js/flowplayer/flowplayer.content.swf', 
				bottom: 25, 
				height: 40, 
				backgroundColor: 'transparent', 
				backgroundGradient: 'none', 
				border: 0, 
				textDecoration: 'outline', 
				style: {  
					body: {  
						fontSize: 14,  
						fontFamily: 'Arial', 
						textAlign: 'center', 
						color: '#ffffff' 
					}  
				},
				html: ''
			}
		}
	});
}

function flowplayerSub(elementId, inputElements) {
	getSubs(inputElements);

	subPlayerObject = $f(elementId, '/fileadmin/widgets/js/flowplayer/flowplayer.swf', {
//		log: { level: 'debug', filter: 'org.flowplayer.util.*' },
		clip: {
			onCuepoint: [
				subCuepointTimes, 
				onCuepoint
			],
			autoPlay: false,
			autoBuffering: true,
			scaling: 'fit',
			cuepointMultiplier: 1,
			onSeek: onSeekClip
		},
		plugins: {
			content: {
				url: '/fileadmin/widgets/js/flowplayer/flowplayer.content.swf', 
				bottom: 25, 
				height: 40, 
				backgroundColor: 'transparent', 
				backgroundGradient: 'none', 
				border: 0, 
				textDecoration: 'outline', 
				style: {  
					body: {  
						fontSize: 14,  
						fontFamily: 'Arial', 
						textAlign: 'center', 
						color: '#ffffff' 
					}  
				},
				html: ''
			}
		}
	});

}

function getSubs(inputElements) {
	subCaptionsObject = {};
	subCuepointTimes = [];
	
	inputElements.each(function(element) {
		var tc = element.get('rel').split('|');
		tc[0] = Math.round(tc[0] / 100) * 100;
		subCaptionsObject[tc[0]] = element;
		subCuepointTimes.push(parseInt(tc[0]));
		element.addEvent('focus', function(e) {
			var tc = $(this).get('rel').split('|');
			var tm = Math.round(parseInt(tc[0]) / 100) * 100;

			subPlayerObject.seek(tm / 1000);
			subPlayerObject.pause();

			if (subActiveCuetime == tm) {
				onCuepoint(null, tm);
			}
		}).addEvent('keyup', function(e) {
			var tc = $(this).get('rel').split('|');
			var tm = Math.round(parseInt(tc[0]) / 100) * 100;

			if (subActiveCuetime == tm) {
				onCuepoint(null, tm);
			}
		});
	});

	return subCaptionsObject;
}

function onCuepoint(clip, cuepoint) {
	subPlayerObject.getPlugin('content').setHtml(subCaptionsObject[cuepoint].get('value'));
	subActiveCuetime = cuepoint;
}

function onSeekClip() {
	var tm = subPlayerObject.getTime() * 1000;
	var lastTimeMark = -1;
	var compareTime = 0;

	for (i = 0; i < subCuepointTimes.length; i++) {
		compareTime = Math.round(parseInt(subCuepointTimes[i]) / 100) * 100;
		if ((parseInt(subCuepointTimes[i]) > lastTimeMark) && (compareTime <= tm)) {
			lastTimeMark = parseInt(subCuepointTimes[i]);
		}
	}

	if (lastTimeMark > -1) {
		onCuepoint(null, lastTimeMark);
	}
}

function onListCuepoint(clip, cuepoint) {
	listPlayerObjects[this.elementId].getPlugin('content').setHtml(this.captions[cuepoint]);
	this.activeCueTime = cuepoint;
}

function onListSeekClip() {
	var tm = listPlayerObjects[this.elementId].getTime() * 1000;
	var lastTimeMark = -1;
	var compareTime = 0;

	for (i = 0; i < this.cuepointTimes.length; i++) {
		compareTime = Math.round(parseInt(this.cuepointTimes[i]) / 100) * 100;
		if ((parseInt(this.cuepointTimes[i]) > lastTimeMark) && (compareTime <= tm)) {
			lastTimeMark = parseInt(this.cuepointTimes[i]);
		}
	}

	if (lastTimeMark > -1) {
		onListCuepoint.bind(this, [null, lastTimeMark])();
	}
}



