// JavaScript Document MoreLess - requires mootools 1.2

var MoreLess = new Class({

	options : {
		debug : false,
		prop : 'margin-top',
		moreButton : '.more',
		lessButton : '.less',
		transition : {
			expand : 'sine:out',
			contract : 'sine:out'
		},
		pixelsPerIteration : 3
		
	},

	initialize: function( divID, options ){
		this.setOptions(options)
		this.container = $(divID);
                this.container.setStyles({
                     'overflow' : 'hidden'
                });
		this.minHeight = this.container.getSize().x - (this.container.getSize().x * 2);
		this.maxHeight = 0; 
		var dur = 1000 * ( ( (this.maxHeight - this.minHeight) / this.options.pixelsPerIteration ) /50 );
		if( isNaN(dur) || dur > 20000 || dur < 200 ) this.debug('duration is ' + dur.toString() + '. Is this correct?');
		this.contracted = false;
		this.moreButton = this.container.getParent().getElement(this.options.moreButton);
		this.lessButton = this.container.getParent().getElement(this.options.lessButton);
		try{
			this.moreButton.addEvent('click', this.expand.bindWithEvent(this) );
		}catch(e){
			this.debug('More button not Found');
		}
		try{
			this.lessButton.addEvent('click', this.contract.bindWithEvent(this) );
		}catch(e){
			this.debug('Less button not found');
		}
		this.container.set('tween', { duration : dur, link:'ignore', onComplete:this.sortOutButtons.bindWithEvent(this) } );
		this.container.slide('hide');
                this.contracted = true;
                 this.sortOutButtons();
	},

	expand: function(){
		this.container.set('slide', { transition : this.options.transition.expand } );
		this.container.slide('in')
		this.contracted = false;
                this.sortOutButtons();
	},

	contract: function(){
		this.container.set('slide', { transition : this.options.transition.contract } );
		this.container.slide('out')
		this.contracted = true;
                this.sortOutButtons();
	},

	sortOutButtons: function(){
		if(this.contracted == true){
			this.moreButton.setStyle('display', 'inline');
			this.lessButton.setStyle('display', 'none');
		}else if(this.contracted == false ){
			this.moreButton.setStyle('display', 'none');
			this.lessButton.setStyle('display', 'inline');
		}
	},

	debug:function(txt){
		if(this.options.debug == true) alert(txt);
	}
});

MoreLess.implement(new Options);
		

