﻿// Function - autocalled when editor window is closed
function EditorClosed(oWnd, args) {
    //Getting rad window manager
    var oManager = GetRadWindowManager();
    //Get an existing window DialogWindow using getWindowByName
    oManager.closeActiveWindow();
    // Automatically reboot our parent window
    if (isDirty == true) {
        if (reloadto == null) { location.replace(location.href); } else { location.replace(reloadto); }
    }
}
// Function - called to set and show the editor window
function SetEditorWindow(url, width, height) {
    var oWnd = GetEditorWindow();
    oWnd.show();
    oWnd.setSize(width, height);
    oWnd.setUrl(url);
    oWnd.center();
}
// Function - called to set and show a popup, hi-res image
function ShowHiResWindow(url) {
    var oManager = GetRadWindowManager();
    var oWnd = oManager.open(url, "win" + oManager.get_windows().length);
    oWnd.center();
}
// Function to show a specific loading panel
function ramRequestStart(auxloadingpanelIDs) {
    document.body.className = "waiting"; if (auxloadingpanelIDs) { var i; var auxelem; for (i in auxloadingpanelIDs) { auxelem = document.getElementById(auxloadingpanelIDs[i]); if (auxelem) auxelem.className = "ldpanel"; } } 
}
// Function to hide a specific loading panel
function ramResponseEnd(auxloadingpanelIDs) {
    document.body.className = ""; if (auxloadingpanelIDs) { var i; var auxelem; for (i in auxloadingpanelIDs) { auxelem = document.getElementById(auxloadingpanelIDs[i]); if (auxelem) auxelem.className = "hidden"; } }
}
// Function to get a reference to an editor window internally
function getWindow() {
    var oWindow = null;
    if (window.radWindow) oWindow = window.radWindow;
    else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
    return oWindow;
}
//Function to manually close an editor window and return to the parent window
function returnToParent() {
    //get a reference to the current RadWindow
    var oWnd = getWindow();
    //Close the RadWindow and send the argument to the parent page
    if (oWnd) {oWnd.close();}
    else {
        if (window.opener) window.opener.location.replace(window.opener.location.href);
        window.close()
    }
}
//Window tracking to determine if a reload is needed - default is false
var isDirty;
var reloadto = null;
isDirty = false;
function setDirty(redirectTo) {
    var oWnd = getWindow();
    oWnd.BrowserWindow.reloadto = redirectTo;
    oWnd.BrowserWindow.isDirty = true;
}
// Function to ensure the login page is the top window
function topCheck() {
    if (window != top) top.location.href = "/login.aspx?ReturnUrl="+encodeURI(top.location.href);
}
// Function to add the hi-res pop link manager
var myEditor = null;
function addHiResLinkManager() {
    Telerik.Web.UI.Editor.CommandList["HiResLinkManager"] = function(commandName, editor, args) {
        var elem = editor.getSelectedElement(); //returns the selected element.
        if (elem.tagName == "A") {
            editor.selectElement(elem);
            argument = elem;
        } else if (elem.tagName == "IMG") {
            // Try to find the parent link if there is one
            var parent = elem.parentNode;
            argument = null;
            while (parent) {
                if (parent.tagName == "A") {
                    editor.selectElement(parent);
                    argument = parent;
                    parent = null;
                } else {
                    parent = parent.parentNode;
                }
            }
            if (argument == null) {
                var newcontent = editor.getSelectionHtml();
                var newlink = editor.get_document().createElement("A");
                newlink.innerHTML = newcontent;
                argument = newlink;
            }
        } else {
            // Create a new anchor around the current selection
            var content = editor.getSelectionHtml();
            var link = editor.get_document().createElement("A");
            link.innerHTML = content;
            argument = link;
        }
        var myCallbackFunction = function(sender, args) {
            if (args) editor.pasteHtml('<a href="javascript:ShowHiResWindow(\'/gohi-res.aspx?imageUrl=' + encodeURI(args.link) + '&title=' + encodeURI(args.title) + '\');" title="' + args.tooltip + '">' + args.potatoes + '</a>');
        }
        var obj = new Object;
        myEditor = editor;
        obj.href = argument.href;
        obj.title = argument.title;
        obj.innerHTML = argument.innerHTML;
        editor.showExternalDialog('/editordialogs/hiresmanager.aspx', argument, 416, 210, myCallbackFunction, null, 'High Resolution Image Link Manager', true, Telerik.Web.UI.WindowBehaviors.Close + Telerik.Web.UI.WindowBehaviors.Move, false, true);
    };
}