/**
* RUZEE.Ellipsis 0.1
* (c) 2007 Steffen Rusitschka
*
* RUZEE.Ellipsis is freely distributable under the terms of an MIT-style license.
* For details, see http://www.ruzee.com/
*
* Modified for use with jQuery instead of Prototype.
* GitHub - http://gist.github.com/64223
* Fooala - http://opensource.fooala.com/
*/
$(document).ready(function(){
	var x = 0;
	if ( ! $.browser.msie || true) {
		$('.ellipsis').each(function () {
			x = 0;
			var e = this;
			var w = $(e).width() - 10;// - 10000;
			var t = $(this).html();
			var tt = t;
			var i = 0;
			var ii = tt.length;
			var i_ = 0;
			var i_dir = 1;
			var i_div = 2;
			var loop_stop = false;
			$(e).html('<span>' + t + '</span>');
			e = $(e).children(":first-child");
			if (e) {

				if ( $(e).width() <= w ) { loop_stop = true; }

				var j = 0; // failsafe
				while (!loop_stop) {

					i = i + i_dir * (ii / i_div);
					i_div *= 2;
					t = tt.substr(0, i - 1);
                                        $(e).html( t + "...");

					if ( $(e).width() <= w ) { i_dir =  1 }
					else                     { i_dir = -1 }

					if (Math.abs(i - i_) < 1) { loop_stop = true; }
					i_ = i;

					j=j+1; if (j > 10) { $(e).html(tt); loop_stop = true; } // failsafe

				}

/*
				while (t.length > 0 && $(e).width() >= w) {

					x = t.length;
					t = t.substr(0, t.length - 1);
					$(e).html(t + "...");
				}
*/
			}
		});
	}
	else
	{
		$('.ellipsis').css('margin-right', '0');
		$('.ellipsis').css('text-overflow', 'ellipsis');
	}
});

