/**
 * Manages flash movies inside the page. Can convert links to flash files or youtube to players,
 * handles cross-browser issues, and provides an update procedure to the flash installation.
 * @constructor
 */
function FlashPlayer(element, width, height, version) {
	this._element = $(element);
	this._registerId();
	
	// Defaults to youtube dimensions.
	this._width = width || 425;
	this._height = height || 344;
	this._version = version || '8';
	
	if (this._isEmbeddedPlayer()) {
		this._registerFlashPlayer();
	} else {
		this._replaceFlashLink();
	}
}

FlashPlayer.prototype = {
	/**
	 * The HTML element containing the flash player
	 * @private
	 */
	_element: null,
	
	/**
	 * The id of the instance of the flash player. Prevents duplicate id's when
	 * more than one player is on the page.
	 * @private
	 */
	_instanceId: null,
	
	/**
	 * The width of the player.
	 * @private
	 */
	_width: null,
	
	/**
	 * The height of the player.
	 * @private
	 */
	_height: null,

	/**
	 * Should the module show a title or a link?
	 * @type {Boolean}
	 * @return False always, could be implemented as: True if the movie is a link to a movie page (i.e. youtube), false otherwise.
	 * @private
	 */
	_showLink: function () {
		return false;
		//return !this._isEmbeddedPlayer() && this._element.attr('href').indexOf('youtube.com') > -1;
	},

	/**
	 * Gets the URL of the movie to be displayed.
	 * @type {String}
	 * @return The url to the Flash movie.
	 * @private
	 */
	_getMovieUrl: function () {
		var url = this._element.attr('href');
		
		if (url.indexOf('youtube.com') > -1) {
			var youtubeRegex = /(?:^|.*(?:video_id=|\/v\/|\\?v=))([A-Za-z0-9\\-_]{11})(?:$|[^A-Za-z0-9\\-_]+)/i;
            var matches = url.match(youtubeRegex);
            
            if (matches == null || matches[1] == null) {
            	return url;
        	}
        	
        	return "http://www.youtube.com/v/" + matches[1];
		}
		
		return url;
	},
	
	/**
	 * Gets a link to the movie to be displayed. Useful for directing
	 * users to a video page (i.e. Youtube).
	 * @type {String}
	 * @return The url to the Flash movie.
	 * @private
	 */
	_getMovieLink: function () {
		var url = this._element.attr('href');
		
		if (url.indexOf('youtube.com') > -1) {
			var youtubeRegex = /(?:^|.*(?:video_id=|\/v\/|\\?v=))([A-Za-z0-9\\-_]{11})(?:$|[^A-Za-z0-9\\-_]+)/i;
	        var matches = url.match(youtubeRegex);
	        
	        if (matches == null || matches[1] == null) {
	        	return url;
	    	}
	    	
	    	return "http://www.youtube.com/watch?v=" + matches[1];
		}
		
		return url;
	},
	
	/**
	 * Registers the id of the player on the object if no id is present.
	 * @private
	 */
	_registerId: function () {
		this._instanceId = FlashPlayer._instanceId++;
		if (!this._element.attr('id') || this._element.attr('id') == '') {
			this._element.attr('id', 'flashPlayer' + this._instanceId);
		}
	},
	
	/**
	 * Checks if the element passed is an embedded player or a link.
	 * @return True if the element is an object, false otherwise.
	 * @type {Boolean}
	 * @private
	 */
	_isEmbeddedPlayer: function () {
		return this._element.is("object");
	},
	
	/**
	 * Registers the flash movie with the swfobject library.
	 * @private
	 */
	_registerFlashPlayer: function () {
		/*
		Crahses IE6 (oh joy!) See #148.
		Works without this line as well, is this really necessary?
		Maybe for graceful degradation or providing automated update?
		*/
		swfobject.registerObject(this._element.attr('id'), this._version);

		$('.flash-link').each(new Delegate(this, function (index, element) {
			if ($(element).parents('#' + this._element.attr('id')).length > 0) {
				this._element.after('<span class="flash-title">' + $(element).text() + '</span>');
			}
		}));
	},
	
	/**
	 * Replaces a link to the flash movie with a player displaying that movie.
	 * Also shows the link text beneath the player.
	 * @private
	 */
	_replaceFlashLink: function() {
		// Use text to make sure we only get the title stripped of HTML.
		// Don't show any link at all, we don't want to redirect users to youtube. Conversion is king.
		/*
		if (this._showLink()) {
			 this._element.after('<a href="' + this._getMovieLink() + '" class="l1 flash-link flash-title">' + this._element.text() + '</a>');
		} else {
			this._element.after('<span class="flash-title">' + this._element.text() + '</span>');
		}
		*/
		
		swfobject.embedSWF(this._getMovieUrl(), this._element.attr('id'), this._width, this._height, '8', '#fff');
	}
};

/**
 * Static member to track how many instances are active.
 * @member FlashPlayer
 * @private
 */
FlashPlayer._instanceId = 0;
