//<![CDATA[

/* Version 2.0.0
 * Copyright (c) 2009 True Partners, LLC (wearetrue.com)
 *
 * 18 jun 2009 14:12
 * 
 * requires True core v2.0
 * cannot use $().html
 * uses jQuery json plugin
 */
 
if(window.True.SWF == undefined)
{
	if(!window.True){
		Type.registerNamespace('True');
	}
	
	True.SWF = function(src, id, c){
		this.id = id;
		this.src = src;
		c = c || {};
		this.params = c.params || {};
		this.variables = c.variables || {};
		this.attributes = c.attributes || {};
		this.params.quality = this.params.quality || "high";
		this.attributes.width = this.attributes.width || "100%";
		this.attributes.height = this.attributes.height || "100%";
		True.SWF[this.id] = this;
	}
     
	True.SWF.prototype = {
		setVariable: function(name, value){
			if(!this.getMovie()){
				this.variables[name] = value;
			}
			else{
				this.getMovie().SetVariable(name, value);
			}
		},
		getVariablePairs: function(){
			var vps = [];
			for(var key in this.variables){
				vps.push(key + "=" + this.variables[key]);
			}
			return vps;
		},
		getSWFHTML: function() {
			var swfNode = "";
			var pairs = this.getVariablePairs().join("&");
			if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length) { // netscape plugin architecture
				swfNode = '<embed type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" src="' + this.src + '" width="' + this.attributes.width + '" height="' + this.attributes.height + '"';
				swfNode += ' id="' + this.id + '" name="' + this.id + '" ';
				for(var key in this.params){
					swfNode += key + '="' + this.params[key] + '" ';
				}
				if (pairs.length > 0) swfNode += 'flashvars="'+ pairs + '"';
				swfNode += '/>';
			} else { // PC IE
				swfNode = '<object id="'+ this.id + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="' + this.attributes.width + '" height="' + this.attributes.height + '">';
				swfNode += '<param name="movie" value="'+ this.src + '" />';
				for(var key in this.params){
					swfNode += '<param name="' + key + '" value="' + this.params[key] + '" />';
				}
				if(pairs.length > 0) swfNode += '<param name="flashvars" value="' + pairs + '" />';
				swfNode += '</object>';
			}
			return swfNode;  
		},
		create: function(target, fsCommand){
			var t = (typeof target == 'string') ? $("#" + target).get(0) : target;
			var fs = "";
			if(fsCommand){
				this.params.allowScriptAccess = "always";
				fs = this.generateFsCommand();
			}
			t.innerHTML = this.getSWFHTML() + fs;
			return true;
		},
		getMovie : function(){
			if(!this.movie) this.movie = document.getElementById(this.id);
			return this.movie;
		},
		setJSON : function(data){
			if((data instanceof Object)) this.setVariable("JSONData", $.toJSON(data));
		},
		_onFSCommand : function(command, args){
			if(command == "JSON") args = $.parseJSON(args);
			if(this[command]) this[command](args);
			if(this.onFSCommand) this.onFSCommand(command, args);
			this.dispatchEvent("fsCommand", {command: command, args: args});
		},
		generateFsCommand: function(){
			var js = '<script type="text/javascript">function ' + this.id + '_DoFSCommand(command, args) {True.SWF.' + this.id + '._onFSCommand(command, args);}</script>';
			$("head").append(js);
			// Win.IE
			if ($.browser.msie && navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
				return '<script event=FSCommand(command,args) for=' + this.id + '>True.SWF.' + this.id + '._onFSCommand(command, args);</script>';
			}
			return "";
		}
	};
	True.EventDispatcher.decorate(True.SWF.prototype);
	
	True.SWF.register('True.SWF');
}
//]]>
