﻿/**
 * @class SWF
 * @author Adam Grant
 * @created July 28, 2007
 * @copyright (C) 2007 Global Media Services, All rights reserved.
 **/
function SWF(initObject) {
	var CODEBASE = "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=";
	var PLUGINSPAGE = "http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash";
	
	this.majorVersion = SWF.majorVersion;
	this.minorVersion = SWF.minorVersion;
	this.revisionVersion = SWF.revisionVersion;
	this.classid = "clsid:d27cdb6e-ae6d-11cf-96b8-444553540000";
	this.type = "application/x-shockwave-flash";
	this.width = 550;
	this.height = 400;
	this.quality = "high";
	
	for (var p in initObject) {
		this[p] = initObject[p];
	}
	
	if (!this[SWF.OBJ + "movie"] && this.src) this[SWF.OBJ + "movie"] = this.src;
	if (!this[SWF.EMB + "name"] && this.id) this[SWF.EMB + "name"] = this.id;
	if (!this.codebase) this.codebase = CODEBASE + this.majorVersion + "," + this.minorVersion + "," + this.revisionVersion + ",0";
	if (!this.pluginspage) this.pluginspage = PLUGINSPAGE;
	
	// non-mutable
	this.toString = SWF.prototype.toString;
	this.embed = SWF.prototype.embed;
}

///// private static
SWF.OBJ = "obj#";
SWF.EMB = "emb#";

SWF._getParam = function(obj, name, incl, excl) {
	if (name.indexOf(excl) == 0) return "";
	if (name.indexOf(incl) == 0) name = name.substr(incl.length);
	var value = obj[incl + name] || obj[name] || "";
	if (value) {
		return ' <param name="' + name + '" value="' + value + '" />\n';
	}
	else {
		return "";
	}
};

SWF._getAttrib = function(obj, name, incl, excl) {
	if (name.indexOf(excl) == 0) return "";
	if (name.indexOf(incl) == 0) name = name.substr(incl.length);
	var value = obj[incl + name] || obj[name] || "";
	if (value) {
		return '  ' + name + '="' + value + '"\n';
	}
	else {
		return "";
	}
};

///// public static
SWF.majorVersion = 4;
SWF.minorVersion = 0;
SWF.revisionVersion = 0;

/**
 * @method embed
 * @param initObject:Object
 * Immediately writes Flash to the page
 **/
SWF.embed = function(initObject) {
	new SWF(initObject).embed();
};

///// public
/**
 * @method toString
 * @returns String
 * Returns the <object><embed></embed><object> sequence
 **/
SWF.prototype.toString = function() {
	var obj = new Array();
	var emb = new Array();
	var o = SWF.OBJ;
	var e = SWF.EMB;
	
	obj.push('<object');
	obj.push(SWF._getAttrib(this, "classid", o));
	obj.push(SWF._getAttrib(this, "codebase", o));
	obj.push(SWF._getAttrib(this, "width", o));
	obj.push(SWF._getAttrib(this, "height", o));
	obj.push(SWF._getAttrib(this, "id", o));
	obj.push(SWF._getAttrib(this, "name", o));
	obj.push(SWF._getAttrib(this, "tabindex", o));
	obj.push(SWF._getAttrib(this, "hspace", o));
	obj.push(SWF._getAttrib(this, "vspace", o));
	obj.push(SWF._getAttrib(this, "border", o));
	obj.push(SWF._getAttrib(this, "align", o));
	obj.push(SWF._getAttrib(this, "class", o));
	obj.push(SWF._getAttrib(this, "title", o));
	obj.push(SWF._getAttrib(this, "accesskey", o));
	obj.push(SWF._getAttrib(this, "noexternaldata", o));
	obj.push('>\n');
	
	emb.push(' <embed');
	emb.push(SWF._getAttrib(this, "type", e));
	emb.push(SWF._getAttrib(this, "pluginspage", e));
	emb.push(SWF._getAttrib(this, "src", e));
	emb.push(SWF._getAttrib(this, "width", e));
	emb.push(SWF._getAttrib(this, "height", e));
	emb.push(SWF._getAttrib(this, "name", e));
	emb.push(SWF._getAttrib(this, "align", e));
	emb.push(SWF._getAttrib(this, "tabindex", e));
	
	var rem = new RegExp("majorVersion|minorVersion|revisionVersion|classid|codebase|width|"
		+ "height|id|name|tabindex|hspace|vspace|border|align|class|title|accesskey|"
		+ "noexternaldata|type|pluginspage|src|width|height|name|align|toString|embed", "i");
	
	var data = new Object();
	for (var p in this) {
		if (!rem.exec(p)) data[p] = this[p];
	}
	
	for (var p in data) {
		if (this[p]) {
			obj.push(SWF._getParam(data, p, o, e));
			emb.push(SWF._getAttrib(data, p, e, o));
		}
	}
	
	emb.push(' ></embed>\n');
	obj.push(emb.join(""));
	obj.push('</object>');
	
	return obj.join("");
};

/**
 * @method embed
 * @returns void
 * Writes the <object><embed></embed></object> sequence.
 **/
SWF.prototype.embed = function() {
	document.write(this.toString());
};


/**
 * @class EmbedObject
 * @param initObj:Object; required
 * A wrapper class for the new SWF object
 **/
function EmbedObject(initObj) {
	var _swf = new SWF(initObj);
	this.toString = this.getText = this.getHtml = function() { return _swf.toString(); };
}

EmbedObject.prototype.write = function(text) {
	document.write(text || this.toString());
};
