function GlossaryController(
	aElementTerms,
	aElementDefinitions,
	oParams
	) {

	this.aItems = [];
	this.oParams = oParams;
	this.oButtonExpandAll = null;
	this.iCountExpanded = 0;

	this.init(
		aElementTerms,
		aElementDefinitions
		);

};

GlossaryController.CLASS_NAME_INVISIBLE = 'invisible';

GlossaryController.prototype = {

	init : function(
		aElementTerms,
		aElementDefinitions
		) {
		
		var oThis = this;

		for(var i = 0, bExpanded; i < aElementTerms.length; i++) {

			bExpanded = !Common.Class.match(aElementDefinitions[i], GlossaryController.CLASS_NAME_INVISIBLE);

			if(bExpanded) {
				this.iCountExpanded++;
			}

			this.aItems.push(
				{
				oElementTerm       : aElementTerms[i],
				oElementDefinition : aElementDefinitions[i],
				bExpanded          : bExpanded
				}
				);

			Common.Event.add(
				aElementTerms[i],
				'click',
				function(iIndex) {

					return function(oEvent) {

						Common.Event.cancel(oEvent);

						oThis.toggleByIndex(iIndex);

					}

				}(i)
				);

		}

		if(!this.oParams.oElementExpandAll) {
			return;
		}

		this.oButtonExpandAll = new ActionButton(
			this.oParams.oElementExpandAll,
			[
				{
					fHandler : function() {

						oThis.expandAll();

						return false;

					},
					sLabel : this.oParams.oLabels.sExpandAll
				},
				{
					fHandler : function() {

						oThis.collapseAll();

						return false;

					},
					sLabel : this.oParams.oLabels.sCollapseAll
				}
			],
			this.oParams.oElementExpandAllLabel
			);

		this.updateButtonExpandAll();

	},

	toggleByIndex : function(iIndex) {

		if(!this.aItems[iIndex]) {
			return;
		}
		
		if(this.aItems[iIndex].bExpanded) {
			this.collapseByIndex(iIndex);
		}
		else {
			this.expandByIndex(iIndex);
		}

	},

	expandByIndex : function(
		iIndex,
		bSkipUpdateButtonExpandAll
		) {

		if(!this.aItems[iIndex] || this.aItems[iIndex].bExpanded) {
			return;
		}

		Common.Class.remove(
			this.aItems[iIndex].oElementDefinition,
			GlossaryController.CLASS_NAME_INVISIBLE
			);

		this.aItems[iIndex].bExpanded = true;
		this.iCountExpanded++;

		if(!bSkipUpdateButtonExpandAll) {
			this.updateButtonExpandAll();
		}

	},

	expandAll : function() {

		for(var i = 0; i < this.aItems.length; i++) {
			this.expandByIndex(
				i,
				true
				);
		}

		this.updateButtonExpandAll();

	},

	collapseAll : function() {

		for(var i = 0; i < this.aItems.length; i++) {
			this.collapseByIndex(
				i,
				true
				);
		}

		this.updateButtonExpandAll();

	},

	collapseByIndex : function(
		iIndex,
		bSkipUpdateButtonExpandAll
		) {

		if(!this.aItems[iIndex] || !this.aItems[iIndex].bExpanded) {
			return;
		}

		Common.Class.add(
			this.aItems[iIndex].oElementDefinition,
			GlossaryController.CLASS_NAME_INVISIBLE
			);

		this.aItems[iIndex].bExpanded = false;
		this.iCountExpanded--;

		if(!bSkipUpdateButtonExpandAll) {
			this.updateButtonExpandAll();
		}

	},

	updateButtonExpandAll : function() {

		if(!this.oButtonExpandAll) {
			return;
		}

		if(this.iCountExpanded > 0 && this.iCountExpanded == this.aItems.length) {
			this.oButtonExpandAll.setState(1);
		}
		else {
			this.oButtonExpandAll.setState(0);
		}
		
	}

};