var XStandard = {

	add: function(config) 
	{
		if (XStandard.hasPlugin())
		{
			XStandard.addToDom(config);
		}
		else
		{
			XStandard.addAlternateContent(config);
		}		
	},
	
	hasPlugin: function()
	{
		var hasPlugin = false;
		if (navigator.plugins && typeof navigator.plugins['XStandard'] == 'object') 
		{
			hasPlugin = true;
		}
		else if (window.ActiveXObject) 
		{
			hasPlugin = true;
			try 
			{
				var xObj = new ActiveXObject('XStandard.XHTMLEditor');
			}
			catch(e) 
			{
				hasPlugin = false;			
			}
		}
		else
		{
			// Doesn't support navigator.plugins or activex
			// Do it anyway just to be on the safe side
			hasPlugin = true;
		}

		return hasPlugin;
	},
		
	addToDom: function(config) 
	{
		var xObj = document.createElement('object');

		// Add to the DOM
		var placeholder = document.getElementById(config['cleanName']+'_placeholder');
		placeholder.appendChild(xObj);
		
		
		xObj.setAttribute('id', config['cleanName']+'_xstandard');

		if (config['style'])
		{
			var temp = config['style'].split(';');
			var temp2;
			for (i = 0; i < temp.length; i++)
			{
				if (temp[i].indexOf(':') < 1) { continue;}

				temp2 = temp[i].split(':');
				temp2[0] = temp2[0].trim();
				temp2[1] = temp2[1].trim();

				xObj.style[temp2[0]] = temp2[1];
			}
		}

		if (config['value'])
		{
			var param1 = document.createElement('param');
			param1.setAttribute('name', 'Value');
			param1.setAttribute('value', config['value']);
			xObj.appendChild(param1);
		}

		if (config['cache'])
		{
			var param2 = document.createElement('param');
			param2.setAttribute('name', 'EnableCache');
			param2.setAttribute('value', 'yes');
			xObj.appendChild(param2);
		}
		if (config['cssUrl'])
		{
			var param3 = document.createElement('param');
			param3.setAttribute('name', 'CSS');
			param3.setAttribute('value', config['cssUrl']);
			xObj.appendChild(param3);
		}
		if (config['xmlUrl'])
		{
			var param4 = document.createElement('param');
			param4.setAttribute('name', 'Styles');
			param4.setAttribute('value', config['xmlUrl']);
			xObj.appendChild(param4);
		}
		if (config['baseUrl'])
		{
			var param5 = document.createElement('param');
			param5.setAttribute('name', 'Base');
			param5.setAttribute('value', config['baseUrl']);
			xObj.appendChild(param5);
		}
		if (config['toolbar'])
		{
			var param6 = document.createElement('param');
			param6.setAttribute('name', 'Toolbar');
			param6.setAttribute('value', config['toolbar']);
			xObj.appendChild(param6);
		}

		// This MUST appear after everything else for IE?
		xObj.setAttribute('type', 'application/x-xstandard');

		// hidden field
		var hid = document.createElement('input');
		hid.setAttribute('type', 'hidden');
		hid.setAttribute('name', config['name']);
		hid.setAttribute('id', config['cleanName']+'_hidden');
		if (config['value'])
		{
			hid.setAttribute('value', config['value']);
		}

		// Add to DOM
		placeholder.appendChild(hid);

		// Clean up code
		addEvent(document.getElementById(config['cleanName']+'_xstandard').form, 'onsubmit', function()
			{
				var x = document.getElementById(config['cleanName']+'_xstandard');
				var c = document.getElementById(config['cleanName']+'_hidden');

				// If the object is not displayed (display: none) it is not available as x
				if (typeof(x.value) == 'undefined')
				{
					return true;
				}

				x.EscapeUnicode = true;
				c.value = x.value;

				myPatt = /^<!-- Generated by XStandard(.*?)-->/gi;
				c.value = c.value.replace(myPatt, '');
			}
		);
	},

	addAlternateContent: function(config) 
	{
		var altDiv = document.createElement('div');
		altDiv.className = 'error';

		var message = (config['alt']) ? config['alt'] : 'You need the Xstandard plugin installed to edit page content.  This is available here:';

		var txt = document.createTextNode(message+' ');
		altDiv.appendChild(txt);

		var lnk = document.createElement('a');
		lnk.setAttribute('href', 'http://xstandard.com/download.asp?product=lite');
		lnk.setAttribute('id', config['cleanName']+'_link');
		addEvent(lnk, 'onclick', function()
			{
				return newWindow(this.href);
			}
		);		

		var txt2 = document.createTextNode('http://xstandard.com');
		lnk.appendChild(txt2);
		altDiv.appendChild(lnk);

		var txt3 = document.createTextNode('.');
		altDiv.appendChild(txt3);

		// textarea field
		var hid = document.createElement('textarea');
		hid.setAttribute('name', config['name']);
		var tVal = document.createTextNode(config['value']);
		hid.appendChild(tVal);

		// Add to the DOM
		var placeholder = document.getElementById(config['cleanName']+'_placeholder');
		placeholder.appendChild(hid);
		placeholder.appendChild(altDiv);
	}
}
