﻿(function($){
    $.fn.web4sEditor = function(maxNumber,showType){
        var editorId = $(this).attr('id');
        
        //提示语
        var wordTips = '<span class="tips"><span>还可以输入<strong id="'+editorId+'WordTips">'+maxNumber+'</strong>字</span></span>';
        
        if(showType==2){
            wordTips = '<span class="tips"><span id="'+editorId+'WordTips">0</span>/'+maxNumber+'</span>';
        }

        //从配置文件读取表情
        //var editorFace = '<input type="button" id="'+editorId+'InsertFace" class="insertFaceWrap" value="表情"/>'+ 
        var editorFace = '<a id="'+editorId+'InsertFace" class="insertFaceWrap" >表情</a>';

        //编辑器输入框
        var editorTextare = '<textarea name="" cols="50" rows="5" id="'+editorId+'Content"></textarea>';

        $(this).html(showType==1?(wordTips+editorTextare+editorFace):(editorTextare+editorFace+wordTips));

        // 统计字数
        var count = function(){
            var editorArea = $('#'+editorId+'Content');
//          editorArea.removeClass("text");
            var contentLength = Math.ceil(editorArea.val().replace(/[^\x00-\xff]/g,"aa").length/2);

            if(showType==1){
                if(contentLength <= maxNumber){
                    $('#'+editorId).find('.tips').html('<span>还可以输入<strong id="'+editorId+'WordTips">'+(maxNumber - contentLength)+'</strong>字</span>');
                }else{
                    $('#'+editorId).find('.tips').html('<span style="color:#CC0000;">超出<strong id="'+editorId+'WordTips">'+(contentLength - maxNumber)+'</strong>字</span>');                
                }
            }else{
                if(contentLength <= maxNumber){
                    $('#'+editorId+'WordTips').html(contentLength);
                }else{
                    $('#'+editorId+'WordTips').html('<font color="red">'+contentLength+'</font>');
                }
            }
        };
        
        var mouseover = function(){
            var editorArea = $('#'+editorId+'Content');
            
            var contentLength = Math.ceil(editorArea.val().replace(/[^\x00-\xff]/g,"aa").length/2);
            if(showType==1){
                $('#'+editorId+'WordTips').html(Math.abs(maxNumber - contentLength)<0?0:Math.abs(maxNumber - contentLength));
//              if(contentLength == 0){
//                  editorArea.addClass("text");
//              }else{
//                  editorArea.removeClass("text");
//              }
            }else{
            	if(contentLength <= maxNumber){
                    $('#'+editorId+'WordTips').html(contentLength);
                }else{
                    $('#'+editorId+'WordTips').html('<font color="red">'+contentLength+'</font>');
                }
            }
        };
        
        var mouseout = function(){
            var editorArea = $('#'+editorId+'Content');
//          if(editorArea.val().length==0){
//              editorArea.addClass("text");
//          }else{
//              editorArea.removeClass("text");
//          }
        };
        
        
        var whenNullValueAddClass = function(){
            var editorArea = $('#'+editorId+'Content');
//          if(editorArea.val().length==0){
//              editorArea.addClass("text");
//          }
        };
        
        $('#'+editorId+'Content').bind('mouseover', mouseover);
        $('#'+editorId+'Content').bind('mouseout', mouseout);
        $('#'+editorId+'Content').bind('keyup keydown change focus', count);
//        $('#'+editorId+'Content').bind('keyup keydown change focus', count).bind("blur", whenNullValueAddClass);

        $('#'+editorId+'Content').bind('input propertychange', function() {
            var contentLength = Math.ceil($(this).val().replace(/[^\x00-\xff]/g,"aa").length/2);
            if (contentLength > maxNumber) {   
                if(showType==1){
                    $('#'+editorId).find('.tips').html('<span style="color:#CC0000;">超出<strong id="'+editorId+'WordTips">'+(contentLength - maxNumber)+'</strong>字</span>');
                }else{
                    $('#'+editorId+'WordTips').html('<font color="red">'+contentLength+'</font>');
                }
            }else{
                if(showType==1){
                    $('#'+editorId).find('.tips').html('<span>还可以输入<strong id="'+editorId+'WordTips">'+(maxNumber - contentLength)+'</strong>字</span>');                
                }
            }   
        });

        $('#'+editorId+'InsertFace').click(function(event){
            if($('#'+editorId+'FaceWrap').length){
                $('#'+editorId+'FaceWrap').slideUp("fast",function(){
                    $('#'+editorId+'FaceWrap').remove();
                });
            }
            else{
            	if($(event.target).parents('#msgBox_div').length==0){$('#msgBox_div').remove()};
                var win_editorFace = "";
                win_editorFace ='<div id="'+editorId+'FaceWrap" class="faceWrap" style="display:none;z-index:1003"><div class="wrap_border" style="width:338px;">'+
                                '<span class="trig"></span><div class="warp_face">';
                $.each(toolUtil.editor_face,function(i,face){
                    win_editorFace += '<img src="'+web4s.context_path+'/images/gifFace/'+face[0]+'" val="'+face[1]+'" title="'+face[1]+'" style="border:1px solid #eee;" />';
                });
                win_editorFace += '</div></div></div>'; 
                
                $('body').append(win_editorFace);
                $('#'+editorId+'FaceWrap').slideDown("fast").css({"top":$(event.target).offset().top+26+"px","overflow":"visible","left":$(event.target).offset().left+"px"});
            }
        });
        
        $("body").click(function(e){
            var _cell = $(e.target);
            if(_cell.attr("id") != editorId+'InsertFace' && _cell.attr("id") != editorId+'FaceWrap'){
                $('#'+editorId+'FaceWrap').slideUp("fast", function(){
                    $('#'+editorId+'FaceWrap').remove();
                });             
            }
        });
        $('#'+editorId+'FaceWrap img').die();
        $('#'+editorId+'FaceWrap img').live('click',function(){
            //插入表情
        	
            var content = $(this).attr("val");
            $('#'+editorId).insertEditorContent(content);

            $('#'+editorId+'FaceWrap').slideUp("fast"); 
        }).hover(
            function(){
                $(this).css('border','1px solid #0066cc');
            },
            function(){
                $(this).css('border','1px solid #eee');
            }
        );

        return this;
    };

    //设置编辑器文本内容
    $.fn.setEditorContent = function(content){
    	var cont = $('#'+$(this).attr('id')+'Content');
        cont.val(content);
        cont.focus();
        cont.blur();
    };
    
    //在光标位置插入文本
    $.fn.insertEditorContent = function(content){
    	
        var editorId = $(this).attr('id');
        var editor = $('#'+editorId+'Content')[0];
        
        if($.browser.msie){
            editor.focus();
            document.selection.createRange().text=content;
        }else{
            if (editor.selectionStart || editor.selectionStart == '0') {
                var startPos = editor.selectionStart;
                var endPos = editor.selectionEnd;
                var scrollTop = editor.scrollTop;
                editor.value = editor.value.substring(0, startPos) + content + editor.value.substring(endPos, editor.value.length);
                $('#'+editorId+'Content').focus();
                editor.selectionStart = startPos + content.length;
                editor.selectionEnd = startPos + content.length;
                editor.scrollTop = scrollTop;
            }
            else {
                this.value += content;
                $('#'+editorId+'Content').focus();
            }
        }
    };
    
    //获取编辑器文本内容
    $.fn.getEditorContent = function(){
        return $('#'+$(this).attr('id')+'Content').val();
    };
    
    //获取编辑器文本长度
    $.fn.getEditorContentLength = function(){
        return Math.ceil($.trim($('#'+$(this).attr('id')+'Content').val()).replace(/[^\x00-\xff]/g,"aa").length/2);
    };
    
    //返回editor对象
    $.fn.getEditor = function(){
        return $('#'+$(this).attr('id')+'Content');
    };
    
    //闪动提示
    $.fn.errorPrompt = function(){
        var editorId = $(this).attr('id');
        var w = $('#'+editorId+'Content');
        w.focus();
        setTimeout(function(){
            var v = 0;
            var u = function(){
                if (v % 2 === 1) {
                    w.css("background-color","#fff");
                    v = v + 1;
                } else {
                    w.css("background-color","#f7b8b5");
                    v = v + 1;
                }
                if (v === 6) {
                    v = 0;
                    return
                }
                setTimeout(function(){u();}, 100);
            };
            u();
        }, 10);
    };
    
    $.fn.initEditor = function(maxNumber,showType){
        var editorId = $(this).attr('id');

        $('#'+editorId+'Content').val("");
        if(showType==1)
            $('#'+editorId+'WordTips').html(maxNumber);
        else
            $('#'+editorId+'WordTips').html(0);
    };
})(jQuery);
