function Tagger() {
	this.aLists = new Array();
	this.sListSelected = null;
	this.aTags  = new Array();
	this.sTagSelected = null;
	this.aModules = new Array();
	this.sModuleSelected = null;
	this.reSelected = /\bselected\b/g;
	
	this.oDivList = document.getElementById('cm_list_div');
	this.bDivList  = isset(this.oDivList);
	this.oDivTag  = document.getElementById('cm_tags');
	this.bDivTag  = isset(this.oDivTag);
	this.oDivModule = document.getElementById('cm_modules');
	this.bDivModule = isset(this.oDivModule);
	this.oDivModuleList = document.getElementById('cm_module_lists');
	this.bDivModuleList = isset(this.oDivModuleList);
	
	
	var oBtnNewTag = document.getElementById('dm_new_tag_submit');
	
	this._refresh = function() {
		this.aLists = new Array();
		this.aTags  = new Array();
		this.aModules = new Array();
		TaggerInit();
		if(isset(this.sListSelected) && isset(this.aLists[this.sListSelected])) {
			addClass(this.aLists[this.sListSelected], 'selected', this.reSelected);
		} else {
			this.sListSelected = null;
		}
		if(isset(this.sTagSelected) && isset(this.aTags[this.sTagSelected])) {
			addClass(this.aTags[this.sTagSelected], 'selected', this.reSelected);
		} else {
			this.sTagSelected = null;
		}
	}
	
	this._newList = function(oElem) {
		var sId = oElem.id;
		if(!sId) return;
		if(this.aLists.key_exists(sId)) return;
		this.aLists[sId] = oElem;
		oElem.onmousedown = TaggerChangeListSelected;
	}
	
	this._changeList = function(sId) {
		if(isset(this.sListSelected))
			removeClass(this.aLists[this.sListSelected], this.reSelected);
		
		this.sListSelected = sId;
		addClass(this.aLists[this.sListSelected], 'selected', this.reSelected);
		var sList = this.aLists[this.sListSelected].innerHTML;
		sList = sList.replace(/\s/g, '_');
		var sLink = '?Content_Manager/tags/' + sList + '/display:xontemp/';
		oTOwl.sget(sLink, this, this._updateTag);
	}
	
	this._newTag = function(oElem) {
		var sId = oElem.id;
		if(!sId) return;
		if(this.aTags.key_exists(sId)) return;
		this.aTags[sId] = oElem;
		oElem.onmousedown = TaggerChangeTagSelected;
	}
	
	this._changeTag = function(sId) {
		if(isset(this.sTagSelected)) {
			removeClass(this.aTags[this.sTagSelected], this.reSelected);	
		}
		
		this.sTagSelected = sId;
		addClass(this.aTags[this.sTagSelected], 'selected', this.reSelected);
	}
	
	this._newModule = function(oElem) {
		var sId = oElem.id;
		if(!sId) return;
		if(this.aModules.key_exists(sId)) return;
		this.aModules[sId] = oElem;
		oElem.onmousedown = TaggerChangeModuleSelected;
	}
	
	this._changeModule = function(sId) {
		if(isset(this.sModuleSelected)) {
			removeClass(this.aModules[this.sModuleSelected], this.reSelected);	
		}
		
		
		this.sModuleSelected = sId;
		addClass(this.aModules[this.sModuleSelected], 'selected', this.reSelected);
		
		var sModule = this.aModules[this.sModuleSelected].innerHTML;
		sModule = sModule.replace(/\s/g, '_');
		var sLink = '?Content_Manager/Module_Lists/' + sModule + '/display:xontemp/';
		oTOwl.sget(sLink, this, this._updateModuleLists);
	}
	
	this._submitList = function() {
		var oInput = document.getElementById('cm_new_list');
		if(isset(oInput.value))
			var sLabel = oInput.value;
		var sPostVars = "inp_type=0&inp_model=Tag_Group_List&inp_action=0&inp_object_id=0&inp_group_name=" + encodeURI(sLabel);
		var sLink = '?Content_Manager/tags/display:xontemp/';
		oTOwl.spost(sLink, this, this._updateList, sPostVars);
	}
	
	this._submitTag = function() {
		var oInput = document.getElementById('cm_new_tag');
		if(isset(oInput.value))
			var sLabel = oInput.value;
		alert(sLabel);
		if(isset(this.sTagSelected)) {
			var iCurrentTag = this.sTagSelected.match(/[0-9]+$/);
		} else {
			var iCurrentTag = 0;	
		}
		var iCurrentList = this.aLists[this.sListSelected].id;
		iCurrentList = iCurrentList.match(/[0-9]+$/);
		var sCurrentList = this.aLists[this.sListSelected].innerHTML;
		sCurrentList = sCurrentList.replace(/\s+/g, '_');
		var sPostVars = "inp_type=0&inp_model=tag_groups&inp_action=0&inp_object_id=0&inp_list_id=" + iCurrentList + '&inp_tag_label=' + encodeURI(sLabel) + '&inp_parent=' + encodeURI(iCurrentTag[0]);
		var sLink = '?Content_Manager/tags/' + sCurrentList + '/display:xontemp/';
		alert(sPostVars);
		oTOwl.spost(sLink, this, this._updateTag, sPostVars);
	}
}

Tagger.prototype._updateList = function(sHTML) {
	if(this.bDivList) {
		this.oDivList.innerHTML = sHTML;
	}
	this._refresh();
	if(this.bDivList)
		oListLegion._newLegion(document.getElementById('cm_list_list'));
}

Tagger.prototype._updateTag = function(sHTML) {
	if(this.bDivTag) {
		this.oDivTag.innerHTML = sHTML;
	}
	this._refresh();
	if(this.bDivTag)
		oListLegion._newLegion(document.getElementById('cm_tag_list'));
}

Tagger.prototype._updateModuleLists = function(sHTML) {
	if(this.bDivModuleList) {
		this.oDivModuleList.innerHTML = sHTML;
	}
	this._refresh();
	if(this.bDivModuleList)
		oListLegion._newLegion(document.getElementById('cm_module_lists_list'));
}

function newList(label) {
	var sPostVars = "inp_type=0&inp_model=Tag_Group_List&inp_action=0&inp_object_id=0&inp_group_name=" + encodeURI(label);
	sendFormRequest('?Input/', sPostVars);
}

function newTag(label) {
	if(iCurrentList == 0)
		return;
		
	var sPostVars = "inp_type=0&inp_model=tag_groups&inp_action=0&inp_object_id=0&inp_list_id=" + encodeURI(iCurrentList) + '&inp_tag_label=' + encodeURI(label) + '&inp_parent=' + encodeURI(iCurrentObject);
	sendFormRequest('?Input/', sPostVars);
}

function TaggerInit() {
	if(!isset(oTagger))
		oTagger = new Tagger();
	TaggerAddListEvents(fGEOBJLI('a', /\bcm_list\b/));
	TaggerAddTagEvents(fGEOBJLI('a', /\bcm_tag\b/));
	TaggerAddModuleEvents(fGEOBJLI('a', /\bcm_module\b/));
	
	var oBtnNewList = document.getElementById('cm_new_list_submit');
	if(isset(oBtnNewList))
		oBtnNewList.onclick = TaggerSubmitList;
		
	var oBtnNewTag = document.getElementById('cm_new_tag_submit');
	if(isset(oBtnNewTag))
		oBtnNewTag.onclick = TaggerSubmitTag;
}

function TaggerAddListEvents(aElems) {
	for(i in aElems) {
		if(isnumeric(i))
			oTagger._newList(aElems[i]);
	}
}

function TaggerAddTagEvents(aElems) {
	for(i in aElems) {
		if(isnumeric(i))
			oTagger._newTag(aElems[i]);
	}
}

function TaggerAddModuleEvents(aElems) {
	for(i in aElems) {
		if(isnumeric(i))
			oTagger._newModule(aElems[i]);
	}
}

function TaggerChangeListSelected(oEvent) {
	oElem = fGEOBJ(oEvent);
	if(isset(oElem.id))
		oTagger._changeList(oElem.id);
	return(false);
}

function TaggerChangeTagSelected(oEvent) {
	oElem = fGEOBJ(oEvent);
	if(isset(oElem.id))
		oTagger._changeTag(oElem.id);
	return(false);
}

function TaggerChangeModuleSelected(oEvent) {
	oElem = fGEOBJ(oEvent);
	if(isset(oElem.id))
		oTagger._changeModule(oElem.id);
	return(false);
}

function TaggerSubmitList() {
	oTagger._submitList();
	return(false);
}

function TaggerSubmitTag() {
	oTagger._submitTag();
	return(false);
}
var oTagger = null;
WindowOnload(TaggerInit);