﻿
var editor = "";
var editorWindow = "";

var curredit = "";
var _runOnce = true;

function updatePage() {    // only update page content if an editor has been used    if(curredit != "") {
        var edittext = document.getElementById(curredit); 
        var frm = document.getElementById("frm_" + curredit);        var text = $find(editorID).get_html();
        
        // If text has been altered then a new save is required
        if(text != frm.value) {
            saveRequired();
        }
        frm.value = edittext.innerHTML = text;     }}

function openEditor(url, spanid) {    var editor = $find(editorID);
    
    if(_runOnce) {
        _runOnce = false;        editor.attachEventHandler ("onkeyup", function (e) {             updatePage();        }        );
    }
    curredit = spanid;
    
    // if editor has changed clear class on old editor    var ed = $get("pnlEditor");    ed.style.visibility = "visible";     // store the pagecontent element in the current global object    var element = $get(spanid);    var content = element.innerHTML;        // store the pagecontent hidden element in the current global object    document.getElementById("frm_" + spanid).value = content;        editor.setFocus();    editor.set_html(content);            // check style on page element, set editor backgroundColor to the same as on the page
    var style = editor.get_contentArea().style;    style.backgroundColor = getBackgroundColor(element);        style.height = "9999px";    // reset element display    //element.style.display = "inline";    style.backgroundImage = "none";    style.color = getCurrentStyle(element).color;    style.fontSize = getCurrentStyle(element).fontSize;    style.textAlign = getCurrentStyle(element).textAlign;
    
    return false;
}

function getDefaultText() {
    return edittext.innerHTML;
}


function hideEditor() {
    var span = document.getElementById("pnlEditor");
    span.style.display = "none";
}

/// Receives the editor object
function setEditor(ed, edWin) {
    editor = ed;
    editorWindow = edWin;
}

// -- Functions to manage the style
function contentobj(pvID, contentid, hidden, displayid) {
    this.PageVersionID = pvID;
    this.ContentID = contentid;
    this.HiddenField = hidden;
    this.Start = document.getElementById(hidden).value;
    if(this.Start == "" && typeof(displayid) != "undefined") {        
        this.Start = document.getElementById(hidden).value = document.getElementById(displayid).innerHTML;
    }
    
}

// Gets the currentStyle object for the passed in DomElement
function getCurrentStyle(element) {
    var currentStyle = null;
    if (element.currentStyle) {
        currentStyle = element.currentStyle;
    } else if (window.getComputedStyle) {
        currentStyle = document.defaultView.getComputedStyle(element, null);
    } else {
        currentStyle = element.style;
    }
    return currentStyle;
}

// Gets the actual background colour of a dom element (i.e. If element is transparent this function will display the background color actually displayed)
function getBackgroundColor(element) {
    var currentStyle = getCurrentStyle(element);
    if(currentStyle.backgroundColor.toLowerCase() == "transparent") {
        return getBackgroundColor(element.parentNode);
    }
    return currentStyle.backgroundColor;
}


