if (typeof Driebit == 'undefined') Driebit = {}

Driebit.expandableList = new Class({
	Implements: Options,
	options: {
		cutOff: 10,
		moreText: 'Show all \u00bb',
		lessText: '\u00ab Show less'
	},
	initialize: function (el, options) {
		this.setOptions(options);

		this._collection = $(el);
		this._showsAll   = true;
		this._items      = this._collection.getElements('li');

		//if there are more items than the cutoff number create the more/less toggle
		if (this._items.length > this.options.cutOff) {
			this._moreLessToggle = new Element('li');
			this._collection.adopt(this._moreLessToggle);
			this._moreLessToggle.addClass('moreless');

			this._moreLessToggle.addEvent('click', (function () {
				if (this._showsAll) {
					this.hideMore();
				} else {
					this.showMore();
				}
			}).bind(this));

			this.hideMore();
		}
	},

	/**
	 * Shows all facets
	 */
	showMore: function () {
		this._moreLessToggle.set('text', this.options.lessText);
		this._showsAll = true;

		for (var i=this.options.cutOff; i<this._items.length; i++) {
			this._items[i].setStyle('display', '');
		}
	},

	/**
	 * Hides the excess facets
	 */
	hideMore: function () {
		this._moreLessToggle.set('text', this.options.moreText)
		this._showsAll = false;

		for (var i=this.options.cutOff; i<this._items.length; i++) {
			this._items[i].setStyle('display', 'none');
		}
	}
});

window.addEvent('domready', function () {
	$$('ul.expandable', 'ol.expandable').each(function (el) {
		if (el.getParent('.delegates-collection') || el.getParent('.companies-collection')) {
			new Driebit.expandableList(el, {'cutOff': 10, 'moreText': 'Show all results \u00bb', 'lessText': '\u00ab Show first 10'});
		} else {
			new Driebit.expandableList(el, {'cutOff': 10, 'lessText': '\u00ab Show first 10'});
		}
	});
});