/*******************************************************************************
* SWF object from link                                                        **
*******************************************************************************/

/**
 * SWFObjectFromLink. Find links to swf objects and convert them to 
 * those objects
*/

openc.SWFObjectFromLink = function(link) {

	var anchor = jq(link);
	var href = anchor.attr('href');
	this.href = href;
	
	/**
	 * Store a reference to the filename, contiguous non slash characters at end
	*/
	this.file = this.href.match(/[^\/]+$/);
	
	
	/**
	 * Establish the base, this involves quite a few steps
	*/
	// The path to file is everything up to the last slash if href contains a 
	// slash, or an empty string
	var pathToFile = this.href.match(/\//) ? this.href.replace(/(.*)\/[^\/]+$/, '$1') : '';
	
	// The path to file is correct from the current page, its start point
	var pathToStartPoint = openc.PATH_TO_PAGE;
	
	// If the  contains any parent names (..) strip these and the corresponding
	// part of the pathToStartPoint
	
	while(pathToFile.indexOf('../') == 0) {
		pathToFile = pathToFile.substr(3);
		pathToStartPoint = pathToStartPoint.replace(/(.*)\/[^\/]+$/,'$1');
	}
	
	// The base of this movie is the path to this page and the path to the file
	this.base = pathToStartPoint + '/' + pathToFile;
	
	/**
	 * Absolute path
	*/
	this.absolutePath = this.base + '/' + this.file;
	
	
	
	/**
	 * Metadata
	*/
	
	// retreive metadata (prepended manually or the server) from the filename:
	// [path]v[version]w[width]h[height]-[filename].[objectType]
	var metadataPattern = new RegExp(".*(v\\d+w\\d+h\\d+)-[^\\/]+\\." + this.objectType + "$");
	var metadata = this.href.replace(metadataPattern,'$1');
	
	//openc.log(metadata);
	
	
	// If movieInfo is the same as href
	if(metadata == this.href) {
		// then no info was found, do nothing
		return
	}
	
	// Extract metadata from string, no need to validate - the regex passed earlier
	var majorVersion = parseInt(metadata.replace(/^v(\d).+/,'$1'));
	var width = parseInt(metadata.replace(/.+w(\d+).+/,'$1'));
	var height = parseInt(metadata.replace(/.+h(\d+)/,'$1'));
	
	// If the right version of Flash is not available
	if(!this.hasPlayerVersion(majorVersion)) {
	 	return;
	 }
	 
	// Hide link
	anchor.css('visibility','hidden');
	
	// generate and set a random id for the anchor
	var randomId = 'ObjFromLink' + Math.round(Math.random()*999999);
	anchor.attr('id', randomId);
	
	// use swfobject to replace alternate content
	swfobject.embedSWF(
		this.getObjectURL(),
		randomId,
		width,
		height + this.paddingBottom,
		majorVersion + '.0.0',
		{},
		this.getParams(),
		{
			wmode:'transparent'
			// base is necessary for nested flash movieclips
			,base:this.base
		}
	);
	
	// Dispatch event proclaiming the new object
	openc.eventMediator.dispatchEvent("afterObjectFromLink", {href: this.href});
}

/*
/home/zope/plone/basket
*/


/**
 * Class methods and propeties
*/

// objectType
openc.SWFObjectFromLink.prototype.objectType = "swf";
openc.SWFObjectFromLink.prototype.getParams = function() {
	return {};
}

// Check player available
openc.SWFObjectFromLink.prototype.hasPlayerVersion = function(majorVersion) {
	return swfobject.hasFlashPlayerVersion(majorVersion + '.0.0');
}

// paddingBottom
openc.SWFObjectFromLink.prototype.paddingBottom = 0;

// getObjectURL
openc.SWFObjectFromLink.prototype.getObjectURL = function() {

	// if this points to a resource
	if(this.href.match(/__resource__|\+\+resource\+\+/)) {
		// return the raw href
		return this.absolutePath;
	}
	// Otherwise add '/download' to reference the binary data
	return this.absolutePath + '/download';
}





/*******************************************************************************
* FLV object from link                                                        **
*******************************************************************************/

openc.FLVObjectFromLink = function(link) {
	// Call superclass
	openc.SWFObjectFromLink.call(this, link);
}
// Extend superclass
openc.FLVObjectFromLink.extend(openc.SWFObjectFromLink);



/**
 * Class methods
*/


// objectType
openc.FLVObjectFromLink.prototype.objectType = "flv";

// getParams
openc.FLVObjectFromLink.prototype.getParams = function() {
	return {
		borderColour:'0x999999',
		autoload:false,
		// This resource URL will work from any depth
		skin: '__resource__openc.vanillatheme.swf/SteelExternalPlaySeekMute.swf'
		,contentPath: this.absolutePath
	}
}

// paddingBottom
openc.FLVObjectFromLink.prototype.paddingBottom = 50;


// Check player available - specialised as player relevant to prxy and media
openc.FLVObjectFromLink.prototype.hasPlayerVersion = function(majorVersion) {
	
	var proxyVersion = 8;
	return swfobject.hasFlashPlayerVersion(Math.max(majorVersion, proxyVersion) + '.0.0');
}



// getObjectURL
openc.FLVObjectFromLink.prototype.getObjectURL = function() {
	// Always rteturn the proxy, the URL of the file is passed as a param
	return '__resource__openc.vanillatheme.swf/flvp8.swf';
}

/**
 * Search for anchors pointing to SWF or FLV files and replace them with the 
 * linked to object using swfobject. TODO: QuickTime support.
*/

openc.createObjectsFromAnchors = function() {

	// Links to SWF files
	jq('a[href$=.swf]').each(function(){
		new openc.SWFObjectFromLink(this);
	})

	// Links to FLV files
	jq('a[href$=.flv]').each(function(){
		new openc.FLVObjectFromLink(this);
	})
}

// DOM loaded
jq(function() {

	if(openc.IS_RIA_CLIENT && !openc.IS_EDIT_PAGE && openc.IS_DESKTOP) {
	
		openc.createObjectsFromAnchors();
	}
});

