/**
 * FoxFlash: Generic behaviour for flash movies
 * @author Josh Johnston josh@xhtmlized.com
 * @namespace
 */
var FoxFlash = function(){
	/* global document, window, SWFObject */

	var self = /** @scope FoxFlash */{
		/**
	     * Create an instance
	     * @param {DOMid} Id of the flash holder element
		 * @param {hash} options
		 * @config {path} swf_path Path to the swf file
		 * @config {path} href Where the swf links too
		 * @config {path} target Specify link target i.e. _self, _blank, etc
	     */
	    create: function(holderId, options, callbacks) {
			var instance = {
				holderId: holderId,
				movieId: holderId+"_embed",
				movie: null,
				background_mode: false,
				options: options,
				callbacks: {
					setFlashVars: callbacks.setFlashVars || self.setFlashVars
				}
			};

			return instance;
		},

		/**
		 * Embed the flash movie
		 */
		embed: function(instance) {
			// flash movie already embedded? do nothing
			if (self.getMovie(instance)) {
				return;
			}

			var so = new SWFObject(instance.options.swf_path,
								   instance.movieId,
								   instance.options.width,
								   instance.options.height,
								   instance.options.required_version,
								   instance.options.background_color);
			so.addParam('allowScriptAccess', 'always');	// (REQUIRED)
			so.addParam('wmode', 'transparent');
			so.addParam('scale', 'noscale');
			so.addParam('allowFullScreen', 'true');

			// set flash vars
			instance.callbacks.setFlashVars(so);

			so.write(instance.holderId);
		},

		/**
		 * Set flash vars for the movie, before it's embedded
		 * @param {SWFObject} so
		 */
		setFlashVars: function(so) {

		},

		/**
		 * Get a reference to the movie
		 */
		getMovie: function(instance) {
			// lazy-load the flash object reference
			if (!instance.movie) {
				// resolve the string _movieId to a Flash object reference based on browser type.
				instance.movie = (!!document[instance.movieId])
					? document[instance.movieId]
					: window[instance.movieId];
			}
			return instance.movie;
		},

		/**
		 * Put the movie into background mode
		 */
		startBackgroundMode: function(instance) {
			var movie = self.getMovie(instance);
			if (movie && movie.sendToFlash && !instance.background_mode) {
				try {
					movie.sendToFlash('start_background_mode');
					instance.background_mode = true;
				}
				catch(e) { }
			}
		},

		/**
		 * Bring the movie back out of background mode
		 */
		stopBackgroundMode: function(instance) {
			var movie = self.getMovie(instance);
			if (movie && movie.sendToFlash && instance.background_mode) {
				try {
					movie.sendToFlash('stop_background_mode');
					instance.background_mode = false;
				}
				catch(e) { }
			}
		}
	};

	return self;
}();
