jQuery.fn.videoPlayer = function(height, width, flashPath, videoPath, 
									imagePath, hd, sd, 
									playURL, artistSongDescription, 
									initialSplash, watchURL, generoID) {
	return this.each(function() {
		if(watchURL)
		{
				  var components = watchURL.split('/');
				  var vsID = components.pop();
		}
		else
		{
			var vsID = 1;
		}
		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 = false;
		}

		var playlist;
		var hdPlugin;
		var embedTop;

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

					if(!definition || definition == 'standard')
					{
						definition = 'high';
						$f().getPlugin('hd').css({background: '#343434 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: '#343434 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: '#009ab2 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: '#343434 url(' + imagePath + def + '.png) top left no-repeat'});
					//$f().getPlugin('hd').css({backgroundColor: '#343434'});
				},
				border: 'none',
				backgroundGradient: [0, 0],
				opacity: 1,
				background: '#343434 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
		//$key = '#$1f4ce3191ee71f02b6d';  //for *.genero.tv
		//$key = '#$620c777b2780ce1fd39';  // new version (3.2.6)
		$key = '#$620c777b2780ce1fd39';
		$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%';

		$(this).flowplayer({src: flashPath + "flowplayer.commercial-3.2.6.swf", wmode: 'transparent'}, {
			//key: '#$0f4ce3191ee71f02b6d',	  //for *.genero.tv
			key: '#$620c777b2780ce1fd39',     //new version 3.2.6
			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,
					  autoHide: { fullscreenOnly: 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',
					  background: '#121212',
					  tooltipTextColor: '#ffffff',
					  height: 30,
					  opacity: 0.8,
					  width: '94%',
					  bottom: '22px',
					  left: '50%'
				},
				embed: {
					url: 'flowplayer.content-3.2.0.swf',
					onClick: function() {
						var embedConfig = {
							href: watchURL, 
							text: artistSongDescription, 
							generoID: generoID,
							contentPluginURL: flashPath + 'flowplayer.content-3.2.0.swf'};
						$('#embed').html('<p>Copy and paste this code to embed this video into your website:</p>' 
							+ '<div style="overflow: hidden; width: 562px;">' + 
			'&lt;iframe width="608" height="380" frameBorder="0" allowtransparency="true" src="http://genero.tv/embed.php?vid=' + vsID + '"&gt;&lt;/iframe&gt;'
					//$f().embed(embedConfig).getEmbedCode() - does not work for some blog sites
						/*
'&lt;object width="608" height="342" data="http://genero.tv/core/flash/public/flowplayer.commercial-3.2.6.swf?0.879228216009167" type="application/x-shockwave-flash"&gt;' + 
                            '&lt;param name="movie" value="http://genero.tv/core/flash/public/flowplayer.commercial-3.2.6.swf?0.879228216009167" /&gt;' + 
                            '&lt;param name="allowfullscreen" value="true" /&gt;&lt;param name="allowscriptaccess" value="always" /&gt;' + 
                            '&lt;param name="href" value="' + watchURL + '" /&gt;' + 
                            '&lt;param name="text" value="' + artistSongDescription + '" /&gt;' + 
                            '&lt;param name="generoID" value="' + generoID + '" /&gt;' + 
                            '&lt;param name="contentPluginURL" value="http://genero.tv/core/flash/public/flowplayer.content-3.2.0.swf" /&gt;' + 
                            '&lt;param name="flashvars" value="config=' + 
                            '{\'key\':\'' + $key + '\',\'playlist\':[{' + 
                            '\'url\':\'' + videoPath + sd + '\',\'autoPlay\':false,\'scaling\':\'fit\'}],\'clip\':{\'autoPlay\':false,\'scaling\':\'fit\',\'url\':\'#\'},\'canvas\':{\'backgroundGradient\':\'none\',\'backgroundColor\':\'#000000\'},\'play\':{\'opacity\':0},\'logo\':{\'opacity\':0.5},\'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\':\'94pct\',\'bottom\':\'22px\',\'left\':\'50pct\'},\'titleLink\':{\'url\':\'http://genero.tv/core/flash/public/flowplayer.content-3.2.0.swf\',\'html\':\'&lt;a class=\\\'generoLink\\\' href=\\\'' + watchURL + '\\\'&gt;&lt;span class=\\\'artist\\\'&gt;' + artistSongDescription + '&lt;/span&gt; by &lt;span class=\\\'member\\\'&gt;' + generoID  + '&lt;/span&gt; &lt;/a&gt;\',\'border\':\'none\',\'autoHide\':\'always\',\'backgroundGradient\':[0,0],\'borderRadius\':0,\'backgroundColor\':\'transparent\',\'width\':\'240px\',\'height\':\'45px\',\'left\':\'7px\',\'top\':\'7px\',\'style\':{\'.artist\':{\'fontSize\':\'12px\',\'fontWeight\':\'bold\',\'fontFamily\':\'Tahoma\'},\'.generoID\':{\'fontSize\':\'15px\',\'fontWeight\':\'bold\'},\'a:hover\':{\'color\':\'#60e9ff\'}}}}}" /&gt;&lt;/object&gt;'
*/

							+ '</div>');
						$('#embed').dialog('open');
					},
					onMouseOver: function() {
						$f().getPlugin('embed').css({background: '#009ab2 url(' + imagePath + 'embed_hover.png) top left no-repeat'});
						//$f().getPlugin('embed').css({backgroundColor: '#009ab2'});
					},
					onMouseOut: function() {
						$f().getPlugin('embed').css({background: '#343434 url(' + imagePath + 'embed.png) top left no-repeat'});
						//$f().getPlugin('embed').css({backgroundColor: '#343434'});
					},
					border: 'none',
					opacity: 1,
					backgroundGradient: [0, 0],
					background: '#343434 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) {}
			},

			// buffer stats
			onBufferEmpty: function() {
				try
				{
					$.get(getBaseURL() + 'videoPlayer.txt?buffer=empty&url=' + playURL);
					if(pageTracker)
					{
						pageTracker._trackEvent('Debug', 'bufferEmpty', playURL);
					}
				}
				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();
			}
		}
	}
};

