// jquery-lightbox gebaseerd op jwbox
// [ http://www.longtailvideo.com/support/jw-player/21/jw-box-the-jw-player-lightbox ]

// aanpassingen en toevoegingen
// (c)2010 arthur van zuylen, [arthur at vanzuylen dot org]  

jQuery.jwbox = {
	lightbox: null,
	player	: null,
	toggle	: function() {
		
		if (!$.jwbox.lightbox) {
				$.jwbox.lightbox = $('.jwbox_hidden');
				$.jwbox.center();
				$('#jwbox_background').show();
				$.jwbox.lightbox.css('display','block');
				$.jwbox.center();
				$('#jwbox_background').css('height',$(document).height() + 'px');
				$('object').each(function(){
					$.jwbox.player = document.getElementById(this.id);
				});
		} else {
			try {
				$.jwbox.player.sendEvent('STOP');
				$.jwbox.player = null;
			} catch (err) {
			}
			$.jwbox.lightbox.css('display','none');
			$.jwbox.lightbox = null;
			$('#jwbox_background').hide();
		}
	},
	center	: function() {
		if ($.jwbox.lightbox) {
			$.jwbox.lightbox.center();
		}
	}
}

// hulpfunctie voor positioneren lightbox
jQuery.fn.center = function () {
	this.css('position','fixed');
	this.css('top', ( $(window).height() - this.outerHeight() ) / 2 + 'px'); 
	this.css('left', ( $(window).width() - this.outerWidth() ) / 2 + 'px');
    return this;
}

$(document).ready(function () {
	
	// definieer HTML voor lightbox
	var jwlb = '';
	jwlb =     '<div id="jwbox_background">';
	jwlb +=    '	<div class="jwbox_hidden">';
	jwlb +=    '		<div class="jwbox_content">';
	jwlb +=    '			<div id="videoplayer"></div>';
	jwlb +=    '		</div>';
	jwlb +=    '		<a id="jw_sluit" href="#">sluit venster</a>';
	jwlb +=    '	</div>';
	jwlb +=    '</div>';
	jwlb +=    '<script type="text/javascript">';
	jwlb +=    '	function genereerSWFObject(video_data) {';
	jwlb +=    '		var video_file = video_data;';
	jwlb +=    '		var video_hoogte = 512;';
	jwlb +=    '		var video_breedte = 438;';
	jwlb +=    '		if(video_file.indexOf("|") != -1) {';
	jwlb +=    '			var video_data_array = new Array();';
	jwlb +=    '			video_data_array = video_file.split("|");';
	jwlb +=    '			video_file = video_data_array[0];';
	jwlb +=    '			video_hoogte = video_data_array[1];';
	jwlb +=    '			video_breedte = video_data_array[2];';
	jwlb +=    '		}';
	jwlb +=    '		var so = new SWFObject("video/uva-player.swf","mpl",video_hoogte,video_breedte,"9");';
	jwlb +=    '		so.addParam("allowfullscreen","true");';
	jwlb +=    '		so.addParam("allowscriptaccess","always");';
	jwlb +=    '		so.addParam("wmode","opaque");';
	jwlb +=    '		so.addVariable("config","video/config.xml");';
	jwlb +=    '		so.addVariable("file",video_file);';
	jwlb +=    '		so.addVariable("streamer","rtmp://flashmedia.uva.nl/video");';
	jwlb +=    '		so.write("videoplayer");';
	jwlb +=    '	}';	
	jwlb +=    '</script>';
	
	// voeg lightbox toe aan DOM
	$('body').append(jwlb);
	
	// toggle-functies tonen/verbergen lightbox:
	
	// 1. navigatielink
	$('a.jwbox').click(function () {	
		// genereer SWFObject
		// [functie staat hierboven in lightbox]
		genereerSWFObject(this.rel);
		
		$.jwbox.toggle();
		return false;
	});
	
	// 2. achtergrond
	$('#jwbox_background').click(function () {
		$.jwbox.toggle(); 
		return false;
	});
	
	// 3. sluitbutton
	$('#jw_sluit').click(function() {
		$.jwbox.toggle();
		return false;
	});
	
	// 4. ESC-toets keyboard
	$(document).keyup(function(event){
		if (event.keyCode == 27 && $.jwbox.lightbox) {
			$.jwbox.toggle($('#jwbox_background'));
    	}
	});
	
	// 5. voorkom sluiten lightbox wanneer op content wordt geclickt
	// [anders werken linkjes en videoplayer-buttons niet]
	$('.jwbox_hidden,.jwbox_content').click(function () {
		return false;
	});
	
	// (her)positioneer lightbox wanneer formaat browservenster wordt aangepast 
	$(window).resize(function() {
		$.jwbox.center();
	});
});
