// Start of script: OpenQuarters.WebQuarters.Modules.Core.Modules.Navigation.AdminScript.js
// Custom editor for navigation module
// Provides sortable pages as inline editing

jQuery.fn.cmsModuleEditor_Navigation = function(opts) {
    return this.each(function() {

        // TODO: Permission check for page ordering
        // At the moment any editor can try to reorder but will be prevented by the server's checks

        // Find each individual UL in the specified module and make its items sortable

        $(this).find('ul').each(function() {
            var thisRef = $(this);
            thisRef.sortable({
                containment: 'parent', // Ensures all levels can be ordered independently
                cursor: 'crosshair',
                start: function(event, ui) {
                    // Disable all links when sorting starts so that no accidental clicks can be made
                    // Stores the link's original URL in $(this).data('url')
                    $(ui.item).parent().find('a').each(function() {
                        $(this).data('url', $(this).attr('href'));
                        this.href = 'javascript:void(0);';
                    });
                },
                update: function(event, ui) {
                    // Set a timeout of 50ms so that jQuery has a chance to sort everything out
                    // Without this pause, the hrefs get set back too quickly and can cause accidental clicks
                    $(ui.item).animate({ width: $(ui.item).width() }, 50, function() {

                        // Create an array of page IDs in their new order
                        var pageOrdering = [];
                        $(ui.item).parent().find(' > li').each(function() {
                            // Page ID has been rendered as the rel attribute of each LI by the Navigation Module
                            pageOrdering.push($(this).attr('rel'));
                        });

                        // Work out what the parent page of this ordering set is by traversing up the LI chain
                        var parentId = 'HOME';
                        var parentLI = $(ui.item).parents('li:first');
                        if (parentLI && parentLI.attr('rel')) {
                            parentId = parentLI.attr('rel');
                        }
                        // TODO: Work out what happens when the module itself is contained with an LI - suspect breakage

                        // Send the parent page ID and the newly ordered page ID array to the SetOrder action
                        $.postJSON('/cms/pages/SetOrder', { parent: parentId, ordering: $.compactJSON(pageOrdering) }, function(json) {
                            if (json.success) {
                                // TODO: Inform user of workflow item in Inbox
                            }
                        });

                        // Set the links of URLs back so that the module functions as a menu again
                        $(ui.item).parent().parent().find('a').each(function() {
                            $(this).animate({ width: $(this).width() }, 50, function() {
                                this.href = $(this).data('url');
                            });
                        });
                    });
                }
            });
        });
    });
};
// End of script: OpenQuarters.WebQuarters.Modules.Core.Modules.Navigation.AdminScript.js

// Start of script: OpenQuarters.WebQuarters.Modules.Core.Modules.Content.AdminScript.js
//NicEdit event handlers
var cmsNicPanel = null;
var myNicEditor = null;

function nicEdit_InsertImage(src, alt, width, height) {
    if (!cmsNicPanel.im) {
        var tmp = 'javascript:nicImTemp();';
        cmsNicPanel.ne.nicCommand("insertImage", tmp);
        cmsNicPanel.im = cmsNicPanel.findElm('IMG', 'src', tmp);
    }
    if (cmsNicPanel.im) {
        cmsNicPanel.im.setAttributes({
            src: src,
            alt: alt
        });
        if (width.indexOf('px') == -1)
            width = width + 'px';
        if (height.indexOf('px') == -1)
            height = height + 'px';
        cmsNicPanel.im.setStyle({
            width: width,
            height: height
        });
    }
    cmsNicPanel.removePane();
}

function nicEdit_InsertFile(src, target) {
    if (!cmsNicPanel.ln) {
        var tmp = 'javascript:nicTemp();';
        cmsNicPanel.ne.nicCommand("createlink", tmp);
        cmsNicPanel.ln = cmsNicPanel.findElm('A', 'href', tmp);
    }
    if (cmsNicPanel.ln) {
        cmsNicPanel.ln.setAttributes({
            href: src,
            title: src,
            target: target
        });
    }
    cmsNicPanel.removePane();
}

var cmsCtrlPressed = false;

$(window).keydown(function(event) {
    if (event.ctrlKey) {
        cmsCtrlPressed = true;
    }
    return true;
});

$(window).keyup(function(event) {
    cmsCtrlPressed = false;
    return true;
});

// Content module editor

function cmsContentModuleEditor_DefaultSave(moduleElement, nicEditor) {
    try {
        if ($(moduleElement).data('config').Content != nicEditor.getContent()) {
            $(moduleElement).data('config').Content = nicEditor.getContent();
            cmsSaveDraft();
        }
    } catch (e) {
    }
}

function cmsContentModuleEditor_DefaultSetup(moduleElement, nicEditor) {
    $(moduleElement).data('config').Content = nicEditor.getContent();
}

var cmsContentModuleEditor_CheckTargetCallback = null;
$().ready(function() {
    $(window).click(function(evt) {
        if (!evt && window.event) {
            evt = window.event;
        }
        if (evt) {
            var lastClickInNicEditPanel = $(evt.target).parents('.nicEdit-pane').length > 0 || $(evt.target).parents('.nicEdit-panel').length > 0;
            if (cmsContentModuleEditor_CheckTargetCallback != null) {
                cmsContentModuleEditor_CheckTargetCallback(lastClickInNicEditPanel);
                cmsContentModuleEditor_CheckTargetCallback = null;
            }
        }
        return true;
    });
});

jQuery.fn.cmsContentModuleEditor = function(opts) {
    return this.each(function() {
        if (!$(this).hasClass('cmsInherited')) {
            $('#cmsNicPanelContainer').remove();
            cmsNicPanel = $('<div id="cmsNicPanelContainer"><div id="cmsNicPanel"></div></div>').appendTo($('body'));
            myNicEditor = new nicEditor({ fullPanel: true }).panelInstance('cmsNicPanel');
            $('#cmsNicPanelContainer').hide(0);

            if (opts.onSetup == null || opts.onSetup == undefined) {
                opts = $.extend(opts, { onSetup: cmsContentModuleEditor_DefaultSetup });
            }

            if (opts.onSave == null || opts.onSave == undefined) {
                opts = $.extend(opts, { onSave: cmsContentModuleEditor_DefaultSave });
            }

            $(this).click(function() {

                $('#cmsNicPanelContainer').show(0).css('top', parseInt((parseInt($(window).scrollTop()) + 47)) + 'px');
                $('#cmsNicPanelContainer > div').width($('body').width());
                $('.nicEdit-panelContain').css('border-right', '0px').css('border-left', '0px');

                if (!nicEditors.findEditor(this.id)) {
                    $('#cmsNicPanelContainer').show(0).css('top', '47px').scrollFollow({ speed: 250, offset: 47 });
                    try {
                        var contentModuleNicEditor = myNicEditor.addInstance(this.id);
                        opts.onSetup(this, nicEditors.findEditor(this.id));
                        this.focus();
                    }
                    catch (e) { }
                }
            }).blur(function() {
                // Blur happens before click, so we need to find out if the click was in a NicEdit pane

                var cmsContentModuleEditor_CheckTargetCallbackElement = this;
                var cmsContentModuleEditor_CheckTargetCallbackOnSave = opts.onSave;
                cmsContentModuleEditor_CheckTargetCallback = function(r) {
                    if (!r) {
                        $('#cmsNicPanelContainer').hide(0);
                        cmsContentModuleEditor_CheckTargetCallbackOnSave(cmsContentModuleEditor_CheckTargetCallbackElement, nicEditors.findEditor(cmsContentModuleEditor_CheckTargetCallbackElement.id));
                        myNicEditor.removeInstance(cmsContentModuleEditor_CheckTargetCallbackElement.id);
                    }
                };
                
                // Hack to force IE to call method
                if (!jQuery.support.noCloneEvent)
                    cmsContentModuleEditor_CheckTargetCallback(false);
            });
        }
    });
};// End of script: OpenQuarters.WebQuarters.Modules.Core.Modules.Content.AdminScript.js

