///
///
///
function EmbedControlContextMenu(contentType)
{
	if (contentType == "EditDesign")
		return CONTROLCONTEXTMENU_ENABLEDISABLE | CONTROLCONTEXTMENU_CLEAR | CONTROLCONTEXTMENU_EDIT | CONTROLCONTEXTMENU_PROPERTIES | CONTROLCONTEXTMENU_CUSTOMPROPERTIES | CONTROLCONTEXTMENU_DEPTHORDERING | CONTROLCONTEXTMENU_DELETERESTORE;
	else if (contentType == "EditContent")
		return CONTROLCONTEXTMENU_EDIT | CONTROLCONTEXTMENU_CLEAR | CONTROLCONTEXTMENU_DELETERESTORE;

	return 0;
}

function CreateEmbedControl(container, controlID)
{
	ClearChildElements(container);

	var content = $get(controlID.replace("_SpanItemBase", "_HiddenFieldContent")).value;
	var properties = $get(controlID.replace("_SpanItemBase", "_HiddenFieldCustomProperties")).value;
	var embedObj = CreateObject(
			{
				id: controlID.id + "_Embed",
				src: unescape(content),
				type: GetSourceClassID(content),
				params: properties
			});
	// HTML 5 variant
	//		var embedObj = CreateEmbed(
	//			{
	//				id: this.controlObject.id + "_Embed",
	//				src: unescape(this.GetContent()),
	//				type: GetSourceClassID(this.GetContent()),
	//				params: this.GetCustomProperties()
	//			});

	container.appendChild(embedObj);
}

function InitializeEmbedControl(contentType, controlID, container)
{
	if (contentType != "View")
	{
		var control = eval("_" + controlID);
		embeddedControl_PresentationControls[controlID] = $get(container);
		control.GetEditorControlID = function() { return "EmbedSourceEditor"; }
		//control.GetCustomContent = function() { return [control.GetContent(), control.GetCustomProperties()]; }
		control.PreProcessEditorContent = PreProcessContent_SaveFileReferences;
		control.ContentUpdated = EmbedControlUpdateSource;
		control.CustomPropertiesUpdated = function() { CreateEmbedControl($get(container), controlID); };
		control.OnContextMenu = EmbedControlContextMenu;
		control.GetCustomPropertyControlID = function() { return "EmbedSettings"; }
	}
	
	ClearChildElements($get(container));
	Page.AddOnPageLoad(function()
	{
		CreateEmbedControl($get(container), controlID);
	});
}

function GetSourceClassID(src)
{
	try
	{
		var extension = src.substring(src.lastIndexOf(".")).toLowerCase();

		switch (extension)
		{
			case ".wmv":
			case ".avi":
			case ".mpg":
			case ".mpeg":
			case ".xvid":
			case ".mkv":
			case ".mov": return "clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B";
				break;

			//default is always flash 
			default: return "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000";
				break;
		}
	}
	catch (err)
	{
		return "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000";
	}
}

///
///
///
function EmbedControlUpdateSource(e)
{
	CreateEmbedControl(embeddedControl_PresentationControls[this.controlObject.id], this.controlObject.id);
////	try
////	{
//		var container = embeddedControl_PresentationControls[this.controlObject.id];
//		ClearChildElements(container);

//		var embedObj = CreateObject(
//			{
//				id: this.controlObject.id + "_Embed",
//				src: unescape(this.GetContent()),
//				type: GetSourceClassID(this.GetContent()),
//				params: this.GetCustomProperties()
//			});

////		var embedObj = CreateEmbed(
////			{
////				id: this.controlObject.id + "_Embed",
////				src: unescape(this.GetContent()),
////				type: GetSourceClassID(this.GetContent()),
////				params: this.GetCustomProperties()
////			});
//		
//		container.appendChild(embedObj);
////		embeddedControl_PresentationControls[this.controlObject.id] = embedObj;

////	}
////	catch (err) {}
}

function CreateObjectParam(name, value)
{
	return '<param name="' + name + '" value="' + value + '" />\n';
}

///
/// Creates an HTML 4 compliant <object> object with parameters and <embed>
///
function CreateObject(attributes)
{
	var container = document.createElement("span");
	var objectHtml = "<object ";
	var embedHtml = "<embed ";
	var params = "";
	
	for (var attribute in attributes)
	{
		switch (attribute)
		{
			case "src":
				objectHtml += " src='" + attributes[attribute] + "'";
				//objectHtml += " data='" + attributes[attribute] + "'";
				embedHtml += " src='" + attributes[attribute] + "'";
				
				params += CreateObjectParam("Movie", attributes[attribute]);
				params += CreateObjectParam("src", attributes[attribute]);
				break;

			case "params":
				var embedParams = attributes[attribute].split("|");
				//autostart
				if (embedParams.length > 0 && (parseInt(embedParams[0]) > 0))	//atleast 1 parameter was specified
				{
					params += CreateObjectParam("autostart", parseInt(embedParams[0]) > 0);
					embedHtml += " autostart='" + (parseInt(embedParams[0]) > 0) + "'";
				}
				else
				{
					params += CreateObjectParam("autostart", false);
					embedHtml += " autostart='false'";
				}

				//repeat
				if (embedParams.length > 1)
				{
					params += CreateObjectParam("loop", parseInt(embedParams[1]));
					embedHtml += " loop='" + parseInt(embedParams[1]) + "'";
				}

				//width
				if (embedParams.length > 2 && !isNaN(parseInt(embedParams[2])))
				{
					params += CreateObjectParam("width", embedParams[2]);
					embedHtml += " width='" + embedParams[2] + "'";
				}
				else
				{
					params += CreateObjectParam("width", "250px");
					embedHtml += " width='250px'";
				}

				//height
				if (embedParams.length > 3 && !isNaN(parseInt(embedParams[3])))
				{
					params += CreateObjectParam("height", embedParams[3]);
					embedHtml += " height='" + embedParams[3] + "'";
				}
				else
				{
					params += CreateObjectParam("height", "250px");
					embedHtml += " height='250px'";
				}

				break;

			default:
				objectHtml += " " + attribute+ "='" + attributes[attribute] + "'";
				break;
		}
	}

	params += CreateObjectParam("wmode", "transparent");
	embedHtml += " wmode='transparent'";
	
	params += CreateObjectParam("menu", "false");
	embedHtml += " menu='false'";
	
	params += CreateObjectParam("WindowlessVideo", "-1");
	embedHtml += " WindowlessVideo='-1'";

	//fix for Quicktime - by default it does not scale the embedded media
	params += CreateObjectParam("scale", "tofit");
	embedHtml += " scale='tofit'";

	container.innerHTML = objectHtml + ">" + params + embedHtml + "/></object>";
	return container;
}

///
/// Creates an HTML 5 compliant <embed> object
///
function CreateEmbed(attributes)
{
	var embedObj = document.createElement("embed");

	for (var attribute in attributes)
	{
		if (attribute.toLowerCase() != "params")
			embedObj.setAttribute(attribute, attributes[attribute]);
		else
		{
			var embedParams = attributes[attribute].split("|");
			//autostart
			if (embedParams.length > 0 && parseInt(embedParams[0]) > 0)	//atleast 1 parameter was specified
				embedObj.setAttribute("autostart", parseInt(embedParams[0]) > 0);
			
			//repeat
			if (embedParams.length > 1)
				embedObj.setAttribute("loop", parseInt(embedParams[1]));

			//width
			if (embedParams.length > 2)
				embedObj.setAttribute("width", embedParams[2]);

			//height
			if (embedParams.length > 3)
				embedObj.setAttribute("height", embedParams[3]);
		}
	}

	return embedObj;	
}
