﻿

function CloseModalContent() {
    if (confirm('To save changes?')) {
        tinyMCE.triggerSave(false, false);
        $("#mainContent" + contentKey).load(saveAction, { content: $('#pageContent' + contentKey).val(),
            contentKey: $("#contentKey" + contentKey).val()
        }, function(responseText, textStatus, XMLHttpRequest) { });
    }
    $(this).dialog('close');
    tinyMCE.execCommand('mceRemoveControl', false, 'pageContent' + contentKey);
    $(this).html('');
}
function InitializeModalContent(contentKey, saveAction, saveAndContinueAction, reloadAction, deleteAction,language) {
    $('#modalContent' + contentKey).dialog(
            {
                bgiframe: true,
                autoOpen: false,
                modal: true,
                width: 700,
                close: function() {
                    $("#mainContent" + contentKey).load(reloadAction, { contentKey: $("#contentKey" + contentKey).val()
                    }, function(responseText, textStatus, XMLHttpRequest) {
                        InitializeTabs("tabs" + contentKey, 1);
                    });
                    tinyMCE.execCommand('mceRemoveControl', false, 'pageContent' + contentKey);
                    $(this).html('');
                },
                buttons: { 
                    'Delete': function() {
                    $.ajaxSetup({ cache: false });
                    if (confirm('Delete this draft?')) {
                        tinyMCE.triggerSave(false, false);
                        $.post(deleteAction,
                        { content: $('#pageContent' + contentKey).val(),
                            contentKey: $("#contentKey" + contentKey).val(),
                            language:language
                        }, function(json) { $('#modalContent' + contentKey).dialog('close'); });
                    }
                    $('#modalContent' + contentKey).dialog('close');
                    tinyMCE.execCommand('mceRemoveControl', false, 'pageContent' + contentKey);
                    $(this).html('');
                    },
                    'Close': function() {
                        $.ajaxSetup({ cache: false });
                        if (confirm('Save changes?')) {
                            tinyMCE.triggerSave(false, false);
                            $.post(saveAction,
                        { content: $('#pageContent' + contentKey).val(),
                            contentKey: $("#contentKey" + contentKey).val(),
                            language:language
                        }, function(json) { $('#modalContent' + contentKey).dialog('close'); });
                        }
                        $('#modalContent' + contentKey).dialog('close');
                        tinyMCE.execCommand('mceRemoveControl', false, 'pageContent' + contentKey);
                        $(this).html('');
                    },
                    'Save and close': function() {
                        tinyMCE.triggerSave(false, false);
                        $.ajaxSetup({ cache: false });
                        $.post(saveAction,
                        { content: $('#pageContent' + contentKey).val(),
                            contentKey: $("#contentKey" + contentKey).val(),
                            language:language
                        }, function(json) { $('#modalContent' + contentKey).dialog('close'); });

                    },
                    'Save': function() {
                        tinyMCE.triggerSave(false, false);
                        $.ajaxSetup({ cache: false });
                        $.post(saveAndContinueAction,
                        { content: $('#pageContent' + contentKey).val(),
                            contentKey: $("#contentKey" + contentKey).val(),
                            language:language
                        }, function(json) { });
                    }
                }
            });
}

function Publish(contentKey, action) {
    if (confirm('Are you sure you want to publish the current version?')) {
        $("#mainContent" + contentKey).load(action,
            { contentKey: $("#contentKey" + contentKey).val() },
             function(responseText, textStatus, XMLHttpRequest) {
                 InitializeTabs("tabs" + contentKey, 0);
             });
    }
}
function CheckIn(contentKey, checkInAction) {
    $.ajaxSetup({ cache: false });
    if (confirm('Are you sure you want to return current version?')) {
        $("#mainContent" + contentKey).load(checkInAction,
          { content: $('#pageContent' + contentKey).html(), contentKey: $("#contentKey" + contentKey).val() }, function(responseText, textStatus, XMLHttpRequest) {
              InitializeTabs("tabs" + contentKey, 1);
          });
    }
}

function GetHistory(contentKey, action) {
    $.ajaxSetup({ cache: false });
    $("#history" + contentKey).load(action, { contentKey: $("#contentKey" + contentKey).val() }, function(responseText, textStatus, XMLHttpRequest) {
        InitializeHistoryGrid();
    });
}
function ShowEditMode(div, contentKey, action) {
    $.ajaxSetup({ cache: false });
    $("#" + div).load(action, { contentKey: contentKey }, function(responseText, textStatus, XMLHttpRequest) {
    });
}
function CloseEditMode(div, contentKey, action) {
    $.ajaxSetup({ cache: false });
    $("#" + div).load(action, { contentKey: contentKey }, function(responseText, textStatus, XMLHttpRequest) {
    });
}

function InitializeTabs(div, index) {
    $("#" + div).tabs({ selected: index });
}

function OpenEditContentDialog(contentKey, action) {
    $('#modalContent' + contentKey).html('');
    $.ajaxSetup({ cache: false });
    $.getJSON(action, { contentKey: contentKey }, function(json) {
        var dialogContent = '<div style= "padding-left:2px;">';

        dialogContent += '<input type="hidden" name="contentKey" value="' + contentKey + '" />';

        dialogContent += '<textarea cols="33" rows="5" name="note" id="pageContent';
        dialogContent += contentKey;
        dialogContent += '">';
        dialogContent += json.Content + '</textarea>';
        dialogContent += '</div>';
        dialogContent += '<input id="contentKey';
        dialogContent += contentKey;
        dialogContent += '" value="' + json.ContentKey + '" type="hidden"/> ';
        $('#modalContent' + contentKey).html(dialogContent);
        $('#modalContent' + contentKey).dialog('open');
        tinyMCE.execCommand('mceAddControl', false, 'pageContent' + contentKey);
    });


}