﻿function setUpBlogPages() {
    try {
    
        // only match blog pages
        if (!document.URL.match(/blog.aspx/i) && !document.URL.match(/post.aspx/i)) {
            return;
        }

        if (document.URL.match(/editpost.aspx/i)) {
            setUpVideoEmbedButton();
        }
        else if (document.URL.match(/post.aspx/i)) {
            decodeEmbeddedVideos();
        }

        removeUserProfileLinksFromBlogs();
    
    }catch (e) {}
}

function removeUserProfileLinksFromBlogs() {

    var links = null;

    if (null != document.URL.match(/blog.aspx/i) ||
        null != document.URL.match(/post.aspx/i)) {
        links = $('td[@class=ms-PostFooter] a');
    }

    if (null != links) {
        removeLinksFromBlogs(links);
    }
}

function removeLinksFromBlogs(userProfileLinks) {
    if (null == userProfileLinks) {
        return;
    }

    for (var i = 0; i < userProfileLinks.length; i++) {
        if (null != userProfileLinks[i] &&
            null != userProfileLinks[i].href &&
            userProfileLinks[i].href.match(/userdisp.aspx/i)) {
            userProfileLinks[i].outerHTML = userProfileLinks[i].innerText;
        }
    }
}

function decodeEmbeddedVideos() {
    $("span.embeddedVideo").css("display", "none");

    $("span.embeddedVideo").each(function() {
        $(this).html(decodeHTML(this.innerHTML));
    });

    $("span.embeddedVideo").css("display", "inline");
}

function validateVideoEmbedForm(form) {
    var validated = true;

    if (form.embedhtml.value == "") {
        hasErrors = false;
    }

    return validated;
}

function submitEmbedVideo(form) {
    if (validateVideoEmbedForm(form)) {
        var id = $("textarea[title=Body][class=ms-long]").attr("id");

        if (undefined != id) {

            var html = STSHtmlEncode(form.embedhtml.value);

	    RTE_GiveEditorFocus(id);
	    RTE_SaveSelection(id);

            RTE_GetSelection(id).pasteHTML("<span style='display: none;' class='embeddedVideo'>" + html + "<span>");

            // show the video on the edit page
            $("span.embeddedVideo").css("display", "inline");

            if (null != embedWindow) {
                embedWindow.close();
                embedWindow = null;
            }

            return true;
        }

        return false;
    }
    else {
        // show errors on form, focus window and put it on top
        alert('HTML cannot be empty.');

        return false;
    }
}

function closeEmbedVideoForm() {
    if (null != embedWindow) {
        embedWindow.close();
        embedWindow = null;
    }
}

function initializeEmbedWindow() {
    if (null == embedWindow) {
        embedWindow = window.open('', 'embedWindow', 'status=0,toolbar=0,height=300,width=500');
        embedWindow.document.write(formHtml);

        embedForm = embedWindow.document.getElementById(videoEmbedFormId);

        if (null != embedForm) {
            $(embedForm).submit(function() {
                return submitEmbedVideo(this);
            });

            embedFormCancelButton = embedWindow.document.getElementById(videoEmbedFormCancelButtonId);

            if (null != embedFormCancelButton) {
                $(embedFormCancelButton).click(function() {
                    closeEmbedVideoForm();
                });
            }
        }
    }
}

// video embed button
var embedButtonId = "videoEmbedButton";
var embedButtonHtml = "<td><table><tr><td><img id='" + embedButtonId + "' alt='Embed Video' src='http://msofficelb.vo.llnwd.net/o25/StyleLibrary/Images/PlayVideoArrow.png' /></td></tr></table></td>";

// reference to popup window
var embedWindow = null;

// video embed form
var videoEmbedFormId = "videoEmbedForm";
var videoEmbedFormCancelButtonId = 'videoEmbedFormCancelButton';
var formHtml = "<html><head><title>Video Embedder</title></head><body><form id='" + videoEmbedFormId + "'><table><tr valign='top'><tr valign='top'><td>html:</td><td><textarea name='embedhtml' rows='10' cols='50'></textarea></td></tr><tr><td></td><td><input type='submit' value='Emded' /><input type='button' value='Cancel' id='" + videoEmbedFormCancelButtonId + "' /></td></tr></table></form></body></html>";

// reference to form
var embedForm = null;

function setUpVideoEmbedButton() {
    // add the image
    $("table.ms-rtetoolbarmenu tr:eq(0) td.ms-rtetoolbarmenu:last").after(embedButtonHtml);

    // add the image click function
    $("img#" + embedButtonId).mouseup(function() {
        initializeEmbedWindow();
    });

    // disable the embed video button
    $("img#" + embedButtonId).each(function() {
        RTE_TB_SetButtonDisabled(this);
    });

    // enable image button when rte is focused
    $("iframe[title='Rich Text Editor']").focus(function() {
        $("img#" + embedButtonId).each(function() {
            RTE_TB_ClearButtonDisabled(this);
        });
    });

    // disable image button when rte is blurred
    $("iframe[title='Rich Text Editor']").blur(function() {
        $("img#" + embedButtonId).each(function() {
            RTE_TB_SetButtonDisabled(this);
        });
    });
}

function decodeHTML(str) {
    return str.replace(/&amp;/g, '&').replace(/&lt;/g, '<').replace(/&gt;/g, '>');
}
