var schooltype = null;

function updateInfo(id){
	p = $(id + 'Panel');
	if(p && !p.isExpanded)showPanel(id);
	//if(bpStatus == 2)return;
	$('tx-ndhevents-pi1-searchResult').innerHTML = '';
	f = $$('div.tx-ndhevents-pi1-checkbox input');
	varstring = 'tx_ndhevents_pi1[ajaxRequest]=1';
	var url = window.location.href;
		//if(url.indexOf('?')>-1)varstring = '';
		//else varstring = '?';
	checkedBoxes = false;
	f.each( function(c){
			if(c.checked){
				checkedBoxes = true;
				varstring += '&' + c.name + '=' + c.value;
			}
								 });
	if(!checkedBoxes)$('tx-ndhevents-pi1-searchResult').innerHTML = '';
	else {
		var myAjax = new Ajax.Updater(
					'tx-ndhevents-pi1-searchResult', 
					url, 
					{
						method: 'post', 
						parameters: varstring
					});
	}
	//myAjax = new Ajax.Request( url, { method: 'post', parameters: varstring, onComplete:eval('display' + vars['action'])})
}

function updateBp(id){

	c = $(id + '_checkbox').checked;
	if(bpStatus == 2){
		x = confirm('Wenn Sie die Zielgruppe ändern geht Ihre Auswahl der Bildungsplan-Fächer verloren. Wollen Sie trotzdem fortfahren?');
		if(!x){
			//alert($(id + '_checkbox').checked);
			if(c)$(id + '_checkbox').checked = false;
			else $(id + '_checkbox').checked = true;
			return false;
		}
		else {
			$('bildungsplan_fach_select').innerHTML = '';
			f = $$('div#bildungsplanPanel div.tx-ndhevents-pi1-checkbox');
	
			for(var i=0; i<f.length; i++){
				cb = $(f[i].id + '_checkbox');
				if(cb){
						cb.checked = false;
						f[i].style.display='none';
				}
				else f[i].style.display='none';
				//else alert(f[i].id);
			}
			bpStatus = 0;
		}
	}
	else if(bpStatus == 1){
		checkSchooltypes();
	}

}
		

function showPanel(id){
	if(id=='bildungsplan' && bpStatus == 2)return;
	tmp = $$('div.tx-ndhevents-pi1-' + id + 'Wrapper a');
	obj = tmp[0];
	panel = $(id + 'Panel');
	if(typeof panel.isExpanded == 'undefined')panel.isExpanded = false;
	if(panel.isExpanded){
			reducePanel(panel);
			obj.id = '';
			panel.isExpanded = false;
	}
	else {
			if(id=='bildungsplan'){
				if(!checkSchooltypes())return;
			}
			else expandPanel(id);
			if(showPanel.arguments.length >2)$(id + 'Panel').style.height = showPanel.arguments[2] + 'px';
			reduceAllPanels(panel);
			if($('activeLink'))$('activeLink').id = '';
			obj.id = 'activeLink';
			panel.isExpanded = true;
	}
	//blur(obj);
	return false;
}

function reduceAllPanels(excludePanel){
	
	panels =  $$('div.tx-ndhevents-pi1-panel');
	panels.each( function(p){
			if(p!=excludePanel && typeof p.isExpanded != 'undefined' && p.isExpanded)reducePanel(p);
								 }); //&& p.id != 'bildungsplanPanel'
}

function reducePanel(p){
	if(p.id=='bildungsplanPanel' && bpStatus == 2)return;
	p.style.height = 'auto';
	f = $$('div#' + p.id + ' div.tx-ndhevents-pi1-checkbox');
	for(var i=0; i<f.length; i++){
		if($(f[i].id + '_checkbox')){
				if($(f[i].id + '_checkbox').checked!=true)f[i].style.display='none';
				else f[i].style.display='';
		}
		else f[i].style.display='none';
		//else alert(f[i].id);
	}
	p.isExpanded = false;
	
}

function expandPanel(id){
	f = $$('div#' + id + 'Panel div.tx-ndhevents-pi1-checkbox');
	for(var i=0; i<f.length; i++){
		f[i].style.display='';
	}
	$(id + 'Panel').isExpanded = true;
}

function check(id){
	c = $(id + '_checkbox');
	if(!c)return;
	if(c.checked == true)c.checked = false;
	else c.checked = true;
	if(id.indexOf('schooltypes')==0)updateBp(id);
}

var bpStatus = 0; // 0 = nichts ausgewählt, 1 = error, 2 = fächer ausgewählt


function checkSchooltypes(){
	f = $$('div#schooltypesPanel div.tx-ndhevents-pi1-checkbox');
	counter = 0;
	for(var i=0; i<f.length; i++){
		if($(f[i].id + '_checkbox') && $(f[i].id + '_checkbox').checked==true){
			schooltype =  $(f[i].id + '_checkbox').value;
			counter++;
		}
		//else alert(f[i].id);
	}
	// kein schooltype ausgewählt!
	if(!counter){
		$('bildungsplan_fach_select').innerHTML = '<div class="errorMessage"><br />Bitte wählen Sie zuerst eine Zielgruppe!</div>';
		$('bildungsplan_fach_select').style.display = '';
		expandPanel('schooltypes');
		bpStatus = 1;
		return false;
	}
	// mehrere schooltypes ausgewählt!
	if(counter>1){
		$('bildungsplan_fach_select').innerHTML = '<div class="errorMessage"><br />Bitte wählen Sie nur eine Zielgruppe aus!</div>';
		$('bildungsplan_fach_select').style.display = '';
		bpStatus = 1;
		return false;
	}
	else {
		if(bpStatus==1)$('bildungsplan_fach_select').innerHTML = ''; // Bildungsplan-Select zurücksetzen
		validFach = [];
		bp.each( function(p){
			if(p[1]==schooltype){
				if(typeof validFach[p[3]] == 'undefined')validFach[p[3]] = fach[p[3]];
			}
		});
		if(validFach.length > 0){
			for(k in validFach){
				if(typeof validFach[k] !='function' && !$('bildungsplan_fach_' + k))addCheckbox1(k,validFach[k]);
				else if($('bildungsplan_fach_' + k))$('bildungsplan_fach_' + k).style.display = '';
			}
			//$('bildungsplanPanel').innerHTML = $('bildungsplanPanel').innerHTML + '<div id="bildungsplan_klassenstufe_select" class="tx-ndhevents-pi1-checkbox" style="display:none;border-top:2px solid #999;border-bottom:2px solid #999" >Bitte wählen Sie ein Klassenstufe:';
			
			// ks enthält die Label der Klassenstufen
			for(k in ks){
				if(typeof ks[k] !='function'){
					if(ks[k].length < 3)ks[k] = 'Klasse ' +  ks[k];
					if(!$('bildungsplan_klassenstufe_' + k))$('bildungsplan_klassenstufe_select').innerHTML = $('bildungsplan_klassenstufe_select').innerHTML + '<div id="bildungsplan_klassenstufe_' + k + '" class="tx-ndhevents-pi1-checkbox" style="display:none" ><input name="tx_ndhevents_pi1[klassenstufe][]" value="' + k + '"  id="bildungsplan_klassenstufe_' + k + '_checkbox" type="checkbox" ><label onclick="check(\'bildungsplan_klassenstufe_' + k + '_checkbox\')">' + ks[k] + '</label></div>';
				}
			}
			$('bildungsplan_fach_select').style.display = '';
			//$('bildungsplanPanel').innerHTML = $('bildungsplanPanel').innerHTML + '</div>';

		}
		else {
			$('bildungsplan_fach_select').innerHTML = '<div class="errorMessage"><br />Für diese Zielgruppe sind keine Bildungsplan Fächer eingetragen!</div>';
			$('bildungsplan_fach_select').style.display = '';
			bpStatus = 1;
			return false;
		}
		reducePanel($('schooltypesPanel'));
		return true;
	}
}


function addKlassenstufen(fachId){
	//reducePanel('bildungsplan');
   $('tx-ndhevents-pi1-searchResult').innerHTML = '';
	f = $$('div#bildungsplanPanel div.tx-ndhevents-pi1-checkbox');
	hasChecked = false;
	for(var i=0; i<f.length; i++){
		if($(f[i].id + '_checkbox') && f[i].id.indexOf('bildungsplan_fach')==0){
			if($(f[i].id + '_checkbox').checked!= true){
				f[i].style.display='none';
			}
			else hasChecked = true;
			//alert(f[i].id + '_checkbox: ' +  $(f[i].id + '_checkbox').checked);
		}
		
	}
	if(!hasChecked)d = 'none';
	else d = '';
	$('bildungsplan_klassenstufe_select').style.display = d;
	bp.each( function(p){
			if(p[1]==schooltype && p[3] == fachId && hasChecked){
				for(i=0;i< p[2].length;i++){
				//kl = p[2][0];
					$('bildungsplan_klassenstufe_' + p[2][i]).style.display = d;
				}
			}
		});
	if(!hasChecked){
		for(var i=0; i<f.length; i++){
			if($(f[i].id + '_checkbox') && f[i].id.indexOf('bildungsplan_klassenstufe')==0){
				$(f[i].id + '_checkbox').checked = false;
				f[i].style.display = 'none';
			}
		}
		$('bildungsplan_fach_select').innerHTML = '';
		$('bildungsplanPanel').isExpanded = false;
		
		bpStatus = 0;
	}
	else bpStatus = 2;
}

function in_array(needle, haystack){
	haystack.each( function(p){
			if(p == needle)return true;
		});
	return false;
}

function Numsort(a,b){ 
	return a-b; 
}
	

function addCheckbox1(value,label) {
	if(!$('bildungsplan_fach_' + value)){
		$('bildungsplan_fach_select').innerHTML = $('bildungsplan_fach_select').innerHTML + '<div id="bildungsplan_fach_' + value + '" class="tx-ndhevents-pi1-checkbox" style="" ><input name="tx_ndhevents_pi1[fach][]" value="' + value + '"  id="bildungsplan_fach_' + value + '_checkbox" type="checkbox" onclick="addKlassenstufen(' + value + ')"><label >' + label + '</label></div>';
	}
}

function showMap(){
	$('singleViewMap').style.display = '';
	window.scrollTo(600,2000);
	loadMap();
}

function updatePanels(){
		panels =  $$('div.tx-ndhevents-pi1-panel');
		panels.each( function(p){
			expandPanel(p.id.substr(0,p.id.length - 5));
			reducePanel(p);
								 }); //&& p.id != 'bildungsplanPanel'
}

window.onload=updatePanels;
