jQuery(document).ready(function($) {

	var timer;
	
	$("#videogallery img[rel]").overlay({
		fixed: false,
		closeSpeed: 'slow',
		effect: 'apple',
		speed: 'slow',
		api: true,
		top: 'center',
		closeOnClick: false,
		
		mask: '#000000',

		// when overlay is opened, load our player
		onLoad: function() {
		
			$f().load();
			
			// load data for description box
			var url = this.getOverlay().children('.myplayer').attr("href");
			if (url) {
				var baseurl = 'http://www.icf-karlsruhe.de/fileadmin/content/karlsruhe/Media/Movies/';

				var filename = url;
				filename = basename(filename, '.mp4');
				filename = basename(filename, '.f4v');
				filename = basename(filename, '.m4v');
				//filename = filename.replace('.mp4', '.html');
				//filename = filename.replace('.f4v', '.html');
				filename = baseurl + filename;
				
				var overlay = this.getOverlay();
				
				//alert(filename + '.html');
				
				$.get(filename + '.html', function(data){
					overlay.children('.details').html(data);
					var newHeight = overlay.children('.details').height();
					//console.log(newHeight);
					overlay.children('.transparency').height(20 + newHeight);
				});
			}
			
			/* click function for SD-HD-Switch */
			$('.sd_hd a').bind('click', function() {
				var url = overlay.children('.myplayer').attr('href');
				
				if ( $(this).children('img.sd').css('display') == 'inline' ) {
					if (url) {
						var filename = url;
						filename = filename.replace('SD', 'HD');
											
						$f().stop();
						
						$f().getClip(0).update({url:filename});
						$f().play(filename);
					}
			
					// switch auf HD
					$(this).children('img.sd').css('display', 'none');
					$(this).children('img.hd').css('display', 'inline');
				} else {
					if (url) {
						var filename = url;
						filename = filename.replace('HD', 'SD');
						
						$f().stop();
						
						$f().getClip(0).update({url:filename});
						$f().play(filename);
					}
					
					// switch auf SD
					$(this).children('img.sd').css('display', 'inline');
					$(this).children('img.hd').css('display', 'none');
				}
				$(this).blur();
				return false;
			});
		},

		// when overlay is closed, unload our player
		onClose: function() {
			$('.sd_hd a').children('img.sd').css('display', 'inline');
			$('.sd_hd a').children('img.hd').css('display', 'none');
			$('.sd_hd a').unbind('click');
			$f().unload();
		}
	});
	
	/* hover effect for video description */
	$('.voverlay').hover(function(){
		$(this).children('.animate').stop(true, true).fadeIn('slow');
	}, function(){
		$(this).children('.animate').stop(true, true).fadeOut('slow');
	});
	
	
	/* mouseover effect for video description */
	$('.voverlay').mousemove( function(e){
		var this1 = $(this);
		this1.children('.animate').show();
		window.clearTimeout(timer);
		
		timer = window.setTimeout( function () {
			this1.children('.animate').fadeOut('slow');
		}, 2250);
	});
	
	/* hover effect for video description */
	$('.voverlay').mouseout( function(e){
		window.clearTimeout(timer);
	});
	

	// wmode funktioniert hier nicht!
	//$('a.myplayer').flowplayer('http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf', {wmode: 'opaque'});
	
	flowplayer('a.myplayer', {
		src: 'http://flowplayer.icf-ka-download.de/flowplayer-3.2.1.swf', 
		wmode: 'opaque'
	});
	
	/* alternativ dazu */
	/*
	var src = this.getTrigger().attr("href");
	swfobject.createSWF({
		data: src,
		width: "100%",
		height: "100%",
		wmode: "opaque"
	},{
		allowScriptAccess: "always",
		allowFullScreen: true
	}, 'a.myplayer');
	*/
	
	
	/* hover effekt for thumbnail description */
	$('.videogallery_thumb_current, .videogallery_thumb').hover(function(){
		$(this).children('.animate').stop(true, true).fadeIn();
	}, function(){
		$(this).children('.animate').stop(true, true).fadeOut();
	});
});


/* Misc functions */
function basename (path, suffix) {
	// Returns the filename component of the path  
	// 
	// version: 1004.2314
	// discuss at: http://phpjs.org/functions/basename
	// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   improved by: Ash Searle (http://hexmen.com/blog/)
	// +   improved by: Lincoln Ramsay
	// +   improved by: djmix
	// *     example 1: basename('/www/site/home.htm', '.htm');
	// *     returns 1: 'home'
	// *     example 2: basename('ecra.php?p=1');
	// *     returns 2: 'ecra.php?p=1'
	var b = path.replace(/^.*[\/\\]/g, '');
	
	if (typeof(suffix) == 'string' && b.substr(b.length-suffix.length) == suffix) {
		b = b.substr(0, b.length-suffix.length);
	}
	
	return b;
}
