function Category(me, masterMenuId, slaveMenuId, strId, showAreaId) {

	this.me					= me;
	this.masterMenu			= masterMenuId;
	this.slaveMenu			= slaveMenuId;
	this.categoriesStr		= strId;
	this.showArea			= showAreaId;

	this.globals 			=  {};
	this.subGlobals 		= {};
	this.selections 		= "";
	this.subGlobalsMenus	= {};
	// this.categoryLinks 		= {};
	this.globalView 		= null;

	this.addGlobal = function(categoryId, categoryName) {
			this.globals[categoryId] = categoryName;
	};

	this.addSubGlobal = function(categoryId, parentCategoryId, categoryName) {
			this.subGlobals[categoryId] = categoryName;
			// this.categoryLinks[categoryId] = parentCategoryId;

			if (typeof this.subGlobalsMenus[parentCategoryId] == "undefined") {
				this.subGlobalsMenus[parentCategoryId] = new Array();
			}

			option =  new Option(categoryName, categoryId);
			this.subGlobalsMenus[parentCategoryId].push(option);
	};

	this.init = function(str) {
		var categories = {};
		var a = str.split(",");
		for (var i=0; i<a.length; i++) {
			categories[a[i]] = true;
		}

		for (var g in this.globals) {
			if ( typeof this.subGlobalsMenus[g] != "undefined" ) {
				for (var i=0; i < this.subGlobalsMenus[g].length; i++ ) {
					if (categories[ this.subGlobalsMenus[g][i].value ] == true) {
						this.subGlobalsMenus[g][i].selected = true;
					}
				}
			}
		}
		this.update();
	};

	this.update = function() {
		var ret = new Array();
		var showArea = jQuery("#" + this.showArea );
		showArea.html(""); // clearscreen

		for (var g in this.globals) {
			var isShow = false;
			if ( typeof this.subGlobalsMenus[g] != "undefined" ) {
				for (var i=0; i < this.subGlobalsMenus[g].length; i++ ) {
					if ( this.subGlobalsMenus[g][i].selected == true) {
						if (!isShow) {
							showArea.html( showArea.html() + "<a href='javascript:" + this.me + ".clearRow("+ g +")'><img src='imgs/admin/icon_delete-16.png' alt='' /></a>&nbsp;" + this.globals[g] +" -> ");
							isShow = true;
						} else {
							showArea.html( showArea.html() + ", ");
						}

						// console.info( showArea.html() );
						showArea.html( showArea.html() + this.subGlobals[this.subGlobalsMenus[g][i].value]);
						ret.push(this.subGlobalsMenus[g][i].value);
					}
				}
			}
			if (isShow) {
				showArea.html( showArea.html() + "<br />");
			}
		}

		document.getElementById(this.categoriesStr).value = ret.join(",");
	};

	this.clearRow = function(rowId) {
		for (var i =0; i < this.subGlobalsMenus[rowId].length; i++) {
			this.subGlobalsMenus[rowId][i].selected = false;
		}
		this.update();
	},

	this.updateSubGlobalView = function(obj) {

		this.globalView = obj.options[obj.selectedIndex].value;

		var selectObj = document.getElementById(this.slaveMenu);
		var count = selectObj.options.length;

		var lastCount = -1;

		if (typeof this.subGlobalsMenus[this.globalView] != "undefined") {
			for (var i = 0; i < this.subGlobalsMenus[this.globalView].length; i++) {
				selectObj.options[i] = this.subGlobalsMenus[this.globalView][i];
				lastCount = i;
			}
		}

		// clearing
		for (var i = count-1; i > lastCount; i--) {
			selectObj.options[i] = null;
		}
	};
}
