// Extended RTE for SharePoint
// Created By Boris Gomiunik 
// Please visit my Blog to find more tricks for SharePoint: http://webborg.blogspot.com
// Project posted on CodePlex -- http://www.codeplex.com/erte

//this is the function that generates the "Insert Flash" dialog.
function embedFlash(gb_whichElement) {
  while (gb_whichElement.id.indexOf('_toolbar') == -1) {
  	gb_whichElement = gb_whichElement.parentNode;
  }
  fieldID = gb_whichElement.id;
  fieldID = fieldID.substring(0,fieldID.indexOf('_toolbar'));
  RTE_SaveSelection(fieldID);
  var generator=window.open('','question','height=300,width=300,scrollbar=no,menu=no,toolbar=no,status=no,location=no');
  generator.document.write('<html><head><title>Please input details</title>' + 
  '<script type="text/javascript">function insertFlash() { \n' + 
  'var finalString = "<span class=erte_embed id=" \n' + 
  'finalString += escape(document.getElementById("embed").innerText) \n' + 
  'finalString += ">" \n' + 
  'finalString += document.getElementById("alt").value\n' + 
  'finalString += "</span>"\n' + 
  'window.opener.RTE_GetSelection(document.getElementById("field").value).pasteHTML(finalString)\n' + 
  'window.close()\n;' +
  '}'+"<\/script>" +
  '</head><body style="margin:10px; font-family: verdana; font-size: 10px;">' + 
  '<strong>Alt Text:</strong> <input id="alt" type="text" style="border: 1px black solid; width: 200px; font-family: verdana; font-size: 10px;"/><br/><span style="color: gray">(This will be displayed in RSS feeds)</span>' + 
  '<br/> <br/><strong>Embed Code:</strong><br/>' + 
  '<textarea id="embed" style="width: 250px; height: 100px; border: 1px black solid; font-family: verdana; font-size: 10px;"></textarea>' + 
  '<br/><span style="color: gray">(Paste your Embed code here)</span><br/>' +
  '<input type="hidden" id="field" value="'+fieldID+'"' + 
  '<br/> <br/><button style="font-family: verdana; font-size: 10px;" onclick="insertFlash()">Insert</button>' +
  '&nbsp;&nbsp;<button style="font-family: verdana; font-size: 10px;" onclick="window.close()">Close</button>' + 
  '</body></html>');
  generator.document.close();
}


/* --- This part here adds the button --- */
objekty = document.getElementsByTagName('table');
for (i=0;i<objekty.length;i++) {
	if (objekty[i].className == 'ms-rtetoolbarmenu ms-long' && objekty[i].childNodes[0].childNodes.length == 2) {
		var newCell = objekty[i].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].insertCell();
		newCell.innerHTML = '<a href="javascript:" unselectable="on" onclick="embedFlash(this); return false"><img border="0" src="/_catalogs/masterpage/erte/erte.gif"/></a>'
	}
}

/* --- This part will make the function --- */
	objekty = document.getElementsByTagName('span');
	for (i=0;i<objekty.length;i++) {
		if (objekty[i].className == 'erte_embed') {
			objekty[i].innerHTML = unescape(objekty[i].id)
		}
	}
