$(function(){

	// auto video detection via class
	var base = $('base').attr('href');
	
	$('.video').each(function (i) {
		var $this	 	= $(this);
		
		// local setup
		var setup = {
			autostart: false, 
			file: base + $this.attr('href'), 
			flashplayer: "typo3conf/ext/mbbs_slideshow/pi1/res/player.swf", 
			volume: 80, 
			width: 720, 
			height: 576, 
			loaded:false
		};
		
		$this.data('video', setup);
		$('body').append('<div id="container"/>');
	});
	
	$('.video').click(function (e) {
		
		e.preventDefault();		
		var $this	 	= $(this),
			localSetup	= $this.data('video');

		// get dimensions, only if not yet loaded
		if (localSetup.loaded !== true) {
			$.get('typo3conf/ext/mbbs_slideshow/pi1/res/dimensions.php', {'file': $this.attr('href')}, function(e) {
				if (typeof(e) != 'undefined' && e.success !== false) {
					localSetup.width	= e.resolution_x;
					localSetup.height	= e.resolution_y;
					localSetup.loaded	= true;
					foo();
				}
			});
		} else {
			foo();
		}
		
		function foo() {
			// put container into place
			jwplayer("container").setup(localSetup);
			$.colorbox({
				inline: true, 
				href: '#container', 
				innerWidth:	localSetup.width,
				innerHeight:localSetup.height,
				opacity: .5,
				title: $this.attr('title'),
				onCleanup : function() {
					$('#container_wrapper').hide();
					jwplayer().stop();
				},
				onComplete : function () {
					$('#container_wrapper').show();
					jwplayer().play();
				}
			});
		}
		
	});
	
	// get slides for each slideshow
	$('a.slideshow').each(function(i) {
		if (typeof(base) == 'undefined')
			var base = $('base').attr('href');
		
		var $this 			= $(this),
			slideshowUrl 	= base + $this.attr('href');
			
		
		$.get(slideshowUrl, function(e) {
			if (typeof(e) != 'undefined') {
				
				// append wrapper
				if ($('#slideshowContainer').length == 0)
					$('body').append('<div id="slideshowContainer" style="display: none;"/>');
				
				var container = $('#slideshowContainer');
				
				// load html
				container.html(container.html() + e.html);
				
				// attach colorbox to new links
				$('a.runtheshow', container).colorbox({
					opacity: .5,
					photo: true,
					slideshow: true,
					slideshowAuto : true
				});
				
				// attach click event to initial link
				$this.click(function(x) {
					x.preventDefault();
					// click first invisible link
					$('a.runtheshow[rel=' + e.rel + ']:eq(0)', container).click();		
				});				
			}
		});
	});
	
	
});

