var Expander =  new Class({
	togglers : null,
	items : null,
	bookings: null,
	activeElement : null,
	height : 0,
	
	initialize : function (togglers,items,bookings) {
		this.togglers = $$(togglers);
		this.items = $$(items);
		this.bookings = $$(bookings);
		
		if (!this.togglers || !this.items) return;
		
		this.bookings.forEach(function(item,index) {
			item.addEvent('click',function() {
				_gaq.push(['_link',item.href]);
				
				return false;
			}.bind(this));
		}.bind(this));
		
		this.togglers.forEach(function(item,index) {
			item.index = index;
			item.expandElement = this.items[index];
			
			item.addEvent('click',this.onToggleClick.bindWithEvent({'source' : this, 'target' : item}));
		}.bind(this));
		
		this.items.forEach(function(item,index) {
			item.addEvent('expand',this.onExpand.bindWithEvent({'source' : this, 'target' : item}));
			item.addEvent('collapse', this.onCollapse.bindWithEvent({'source' : this, 'target' : item}));
		}.bind(this));
	},
	
	onToggleClick : function(event) {
		if (!window.ie) {
			event = new Event(event);
			event.preventDefault();
		}else {
		    event.cancelBubble = true;
		    event.returnValue = false;
		}
		
		if (this.target.hasClass('more')) {
			if (this.source.activeElement) {
				this.source.activeElement.removeClass('close');
				this.source.activeElement.addClass('more');
				this.source.activeElement.set('html','Read more');
				this.source.activeElement.expandElement.fireEvent('collapse');
			}
			
			if (this.target.expandElement) {
				this.source.activeElement = this.target;
				
				this.source.activeElement.removeClass('more');
				this.source.activeElement.addClass('close');
				
				this.source.activeElement.set('html','Close');
				
				this.target.expandElement.fireEvent('expand');
			}
		}else if (this.target.hasClass('close')) {
			this.target.removeClass('close');
			this.target.addClass('more');
			this.target.set('html','Read more');
			this.target.expandElement.fireEvent('collapse');
			
			this.source.activeElement = null;
		}
	},
	
	onExpand : function() {
		var tween = this.target.get('tween', {property: 'height', duration: 'normal'});
		tween.start(this.target.scrollHeight);
	},
	
	onCollapse : function() {
		var tween = this.target.get('tween', {property: 'height', duration: 'normal'});
		tween.start(this.source.height);
	}
});