
// Extended RTE 1.2 for SharePoint
// Created By Boris Gomiunik 
// Please visit my Blog to find more tricks for SharePoint: http://boris.gomiunik.net
// More info about ERTE on http://boris.gomiunik.net/erte
// Project posted on CodePlex -- http://www.codeplex.com/erte

///Settings:
//imagePath - enter the RTE Toolbar button icon if location is different than the one below
var imagePath = "/_layouts/images/rlsoft/erte.gif";



//Localization arrays. To localize, copy the line below and change the LCID.
var erte1033 = new Array("Please input details", "Alt Text", "This will be displayed in RSS feeds", "Embed Code", "Paste your Embed code here", "Insert", "Close"); //english
var erte1060 = new Array("Vnesite parametre", "Besedilo", "Besedilo bo izpisano v RSS-viru", "Embed koda", "Prilepite Embed kodo v polje", "Vstavi", "Zapri"); //english
var erte1036 = new Array("Parametres de configuration", "Texte de remplacement", "Ce texte sera affiche dans le flux RSS a la place de l'objet insere.", "Code Embed", "Coller le code HTML de l'object ici.", "Inserer", "Fermer"); //French - thanks to Slash71

/*-------------No need to edit below this line------------------*/

//set the language. If there is no localization array available, use english as default
if (window['erte' + L_Menu_LCID]) {
    erte_lang = window['erte' + L_Menu_LCID];
}
else {
    erte_lang = window['erte1033']
}

//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>' + erte_lang[0] + '</title>' +
  '<script type="text/javascript">function insertFlash() { \n' +
  'var finalString = "<span class=erte_embed id=" \n' +
  'var embedCode = document.getElementById("embed").innerText; \n' +
  'if(embedCode.indexOf("<object") != -1) { \n' +
  '	embedCode = embedCode.substring(embedCode.indexOf("<embed")); \n' +
  '	embedCode = embedCode.substring(0, embedCode.indexOf("</embed>")+8); \n' +
  '} \n' +
  'finalString += escape(embedCode) \n' +
  'finalString += ">" \n' +
  'finalString += document.getElementById("alt").value\n' +
  'finalString += "</span>"\n' +
  'window.opener.document.getElementById("' + fieldID + '_iframe").focus();' +
  '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>' + erte_lang[1] + ':</strong> <input id="alt" type="text" style="border: 1px black solid; width: 200px; font-family: verdana; font-size: 10px;"/><br/><span style="color: gray">(' + erte_lang[2] + ')</span>' +
  '<br/> <br/><strong>' + erte_lang[3] + ':</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">(' + erte_lang[4] + ')</span><br/>' +
  '<input type="hidden" id="field" value="' + fieldID + '"' +
  '<br/> <br/><button style="font-family: verdana; font-size: 10px;" onclick="insertFlash()">' + erte_lang[5] + '</button>' +
  '&nbsp;&nbsp;<button style="font-family: verdana; font-size: 10px;" onclick="window.close()">' + erte_lang[6] + '</button>' +
  '</body></html>');
    generator.document.close();
}

function embedVideos() {
    /* --- 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.setAttribute('unselectable', 'on');
            newCell.setAttribute('class', 'ms-rtetoolbarmenu');
            newCell.innerHTML = '<a href="javascript:" unselectable="on" onclick="embedFlash(this); return false"><img unselectable="on" border="0" src="' + imagePath + '"/></a>'
        }
    }

    /* --- This part will render the display --- */
    objekty2 = document.getElementsByTagName('span');
    for (i = 0; i < objekty2.length; i++) {
        if (objekty2[i].className == 'erte_embed') {
            objekty2[i].innerHTML = unescape(objekty2[i].id)
        }
    }

    
    var objDivs = getElementsByPrefix("PostDateTopBox", document);
    for (i = 0; i < objDivs.length; i++) {
        //replace [youtube]url[/youtube] tags with corresponding HTML object    
        if (objDivs[i].parentNode.parentNode.innerHTML.indexOf("[youtube]") > -1) {
            //find all [youtube][/youtube] tags        
            var arr = objDivs[i].parentNode.parentNode.innerHTML.split("[youtube]");
            var tempSrc = objDivs[i].parentNode.parentNode.parentNode.parentNode.parentNode.innerHTML;
            for (j = 0; j < arr.length; j++) {
                //get the url between the [youtube][/youtube] tags           
                urltemp = arr[j].substring(0, arr[j].indexOf("[/youtube]"));
                if (urltemp == "") continue;
                //format the URL to the correct format to use with the object-element            
                url = urltemp.replace("/watch?v=", "/v/");
                if (url.indexOf("&") > -1)
                    url = url.replace(url.substring(url.indexOf("&"), url.length), "");
                url += "?fs=1&amp;hl=nl_NL&amp;rel=0";
                //replace the [youtube][/youtube] tags with the corresponding object-element            
                tempSrc = tempSrc.replace("[youtube]" + urltemp + "[/youtube]", "<object width='480' height='390'><param name='movie' value='" + url + "'></param><param name='allowFullScreen' value='true'></param><param name='allowscriptaccess' value='always'></param><embed src='" + url + "' type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width='480' height='390'></embed></object>");
            }
            

            //note the 5x .parentNode suddenly, this is for IE. in IE you cannot edit the innerHTML of a non-div element. So I went to search the nearest div-element.        
            objDivs[i].parentNode.parentNode.parentNode.parentNode.parentNode.innerHTML = tempSrc;

            
        }
    }
}


function getElementsByPrefix(inPrefix, inRoot) { var elem_array = new Array; if (typeof inRoot.firstChild != 'undefined') { var elem = inRoot.firstChild; while (elem != null) { if (typeof elem.firstChild != 'undefined') { elem_array = elem_array.concat(getElementsByPrefix(inPrefix, elem)); } if (typeof elem.id != 'undefined') { var reg = new RegExp('^' + inPrefix + '.*'); if (elem.id.match(reg)) { elem_array.push(elem); } } elem = elem.nextSibling; } } return elem_array; }

if (window['_spBodyOnLoadFunctionNames']) {
    _spBodyOnLoadFunctionNames.push("embedVideos");
}
else {
    embedVideos()
}
