jQuery.fn.videoPlayer = function(height, width, flashPath, videoPath, 
									imagePath, hd, sd, 
									playURL, artistSongDescription, 
									initialSplash, watchURL, generoID) {
	return this.each(function() {
		definition = 'standard';

		// Set up the embed dialog
		$("#embed").remove();
		$(this).after('<div id="embed"></div>');
		$('#embed').dialog({
			bgiframe: true,
			autoOpen: false,
			height: 400,
			dialogClass: 'generoDialog embedDialog',
			title: '<h2>Embed in your website</h2>',
			width: 600,
			modal: true,
			open: function() {
				if($f().isPlaying())
				{
					$f().pause();
				}
				replacePageTitles();
			},
			close: function() {
				if($f().isPaused())
				{
					$f().resume();
				}
				showControls();
			}
		});

		if(!initialSplash)
		{
			initialAutoPlay = false;
		}
		else
		{
			initialAutoPlay = true;
		}

		var playlist;
		var hdPlugin;
		var embedTop;

		if(hd == '')
		{
			hdPlugin = null;
			embedTop = '22px';
		}
		else
		{
			embedTop = '72px';
			hdPlugin = 
			{
				url: flashPath + 'flowplayer.content-3.1.0.swf',
				onClick: function() {
					$f().stop();
					$f().getPlugin('hd').css({background: 'none'});

					if(!definition || definition == 'standard')
					{
						definition = 'high';
						$f().getPlugin('hd').css({background: 'url(' + imagePath + 'hd.png) top left no-repeat'});
						$f().setClip({url: videoPath + hd, autoPlay: false, autoBuffering: false});
					}
					else
					{
						definition = 'standard';
						$f().getPlugin('hd').css({background: 'url(' + imagePath + 'sd.png) top left no-repeat'});
						$f().setClip({url: videoPath + sd, autoPlay: false, autoBuffering: false});
					}
					showControls();
				},
				onMouseOver: function() {
					var def;

					if(!definition || definition == 'standard')
					{
						def = 'sd';
					}
					else
					{
						def = 'hd';
					}
					$f().getPlugin('hd').css({background: 'url(' + imagePath + def + '_hover.png) top left no-repeat'});
					$f().getPlugin('hd').css({backgroundColor: '#009ab2'});
				},
				onMouseOut: function() {
					var def;

					if(!definition || definition == 'standard')
					{
						def = 'sd';
					}
					else
					{
						def = 'hd';
					}
					$f().getPlugin('hd').css({background: 'url(' + imagePath + def + '.png) top left no-repeat'});
					$f().getPlugin('hd').css({backgroundColor: '#343434'});
				},
				border: 'none',
				backgroundGradient: [0, 0],
				opacity: 1,
				background: 'url(' + imagePath + 'sd.png) top left no-repeat',
				width: '42px',
				height: '42px',
				top: '22px',
				right: '20px'
			}
		}

		//prevent the image dragging in firefox, breaks safari though
		$(this).mousedown(function(event) {
			if(!$.browser["safari"])
			{
				event.preventDefault();
			}
		});

		// Set up the video player
		$(this).flowplayer({src: flashPath + "flowplayer.commercial-3.1.3.swf", wmode: 'transparent'}, {
			key: '#$1f4ce3191ee71f02b6d',	  //for *.genero.tv
			//key: '$1f4ce3191ee71f02b6d',    //for genero.tv
			playlist: [{url: videoPath + sd}],
			clip: {
				autoPlay: initialAutoPlay,
				scaling: 'fit'
			},
			canvas: {
				backgroundGradient: 'none',
				backgroundColor: '#000000'
			},
			play: {opacity: 0},
			logo: {opacity: 0},
			plugins: {
			   controls: {
					  play: true,
					  volume: true,
					  fullscreen: true,
					  mute: true,
					  bufferGradient: 'none',
					  backgroundGradient: 'none',
					  timeColor: '#009baf',
					  tooltipColor: '#5F747C',
					  sliderColor: '#125459',
					  progressGradient: 'medium',
					  sliderGradient: 'none',
					  progressColor: '#009baf',
					  volumeSliderGradient: 'none',
					  volumeSliderColor: '#d0d7d7',
					  timeBgColor: 'transparent',
					  buttonBgColor: 'transparent',
					  bufferColor: '#d0d7d7',
					  buttonOverColor: '#009baf',
					  durationColor: '#ffffff',
					  buttonColor: '#555555',
					  borderRadius: 15,
					  backgroundColor: '#121212',
					  tooltipTextColor: '#ffffff',
					  height: 30,
					  opacity: 0.8,
					  width: '94%',
					  bottom: '22px',
					  left: '50%'
				},
				embed: {
					url: 'flowplayer.content-3.1.0.swf',
					onClick: function() {
						var embedConfig = {
							href: watchURL, 
							text: artistSongDescription, 
							generoID: generoID,
							contentPluginURL: flashPath + 'flowplayer.content-3.1.0.swf'};
						$('#embed').html('<p>Copy and paste this code to embed this video into your website:</p>' 
							+ '<div style="overflow: hidden; width: 562px;">' + $f().embed(embedConfig).getEmbedCode() + '</div>');
						$('#embed').dialog('open');
					},
					onMouseOver: function() {
						$f().getPlugin('embed').css({background: 'url(' + imagePath + 'embed_hover.png) top left no-repeat'});
						$f().getPlugin('embed').css({backgroundColor: '#009ab2'});
					},
					onMouseOut: function() {
						$f().getPlugin('embed').css({background: 'url(' + imagePath + 'embed.png) top left no-repeat'});
						$f().getPlugin('embed').css({backgroundColor: '#343434'});
					},
					border: 'none',
					opacity: 1,
					backgroundGradient: [0, 0],
					background: 'url(' + imagePath + 'embed.png) top left no-repeat',
					width: '42px',
					height: '42px',
					top: embedTop,
					right: '20px'
				},
				hd: hdPlugin
			},
			onFullscreen: function() {
				$f().getPlugin('embed').hide();
				if(hd != '')
				{
					$f().getPlugin('hd').hide();
				}
			},
			onFullscreenExit: showControls,
			onMouseOver: showControls,
			onMouseOut: hideControls,
			onBegin: function() {
				if(playURL)
				{
					playURL += "&definition=" + definition;
					$.getJSON(playURL);
				}

				try
				{
					if(artistSongDescription && pageTracker)
					{
						pageTracker._trackEvent('Video', 'Play', artistSongDescription);
					}
				}
				catch(e) {}
			}		
		});

		showControls();
	});


	function hideControls()
	{
		if(!$f().isFullscreen() && $f().isPlaying())
		{
			$f().getPlugin('embed').hide();
			$f().getPlugin('controls').hide();

			if(hd != '')
			{
				$f().getPlugin('hd').hide();
			}
		}
	}


	function showControls()
	{
		if(!$f().isFullscreen() && $f().isPlaying())
		{
			$f().getPlugin('embed').show();
			$f().getPlugin('controls').show();

			if(hd != '')
			{
				$f().getPlugin('hd').show();
			}
		}
	}
};
