﻿

var editor;
function DeleteDiv(div) {

    PageMethods.Delete(div.id, onSucceed, onError);
}
function replaceDiv(div) {
    if (editor)
        editor.destroy();

    editor = CKEDITOR.replace(div);
    editor.config.contentsCss = '/Styles/Site.css';
    CKFinder.setupCKEditor(editor, { basePath: '/ckfinder/', rememberLastFolder: false });
    editor.on("instanceReady", function () {

        editor.on('key', function (e) {
            if (e.data.keyCode == 27) {
                e.cancel();
                e.editor.execCommand('discard');
            }
        });

        // overwrite the default save function
        editor.addCommand("save", {
            modes: { wysiwyg: 1, source: 1 },
            exec: function () {

                // get the editor content

                var theData = editor.getData();
                // PageMethods.Edit(div.id, theData, onSucceed, onError);
                var dataString = JSON.stringify({ Id: div.id, data: theData})

                $.ajax({
                    type: "POST",
                    url: "Default.aspx/Edit",
                    contentType: "application/json; charset=utf-8",
                    data: dataString,
                    dataType: "json",
                    success: onSucceed,
                    error: onError
                }); 

                return false;


            }
        });
    });
}


function onSucceed(result) {
    if (result == '-1') {
        alert('Fout bij het opslaan!');
    } else {
        location.reload(true)
    }
}
function onError(result) {
    alert(result.status + ' ' + result.statusText);


}

if (window.CKEDITOR) {
    (function () {
        var showCompatibilityMsg = function () {
            var env = CKEDITOR.env;

            var html = '<p><strong>Your browser is not compatible with CKEditor.</strong>';

            var browsers =
			{
			    gecko: 'Firefox 2.0',
			    ie: 'Internet Explorer 6.0',
			    opera: 'Opera 9.5',
			    webkit: 'Safari 3.0'
			};

            var alsoBrowsers = '';

            for (var key in env) {
                if (browsers[key]) {
                    if (env[key])
                        html += ' CKEditor is compatible with ' + browsers[key] + ' or higher.';
                    else
                        alsoBrowsers += browsers[key] + '+, ';
                }
            }

            alsoBrowsers = alsoBrowsers.replace(/\+,([^,]+), $/, '+ and $1');

            html += ' It is also compatible with ' + alsoBrowsers + '.';

            html += '</p><p>With non compatible browsers, you should still be able to see and edit the contents (HTML) in a plain text field.</p>';

            var alertsEl = document.getElementById('alerts');
            alertsEl && (alertsEl.innerHTML = html);
        };

        var onload = function () {
            // Show a friendly compatibility message as soon as the page is loaded,
            // for those browsers that are not compatible with CKEditor.
            if (!CKEDITOR.env.isCompatible)
                showCompatibilityMsg();
        };

        // Register the onload listener.
        if (window.addEventListener)
            window.addEventListener('load', onload, false);
        else if (window.attachEvent)
            window.attachEvent('onload', onload);
    })();
}

