var srch = {
	
	configData: { '0': ['all', 'page', 'produkt', 'produkt_cat', 'instalator', 'address', 'service', 'faq', 'news', 'training'],
				 '59': ['all', 'page'],
				 '63': ['all', 'page', 'produkt', 'produkt_cat'],
				 '64': ['all', 'page', 'instalator', 'address', 'faq'],
				 '65': ['all', 'page', 'service', 'faq'],
				 '66': ['all', 'page', 'faq'], 
				 '67': ['all', 'page', 'news'],
				 '68': ['all', 'page', 'news'],
				 '69': ['all', 'page'],
				 '71': ['all', 'page'],
				 '70': ['all', 'page', 'training'],
				 '72': ['all', 'page']
				 },
	checkedSections: new Array(),

	init: function() {
		var sectionsPanels = document.getElementsByName('SubTreeArray[]');
		for(var i=0; i<sectionsPanels.length; i++) {
			if(sectionsPanels[i].checked) {
				this.arrayAdd(sectionsPanels[i].value, this.checkedSections);
			}
		}
		//alert(this.checkedSections.toString());
		this.refreshClassPanel();
	},
	
	setClass: function(el) {
		if (el.checked) {
			//alert('dodajemy');
			if (el.value>0) {
				$("#chk_all").removeAttr("checked");
			} else {
				$("input[name='SearchContentClassID[]']").removeAttr("checked");
				$("#chk_all").attr("checked", "checked");
				this.checkedSections = [];				
			}
		} else {
			//alert('usuwamy');
			if (el.value=="") {
				$("#chk_all").attr("checked", "checked");
			}
		}
	},
	
	setSection: function(el) {
		//alert(el.value);
		var tmpVal = el.value
		if (tmpVal=='') {tmpVal = '0';}
		if (el.checked) {
			//alert('dodajemy');
			if (tmpVal>0) {
				$("#SubTree_").removeAttr("checked");
				this.arrayRemove("0", this.checkedSections);
			} else {
				$("input[name='SubTreeArray[]']").removeAttr("checked");
				$("#SubTree_").attr("checked", "checked");
				this.checkedSections = [];				
			}
			this.arrayAdd(tmpVal, this.checkedSections);
		} else {
			//alert('usuwamy');
			this.arrayRemove(tmpVal, this.checkedSections);
		}
		//alert(this.checkedSections.toString());
		this.refreshClassPanel();
	},
	
	refreshClassPanel: function() {
		if (this.checkedSections.length==0) {
			$(".docType").attr("class", "docType");
			$(".docTypeChk").removeAttr("disabled");
			$("#SubTree_").attr("checked", "checked");
		} else {
			$(".docType").attr("class", "docType disabled");
			$(".docTypeChk").attr("disabled", "disabled");
			var localClassList = new Array();
			for (var i=0; i<this.checkedSections.length; i++) {
				for (var j=0; j<this.configData[this.checkedSections[i]].length; j++) {
					this.arrayAdd(this.configData[this.checkedSections[i]][j], localClassList);
				}
			}
			//alert(localClassList.toString());

			//var panelList = $(".docType");
			for (var i=0; i<localClassList.length; i++) {
				//alert("#chk_"+localClassList[i]);
				$("#chk_"+localClassList[i]).removeAttr("disabled");
				$("#div_"+localClassList[i]).attr("class", "docType");
			}
		}		
	},
	
	showPanel: function(sourceElement) {
		var myPanel = $('#advancePanel');
		if (sourceElement.checked) {
			myPanel.show("fast");
		} else {
			myPanel.hide("fast");
			$(".docTypeChk").removeAttr("checked");
			$("#chk_all").attr("checked", "checked");
			
			$("input[name='SubTreeArray[]']").removeAttr("checked");
			$("#SubTree_").attr("checked", "checked");
		}
	},

	arrayAdd: function(element, table) {
		var jest = false;
		for (var i=0; i<table.length; i++) {
			if (table[i] == element) {
				jest = true;
				break;
			}
		}
		if (jest == false) {
			table.push(element);
		}
	},
	
	arrayRemove: function(element, table) {
		for (var i=0; i<table.length; i++) {
			if (table[i] == element) {
				table.splice(i,1);
				break;
			}
		}
	}
	
}
