﻿function ReceiveServerDataCmsTextBox(rvalue, context)
{
    if (context == 'setLanguage')
    {
        eval(rvalue);
    }
}

function setLanguage(langSwitchId, codeFldId, textId)
{
    var langSwitch = document.getElementById(langSwitchId);
    var codeFld = document.getElementById(codeFldId);
    var text = document.getElementById(textId);
    if (langSwitch)
    {
        var langCode = langSwitch.getAttribute('lang');
        var langText = langSwitch.getAttribute('description');
        if (codeFld)
            codeFld.value = langCode;
        if (text)
            text.innerHTML = langText;
        var data = {
            operation: 'select',
            language: langCode
        };
        CallServerCmsTextBox(Object.toJSON(data), 'setLanguage');
    }
}

function updateData(langFldId, titleFldId, textFldId, textControlClientID, titleControlClientID)
{
    var langFld = document.getElementById(langFldId);
    var titleFld = document.getElementById(titleFldId);
    var textFld = document.getElementById(textFldId);

    if (typeof (FCKeditorAPI) != 'undefined')
    {
        var oEditor = FCKeditorAPI.GetInstance(textFldId);
        if (oEditor)
        {
            var enc = oEditor.Config.HtmlEncodeOutput;
            oEditor.Config.HtmlEncodeOutput = false;
            oEditor.UpdateLinkedField();
            oEditor.Config.HtmlEncodeOutput = enc;
        }
    }

    var data = {
        operation: 'update',
        language: langFld ? langFld.value : null,
        title: titleFld ? titleFld.value.toJSON() : null,
        fulltext: textFld ? textFld.value.toJSON() : null
    };

    CallServerCmsTextBox(Object.toJSON(data), 'updateData');

    if (langFld.value == currentWebsiteLanguage)
    {
        var parentWindow = window.parent;
        if (textFld && textFld.value && parentWindow.globalUpdatedText)
            parentWindow.globalUpdatedText.innerHTML = unescape(textFld.value);
        if (titleFld && titleFld.value && parentWindow.globalUpdatedTitle)
            parentWindow.globalUpdatedTitle.innerHTML = unescape(titleFld.value);
        if (typeof (parentWindow.onCmsTextUpdated) == "function")
            parentWindow.onCmsTextUpdated(textFld.value, titleFld.value);
    }
}

function CloseDialog()
{
    hideCmsDialog(window.parent.document);
}

function webToolsUpdateCmsText(sender, alias, fields, updatedTextId, updatedTitleId)
{
    try
        {
        window.globalUpdatedText = document.getElementById(updatedTextId);
        window.globalUpdatedTitle = document.getElementById(updatedTitleId);
        showCmsDialog(applicationVirtualPath + 'CmsTextBoxEditDialog.aspx?alias=' + escape(alias) + '&fields=' + fields, 720, 520);
        }
    catch (e)
   {
   }

}

function showCmsDialog(url, w, h, callback) {
    //based on http://feeds.feedburner.com/~r/netSlave/~3/341904655/post.aspx

    //First, we need to add the semi-transparent div to lie on top of the entire page. This is done like so:
    var width = document.documentElement.clientWidth + document.documentElement.scrollLeft;
    var height = document.documentElement.clientHeight + document.documentElement.scrollTop;
    var clientHeight = document.documentElement.clientHeight;
    var clientWidth = document.documentElement.clientWidth;
    var layer = document.createElement('div');
    layer.style.zIndex = 3000;
    layer.id = 'cmsDialog_layer';
    layer.style.position = 'absolute';
    layer.style.top = '0px';
    layer.style.left = '0px';
    layer.style.height = document.documentElement.scrollHeight + 'px';
    layer.style.width = width + 'px';
    layer.style.backgroundColor = 'black';
    layer.style.opacity = '.6';
    layer.style.filter += ("progid:DXImageTransform.Microsoft.Alpha(opacity=60)");
    document.body.appendChild(layer);
    //Next we add a div in the middle of the page on top of the semi-transparent layer. 
    var div = document.createElement('div');
    div.style.zIndex = 3001;
    div.id = 'cmsDialog_box';
    div.style.position = (navigator.userAgent.indexOf('MSIE 6') > -1) ? 'absolute' : 'fixed';
    div.style.top = (clientHeight / 2) - (h / 2) + 'px';
    div.style.left = (clientWidth / 2) - (w / 2) + 'px';
    div.style.height = h+'px';
    div.style.width = w+'px';
    div.style.backgroundColor = 'white';
    div.style.border = '2px solid silver';
    div.style.padding = '10px';
    document.body.appendChild(div);
    //And finally we put some text and a link that closes the popup when clicked: 
    var iframe = document.createElement('iframe');
    iframe.src = url;
    iframe.width = "100%";
    iframe.height = "100%";
    div.appendChild(iframe);
    document.cmsDialogClosedCallback = callback;
}

function hideCmsDialog(xdoc) {
    //xdoc is the document object of window containing dialog
    xdoc.body.removeChild(xdoc.getElementById('cmsDialog_layer'));
    xdoc.body.removeChild(xdoc.getElementById('cmsDialog_box'));
    if (typeof (xdoc.cmsDialogClosedCallback) === 'function')
    {
        xdoc.cmsDialogClosedCallback();
        xdoc.cmsDialogClosedCallback = null;
    }
}
