/*
*
*  showMoreLess
*  jQuery show more / show less plugin
*
*  author Lars Køie (lars@koeie.dk)
*
*
*/
	
	
	var defaults = {
		height: 200,
		slideOver : false,
		showMoreText : 'show more',
		showLessText : 'show less',
		addWhenCollapsed : ' ...'
	};
	
	jQuery.showMoreLess = function(el, o) {
		this.el = jQuery(el);
		this.opts = jQuery.extend({}, defaults, o || {});			
		var obj=this;
		this.lessh = this.opts.height;

		var d = this.el.html();
		this.el.addClass('showmoreless-container');
		this.textEl = jQuery('<div>');
		this.textEl.addClass('showmoreless-text').html(d);
		this.moreButton=jQuery('<div>'); this.moreButton.html(this.opts.showMoreText).addClass('showmoreless-more');
		this.lessButton=jQuery('<div>'); this.lessButton.html(this.opts.showLessText).addClass('showmoreless-less');
		
		this.el.
			empty().
			append(this.textEl).
			append(this.moreButton).
			append(this.lessButton);
		
		var h=this.textEl.height();
		var recursion = 0;
		this.mored=d;
		this.moreh=h;
		
		if (h>this.lessh) {		
			while (h>this.lessh && d.length>100) {
				d=d.substr(0, d.length-2);
				this.textEl.html(d);
				h=this.textEl.height();
				// recursion++; if(recursion>400) break;
			}
		
			d=d.substr(0,d.length-4)+' ...';
			this.lessd = d;
			this.showLess();
			this.moreButton.click(function(){obj.showMore();});
			this.lessButton.click(function(){obj.showLess();});
		}	else {
			this.moreButton.hide();
			this.lessButton.hide();
		}
		
		if (this.opts.slideOver) {
			var l = this.el.offsetParent().left;
			var t = this.el.offsetParent().top;
			this.lessEl=this.el.clone();
			this.el.after(this.lessEl);
			this.el.css({position: 'absolute', left: l, top: t, 'z-index': '50'});
			this.lessEl.find('.showmoreless-less').remove();
		}

		
	};
	
	var $el = jQuery.showMoreLess;

  $el.fn = $el.prototype = {
      showMoreLess: '0.1'
  };

	jQuery.fn.showMoreLess = function(o) {							
		return this.each(function() {			
			var d = new $el(this, o);
		});
	};

  $el.fn.extend = $el.extend = jQuery.extend;

  $el.fn.extend({
		
		showMore : function () {
			var obj = this;
			this.textEl.html(obj.mored);
			this.textEl.animate({height:obj.moreh}, function(){				
				obj.moreButton.hide();
				obj.lessButton.show();
			});
		},
		
		showLess : function () {
			var obj = this;
			obj.textEl.animate({height:obj.lessh}, function(){
				obj.textEl.html(obj.lessd);
				obj.moreButton.show();
				obj.lessButton.hide();
			});
		}
	});		
	
