(function($){
	$(function(){
		/* custom player */
		$('a.flv').each(function(){
			$(this).flowplayer("/wp-content/plugins/wm_flv/flowplayer/flowplayer-3.1.5.swf");
		});
		$('a.flv').click(function(){
			$(this).css({
				'display':'block',
				'width':'100%',
				'height':'330px'
			}); 
			// Todo: check why we need to call flowplayer. We have no time at the moment...
			$(this).flowplayer("/wp-content/plugins/wm_flv/flowplayer/flowplayer-3.1.5.swf");
			return false;
		});
		/* remote sources */
		$('a.wm_flv').click(function(){return false;});
		$('a.wm_flv').each(function(){
			$(this).css({
				'display':'block',
				'width':'100%'
			}); 
			$w = $(this).width();
			$h = $w * 3/4;
			var vars = {};
			/* wich origin? */
			$is_sapo = $(this).attr('href').match(/videos.sapo.pt/);
			$is_youtube = $(this).attr('href').match(/youtube.com/);
			/* videos sapo! */
			if( $is_sapo ){
				/* parse querystring */
				var $v = $(this).attr('href').replace('http://videos.sapo.pt/', '');
				$html = '<embed src="http://rd3.videos.sapo.pt/play?file=http://rd3.videos.sapo.pt/' + $v + '/mov/1" type="application/x-shockwave-flash" allowFullScreen="true" width="' + $w + '" height="' + $h + '"></embed>';
			}
			/* youtube! */
			if( $is_youtube ){
				/* parse querystring */
				var parts = $(this).attr('href').replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
					vars[key] = value;
				});
				$v = vars['v'];
				$html = '<object width="' + $w + '" height="' + $h + '"><param name="movie" value="http://www.youtube.com/v/' + $v + '&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/' + $v + '&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="' + $w + '" height="' + $h + '"></embed></object>';
			}
			$(this).html($html);
		});
	});
})(jQuery);
