/*
Script: txtcutter.js v1.0
		Script to cut a text.

License:
        MIT-style license.

About:
		txtcutter.js for mootools framework <http://www.mootools.net/> (c) 2007 Valerio Proietti, MIT-style license.
		Class created by Sylvain Barraud, Yvan Cottet and Gregory Paccaud, <http://www.esl.ch/>.
		Last modification, 29 march 2010.

Version: 1.0

Note:
		XHTML doctype is required.
		
*/

/*
Class: TxtCutter
		On click on "read more/close", show the full/partial text. 
		
Events:
		click	On "read more/close"

Example:

		(start code)
		var TxtCutter = new TxtCutter();
		(end)
		
Note:
		Don't forget to insert "<script language="Javascript" type="text/javascript" src="js/mootools.js"></script>" into the head page before this js file.
*/
var TxtCutter = new Class({
	Implements : Options,

	options: {
		nb_char : '195', //characters limit number
		unique : true,  //one text only
		txt : 'txt'    //text to cut (id or class according to unique or not)
	},
	
	initialize : function(options) {
		//this.small = true;
		this.setOptions(options);
		if (!this.options.unique) {
			//this.more = $$('.txt')[0].getNext('a').get('text');
			$$(this.options.txt).each(function(txt){
				this._parseWords(txt);
				this._setAction(txt);
			}.bind(this));
		} 
		else {
			//this.more = $('txt').getNext('a').get('text');
			this._parseWords($(this.options.txt));
			this._setAction($(this.options.txt));
		}
		
		
	},
		
	// -----------------------------------------------------------------
	// function which cut the text
	_parseWords : function(txt) {
		
		txt.store('small',true);
		txt.store('fullheight',txt.getHeight());
		
		//get the html content
		var content = txt.get('html').clean();
		
		//if text contains more characters than the limit
		if(txt.get('text').length > this.options.nb_char){
			//cut the text with a regular expression
			var reg = new RegExp("^(.{0,"+this.options.nb_char+"} )");
			var part = reg.exec(content);
			txt.store('smallpart',part);
			//set the small part in the box
			txt.set('html', part[1]+'...');
		}
		
		txt.store('smallheight',txt.getHeight());
		
		//display the content
		txt.getParent().tween('margin-top',0);
		txt.setStyle('height',txt.getHeight());
	},
	
	_setAction : function(txt) {
		//create the effect
		var myFx = new Fx.Tween(txt);
		
		myFx.addEvent('complete',function(){
			if (!txt.retrieve('small')) {
				txt.set('html', txt.retrieve('smallpart')[1] + '...');
				txt.store('small',true);
				//txt.getNext('a').set('text', this.more);
			}
			else {
				txt.store('small',false);
				//txt.getNext('a').set('text', galleryLanguage.close);
			}
		}.bind(this));
		
		//when click on title, display full/small text with the effect
		/*txt.getNext('a').addEvent('click',function(e){
			e.stop(e);
			if(txt.retrieve('small')){
				txt.set('html', txt.retrieve('smallpart').input);
				myFx.start('height', txt.retrieve('fullheight'));
			}else myFx.start('height', txt.retrieve('smallheight'));
		}.bind(this));*/
	}
});


window.addEvent('domready',function(){
	new TxtCutter({'nb_char' : 180, 'unique': false, 'txt' : '.txt' });
});
