/*
Copyright (c) 2003-2010, CKSource - Frederico Knabben. All rights reserved.
For licensing, see LICENSE.html or http://ckeditor.com/license
*/

(function () {
    (function () {
        var a = { id: { name: 'id' }, src: { name: 'src' }, width: { name: 'width' }, height: { name: 'height' }, autostart: { name: 'autostart'} };

        function b(e) {
            var i = this;
            var f = a[i.id];
            if (!f) return;
            var g = i instanceof CKEDITOR.ui.dialog.checkbox, h = f;
            if (e.getAttribute(h.name)) {
                value = e.getAttribute(h.name);
                if (i.id == 'src') if (value.indexOf('player_flv_maxi.swf') > 0) value = e.getAttribute('FlashVars').replace('file=', '').replace('&showplayer=always&usefullscreen=true&autostart=true', '');
                if (g) i.setValue(value.toLowerCase() == 'true'); else i.setValue(value);
                return;
            } else if (g) i.setValue(!!h['default']);
        }

        ;
        function c(e) {
            var l = this;
            var f = a[l.id];
            if (!f) return;
            var g = l.getValue() === '', h = l instanceof CKEDITOR.ui.dialog.checkbox, i = f;
            value = l.getValue();
            if (l.id == 'src') {
                var j = d(value);
                if (j == 'flv' || j == 'fla') if (j == 'flv') {
                    if (g || h && value === i['default']) e.removeAttribute(i.name); else {
                        var k = CKEDITOR.plugins.get('media').path + 'swf/player_flv_maxi.swf';
                        e.setAttribute('allowscriptaccess', 'always')
                        e.setAttribute('allowFullScreen', 'true');
                        e.setAttribute(i.name, k);
                        e.setAttribute('FlashVars', 'file=' + value + '&showplayer=always&usefullscreen=true&autostart=true');
                        return;
                    }
                } else if (value.indexOf('youtube.com/') > 0) value = value.replace(/youtube\.com\/watch\?v=/i, 'youtube.com/v/');
            }
            if (g || h && value === i['default']) e.removeAttribute(i.name); else e.setAttribute(i.name, value);
        }

        ;
        function d(e) {
            var f = e.match(/\.(avi|asf|flv|mov|mp3|mp4|mpg|mpeg|qt|swf|wma|wmv)$/i);
            if (f != null && f.length && f.length > 0) f = f[1]; else if (e.indexOf('youtube.com/') > 0) f = 'swf'; else f = '';
            return f;
        }

        ;
        CKEDITOR.dialog.add('media', function (e) {
            var f, g = '<div>' + CKEDITOR.tools.htmlEncode(e.lang.common.preview) + '<br>' + '<div id="FlashPreviewLoader" style="display:none"><div class="loading">&nbsp;</div></div>' + '<div id="FlashPreviewBox"></div></div>';
            return { title: '媒体属性', minWidth: 420, minHeight: 310, onShow: function () {
                var k = this;
                k.fakeImage = k.embedNode = null;
                f = new CKEDITOR.dom.element('embeded', e.document);
                var h = k.getSelectedElement();
                if (h && h.getAttribute('_cke_real_element_type') && h.getAttribute('_cke_real_element_type') == 'media') {
                    k.fakeImage = h;
                    var i = e.restoreRealElement(h), j = null;
                    if (i.getName() == 'cke:embed') j = i;
                    k.embedNode = j;
                    k.setupContent(j, h);
                }
            }, onOk: function () {
                var k = this;
                var h = null;
                if (!k.fakeImage) h = CKEDITOR.dom.element.createFromHtml('<cke:embed></cke:embed>', e.document); else h = k.embedNode;
                var i = {};
                k.commitContent(h, i);
                var j = e.createFakeElement(h, 'cke_media', 'media', true);
                j.setStyles(i);
                if (k.fakeImage) {
                    j.replace(k.fakeImage);
                    e.getSelection().selectElement(j);
                } else e.insertElement(j);
            }, onHide: function () {
                if (this.preview) this.preview.setHtml('');
            }, contents: [
                { id: 'info', label: e.lang.common.generalTab, accessKey: 'I', elements: [
                    { type: 'vbox', padding: 0, children: [
                        { type: 'html', html: '<span>' + CKEDITOR.tools.htmlEncode(e.lang.common.url) + '</span>' },
                        { type: 'hbox', widths: ['280px', '110px'], align: 'right', children: [
                            { id: 'src', type: 'text', label: '', validate: CKEDITOR.dialog.validate.notEmpty('源文件地址不能为空'), setup: b, commit: c, onLoad: function () {
                                var h = this.getDialog(), i = function (j) {
                                    var k = j, l = d(j);
                                    if (l == 'flv' || l == 'fla') if (l == 'flv') k = CKEDITOR.plugins.get('media').path + 'swf/player_flv_maxi.swf'; else if (k.indexOf('youtube.com/') > 0) j = j.replace(/youtube\.com\/watch\?v=/i, 'youtube.com/v/');
                                    var m = '<embed height="100%" width="100%" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowscriptaccess="always" src="' + CKEDITOR.tools.htmlEncode(k);
                                    if (k.indexOf('player_flv_maxi.swf') > 0) m += '" flashVars="file=' + j + '&showplayer=always&showiconplay=true';
                                    m += '" autostart="true"></embed>';
                                    h.preview.setHtml(m);
                                };
                                h.preview = h.getContentElement('info', 'preview').getElement().getChild(3);
                                this.on('change', function (j) {
                                    if (j.data && j.data.value) i(j.data.value);
                                });
                                this.getInputElement().on('change', function (j) {
                                    i(this.getValue());
                                }, this);
                            }
                            }
                        ]
                        }
                    ]
                    },
                    { type: 'hbox', widths: ['25%', '25%', '25%', '25%', '25%'], children: [
                        { type: 'text', id: 'width', style: 'width:95px', label: e.lang.flash.width, validate: CKEDITOR.dialog.validate.integer(e.lang.flash.validateWidth), setup: function (h, i) {
                            b.apply(this, arguments);
                            if (i) {
                                var j = parseInt(i.$.style.width, 10);
                                if (!isNaN(j)) this.setValue(j);
                            }
                        }, commit: function (h, i) {
                            c.apply(this, arguments);
                            if (this.getValue()) i.width = this.getValue() + 'px';
                        }
                        },
                        { type: 'text', id: 'height', style: 'width:95px', label: e.lang.flash.height, validate: CKEDITOR.dialog.validate.integer(e.lang.flash.validateHeight), setup: function (h, i) {
                            b.apply(this, arguments);
                            if (i) {
                                var j = parseInt(i.$.style.height, 10);
                                if (!isNaN(j)) this.setValue(j);
                            }
                        }, commit: function (h, i) {
                            c.apply(this, arguments);
                            if (this.getValue()) i.height = this.getValue() + 'px';
                        }
                        },
                        { type: 'vbox', padding: 0, children: [
                            { type: 'html', html: '选项' },
                            { type: 'checkbox', id: 'autostart', label: '自动播放', 'default': true, setup: b, commit: c }
                        ]
                        }
                    ]
                    },
                    { type: 'vbox', children: [
                        { type: 'html', id: 'preview', style: 'width:100%;', html: g }
                    ]
                    }
                ]
                },
                { id: 'Upload', hidden: true, filebrowser: 'uploadButton', label: e.lang.common.upload, enabledUpload: e.config.flashUpload, elements: [
                    { type: 'file', id: 'upload', label: e.lang.common.upload, size: 38 },
                    { type: 'fileButton', id: 'uploadButton', filebrowser: { action: 'QuickUpload', target: 'info:src', params: { type: 'flash', modelId: e.config.modelId, nodeId: e.config.nodeId, moduleName: e.config.moduleName, fieldName: e.config.fieldName, wordPic: e.config.wordPic, foreground: e.config.foreground, watermark: e.config.watermark, thumbnail: e.config.thumbnail, fileRecord: e.config.fileRecord, departmentUploader: e.config.departmentUploader} }, label: e.lang.common.uploadSubmit, 'for': ['Upload', 'upload'] }
                ]
                },
                { id: 'browseUploaded', label: '从已上传中选择', hidden: !e.config.imageUpload, elements: [
                    { type: 'iframe', src: CKEDITOR.basePath.replace('/Editor/','/') + (e.config.foreground ? 'user/Accessories/ShowUploadFiles.aspx' : e.config.manageDir+'/Accessories/ShowUploadFiles.aspx'), params: { type: 'flash', CKEditor: e.name, CKEditorFuncNum: e._.filebrowserFn }, width: '100%', height: '300px', onLoad: function () {
                        var h = this.getDialog(), i = h.getParentEditor();
                        i._.filebrowserSe = h.getContentElement('Upload', 'upload');
                    }
                    }
                ]
                }
            ]
            };
        });
    })();
})();
