﻿// copyright c by zhangxinxu v1.0 2009-09-05 modify by zsz in 2010-12-01
// http://www.zhangxinxu.com
/* $(".test1").wordLimit(); 自动获取css宽度进行处理，如果css中未对.test1给定宽度，则不起作用
$(".test1").wordLimit(1); 自动获取css宽度和高度进行处理，如果css中未对.test1给定宽高，则不起作用
注意事项，在IE8以及其他浏览器中，上级元素设置样式可以在子元素中继承，在IE7中部分不起作用：
1、单行需设置white-space:nowrap;否则会自动换行
2、需要设置对象的width、overflow:hidden和display:block三个样式，否则在IE7下不起作用。
3、暂时只支持标签中只包含文本的元素。
*/
var limitCount = 300;
(function($) {
    $.fn.wordLimit = function(num, container) {
        if (limitCount-- <= 0) {
            return;
        }
        this.each(function() {            
            if (!num) {
                var copyThis = $(this.cloneNode(true)).hide().css({
                    'position': 'absolute',
                    'width': 'auto',
                    'overflow': 'visible',
                    'max-width': '10000px'
                });
                //if ($(this).attr("itype") == "view") alert($(this).text());
                $(this).after(copyThis);
                if (copyThis.width() > $(this).width()) {
                    if ($(this).attr("title") == "")
                        $(this).attr("title", $.trim($(this).text()));
                    //$(this).attr("title", $(this).attr("title"));
                    $(this).text($(this).text().substring(0, $(this).text().length - 4));
                    $(this).html($(this).html() + '...');
                    copyThis.remove();
                    $(this).wordLimit();
                }
                else {
                    //$(this).removeAttr("title");
                    copyThis.remove(); //清除复制
                    return;
                }
            }
            else {
                if (num == 1) {
                    var copyThis = $(this.cloneNode(true)).hide().css({
                        'position': 'absolute',
                        'height': 'auto',
                        'overflow': 'visible'
                    });
                    //if ($(this).attr("itype") == "view") alert($(this).text());
                    $(this).after(copyThis);
                    if (copyThis.height() > $(this).height()) {
                        if ($(this).attr("title") == "") {
                            $(this).attr("title", $.trim($(this).text()));
                            var dd = $.trim($(this).text());
                            var numdd = dd.length * $(this).height() / copyThis.height();
                            copyThis.text(dd.substring(0, numdd));
                            for (var i = 0; i < 50; i++) {
                                if (copyThis.height() <= $(this).height()) {
                                    copyThis.text(dd.substring(0, numdd + 50 * i));
                                }
                                else {
                                    break;
                                }
                            }
                            $(this).text(copyThis.text());

                        }
                        $(this).text($(this).text().substring(0, $(this).text().length - 4));
                        $(this).html($(this).html() + '...');
                        copyThis.remove();
                        $(this).wordLimit(1);
                    }
                    else {
                        copyThis.remove(); //清除复制
                        return;
                    }
                }
                else {
                    var maxwidth = num;
                    var copyThis = $(this.cloneNode(true)).hide().css({
                        'position': 'absolute',
                        'width': 'auto',
                        'max-width': '10000px',
                        'overflow': 'visible'
                    });
                    copyThis.show();
                    container.append(copyThis);
                    if (copyThis.width() > maxwidth) {
                        if ($(this).attr("title") == "")
                            $(this).attr("title", $.trim($(this).text()));
                        $(this).text($(this).text().substring(0, $(this).text().length - 4));
                        $(this).html($(this).text() + '...');
                        copyThis.remove();
                        $(this).wordLimit(maxwidth, container);
                    }
                    else {
                        copyThis.remove(); //清除复制
                        return;
                    }
                    //                    if ($(this).text().length > maxwidth) {
                    //                        $(this).text($(this).text().substring(0, maxwidth));
                    //                        $(this).html($(this).html() + '...');
                    //                    }
                }
            }
        });
    }
})(jQuery);

