var categoryselector = {
	switchtab : function (forumid,tabid) {
		$('div.tabselected,div.tabselectedlast').each(function(tab){
			if(this.className == 'tabselectedlast'){
				this.className = 'tablast';
			} else {
				this.className = 'tab';
			}
		});
		if($('#tab'+tabid).attr("class") == 'tablast'){
			$('#tab'+tabid).attr("class",'tabselectedlast');
		}else{
			$('#tab'+tabid).attr("class",'tabselected');
		}
		
		categoryselector.selectcat(forumid,tabid,1,0,1);		
	},
	selectcat : function (forumid,catid, haschild, level,isTab) {
		if(!isTab)categoryselector.clicked(catid);
		categoryselector.removeTree(level);
		if(haschild){			
			$.get(basehref + 'edit/form2/categoryselector/loadcategories.comp', {loadid:catid, forumid:forumid, useajax:1, level: level,rowscount:$('#rowscount').val()}, function(data){
				categoryselector.remLoading();			
				var ID = catid.split(".");
				if(!$('#field_'+level+'_'+ID[0]+'_'+ID[1]).length){
					categoryselector.extendWidth();
					$('#catselectbody').html($('#catselectbody').html() + data);
				}
				categoryselector.checkIfAlreadySelected();
			}).ajaxStart(function(){
				categoryselector.displayLoading();
			});
		  } else {
		  	categoryselector.choseCat(forumid,catid);
		  }
		  
	},
	choseCat : function (forumid,catid) {
	
		$.get(basehref + 'edit/form2/categoryselector/getselectedcatbacktrace.comp',{loadid : catid, forumid : forumid, fieldname : $('fieldname').value, fieldid : $('fieldid').value},function(data){
			var ID = catid.split(".");
			if(!$('#catselected_'+ID[0]+'_'+ID[1]).length){
				$('#catselectselected').html($('#catselectselected').html() + data);
				if($('#existingcategoriesfield').length && $('#existingcategoriesfield').val()){
					var fieldToBeField = $('#' + $('#existingcategoriesfield').val());					
					
					var currentValues = new Array();
					if(fieldToBeField.val()){
						currentValues = fieldToBeField.val().split(',');
					}
					
					if(currentValues.indexOf(catid) == -1){
						
						currentValues.push(catid);
						$('#'+$('#existingcategoriesfield').val()).val(currentValues.join(","));
					}
				}
			}
		});
	},
	removeTree : function (level) {
		
		$('#catselectbody .fields').each(function(i){
			var fid = this.id;
			var fielddata = fid.split("_");
			flevel = fielddata[1];
			if(flevel >= level){
            	document.getElementById('catselectbody').removeChild(this); 
			}
		});		
		return 1;
	},
	removeSelected : function (removeid) {
		if(removeid){
			$('#catselectselected #'+removeid).remove();
			var elements = removeid.split("_");//catselected_<%$ID->[0]%>_<%$ID->[1]%>
			var catid = elements[1]+'.'+elements[2];
			if($('#existingcategoriesfield').length && $('#existingcategoriesfield').val()){
				var fieldToBeField = $('#'+$('#existingcategoriesfield').val());
				
				var currentValues = fieldToBeField.val().split(',');
				var newValues = [];
				currentValues.each(function(i){
					if(this!=catid)newValues.push(this);
				});
				$('#'+$('#existingcategoriesfield').val()).val(newValues.join(","));        		
			}
		}
	},
	clicked: function (catid) {
		var mydiv = $('#row'+catid);
		var classn = mydiv.attr("class");
		if(classn == 'catselectrow'){
			mydiv.attr("class", 'catselectrowselected');
		}
		if(classn == 'catselectrownochild'){
			mydiv.attr("class", 'catselectrownochildselected');
		}
		var myparentinfo = $('#row'+catid).parent().attr("id").split("_");
		var mylevel = myparentinfo[1];
		//var allclicked = $$('div.catselectrowselected');
		$('#catselectbody .catselectrowselected').each(function(i){
			var parentinfo = this.parentNode.id.split("_");
			var level = parentinfo[1];			
			if(mylevel <=level && this.id != $('#row'+catid).attr("id")){
				//alert(level + '--' + mylevel);
				this.className = "catselectrow";
			}
		});
	},
	checkIfAlreadySelected : function (){
		var allSelected = $('#catselectselected .selectedids');
		var allNoChild = $('#catselectbody .catselectrownochild');
		
		allNoChild.each(function(i){
			var el = this;
			allSelected.each(function(j){
				var sel = this;
				var catid = el.id.replace('row', '');
				//alert(catid + '----' + sel.value);
				if(catid == sel.value){
					el.className = 'catselectrownochildselected';
				} else {
					el.className = 'catselectrownochild';
				}
			});
		});
	},
	displayLoading : function () {
		$('#loadingimg').appendTo($('#catselectbody')).show();
	},
	remLoading : function () {
		$('#catselectbody #loadingimg').remove();
	},
	initialFill : function (forumid,sourcefield) {
		if($('#'+sourcefield).length && $('#'+sourcefield).val()){
			var existing = $('#'+sourcefield).val().split(",");
			existing.each(function(i){
				categoryselector.choseCat(forumid,this);
			});
		}
	},
	extendWidth : function () {
		var w = ($('#catselectbody .fields').lenth+1)*188;
		$('#widthextender').css('width', w+'px');
	},
	resetForm : function () {
		$('#catselectselected').html('Currently selected:');
		categoryselector.checkIfAlreadySelected();
		var firsttab = $('#firsttab').val();
		//alert(firsttab);
		if(firsttab){
			categoryselector.switchtab(forumid, firsttab)
		}
		if('#'+$($('#existingcategoriesfield').val())){
			$('#'+$('#existingcategoriesfield').val()).empty();
		}
	}
};
