(function($) {
	$.expr[':'].relToVideo = function(elem, index, match) {
		//return $(elem).is('a') && elem.rel && (/^(rtmp|rtmps)/).test(elem.rel);
		return $(elem).is('a') && elem.rel && (/\.flv$/).test(elem.rel);
	}
	
	$.fn.videoPlayer = function(options) {
		var defaultSettings = {
			showControls:			true,
			videoURL:				"",
			volume:					100,
			width:					"100%",
			height:					"100%",
			autoPlay:				true
		};
		
		settings = $.extend({}, defaultSettings, options);
				
		if(settings.videoURL) {
			this.flash({
				swf: 		"http://www.allmusic.nl/video/swf/player.swf", 
				width:		settings.width,
				height:		settings.height,
				wmode:		"transparent",
				allowScriptAccess: 'always',
				params: {
					wmode:		"transparent",
					allowScriptAccess: 'always'
				},
				flashvars:	{
					videoURL:	settings.videoURL
				,	showControls: settings.showControls ? "true" : "false"
				,	volume: settings.volume
				,	autoPlay: settings.autoPlay ? "true" : "false"
				}
			});
		} else {
			alert('jQuery.videoPlayer error: videoURL not specified');
		}
		
		return this;
	}
		
	$.fn.videoPreview = function(options) {
		var defaultSettings = {
			css:				{
				width:				"400px",
				height:				"300px",
				position:			"absolute"
			},
			volume:					100,
			distanceFromCursor: 	{left: 10, top: 10},
			showControls:			false,
			onShow: 				function(){},
			onHide:					function(){},
			containerID:			'videoPreviewContainer'
		};
		
		// defaultSettings.css = $.extend({}, defaultSettings.css, options.css);
		settings = $.extend({}, defaultSettings, options);
		
		$container = $('<div/>')
			.attr('id',settings.containerID)
			.css(settings.css)
			.css({position:"absolute", top: "100px", left: "100px",zIndex: "100"})
			.hide()
			.prependTo('body');
		
		//$flashContainer = $container;
		$flashContainer = $('<div />').css({width: "100%", height: "100%"}).appendTo($container);
		//$flashContainer = $container.append('<div/');
		
		this.filter(':relToVideo')
			.mousemove(function(e){
				$container.css({
					top: e.pageY + settings.distanceFromCursor.top + 'px'
				,	left: e.pageX + settings.distanceFromCursor.left + 'px'
				});
			})
			.hover(function(){
				var link = this;
				$container.show();
				
				$flashContainer.flash({
					swf: 		"http://www.allmusic.nl/video/swf/player.swf", 
					width:		"100%",
					height:		"100%",
					wmode:		"transparent",
					allowScriptAccess: 'always',
					params: {
						wmode:		"transparent",
						allowScriptAccess: 'always'
					},
					flashvars:	{
						volume:		settings.volume
					,	videoURL:	link.rel
					,	showControls: settings.showControls ? "true" : "false"
					}
				});
			
				settings.onShow.call($container[0], link);
			}, function() {
				var link = this;
				
				$container.hide();
				$flashContainer.html('');
				settings.onHide($container[0], link);
			});
		
		return this;
	}
})(jQuery);
