NDSAdvCatSearch = Class.create({
	subCats: null,
	catMap: null,
	labelAll: null,
	activeCat: null,
	activeSubCat: null,
	
	initialize: function(labelAll, activeCat, activeSubCat){
		this.subCats = new Hash();
		this.catMap = new Hash();
		this.labelAll = labelAll;
		this.activeCat = activeCat;
		this.activeSubCat = activeSubCat;
	},
	
	addSub: function (parentID, childIDs) {
		// add array of sub categories
		var childArr = childIDs.split(",");
		this.subCats.set(parentID, childArr);
	},
	
	addCat: function(id, name) {
		// add category to category map
		this.catMap.set(id, name);
	},
	
	refresh: function() {
		// create correct values in dropdown
		
		var i=1;
		var selectedIndex = 0;
		
		// firstCategorySearch. 0=all
		var opts = "";
		this.subCats.each(function(pair){
			opts += '<option value="' + pair.key + '">' + this.catMap.get(pair.key) + '</option>';
			if (pair.key == this.activeCat) {
				selectedIndex = i;
			}
			i++;
		}.bind(this));
		$('firstCategorySearch').update(opts);
		$('firstCategorySearch').insert({top: '<option value="0">' + this.labelAll + '</option>'});
		delete opts;

		$('firstCategorySearch').selectedIndex = selectedIndex;

		this.refreshSecondDropdown();
		
		// show form
		$('norskdesignadvancedcataloguesearchinput').removeClassName("hidden");
	},
	
	refreshSecondDropdown: function() {
		// secondCategorySearch. 0=all
		
		var c=1;
		var selectedIndex = 0;
		
		var opts = "";
		if (this.activeCat != 0) {
			for (var i=0; i<this.subCats.get(this.activeCat).length; i++) {
				var key = this.subCats.get(this.activeCat)[i];
				opts += '<option value="' + key + '">' + this.catMap.get(key) + '</option>';
				if (key == this.activeSubCat) {
					selectedIndex = c;
				}
				c++;
			}
		}
		$('secondCategorySearch').update(opts);
		$('secondCategorySearch').insert({top: '<option value="0">' + this.labelAll + '</option>'});

		$('secondCategorySearch').selectedIndex = selectedIndex;
	},
	
	refreshSubSearch: function(id) {
		this.activeCat = id;
		if (id == "0") {
			this.activeCat = 0;
			this.activeSubCat = 0;		
		}
		this.refreshSecondDropdown();
	},
	
	debug: function() {
		var str = "";
		this.subCats.each(function(pair){
			str += "key=" + pair.key + "<br />";
			var subArr = pair.value;
			for(var i=0; i<subArr.length; i++) {
				str += subArr[i] + " ";
			}
		});
		str += "<hr />catMap<br />";
		this.catMap.each(function(pair){
			str += "key:" + pair.key + " - value:" + pair.value + "<br />";
		});
		$('ndacsdebug').update(str);
	}
	
});

var ndsAdvCatSearch;