<?php if (!defined('PmWiki')) exit ();

/*
	copyright 2009 Adam Overton
	Built on code from swf.php copyright 2004 Patrick R. Michaud
	and from quicktime.php copyright 2006 Sebastian Siedentopf
	and from teachertube.php copyright 2007 Anno.
	This file is distributed under the terms of the GNU General Public 
	License as published by the Free Software Foundation; either 
	version 2 of the License, or (at your option) any later version.  
	
	This module enables embedding of Audioo.com movies into
	wiki pages. simply use (:audioo http://fullURL:)
	So for example if the URL to share a video is:
	
	http://audioo.com/t/for-blinky-palermo--dog-star-orchestra/4d4e3e080544ffd/
	
	then you'd write (:audioo http://audioo.com/t/for-blinky-palermo--dog-star-orchestra/4d4e3e080544ffd/:).
	
	optional parameters include 'autostart' (default: false) and 'volume' (default: 80), which can be appended:
		(:audioo http://audioo.com/t/for-blinky-palermo--dog-star-orchestra/4d4e3e080544ffd/ autostart=true:).

	* v.2009-12-20  now includes an ROEPattern that will convert any pasted audioo.com embed code into valid pmwiki audioo markup
	
*/

# Version date
$RecipeInfo['Audioo']['Version'] = '2009-12-20';

Markup('audioo', '<img', "/\\(:audioo\\s+([^\\s]*)\\s*(.*)\\s*:\\)/e", "ShowAudiooVid('$1','$2')");
SDVA($AudiooDefaultParams, array(
	'volume' => '80'
	,'autostart' => 'false'
));
SDV($AudiooROEenabled, true);

# ROEPatterns - AUDIOO EMBED CONVERSION 
# Converts pasted Audioo embed code into valid pmwiki (:audioo:) code
if ($AudiooROEenabled) {
	$ROEPatterns['!<object.*audioo\.com.*<\/object>!ie'] = "AudiooROE(PSS('$0'))";
	SDV($AudiooROEFmt, '(:audioo $1 autostart=false:)');
	function AudiooROE($videocode) {
		global $AudiooROEFmt;
		
		# get url
		preg_match('#^<object.*&URL=([^"]+)">.*$#', $videocode, $matches);
		$audiooUrl = $matches[1];
	
		# append extra parameters after the videocode
		$AudiooROEFmt_local = str_replace('$1', $audiooUrl, $AudiooROEFmt);
		# must use _local version, or else multiple conversions on the same page keeping adding params to the ROEFmt array		
		$out = $AudiooROEFmt_local;
		return $out;
	}
}


function ShowAudiooVid($fullUrl, $args) {
	global $AudiooDefaultParams;

	# need to feed the code both the full url, plus the numeric code at the end of the url:
	# -> "http://audioo.com/t/for-blinky-palermo--dog-star-orchestra/4d4e3e080544ffd/"
	# -> and "4d4e3e080544ffd"
	
	# extract the numerical code
	if(!preg_match('#http://audioo.com/t/[^/]+/([^/]+)/?#',$fullUrl,$matches)) return "%error%error: audioo.com input invalid%%";
	$urlCode = $matches[1];
	
	# parameters
	$args = array_merge($AudiooDefaultParams, ParseArgs($args));
	$vol = $args['volume'];
	$autostart = $args['autostart'];
	
	# output	
	$out = "<object width='411' height='176'>";
	$out .= "\n   <param name='movie' value='http://audioo.com/swf/a/PlayerLoader.swf'></param>";
	$out .= "\n   <param name='wmode' value='window'></param>";
	$out .= "\n   <param name='allowScriptAccess' value='always'></param>";
	$out .= "\n   <param name='flashvars' value='autostart=".$autostart."&contentType=TRACK&contentURL=http://audioo.com/swf/a/TrackWidget.swf&cakeGatewayURL=http://audioo.com/cake_gateway.php&contentID=".$urlCode."&volume=".$vol."&enablejs=false&URL=".$fullUrl."'></param>";
	$out .= "\n<embed src='http://audioo.com/swf/a/PlayerLoader.swf' type='application/x-shockwave-flash' wmode='transparent' allowScriptAccess='always' flashvars='autostart=".$autostart."&contentType=TRACK&contentURL=http://audioo.com/swf/a/TrackWidget.swf&cakeGatewayURL=http://audioo.com/cake_gateway.php&contentID=".$urlCode."&volume=".$vol."&enablejs=false&URL=".$fullUrl."' width='411' height='176'>\n</embed></object>";

	return Keep($out);
}