
// jquery vom parent frame reinladen, falls nicht existent
if (typeof $ == "undefined") {
	$ = window.parent.$;
}

// videos im quiz
jQuery(function($){
	if (!$('body').attr('id').match(/mod-quiz-attempt|question-preview/)) {
		return;
	}
	
	var loopNum = 0;
	
	$('.qtext, .answer label').each(function(){
		var html = $(this).html();
		var originalHtml = html;
		var search = /(?:[\s,]*)([0-9]+)\/([0-9]+)(\/(jpg))?/, matches, htmls = [];
		var hasLoops = false;
		
		while (html && (matches = search.exec(html))) {
		
			htmls.push(html.substring(0, matches.index));
			if (!hasLoops) {
				// first loop
				htmls.push('<div>');
				hasLoops = true;
			}
			html = html.substring(matches.index + matches[0].length);

			var patient = matches[1];
			var loop = matches[2];
			var type = matches[4];

			if (type == 'jpg') {
				htmls.push('<div style="float: left; margin: 0 10px 10px 0;"><img src="/123/blocks/loops/thumb?src=loops/'+patient+'/'+patient+'_'+loop+'.jpg&w=380"></div>');
			} else {
				loopNum++;
				htmls.push('<div style="float: left; margin: 0 10px 10px 0;"><div id="loop-player-'+loopNum+'" class="loop-player"></div></div>');

				swfobject.embedSWF('/123/blocks/loops/swf/player.swf', 'loop-player-'+loopNum, '380', '300', '8', '', {
					file: '/123/blocks/loops/file/loops/'+patient+'/'+patient+'_'+loop+'.flv',
					autostart: false,
					repeat: 'always',
					volume: 80,
					skin: '/123/blocks/loops/swf//123skin_no_vol.swf'
				}, {
					allowfullscreen: true
				});
			}
		}
		html = htmls.join('') + (hasLoops ? '</div>' : '') + (html && hasLoops ? '<div style="clear: both;">'+html+'</div>' : html);
		if (html != originalHtml) {
			$(this).html(html);
		}
	});
});

function sono_parse_video_options(options)
{
	if(typeof options == 'string') {
		options = {file: options};
	}
	
	return options;
}

function sono_video(options)
{
	sono_video.count = ++sono_video.count || 1;
	
	var id = 'loop-player-'+sono_video.count+'-object';

	options = sono_parse_video_options(options);
	options = $.extend({
		autostart: true,
		height: '500',
		width: '690',
		skin: '/123/blocks/loops/swf/123skin.swf',
		volume: 80,
		icons: false
	}, options);
	
	var flashVars = $.extend({}, options);
	delete flashVars.width;
	delete flashVars.height;

	document.write('<div id="loop-player-'+sono_video.count+'"></div>');
	
	swfobject.embedSWF('/123/blocks/loops/swf/player.swf', 'loop-player-'+sono_video.count, options.width, options.height, '8', '/123/js/expressInstall.swf', 
		flashVars, {
			allowfullscreen: true
		}, {
			id: id
		}
	);
	
	return {
		id: id
	}
}

function sono_video_scorm(options)
{
	options = sono_parse_video_options(options);
	
	options.file = document.location.href.replace(/[^/]+$/, '') + options.file;
	options.type = 'video';
	options.width = 660;
	options.height = 445;
	
	var ret = sono_video(options);
	
	

	
	// weiterschalten
	sono_player = null;
	window.playerReady = function(thePlayer) {
		// geht nicht:
		// sono_player = document.getElementById(thePlayer.id);
		// darum fixe id
		sono_player = document.getElementById(ret.id);
		addListeners();
	}

	function addListeners() {
		if (sono_player) { 
			sono_player.addModelListener("STATE", "stateListener");
		} else {
			setTimeout(addListeners, 100);
		}
	}

	window.stateListener = function (obj) { //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED
		currentState = obj.newstate; 
		previousState = obj.oldstate; 

		if ((currentState == "COMPLETED")&&(previousState == "PLAYING")) {
			// finished
			// dirty hack, was moodle kann, kann ich schon lang
			if (top.next) {
				top.location = top.next;
			}
			/*
			doLMSSetValue('nav.event','continue');
			doLMSFinish();
			*/
		}
	}
}

function sono_video_poll(options)
{
	options = sono_parse_video_options(options);
	
	options.width = 200;
	options.height = 150;
	options.repeat = 'always';
	options.controlbar = 'none';
	options.icons = false;

	return sono_video(options);
}

