﻿window.VisionX_initEditor = true;
/////////////////////////////////////////////////////////////////////////////////////////////////////////

var _oVxEditorIdList = new Array();

var VxJsModules = new Array("crossPlatform", "strings", "cursor", "editorActions", "effects", "elements", "emptyMarker",
                            "fields", "images", "nodes", "selection", "specialEvents", "toolbar", "undo",
                            "program", "tables", "debug", "backgroundSave", "links", "xhtml", "keyPressActions",
                            "utility", "innerXHTML", "events", "copypaste", "move", "lists");
                        
var VxTurnOnDebugTracing = false;

var VxLoadedAndInitialized = false;
                        
// Call this function to mark an XHTML element as being editable. Pass optional emptyMarkerTemplate
// NOTE: id IS case sensitive for various reasons.
//                        
function VxMakeElementEditable(id, emptyMarkerTemplate)
{
    var idAlreadyUsed = false;
    
    for (var i=0; i<_oVxEditorIdList.length; i++)
    {
        if (_oVxEditorIdList[i] == id)
        {
            idAlreadyUsed = true;
            break;
        }
    }
    
    if (!idAlreadyUsed)
    {
        _oVxEditorIdList.push(id);
    }
    
    if (VxLoadedAndInitialized)
    {
        // Actually make this element editable too
        //
        var oNewEditor = VxDocument().getElementById(id);
        if (oNewEditor)
        {
            VxInitUndoRedoForEditor(id);
            VxInitializeEditableElement(oNewEditor, emptyMarkerTemplate);
        }
    }
}

function VxStopElementBeingEditable(id)
{
    var oEditor = VxDocument().getElementById(id);
    VxDeInitializeEditableElement(oEditor);
    
    for (var i=0; i<_oVxEditorIdList.length; i++)
    {
        if (_oVxEditorIdList[i] == id)
        {
            _oVxEditorIdList.splice(i, 1);
            break;
        }
    }    
}

// Calling this function causes VisionX to be loaded into the browser!
//                        
function VxLoadEditor(debugTracingOn)
{
    VxTurnOnDebugTracing = debugTracingOn;
    
    if (VxDisplayLoadProgress)
        VxCreateProgressPanel();

    loadjscssfile(VxBaseUrlScripts + "../__visionX.css", "css");

    for (var i = 0; i < VxJsModules.length; i++)
    {
        if (!VxIsScriptLoaded(VxJsModules[i])) // we check whether the surrounding page has pre-loaded some of our modules
        {
            VxLoadCodeModule(VxJsModules[i]);
        }
    }
    
    setTimeout("VxReportEditorLoadProgress()", 150);
}

var _VxCustomOnLoadCallbacks = new Object();
var _VxOnLoadCallbackId = 0;
function VxSetOnLoadCustomCallback(callback)
{
    _VxCustomOnLoadCallbacks[_VxOnLoadCallbackId++] = callback;
}

// Marks the window object to record that the javascript module it is being called from has been
// loaded into the browser. The sister function VxIsScriptLoaded can be used to determine whether a
// particular module is loaded, for example by systems that bootstrap the editor using a progress bar
// in consideration of users with slow connections.
//
// TO DO: dream on, this doesn't work!!! Unfortunately there are threading issues as other scripts
// are loading while we access scripts[scripts.length-1].src
//
//function VxRecordScriptLoaded()
//{
//    var scripts = document.getElementsByTagName("SCRIPT");
//    var script = scripts[scripts.length-1].src;
//    var scriptName = script.match(/[^\\|^\/]+\.\w+\w?$/).toString();
//    scriptName = GetJsModuleName(scriptName);
//    eval("window."+scriptName+" = true");
//    alert(scriptName);
//}

function VxIsScriptLoaded(scriptName)
{
    if (window["VisionX_" + scriptName])
    {
        return true;
    }
    
    return false;
}

function VxReportEditorLoadProgress()
{
    var notLoaded = 0;
    
    for (var i = 0; i < VxJsModules.length; i++)
    {
        if (!VxIsScriptLoaded(VxJsModules[i]))
        {
            notLoaded++;
        } 
    }
    
    if (notLoaded == 0)
    {
        if (VxDisplayLoadProgress)
            VxUpdateProgressPanel( 100 );
        
        VxInitializeEditor();
        
        startDebug(VxTurnOnDebugTracing);
        
        if (VxDisplayLoadProgress)
            setTimeout("VxDestroyProgressPanel()", 350);
    }
    else
    {
        if (VxDisplayLoadProgress)
            VxUpdateProgressPanel( Math.floor(100 * (VxJsModules.length-notLoaded)/VxJsModules.length) );
        
        setTimeout("VxReportEditorLoadProgress()", 200);
    }
}

function GetJsModuleName(filename)
{
    return filename.substr(0, filename.indexOf("."));
}

function VxLoadCodeModule(moduleName)
{
   var e = document.createElement("script");
   e.src = VxBaseUrlScripts + moduleName + ".js";
   e.type = "text/javascript";
   document.getElementsByTagName("head")[0].appendChild(e); 
}

var VxoProgressPanel;

function VxCreateProgressPanel()
{
    var frameWidth;
    var frameHeight;
    
    if (self.innerWidth)
    {
	    frameWidth = self.innerWidth;
	    frameHeight = self.innerHeight;
    }
    else if (document.documentElement && document.documentElement.clientWidth)
    {
	    frameWidth = document.documentElement.clientWidth;
	    frameHeight = document.documentElement.clientHeight;
    }
    else if (document.body)
    {
	    frameWidth = document.body.clientWidth;
	    frameHeight = document.body.clientHeight;
    }

    VxoProgressPanel = document.createElement("div");
    
    with (VxoProgressPanel.style)
    {
        position = "absolute";
        color = "#666699";
        backgroundColor = "white";
        fontFamily = "tahoma, arial";
        fontWeight = "bold";
        fontSize = "14px";
        border = "5px dotted #cccccc";
	    display = "inline";
	    visibility = "hidden";
	    padding = "5px";
	    margin = "0px";
	    overflow = "hidden";
	    zIndex = 10000;
	    opacity = 0.7;
	    filter = 'alpha(opacity=' + 70 + ')';
	 }
	 
	 VxUpdateProgressPanel(0);
	 
	 // Insert at start of body, or risk phantom scrollbar appearing on IE
	 //	 
	 document.body.insertBefore(VxoProgressPanel, document.body.childNodes[0]);
	 
    with (VxoProgressPanel.style)
    {
	    //top = Math.floor( frameHeight/2 - GetElementHeight(VxoProgressPanel)/2 )+"px";
	    //left = Math.floor( frameWidth/2 - GetElementWidth(VxoProgressPanel)/2 )+"px";
	    left = top = 0;
	    visibility = "visible";  
    }	 
}

function VxDestroyProgressPanel()
{
    VxoProgressPanel.parentNode.removeChild(VxoProgressPanel);
}

function VxUpdateProgressPanel(perc)
{
    VxoProgressPanel.innerHTML = "&nbsp;VisionX editable skin technology loading... " + perc + "%&nbsp;";
}

/// Functions borrowed from other modules, which may not have been loaded yet
///

function GetElementHeight(e)
{
	if (e.ownerDocument && e.ownerDocument.getBoxObjectFor)
	{
	    return e.ownerDocument.getBoxObjectFor(e).height;
	}
	return e.offsetHeight;
}

function GetElementWidth(e)
{
	if (e.ownerDocument && e.ownerDocument.getBoxObjectFor)
	{
		return e.ownerDocument.getBoxObjectFor(e).width;
	}
	return e.offsetWidth;
}

function loadjscssfile(filename, filetype)
{
    if (filetype == "js")
    { //if filename is a external JavaScript file
        var fileref = document.createElement('script')
        fileref.setAttribute("type", "text/javascript")
        fileref.setAttribute("src", filename)
    }
    else if (filetype == "css")
    { //if filename is an external CSS file
        var fileref = document.createElement("link")
        fileref.setAttribute("rel", "stylesheet")
        fileref.setAttribute("type", "text/css")
        fileref.setAttribute("href", filename)
    }
    if (typeof fileref != "undefined")
        document.getElementsByTagName("head")[0].appendChild(fileref)
}
