PopJavaScriptFramework.require('dom','plugin');

PopJavaScriptFramework.v1B1.plugin = {
	/**
	 * @constructor Base
	 * @param {String} sId The ID to assign to the instance of the class
	 */
	Base: function(sId) {
		this.embedMethod = PopJavaScriptFramework.v1B1.client.plugin.embedMethod;
		
		// OBJECT VARIABLES
		this.params = {};
		this.attributes = {};
		
		// GETTERS/SETTERS
		this.setAttribute =	function(name, value)	{ this.attributes[name] = value;}
		this.getAttribute =	function(name)			{ return this.attributes[name];	}
		this.getAttributePair =	function(name) {
			var attributeName = (name == 'name') ? 'id' : name ; // force name and id attributes to match
			if (typeof this.getAttribute(attributeName) != 'undefined') {
				return '{0}="{1}"'.format(name, this.getAttribute(attributeName));
			} else {
				return '';
			}
		}
		this.addParam =		function(name, value)	{ this.params[name] = value;	}
		this.getParam =		function(name)			{ return this.params[name];		}
		this.getParamTag =	function(name, value)	{
			return '<param name="{0}" value="{1}" />'.format(name, this.getParam(name));
		}
		
		this.toHTML = function() {
			var pluginNode = "";
			if (this.embedMethod == 'embed') { // netscape plugin architecture
				pluginNode = '<embed {0} {1} {2} {3} {4} {5} {6} />'.format(
								this.getAttributePair('type'),this.getAttributePair('src'),
								this.getAttributePair('width'),this.getAttributePair('height'),
								this.getAttributePair('id'),this.getAttributePair('name'),
								'{0}' /*placeholder for params*/);
				var paramString = '';
				for(var key in this.params){
					paramString += '{0}="{1}"'.format(key,this.params[key]);
				}
				pluginNode = pluginNode.format(paramString);
			} else { // PC IE
				pluginNode = '<object {0} {1} {2} {3}> {4} </object>'.format(
								this.getAttributePair('id'),this.getAttributePair('classid'),
								this.getAttributePair('width'),this.getAttributePair('height'),
								'{0}' /*placeholder for params*/);
				var paramNodes = '<param name="movie" value="'+ this.getAttribute('src') +'" />';
				for(var key in this.params) {
					paramNodes += this.getParamTag(key,this.params[key]);
				}
				pluginNode = pluginNode.format(paramNodes);
			}
			// dbug.log(pluginNode);
			return pluginNode;
		}
		this.write = function(sId) {
			// escape output so it shows up on page as a string instead of actual html
			$dom.getById(sId).innerHTML = this.toHTML().escapeHTML();
		}
		
		// OBJECT INITIALIZATION
		this.setAttribute('id',sId);
		this.setAttribute('src','fake/src/path');
		this.setAttribute('type','fake/mimetype');
		this.setAttribute('classid','clsid:FAKE-CLASS-ID');
	},
	
	/**
	 * @constructor SWF
	 * @extends PopJavaScriptFramework.v1B1.plugin.Base
	 * @param {String}	sSwf		Path to the SWF file
	 * @param {String}	sId			ID for the instance of the Class
	 * @param {Integer}	iWidth		Width of your swf
	 * @param {Integer}	iHeight		Height of your swf
	 * @param {Mixed}	mVersion	Flashplayer version to require; String, Number or Array
	 * @param {String}	sColor		Background color to set for your swf
	 * @param {String}	sQuality	Quality of playback
	 */
	SWF: function(sSwf, sId, iWidth, iHeight, mVersion, sColor, sQuality) {
		// INHERIT FROM BASE
		this.superclass = PopJavaScriptFramework.v1B1.plugin.Base;
		this.superclass(sId);
		
		// OBJECT VARIABLES
		this.variables = {};
		this.wrapper;
		
		// FAILURE
		this.detect = true;
		this.failureRedirectUrl = false;
		this.bypassLink = false;
		
		this.bypassDetection = function() {
			this.detect = false;
			this.write();
		}
		
		this.setBypassLink = function(swfObject) {
			this.bypassLink = $dom.create('a',{href:'#',onclick:swfObject + '.bypassDetection(); return false;'},'Bypass Detection');
		}
		
		this.displayBypassLink = function(sId) {
			var l = $dom.create('p',this.bypassLink);
			$dom.getById(sId).appendChild(l);
		}
		
		// GETTERS/SETTERS
		this.addVariable =		function(name, value)	{ this.variables[name] = value;	}
		this.getVariable =		function(name)			{ return this.variables[name];	}
		this.getVariables =		function()				{ return this.variables;		}
		this.getVariablePairs =	function() {
			var variablePairs = new Array();
			var key;
			var variables = this.getVariables();
			for(key in variables){
				variablePairs.push(key +"="+ variables[key]);
			}
			return variablePairs;
		}
		
		this.appendVariables = function(html) {
			var vars = this.getVariablePairs();
			if (vars.length < 1) { return html; }
			if (this.embedMethod == 'embed') {
				html = html.replace("/>", 'flashvars="{0}" />'.format(vars.join('&')));
			} else {
				html = html.replace('</object>','<param name="flashvars" value="{0}" /></object>'.format(vars.join('&')));
			}
			return html;
		}
		
		/**
		 * Writes the SWF to the page
		 * @param {String} sId The id of the element that you want to write the SWF into
		 */
		this.write = function(sId) {
			this.wrapper = (typeof sId != 'undefined') ? sId : this.wrapper ;
			if(!PopJavaScriptFramework.v1B1.client.plugin.swf.test(mVersion) 
			   && this.failureRedirectUrl != false 
			   && this.detect) {
				// redirect if set
				location.href = this.failureRedirectUrl;
				return;
			} else if(!PopJavaScriptFramework.v1B1.client.plugin.swf.test(mVersion) && this.detect) {
				// display bypass link if set, then fail silently
				if (this.bypassLink !== false) {
					this.displayBypassLink(this.wrapper);
				}
				return;
			} else {
				// write the swf
				var html = this.appendVariables(this.toHTML());
				$dom.getById(this.wrapper).innerHTML = html;
				return;
			}
		}
		
		// OBJECT INITITIALIZATION
		this.setAttribute('src',sSwf);
		this.setAttribute('width',iWidth);
		this.setAttribute('height',iHeight);
		this.setAttribute('version',mVersion);
		this.setAttribute('type','application/x-shockwave-flash');
		this.setAttribute('classid','clsid:D27CDB6E-AE6D-11cf-96B8-444553540000');
		
		this.addParam('bgcolor', (sColor || '#FFFFFF'));
		this.addParam('quality', (sQuality || 'high'));
	},
	/**
	 * Placeholder object for the QT Plugin Object
	 * @constructor QT
	 * @param {String} sType The type of quicktime media, audio or video
	 * @param {String} sId The id of the media file
	 * @param {Integer} iWidth
	 * @param {Integer} iHeight
	 * @param {Mixed} mVersion The QT version to require. To check against revision, pass a string, such as "1.8.1"
	 * @param {Bool} bAutoStart
	 * @param {String} sPlaceHolder Path to the placeholder image
	 */
	QT: function(sType, sId, iWidth, iHeight, mVersion, bAutoStart, sPlaceHolder) {
		dbug.error('Plugin.QT is not yet implemented.')
	},
	Real: function(sType, sId, iWidth, iHeight, mVersion) {
		dbug.error('Plugin.Real is not yet implemented.')
	}
}
