_standartAdmin = Class.create();
_standartAdmin.prototype = {
	initialize: function(){
		this.img_dir = '';
		this.jsData = new Object();
		this.soData = new Object();
		this.xpData = new Object();
		this.blData = new Object();
		this.erData = new Object();
	},
	
	initData: function(dt){
		this.img_dir = dt;
		var js = $('sectors').value;
		var so = $('orientation').value;
		var bl = $('bundesland').value;		
		var er = $('job_type').value;		

		if(js != ''){
			this.saveInitData(js, 'js');
		}
		if(so != ''){
			this.saveInitData(so, 'so');
		}
		/*
		if(xp != ''){
			this.saveInitData(xp, 'xp');
		}
		*/
		if(bl != ''){
			this.saveInitData(bl, 'bl');
		}	
		if(er != ''){
			this.saveInitData(er, 'er');
		}
		document.getElementsByClassName('childs').each(function(elm){
			elm.hide();
		}.bind(this));
		
		this.sectorswitch('1', 'js-');
		this.sectorswitch('1', 'so-');
		this.sectorswitch('1', 'er-');
		this.sectorswitch('1', 'bl-');
	},
	
	saveInitData: function(dt, prefix){
		dt.split(',').each(function(row,idx){
			this[prefix + 'Data'][row] = row;
			$('cb'+prefix+'-' + row).checked = true;
		}.bind(this));
	},
	
	sectorswitch: function(id, pref){
		$('childs' + pref + id).toggle();
		var imgid = 'img' + pref + id;
		
		if($(imgid).hasClassName('plus')){
			$(imgid).removeClassName('plus');
			$(imgid).addClassName('minus');
			if($(imgid).hasClassName('bottom')){
				$(imgid).src = this.img_dir + "minusbottom.gif";
			}else{
				$(imgid).src = this.img_dir + "minus.gif";
			}
		}else{
			$(imgid).removeClassName('minus');
			$(imgid).addClassName('plus');
			if($(imgid).hasClassName('bottom')){
				$(imgid).src = this.img_dir + "plusbottom.gif";
			}else{
				$(imgid).src = this.img_dir + "plus.gif";
			}
		}
	},
	
	parentCheck: function(id, pref){
		this.setChilds(id, $('cb' + pref + id).checked, pref);
		this.setData(id, pref);
	},
	
	setChilds: function(id, stat, pref){
		document.getElementsByClassName('cbpar' + pref + id).each(function(elm){
			elm.checked = stat;
			this.setData(elm.id.split('-')[1], pref);
			if(elm.hasClassName('parent')){
				this.setChilds(elm.id.split('-')[1], stat, pref);
			}
		}.bind(this));
	},
	
	setData: function(id, pref){
		this[pref.split('-')[0] + 'Data'][id] = $('cb' + pref + id).checked?id:'';
	},
	
	sectorCheck: function(id, pref,par_id){	
	  
		if(par_id != 0)
		{
		   this.setParent(par_id, pref);
		}		
	   
	   this.setData(id, pref);		
	},
	
	setParent: function(id,pref) {
	   var parent_checked = $('cb' + pref + id).checked;
	   
	   if(parent_checked == false && id != 1)
	   {
	      $('cb' + pref + id).checked = true;
	      this.setData(id, pref);
	   } else {
	      var i = 0
	      document.getElementsByClassName('cbpar' + pref + id).each(function(elm){
   			if(elm.checked == true)
   			{
   			   i++;
   			}
   		}.bind(this));
   		
   		if(i==0)
   		{
   		   $('cb' + pref + id).checked = false;
   		   this.setData(id, pref);
   		}
	   }
	},
	
	parseData: function(){
		$('sectors').value = this.getSubmitData('js');
		$('orientation').value = this.getSubmitData('so');
		$('bundesland').value = this.getSubmitData('bl');
		$('job_type').value = this.getSubmitData('er');
	},
	
	getSubmitData: function(prefix){
		var dt='';
		for(var i in this[prefix + 'Data']){
			if(this[prefix + 'Data'][i] != '')
				dt += this[prefix + 'Data'][i] + ',';
		}
		if(dt != ''){
			dt = dt.substr(0,dt.length-1);
		}
		return dt;
	},
	
	submitData: function(){
		this.parseData();
	}
};

standartAdminJS = new _standartAdmin();