//Suki
(function($){
	$.fn.autoEllipsis = function(options) {
		var num = function(el, prop) {
			var props = (prop || "").split(/ +/);
			var n = 0;
			var el = $(el);
			var i = 0;
			for (; i < props.length; i++) {
				n += parseInt(el.css(props[i])) || 0;
			}
			return n;
		};
		$(this).each(function() {
			var el = $(this);
			var originalHTML = el.html();
			var ellipsisHTML = el.html();
			var stop = false;
			var saveOverflow = "";
			
			var parseSubChilds = function(element, maxHeight) {
				var element, html, offsetParent, childs, o, i, h, lastNodeIsElement;
				var texts, text, b;
				
				element = $(element);
				html = element.html();
				html = html
						.replace(/ +/, ' ')
						.replace(/\r\n\t/,'')
						.replace(/^ +/, '')
						.replace(/ +$/, '');
						
				
				element.html(html);
				childs = element[0].childNodes;
				offsetParent = element.offset();
				
				
				b = true;
				lastNodeIsElement = false;
				while(b && childs.length > 0) {
					for (i=childs.length-1;i>=0;i--) {
						if (childs[i].nodeType == 1) {
							element[0].removeChild(childs[i]);
							lastNodeIsElement = true;
							break;
						} 
						else if (childs[i].nodeType == 3) {
							//console.debug(childs[i]);
							text = childs[i].nodeValue;
							texts = text.split(/ /g);
							if (texts.length > 1) {
								texts.pop();
								texts.pop();
								texts.push("...");
								text = texts.join(' ');
								childs[i].nodeValue = text;
							} else {
								b = false;
							}
							break;
						}
					}
					
				
					h = element.innerHeight();
					if (h <= maxHeight) {
						break;
					}
				}
				
				if (lastNodeIsElement) {
					element.html(element.html() + "...");
				}
			}
			
			var parseChilds = function(element, maxHeight){
				var element = $(element);
				var childs = element.children();
				var offsetParent = element.offset();
				var o,h,t;
				
				for (i=0; i < childs.length; i++) {
					if (stop) {
						$(childs[i]).remove();
						continue;
					}
					o = $(childs[i]).offset();
					h = $(childs[i]).outerHeight();
					
					o.top -= offsetParent.top;
					o.left -= offsetParent.left; 
					
					o.top -= num(childs[i], "marginTop top");
					o.left -= num(childs[i], "marginLeft left");
					
					t = o.top + h;
					//console.debug(o.top, t, maxHeight);
					
					if (t > maxHeight) {
						//
						//console.debug(o.top, t, maxHeight);
						t = maxHeight - o.top;
						//console.debug(t);
						parseSubChilds(childs[i], t);
						if ($.trim($(childs[i]).html()) == "...") {
							$(childs[i]).remove();
							element.children(":last").html(element.children(":last").html() + " <br/ >...");
						}
						stop = true;
						
					}
				}
			};
			
			saveOverflow = el.css("overflow");
			el.css("overflow", "hidden");
			parseChilds(el, el.innerHeight());
			ellipsisHTML = el.html();
			el.data("autoEllipsis_fullHTML", originalHTML);
			el.data("autoEllipsis_html", ellipsisHTML);
			el.css("overflow", saveOverflow);
			
		});
		return this;
	}
})(jQuery);

/*
Suki
extend autoEllipsis functionality
*/
(function($) {
    $.fn.ellipsis = function(options) {
        var obj, isMethodCall, args, options;
        var isMethodCall = (typeof options == "string") || false;

        if (isMethodCall) {
            obj = $(this[0]).data("ellipsis");
            if (obj) {
                if (obj[options] && typeof obj[options] == "function") {
                    return obj[options].apply(obj, $.makeArray(args).slice(1));
                }
            }
            return null;
        }

        args = arguments;
        options = $.extend({}, options, {});

        $(this).each(function() {
            obj = $(this).data("ellipsis");
            if (isMethodCall && obj) {
                if (obj[options] && typeof obj[options] == "function") {
                    obj[options].apply(obj, $.makeArray(args).slice(1));
                }
                return;
            } else {
                obj = new $.ellipsis(this, options);
                $(this).data("ellipsis", obj);
            }
        });
        return this;
    };

    $.ellipsis = function(elem, options) {
        var moreSpan = $(".label-More");
        if (moreSpan.length) {
            $.ellipsis.defaults.more = moreSpan.text() + " &raquo";
        }

        var closeSpan = $(".label-Close");
        if (closeSpan.length) {
            $.ellipsis.defaults.less = "&laquo; " + closeSpan.text();
        }

        this.elem = elem;
        this.options = $.extend({}, options, $.ellipsis.defaults);
        this.init();
    };

    $.extend($.ellipsis, {
        defaults: {
            more: 'More &raquo;',
            less: '&laquo; Close'
        },
        prototype: {
            init: function() {
                var self = this;

                this.content = $(".autoEllipsis_content", this.elem);
                this.more = $(".more", this.elem);
                this.fullContent = false;

                if (!this.content > length) return;

                //duplicate one for printing
                this.content.clone().hide().addClass("autoEllipsis_clone").appendTo(this.elem);

                this.saveCSS = {
                    overflow: this.content.css("overflow"),
                    height: this.content.css("height")
                }
                this.content.css({
                    overflow: "visible",
                    height: "auto"
                });
                this.fullHeight = this.content.outerHeight();
                this.fullHeight -= parseInt(this.content.css("paddingTop")) || 0;
                this.fullHeight -= parseInt(this.content.css("paddingBottom")) || 0;

                this.content.css(this.saveCSS);
                $(".autoEllipsis_content", this.elem).autoEllipsis();


                this.ellipsisHeight = this.content.innerHeight();
                this.ellipsisHeight -= parseInt(this.content.css("paddingTop")) || 0;
                this.ellipsisHeight -= parseInt(this.content.css("paddingBottom")) || 0;

                this.content.css({
                    height: this.ellipsisHeight + "px",
                    overflow: "hidden"
                });

                if (this.more.length > 0) {
                    this.more.bind("click.ellipsis", function() {
                        self.toggleEllipsis.apply(self, []);
                        this.blur();
                        return false;
                    });
                    this.more.html(this.options.more);
                }
            },

            toggleEllipsis: function() {
                var html;
                this.fullContent = !this.fullContent;
                if (this.fullContent) {
                    //show
                    html = this.content.data("autoEllipsis_fullHTML");
                    this.content.stop().html(html);
                    this.content.animate({ height: this.fullHeight + "px" }, { queue: false, duration: 500, easing: "easeOutExpo" });
                    this.more.html(this.options.less).addClass("more_close");
                } else {
                    //hide
                    html = this.content.data("autoEllipsis_html");
                    this.content.stop();
                    this.content.animate({ height: this.ellipsisHeight + "px" }, { queue: false, duration: 500, easing: "easeOutExpo", complete: function() {
                        $(this).html(html);
                    }
                    });
                    this.more.html(this.options.more).removeClass("more_close");
                }

            }
        }
    });
})(jQuery);

