// $Id: kissy.js,v 1.1.6.2 2010/02/13 23:58:41 sun Exp $
(function($) {

/**
 * Attach this editor to a target element.
 */
Drupal.wysiwyg.editor.attach.kissy = function(context, params, settings) {
	
KISSY.ready(function(S) {
    S.use('editor', function(S) {
        var KE = KISSY.Editor;

        window.editor = KE('#' + params.field, {
            attachForm:true,
            baseZIndex:10000,

            pluginConfig: {
                "image":{
                    upload:{
                        serverUrl:"/sites/all/libraries/kissy/editor/php/upload.php",
                        surfix:"png,jpg,jpeg,gif",
                        fileInput:"file",
                        sizeLimit:2000,//k
                        extraHtml:"<p style='margin-top:10px;'><input type='checkbox' id='ke_img_up_watermark_1' checked='checked'> 图片加水印，防止别人盗用</p>"
                    }
                },
                "font-size":{
                    items:[
                        {
                            value:"14px",
                            attrs:{
                                style:'position: relative; border: 1px solid #DDDDDD; margin: 2px; padding: 2px;'
                            },
                            name:"" +
                                    " <span style='font-size:14px'>标准</span>" +
                                    "<span style='position:absolute;top:1px;right:3px;'>14px</span>"
                        },
                        {
                            value:"16px",
                            attrs:{
                                style:'position: relative; border: 1px solid #DDDDDD; margin: 2px; padding: 2px;'
                            },
                            name:"" +
                                    " <span style='font-size:16px'>大</span>" +
                                    "<span style='position:absolute;top:1px;right:3px;'>16px</span>"
                        },
                        {
                            value:"18px",
                            attrs:{
                                style:'position: relative; border: 1px solid #DDDDDD; margin: 2px; padding: 2px;'
                            },
                            name:"" +
                                    " <span style='font-size:18px'>特大</span>" +
                                    "<span style='position:absolute;top:1px;right:3px;'>18px</span>"
                        },
                        {
                            value:"20px",
                            attrs:{
                                style:'position: relative; border: 1px solid #DDDDDD; margin: 2px; padding: 2px;'
                            },
                            name:"" +
                                    " <span style='font-size:20px'>极大</span>" +
                                    "<span style='position:absolute;top:1px;right:3px;'>20px</span>"
                        }
                    ],
                    width:"115px"
                }
                ,"font-family":{
                    items:[
                        {name:"宋体",value:"SimSun"},
                        {name:"黑体",value:"SimHei"},
                        {name:"楷体",value:"KaiTi_GB2312"},
                        {name:"微软雅黑",value:"Microsoft YaHei"},
                        {name:"Times New Roman",value:"Times New Roman"},
                        {name:"Arial",value:"Arial"},
                        {name:"Verdana",value:"Verdana"}
                    ]
                }
                ,"video":{
                    urlCfg:[
                        {
                            reg:/tudou\.com/i,
                            url:"http://bangpai.taobao.com/json/getTudouVideo.htm?" +
                                    "url=@url@&callback=@callback@"//"&rand=@rand@"
                        }
                    ],
                    urlTip:"请输入视频系统、优酷网、土豆网、酷6网的视频播放页链接...",
                    providers:[
                       {
                           reg:/orthonline\.com.cn/i,
                           width:480,
                           height:400,
                           detect:function(url) {
                               var m = url.match(/v=([^.]+)$/);
                               if (m) {
                                   return "http://video.orthonline.com.cn/player/pak_player/pakplayer.swf?config="
                                   		  + "http%3A%2F%2Fvideo.orthonline.com.cn%2Fplayer%2Fpak_player%2Fembed_player.php%3Fvid%3D"
                                   		  + m[1]
                                   		  + "%26json%3Dtrue%26autoplay%3DYes";
                               }
                               
                           }
                       },
                        {
                            reg:/youku\.com/i,
                            width:480,
                            height:400,
                            detect:function(url) {
                                var m = url.match(/id_([^.]+)\.html$/);
                                if (m) {
                                    return "http://player.youku.com/player.php/sid/" + m[1] + "/v.swf";
                                }
                                m = url.match(/v_playlist\/([^.]+)\.html$/);
                                if (m) {
                                    return;
                                    //return "http://player.youku.com/player.php/sid/" + m[1] + "/v.swf";
                                }
                                return url;
                            }
                        },
                        {
                            reg:/tudou\.com/i,
                            width:480,
                            height:400,
                            detect:function(url) {
                                return url;
                            }
                        },
                        {
                            reg:/ku6\.com/i,
                            width:480,
                            height:400,
                            detect:function(url) {
                                var m = url.match(/show[^\/]*\/([^.]+)\.html$/);
                                if (m) {
                                    return "http://player.ku6.com/refer/" + m[1] + "/v.swf";
                                }
                                return url;
                            }
                        }/*,
                         {
                         reg:/taobaocdn\.com/i,
                         width:480,
                         height:400,
                         detect:function(url) {
                         return url;
                         }
                         }*/
                    ]
                },
                "draft":{
                    interval:5,
                    limit:10,
                    helpHtml:  "<div " +
                            "style='width:200px;'>" +
                            "<div style='padding:5px;'>草稿箱能够自动保存您最新编辑的内容," +
                            "如果发现内容丢失" +
                            "请选择恢复编辑历史</div></div>"
                },
                "resize":{
                    direction:["y"]
                },
                "multi-upload":{
                    serverUrl:"/sites/all/libraries/kissy/editor/php/multi-upload.php",
                    //previewSuffix:"_60x60",
                    fileInput: "file",
                    previewWidth:"80px",
                    sizeLimit:2000//k,
                    ,numberLimit:15,
                    extraHtml:"<p style='margin-top:10px;'>" +
                            "<input type='checkbox' " +
                            "style='vertical-align:middle;margin:0 5px;' " +
                            "id='ke_img_up_watermark_2'>" +
                            "<span style='vertical-align:middle;'>图片加水印，防止别人盗用</span></p>"
                },
            }
        }).use("htmldataprocessor,enterkey,clipboard," +
                "sourcearea,checkbox-sourcearea," +
                "separator," +
                "undo,separator,font,color,separator," +
                "list,indent,justify,separator,link,image,multi-upload,video,smiley," +
                "separator,table,resize,draft,maximize"
                , function() {
                    var self = this,
                            htmlDataProcessor = self.htmlDataProcessor,
                            dataFilter = htmlDataProcessor && htmlDataProcessor.dataFilter;
                    if (dataFilter) {
                        dataFilter.addRules({
                            attributes:{
                                style:function(value) {
                                    value = value.replace(/background-color\s*:[^;]+(;|$)/g, "")
                                            .replace(/background\s*:[^;]+(;|$)/g, "");
                                    if (!value) return false;
                                    return value;
                                }
                            }
                        });
                    }
                });


    });
});
};

/**
 * Detach a single or all editors.
 *
 * See Drupal.wysiwyg.editor.detach.none() for a full desciption of this hook.
 */
Drupal.wysiwyg.editor.detach.kissy = function(context, params) {
  
  if (typeof params != 'undefined') {
      
      //此处如果一个页面有两个editor，就不行了，应该是遍历一下
      window.editor.destroy();
      window.editor = null;
  }

};

})(jQuery);
