jQuery.fn.truncate = function( max, settings ) {
    settings = jQuery.extend( {
        chars: /\s/,
        trail: [ "...", "" ],
        callback: null
    }, settings );
    var myResults = {};
    var ie = $.browser.msie;
    function fixIE( o ) {
        if ( ie ) {
            o.style.removeAttribute( "filter" );
        }
    }

    return this.each( function() {
        var $this = jQuery(this);
        //var myStrOrig = $this.html().replace( /\r\n/gim, "" );
        var myStrOrig = $this.html();
        var myStr = myStrOrig;
        var myRegEx = /<\/?[^<>]*\/?>/gim;
        var myRegExArray;
        var myRegExHash = {};
        var myResultsKey = $("*").index( this );
        while ( ( myRegExArray = myRegEx.exec( myStr ) ) != null ) {
            myRegExHash[ myRegExArray.index ] = myRegExArray[ 0 ];
        }
        myStr = jQuery.trim( myStr.split( myRegEx ).join( "" ) );
        if ( myStr.length > max ) {
            var c;
            while ( max < myStr.length ) {
                c = myStr.charAt( max );
                if ( c.match( settings.chars ) ) {
                    myStr = myStr.substring( 0, max );
                    break;
                }
                max--;
            }
            if ( myStrOrig.search( myRegEx ) != -1 ) {
                $this.html( myStr );
            } else {
                $this.html( myStr );
            }
            
           
            myResults[ myResultsKey ] = myStrOrig;
            $this.html( [ "<div class='truncate_less'>", $this.html(), settings.trail[ 0 ], "</div>" ].join( "" ) )
            .find(".truncate_show",this).click( function() {
                if ( $this.find( ".truncate_more" ).length == 0 ) {
                    $this.append( [ "<div class='truncate_more' style='display: none;'>", myResults[ myResultsKey ], settings.trail[ 1 ], "</div>" ].join( "" ) )
                    .find( ".truncate_hide" ).click( function() {
                        $this.find( ".truncate_more" ).css( "background", "transparent" ).fadeOut( "fast", function() {
                            $this.find( ".truncate_less" ).css( "background", "transparent" ).fadeIn( "fast", function() {
                            	try { 
                            		if (settings.callback) settings.callback();
	                                fixIE( this );
	                                $(this).css( "background", "none" );
//	                                console&&console.log('f1');                      
	                               
                            	}
                            	catch (e) {
//                            		console&&console.log('ex1');
                            	}
                            });
                            fixIE( this );
                        });
                        return false;
                    });
                }
                $this.find( ".truncate_less" ).fadeOut( "fast", function() {
                    $this.find( ".truncate_more" ).fadeIn( "fast", function() {
                    	try {
                    		if (settings.callback) settings.callback();
	                        fixIE( this );
//	                        console&&console.log('f3');
	                        
	                	}
	                	catch (e) {
//	                		console&&console.log('ex3');
	                	}
                    });
                    fixIE( this );
                });
                jQuery(".truncate_show",$this).click( function() {
                    $this.find( ".truncate_less" ).css( "background", "transparent" ).fadeOut( "fast", function() {
                        $this.find( ".truncate_more" ).css( "background", "transparent" ).fadeIn( "fast", function() {
                        	try {
	                            fixIE( this );
	                            $(this).css( "background", "none" );
//	                            console&&console.log('f5');
	                            if (settings.callback) settings.callback();
    	                	}
    	                	catch (e) {
//    	                		console&&console.log('ex5');
    	                	}
                        });
                        fixIE( this );
                    });
                    return false;
                });
                return false;
            });
        }
    });
};