if(typeof com == "undefined") var com = new Object();
if(typeof com.onedigital == "undefined") com.onedigital = new Object();
if(typeof com.onedigital.MediaUtil == "undefined") com.onedigital.MediaUtil = new Object();

com.onedigital.MediaUtil.mimeTypeEquals = function(lst, itm)
{
	return lst===itm;
}

com.onedigital.MediaDefinition = function(mimeType, pluginName, classId, className, customHandler)
{
	this.mimeType = mimeType;
	this.pluginName = pluginName;
	this.classId = classId;
	this.className = className;
	this.customHandler = (customHandler) ? new window[customHandler] : null;
}

com.onedigital.MediaObject = function(mimeType, url, id, w, h, ver, prms)
{
	this.attributes = new Array();
	this.params = new Array();
	
	this.mimeType = mimeType;
	this.url = url;
	this.id = id;
	this.setAttribute("width",w);
	this.setAttribute("height",h);
	this.setAttribute("version",ver);
	
	if (prms)
		for (var paramName in prms) this.params[paramName] = prms[paramName];
		
	this.mediaDefinition = com.onedigital.MediaObject.getMediaDefinition(this.mimeType);
	
	//if (this.mediaDefinition.customHandler && typeof(this.mediaDefinition.customHandler.initialize)=="Function")
	//	alert('asd');
}

com.onedigital.MediaObject.findPlugin = function(ptn)
{
	var re = new RegExp(ptn);
	
	for (var i=0; i<navigator.plugins.length; i++)
		if (re.test( navigator.plugins[i].name ))
			return navigator.plugins[i];
			
	return null;
}

com.onedigital.MediaObject.checkMediaSupport = function(def)
{
	if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length)
	{
		var x = com.onedigital.MediaObject.findPlugin(def.pluginName);
		
		return (x && x.description);
	}
	else
	{
		try
		{
			var axo = new ActiveXObject(def.className);
			return true;
		}
		catch (e)
		{
			return false;
		}
	}
}

com.onedigital.MediaObject.prototype.getHTML = function()
{
	var html = "";
	
	if (this.mediaDefinition)
	{
		if (navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length)
		{
			html += "<embed type=\"" + this.mimeType + "\" src=\""+this.url+"\" id=\""+this.id+"\"";
			if (this.getAttribute("width")) html += " width=\"" + this.getAttribute("width") + "\"";
			if (this.getAttribute("height")) html += " height=\"" + this.getAttribute("height") + "\"";
			
			for (var paramName in this.params)
				html += " " + paramName + "=\"" + this.params[paramName] + "\"";
			
			html += "></embed>";
		}
		else
		{
			html += "<object classid=\"clsid:"+this.mediaDefinition.classId+"\" id=\""+this.id+"\"";
			if (this.getAttribute("width")) html += " width=\"" + this.getAttribute("width") + "\"";
			if (this.getAttribute("height")) html += " height=\"" + this.getAttribute("height") + "\"";
			html += " data=\"" + this.url + "\"";
			html += ">";
			
			html += "<param name=\"movie\" value=\"" + this.url + "\" />";
			
			for (var paramName in this.params)
				html += "<param name=\"" + paramName + "\" value=\"" + this.params[paramName] + "\" />";
			
			html += "</object>";
		}
	}

	return html;
}

com.onedigital.MediaObject.mediaDefinitions = new Array();

com.onedigital.MediaObject.getMediaDefinition = function(mimeType)
{
	for (var i=0; i<com.onedigital.MediaObject.mediaDefinitions.length; i++)
	{
		var def = com.onedigital.MediaObject.mediaDefinitions[i];
		
		if (com.onedigital.MediaUtil.mimeTypeEquals(def.mimeType, mimeType))
			return def;
	}
	
	return null;
}

com.onedigital.MediaObject.prototype.write = function(elementId)
{
	if (this.mediaDefinition && com.onedigital.MediaObject.checkMediaSupport(this.mediaDefinition) )
	{
		if(document.getElementById)
		{
			document.getElementById(elementId).innerHTML = this.getHTML();
		}
	}
}

com.onedigital.MediaObject.addMediaDefinition = function(mimeType, pluginName, classId, className)
{
	com.onedigital.MediaObject.mediaDefinitions.push(
		new com.onedigital.MediaDefinition(mimeType, pluginName, classId, className)
		);
}

com.onedigital.MediaObject.prototype.setAttribute = function(name, val)
{
	this.attributes[name] = val;
}

com.onedigital.MediaObject.prototype.getAttribute = function(name)
{
	return this.attributes[name];
}

var MediaObject = com.onedigital.MediaObject;

// Temporary measure until the MediaPlaceholderControl is built
com.onedigital.MediaObject.addMediaDefinition('application/x-shockwave-flash','Shockwave Flash','D27CDB6E-AE6D-11cf-96B8-444553540000','ShockwaveFlash.ShockwaveFlash');