// Video RTE 0.1 for SharePoint
// Created By Felipe Cardoso
// Last Modified: 08/02/2010

var INVIT_RTE2_ImagePath = "/_layouts/images/avi16.gif";
var INVIT_RTE2_VideoDisplayPath = "/_catalogs/masterpage/Publico/icon_video.png";
var INVIT_RTE2_VideoDialog = "/_catalogs/masterpage/RTE2VideoDialog.aspx";
var INVIT_RTE2_CONTROLS_LOADED = new Array();
var INVIT_RTE2_SITE = "imprensa";
var INVIT_RTE2_PAGE_URL = window.location.pathname.toString();

String.prototype.endsWith = function(str) { return (this.match(str + "$") == str) }

function INVIT_AddOnloadEvent(fnc) {
    if (typeof window.addEventListener != "undefined")
        window.addEventListener("load", fnc, false);
    else if (typeof window.attachEvent != "undefined") {
        window.attachEvent("onload", fnc);
    } else {
        if (window.onload != null) {
            var oldOnload = window.onload;
            window.onload = function(e) {
                oldOnload(e);
                window[fnc]();
            };
        } else window.onload = fnc;
    }
}

//this is the function that generates the "Insert Flash" dialog.
function INVIT_RTE2_ShowVideoDialog(strBaseElementID) {
    var elemViewToEdit = RTE_GetNearestContainingElementOfType(strBaseElementID, "IMG");
    var valuesArray = new Array();
    if ((elemViewToEdit != null) && (elemViewToEdit.className == "vrte2_embed")) {
        var strValues = elemViewToEdit.getAttribute("id");
        if (strValues)
            valuesArray = strValues.split("$");
    }
    var dialogInput = new Object();

    dialogInput.vFonte = valuesArray[0] ? unescape(valuesArray[0]) : "youtube";
    dialogInput.vID = valuesArray[1] ? unescape(valuesArray[1]) : "";
    dialogInput.vWidth = valuesArray[2] ? unescape(valuesArray[2]) : "100";
    dialogInput.vWidthUnit = valuesArray[3] ? unescape(valuesArray[3]) : "%";
    dialogInput.vHeight = valuesArray[4] ? unescape(valuesArray[4]) : "300";
    dialogInput.vHeightUnit = valuesArray[5] ? unescape(valuesArray[5]) : "px";
    dialogInput.vPosicao = valuesArray[6] ? unescape(valuesArray[6]) : "rel";
        
    var callback = function(dialogOutput) {
        INVIT_RTE2_InsertOrChangeVideo(strBaseElementID, dialogOutput);
    };
    //if the page is on _layouts directory
    //var dialogUrl = RTE2_GetServerRelativeDialogUrl(strBaseElementID, INVIT_RTE2_VideoDialog);
    var dialogUrl = INVIT_RTE2_VideoDialog;
    var features = "dialogWidth:400px;dialogHeight:300px;status:no;resizable:yes;scroll:no";
    commonShowModalDialog(dialogUrl, features, callback, dialogInput);
    RTE_GiveEditorFocus(strBaseElementID);
}


function INVIT_RTE2_InsertOrChangeVideo(strBaseElementID, dialogOutput) {
    var docEditor = RTE_GetEditorDocument(strBaseElementID);
    if (docEditor == null) return;
    RTE_RestoreSelection(strBaseElementID);

    var selection = docEditor.selection;
    if (selection != null) {
        try {
            if (selection.type == "Control") {
                var controlRange = selection.createRange();
                if (controlRange != null) {
                    controlRange.execCommand("Delete");
                }
            }
            if (selection.type != "Control") {
                var textRange = selection.createRange();
                if (textRange != null) {
                    var vars = escape(dialogOutput["vFonte"]) + "$" +
                       escape(dialogOutput["vID"]) + "$" +
                       escape(dialogOutput["vWidth"]) + "$" +
                       escape(dialogOutput["vWidthUnit"]) + "$" +
                       escape(dialogOutput["vHeight"]) + "$" +
                       escape(dialogOutput["vHeightUnit"]) + "$" +
                       escape(dialogOutput["vPosicao"]);
                    var pasteStr = '<img class="vrte2_embed" id="' + vars + '" src="' + INVIT_RTE2_VideoDisplayPath + '" ' + (dialogOutput["vPosicao"] == 'rel' ? 'style="margin:0px 10px 10px 0px; float: left; position:inherit;"' : '') + ' />';
                    textRange.pasteHTML(pasteStr);
                }
            }
        }
        catch (e) {
            alert(L_ErrorProcessingImage_TEXT);
        }

    }
    RTE_GiveEditorFocus(strBaseElementID);
}


function INVIT_RTE2_CreateVideoIcon(controlID) {
    var strBaseElementID = controlID + '_RichHtmlField_displayContent';

    if (INVIT_RTE2_CONTROLS_LOADED[controlID] != true && RTE2_IsEditorLoaded(strBaseElementID)) {
        var obj = RTE_GetEditorToolBar(strBaseElementID);
        if (obj != null) {
            var newCell = obj.childNodes[0].childNodes[0].childNodes[1].childNodes[0].childNodes[0].childNodes[0].insertCell();
            newCell.setAttribute('unselectable', 'on');
            newCell.setAttribute('class', 'ms-rtetoolbarmenu');
            newCell.innerHTML = '<table cellpadding="1" cellspacing="0" border="0" unselectable="on"><tr unselectable="on"><td class="ms-rtetoolbarmenu ms-rtetoolbarunsel" nowrap id="' + controlID + '_RichHtmlField_displayContent_Video" onclick="RTE_DD_CloseMenu(); INVIT_RTE2_ShowVideoDialog(' + "'" + strBaseElementID + "'" + '); return false;" title="Inserir/Editar V&iacute;deo" unselectable="on" style="cursor:pointer;"><img unselectable="on" border="0" src="' + INVIT_RTE2_ImagePath + '" alt="Inserir V&iacute;deo" unselectable="on"></td></tr></table>';
            INVIT_RTE2_CONTROLS_LOADED[controlID] = true;
        }
    }
    return false;
}

function INVIT_RTE2_ShowCustomControls() {
    if (INVIT_RTE2_PAGE_URL.endsWith('EditForm.aspx')) {
        objekty = document.getElementsByTagName('a');
        for (i = 0; i < objekty.length; i++) {
            /* --- This part here adds the button on richtext button --- _ControlWrapper_RichHtm  lField RichHtmlField_displayContent_toolbar _RichHtmlField____EditHtml _RichHtmlField____EditHtml_LINK */
            if (objekty[i].id.endsWith('_RichHtmlField____EditHtml')) {
                var controlID = objekty[i].id.replace('_RichHtmlField____EditHtml', '');

                var strFind = objekty[i].outerHTML.substr(objekty[i].outerHTML.indexOf('RTE2_LaunchEditor'));
                strFind = strFind.substr(0, strFind.indexOf(');') + 2);

                var parentObj = objekty[i].parentNode;
                parentObj.innerHTML = parentObj.innerHTML.split(strFind).join(strFind + " if(params[1][1].endsWith('" + INVIT_RTE2_SITE + "')) INVIT_RTE2_CreateVideoIcon('" + controlID + "');");
                
                var parentSpanObj = document.getElementById(controlID + '_RichHtmlField_DisplayHtmlPanel').parentNode;
                parentSpanObj.innerHTML = parentSpanObj.innerHTML.split(strFind).join(strFind + " ; if(params[1][1].endsWith('" + INVIT_RTE2_SITE + "')) INVIT_RTE2_CreateVideoIcon('" + controlID + "');");
                
                INVIT_RTE2_CONTROLS_LOADED[controlID] = false;
            }
        }
    }
    else {
        /* --- This part will render the display --- */
        objekty2 = document.getElementsByTagName('img');
        for (i = 0; i < objekty2.length; i++) {
            if (objekty2[i].className == 'vrte2_embed') {
                var varArray = objekty2[i].id.split("$");
                var innerHTML = "";
                var vID = unescape(varArray[1]).substr(0, 20).replace("<", "&lt;").replace(">", "&gt;");
                var vWidth = unescape(varArray[2]).substr(0, 3) + (unescape(varArray[3]) == '%' ? '%' : '');
                var vHeight = unescape(varArray[4]).substr(0, 3) + (unescape(varArray[5]) == '%' ? '%' : '');
                var vStyle = unescape(varArray[6]) == 'rel' ? 'margin:0px 10px 10px 0px; float: left; position:inherit;' : '';

                if (varArray[0] == 'youtube') {
                    innerHTML = '<object width="' + vWidth + '" height="' + vHeight + '" style="' + vStyle + '">' +
                                '<param name="movie" value="http://www.youtube.com/v/' + vID + '&amp;hl=en_US&amp;fs=1"></param>' +
                                '<param name="allowFullScreen" value="true"></param>' +
                                '<param name="allowscriptaccess" value="always"></param>' +
                                '<embed src="http://www.youtube.com/v/' + vID + '&amp;hl=en_US&amp;fs=1" style="' + vStyle + '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="' + vWidth + '" height="' + vHeight + '"></embed>' +
                                '</object>';
                }
                else if (varArray[0] == 'vimeo') {
                    innerHTML = '<object width="' + vWidth + '" height="' + vHeight + '" style="' + vStyle + '">' +
                                '<param name="allowfullscreen" value="true" />' +
                                '<param name="allowscriptaccess" value="always" />' +
                                '<param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id=' + vID + '&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=1&amp;color=&amp;fullscreen=1&amp;autoplay=0&amp;loop=0"></param>' +
                                '<embed src="http://vimeo.com/moogaloop.swf?clip_id=' + vID + '&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=1&amp;color=&amp;fullscreen=1&amp;autoplay=0&amp;loop=0" style="' + vStyle + '" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="' + vWidth + '" height="' + vHeight + '"></embed>' +
                                '</object>';
                }
                if (innerHTML != "") {
                    objekty2[i].insertAdjacentHTML("AfterEnd", innerHTML);
                    objekty2[i].style.display = 'none';
                }
            }
        }
    }
}

if (window['_spBodyOnLoadFunctionNames'] && INVIT_RTE2_PAGE_URL.endsWith('EditForm.aspx')) {
    _spBodyOnLoadFunctionNames.push("INVIT_RTE2_ShowCustomControls");
}
else {
    INVIT_AddOnloadEvent(INVIT_RTE2_ShowCustomControls);
}

