/**
 * $Id:$
 * vim: expandtab ts=8 sw=8
 *
 * @author Scott Eade, PolicyPoint Technologies Pty. Ltd.
 * @copyright Copyright 2005-2008, PolicyPoint Technologies Pty. Ltd.
 * @author Mariusz Pêkala, iDelfi Polska sp. z o.o.
 * @copyright Copyright 2008, iDelfi Polska sp. z o.o.
 * @license LGPL
 */

(function() {
        // Load plugin specific language pack
        tinymce.PluginManager.requireLangPack('liststyle');

        tinymce.create('tinymce.plugins.ListStylePlugin', {
                /**
                * Initializes the plugin, this will be executed after the plugin has been created.
                * This call is done before the editor instance has finished it's initialization so use the onInit event
                * of the editor instance to intercept that event.
                *
                * @param {tinymce.Editor} ed Editor instance that the plugin is initialized in.
                * @param {string} url Absolute URL to where the plugin is located.
                */
                init : function(ed, url) {
                        // Register the command so that it can be invoked by using tinyMCE.activeEditor.execCommand('mceExample');
                        ed.addCommand('mceListStyle', function(ui, v) {
                                var listStyleType = '', listStart, isIE = tinyMCE.isIE;
                                // ed.selection.getNode(); => selectedElement - common ancestor
                                var listNode = ed.plugins.liststyle._findList(ed, ed.selection.getNode() );
                                if( listNode ) {
                                        listStyleType = listNode.style.listStyleType;
                                        if( listNode.nodeName.toLowerCase() == 'ol' ) {
                                                listStart = parseInt(listNode.start);
                                                if( listStart < 1) { listStart = 1; }

                                                ed.windowManager.open({
                                                                file : url + '/liststyle_ol.htm',
                                                                width : 340 + parseInt(ed.getLang('liststyle.delta_width', 0)),
                                                                height : 300 + (isIE ? 40 : 0) + parseInt(ed.getLang('liststyle.delta_height', 0)),
                                                                inline : 1
                                                        }, {
                                                                plugin_url : url, // Plugin absolute URL
                                                                listStyleType : listStyleType,
                                                                listNode : listNode,
                                                                listStart : listStart,
                                                                classAttr : listNode.className,
                                                                isIE : isIE
                                                        }
                                                );
                                        } else {
                                                ed.windowManager.open({
                                                                file : url + '/liststyle_ul.htm',
                                                                width : 340 + parseInt(ed.getLang('liststyle.delta_width', 0)),
                                                                height : 300 + (isIE ? 40 : 0) + parseInt(ed.getLang('liststyle.delta_height', 0)),
                                                                inline : 1
                                                        }, {
                                                                plugin_url : url, // Plugin absolute URL
                                                                listStyleType : listStyleType,
                                                                listNode : listNode,
                                                                classAttr : listNode.className,
                                                                isIE : isIE
                                                        }
                                                );
                                        }
                                }
                        });

                        ed.onInit.add(function() {
                                if (ed && ed.plugins.contextmenu) {
                                        ed.plugins.contextmenu.onContextMenu.add(function(th, m, e) {
                                                if( ed.plugins.liststyle._findList(ed,ed.selection.getNode() ) ) {
                                                        m.add({title : 'liststyle.desc', cmd : 'mceListStyle', icon : 'liststyle', ui : true});
                                                }
                                        });
                                }
                        });

                        // Register the button
                        ed.addButton('liststyle', {
                                title : 'liststyle.desc',
                                cmd : 'mceListStyle',
                                image : url + '/img/liststyle.gif'
                        });

                        // Enable/disable the button depending on the current selection element
                        // It looks like the node is the same as selection.getNode - it means, a common ancestor of
                        // all selected nodes.
                        ed.onNodeChange.add(function(ed, cm, node) {
                                cm.setDisabled('liststyle', ed.plugins.liststyle._findList(ed,node) == null);
                        });
                },


                /**
                * Helper function: find a list element (<ol> or <ul>) being somewhere
                * amongst parents of given element.
                */
                _isList : function(node) {
                        if( node == null )
                                return null;
                        return node.nodeName == 'OL' || node.nodeName == 'UL';
                },

                _findList : function(ed, node) {
                        if( ed.plugins.liststyle._isList(node) ) {
                                return node;
                        }
                        return ed.dom.getParent( node, ed.plugins.liststyle._isList );
                },


                /**
                * Returns information about the plugin as a name/value array.
                * The current keys are longname, author, authorurl, infourl and version.
                *
                * @return {Object} Name/value array containing information about the plugin.
                */
                getInfo : function() {
                        return {
                                longname : 'ListStyle',
                                author : 'PolicyPoint Technologies Pty. Ltd.',
                                authorurl : 'http://policypoint.net/',
                                infourl : 'http://policypoint.net/tinymce/docs/plugin_liststyle.html',
                                version : "3.1-alfa"
                        };
                }
        });

        // Register plugin
        tinymce.PluginManager.add('liststyle', tinymce.plugins.ListStylePlugin);
})();
