/*
 * Copyright (c) 1995-2011 by 4system Polska Sp. z o.o.
 * All Rights Reserved. v4
 */
if (typeof(moduleDone)!="undefined") moduleDone("dictionary.js");

DictionaryItem = function()
{
	this.visible = true;
	this.id = null;
	this.key = null;
	this.description = null;
	this.sounds = null;
	this.links = null;
};

DictionaryItem.prototype.free = function()
{
	this.visible = true;
	this.id = null;
	this.key = null;
	this.description = null;
	this.sounds = null;
	this.links = null;
};

DictionaryGroupItem = function()
{
	this.id = null;
	this.caption = null;
	this.items = null;
};

DictionaryGroupItem.prototype.free = function()
{
	this.id = null;
	this.caption = null;
	for (var i=0; i<this.items.length; i++) {
		var item = this.items[i];
		item.free();
		item = null;
	}
	this.items = null;
};

Dictionary = function()
{
	this.items = new Array();
	this.groups = new Array();
	this.filter = "";
};

Dictionary.prototype.add = function(id, key, desc, sounds, links)
{
	var result = false;
	if (typeof(id) != "undefined" && id != ""
			&& typeof(key) != "undefined" && key != ""
			&& typeof(desc) != "undefined") {
		var index = this.items.length;
		this.items[index] = new DictionaryItem();
		this.items[index].id = id;
		this.items[index].key = key;
		this.items[index].description = desc;
		if (sounds != "") this.items[index].sounds = sounds.split(",");
		else this.items[index].sounds = new Array();
		if (links != "") this.items[index].links = links.split(",");
		else this.items[index].links = new Array();
		result = true;
	}
	return result;
};

Dictionary.prototype.addGroup = function(id, caption, items)
{
	var result = false;
	if (typeof(caption) != "undefined" && caption != ""
			&& typeof(items) != "undefined") {
		var index = this.groups.length;
		this.groups[index] = new DictionaryGroupItem();
		this.groups[index].id = id;
		this.groups[index].caption = caption;
		this.groups[index].items = items.split(",");
		result = true;
	}
	return result;
};

Dictionary.prototype.getGroup = function(value)
{
	var result = null;
	for (var i=0; i<this.groups.length; i++) {
		if (this.groups[i].id == value) {
			result = this.groups[i];
			break;
		}
	}
	return result;
};

Dictionary.prototype.inGroup = function(id, group)
{
	var result = false;
	for (var i=0; i<group.items.length; i++) {
		if (group.items[i] == id) {
			result = true;
			break;
		}
	}
	return result;
};

Dictionary.prototype.setFilter = function(value)
{
	var group = this.getGroup(value);
	if (group) {
		for (var i=0; i<this.items.length; i++) {
			this.items[i].visible = (this.inGroup(this.items[i].id, group));
		}
	} else {
		for (var i=0; i<this.items.length; i++) {
			this.items[i].visible = true;
		}
	}
	
	var dict_tree_comp = page.getElementsType("componentDictionaryTree");
	for (var i=0; i<dict_tree_comp.length; i++) {
		var comp = dict_tree_comp[i];
		comp.reinit();
	}
};

Dictionary.prototype.setActiveKey = function(value)
{
	if (value && value != "") {
		var dict_tree_comp = page.getElementsType("componentDictionaryTree");
		for (var i=0; i<dict_tree_comp.length; i++) {
			var comp = dict_tree_comp[i];
			comp.setKey(value);
		}
	}
};

Dictionary.prototype.getItem = function(key)
{
	var result = null;
	for (var i=0; i<this.items.length; i++) {
		if (this.items[i].key == key) {
			result = this.items[i];
			break;
		}
	}
	return result;
};

Dictionary.prototype.free = function()
{
	for (var i=0; i<this.items.length; i++) {
		var item = this.items[i];
		item.free();
		item = null;
	}
};

dictionary = new Dictionary();
