/*
based on: ExpandableText

ExpandableText authors:
  - Magic Web Solutions (http://www.magicwebsolutions.co.uk)

Modified by:
  - Morten Skyt, OnlineCity ApS (http://www.onlinecity.dk)

Changes from original:
  - Removed more/less. Now instead, simply shortens text.

license:
  - MIT-style license

requires:
  core/1.3:   '*'

provides:
  - ShortenLongText
*/

var ShortenLongText = new Class({
	initialize: function(options) {
		options = $merge({
			height: 45,
			width: 300
			}, options);
		this.container = options.container;
		this.baseHeight = options.height;
		this.baseWidth = options.width;
		
		this.start();
	},
	
	start: function(){	
        $(this.container).setStyle('height', "");
        $(this.container).setStyle('width', this.baseWidth);
		var coordinates = null;
        coordinates = $(this.container).getCoordinates();
        if(coordinates.height <= this.baseHeight){			
                return;
       	} 
        this.longText = $(this.container).innerHTML;
		var splitText = this.longText.split(' ');
		$(this.container).innerHTML = '';
		var limit = true;
		var i = 0;
		var text = "";
		this.shortText = "";
		for (i=0; i<splitText.length&&limit; i++) {
			text += (splitText[i]+" ");
			$(this.container).innerHTML = (text+this.more);
			coordinates = $(this.container).getCoordinates();
			if(coordinates.height > this.baseHeight){			
				limit = false;
			}
			else {
				this.shortText = text;
			}
		}
		$(this.container).innerHTML = this.shortText;
		if (i < splitText.length)
			this.addMoreText();
	},
	
	addMoreText: function(){
		var span = new Element('span', {text: '...'});
		span.injectInside($(this.container));
	}
});
