// FINDER

// ------------------------------------------------------------------------------
// Default
// ------------------------------------------------------------------------------

var option_1_select = "";
var option_2_select = "";
var option_3_select = "";

var option_1_content = "";
option_1_content = option_1_content + generate_button("klassen", 		"Ganze Schulklassen/Kurse",		    "option_1_schulklassen()");
option_1_content = option_1_content + generate_button("schuler", 		"Einzelne Sch&uuml;ler/innen",			"option_1_schuler()");
option_1_content = option_1_content + generate_button("lehrer", 		"Lehrer/innen (Fortbildungen)",		"option_1_lehrer()");
option_1_content = option_1_content + generate_button("studenten", 		"Studierende",						"option_1_studenten()");

var labors_default = "Das Suchergebnis wird nach Wahl der drei Optionen angezeigt.";


// ------------------------------------------------------------------------------
// Finder Functions
// ------------------------------------------------------------------------------
// onLoad
addLoadEvent(init_finder);
// init_search
function init_finder(){
	change_innerHTML("option_1", option_1_content);
}

// find_labors
function find_labors(){
	// Variablen initialisieren
	var content ="<ul>";
	var count=0;
	// Labore durchsuchen
	for (i=0; i<labors.length; i++){
		// Labor gefunden
		if ( is_in(labors[i][option_1_select][option_2_select], option_3_select) ){
			// Anzahl der Funde erh&ouml;hen
			count++;
			// Ergbiss der schreiben
			content = content+"<li><a onmouseover='labor_pic_show("+i+")' onmouseout='MM_swapImgRestore()' class='link_finder' href='"+labors[i]["url"]+"'><span class='bold'>"+labors[i]["name"]+"</span> "+labors[i]["name2"]+"</a></li>";
		}
	}
	
	content = content+"</ul>";
	// Ergebnmiss anzeigen
	change_innerHTML("finder_result", content);
}

// change_option_3_content
function change_option_3_content(){
	// Variablen initialisieren
	var content ="";
	naw_bio = false;
	naw_che = false;
	naw_phy = false;
	naw_geo = false;
	bio = false;
	phy = false;

	geo = false;
	che = false;
	
	informatik = false;
	math = false;
	
	// content von option_3 ermitteln falls option_2_select gsetzt 
	if (option_2_select != ""){
		for (i=0; i<labors.length; i++){
			if (is_in(labors[i][option_1_select][option_2_select], "NaWi_bio")) 	naw_bio = true;
			if (is_in(labors[i][option_1_select][option_2_select], "NaWi_che")) 	naw_che = true;
			if (is_in(labors[i][option_1_select][option_2_select], "NaWi_phy")) 	naw_phy = true;
			if (is_in(labors[i][option_1_select][option_2_select], "NaWi_geo")) 	naw_geo = true;
			if (is_in(labors[i][option_1_select][option_2_select], "Biologie"))		bio = true;
			if (is_in(labors[i][option_1_select][option_2_select], "Chemie"))		che = true;
			if (is_in(labors[i][option_1_select][option_2_select], "Physik")) 		phy = true;
			if (is_in(labors[i][option_1_select][option_2_select], "Geo")) 			geo = true;
			if (is_in(labors[i][option_1_select][option_2_select], "Informatik")) 	informatik = true;
			if (is_in(labors[i][option_1_select][option_2_select], "Math")) 		math = true;
		}
	
		// content schreiben
		if (naw_bio) content= content + generate_button("NaWi_bio", "NaWi/Biologie","option_3_nawi_bio()");
		if (naw_che) content= content + generate_button("NaWi_che", "NaWi/Chemie","option_3_nawi_che()");
		if (naw_phy) content= content + generate_button("NaWi_phy", "NaWi/Physik","option_3_nawi_phy()");
		if (naw_geo) content= content + generate_button("NaWi_geo", "NaWi/Geowissenschaften","option_3_nawi_geo()");
		
		if (bio) content= content + generate_button("Biologie", "Biologie","option_3_biologie()");
		if (che) content= content + generate_button("Chemie", "Chemie","option_3_chemie()");
		if (phy) content= content + generate_button("Physik", "Physik","option_3_physik()");
		if (geo) content= content + generate_button("Geo", "Geowissenschaften","option_3_geowissenschaften()");
		
		if (informatik) content= content + generate_button("Informatik", "Informatik","option_3_informatik()");
		if (math) content= content + generate_button("Math", "Mathematik","option_3_math()");
	}
	
	// content wechseln
	change_innerHTML("option_3", content);
}

// change_option_2_content
function change_option_2_content(){
	// Variablen initialisieren
	var content ="";
	var grund = false;
	var mittel = false;
	var ober = false;
	// content ermitteln
	for (i=0; i<labors.length; i++){
		if ((labors[i][option_1_select]["gst"]) !="") 	grund = true;
		if ((labors[i][option_1_select]["ost"]) !="") 	ober = true;
		if ((labors[i][option_1_select]["mst"]) !="") 	mittel = true;
	}
	// content schreiben
	if (grund) 	content = content + generate_button("gst", "Grundstufe (1.&#8211;6.)",		"option_2_grundstufe()");
	if (mittel) content = content + generate_button("mst", "Mittelstufe (7.&#8211;10.)",	"option_2_mittelstufe()");
	if (ober) 	content = content + generate_button("ost", "Oberstufe (11.&#8211;13.)",		"option_2_oberstufe()");
	// content wechseln
	change_innerHTML("option_2", content);
}


// clear_labors
function clear_labors(){
	change_innerHTML("finder_result", labors_default);
}

// labor_pic_show
function labor_pic_show(num){
	MM_swapImage('finder_logo','',labors[num]["url_pic"],1);
}

// change_innerHTML
function change_innerHTML(id, html){
	MM_findObj(id).innerHTML = html;
}

function exist(v){
	if (!(typeof v == 'undefined')) return true;
	else return false;
}

// is_in pr&uuml;ft ob str_2 in str_1 enthalten ist
function is_in(str_1, str_2){
		return str_1.search(str_2)!=-1;

}
// ------------------------------------------------------------------------------
// Option Functions
// ------------------------------------------------------------------------------

// Funktionen Option 1
function option_1_schuler(){ 
	option_2_select = ""; 
	option_3_select = ""; 
	option_1_active("schuler"); 	
	change_option_2_content(); 
	change_option_3_content(); 
	clear_labors()
}

function option_1_lehrer(){
	option_3_select = ""; 
	option_2_select = ""; 
	option_1_active("lehrer"); 	
	change_option_2_content(); 
	change_option_3_content(); 
	clear_labors()
}

function option_1_schulklassen(){
	option_3_select = ""; 
	option_2_select = ""; 
	option_1_active("klassen"); 	
	change_option_2_content(); 
	change_option_3_content(); 
	clear_labors()
}

function option_1_studenten(){
	option_3_select = ""; 
	option_2_select = ""; 
	option_1_active("studenten"); 
	change_option_2_content(); 
	change_option_3_content(); 
	clear_labors()
}

// Funktionen Option 2
function option_2_grundstufe(){		option_3_select = ""; option_2_active("gst");  change_option_3_content(); clear_labors()}
function option_2_oberstufe(){ 		option_3_select = ""; option_2_active("ost");  change_option_3_content(); clear_labors()}
function option_2_mittelstufe(){ 	option_3_select = ""; option_2_active("mst");  change_option_3_content(); clear_labors()}

// Funktionen Option 3
function option_3_biologie (){ 			option_3_active("Biologie"); find_labors();}
function option_3_physik   (){ 			option_3_active("Physik"); find_labors();}

// Special NaWi Block
function option_3_nawi_bio (){ 			option_3_active("NaWi_bio"); find_labors();}
function option_3_nawi_che (){ 			option_3_active("NaWi_che"); find_labors();}
function option_3_nawi_phy (){ 			option_3_active("NaWi_phy"); find_labors();}
function option_3_nawi_geo (){ 			option_3_active("NaWi_geo"); find_labors();}

function option_3_geowissenschaften(){	option_3_active("Geo"); find_labors();}
function option_3_chemie (){			option_3_active("Chemie"); find_labors();}

function option_3_informatik(){			option_3_active("Informatik"); find_labors();}
function option_3_math(){				option_3_active("Math"); find_labors();}

// ------------------------------------------------------------------------------
// Button
// ------------------------------------------------------------------------------

// option_1_active
function option_1_active(x){  
	// Button intern aktivieren
	if (!MM_findObj(x).act ){
		MM_findObj(x).act = true; 
		if(option_1_select!="") MM_findObj(option_1_select).act = false; 
		sub_over(x);
		temp = option_1_select; 
		option_1_select = x; 
		if(temp!="")sub_out(temp);
	}
}

// option_1_active
function option_2_active(x){  
	// Button intern aktivieren
	if (!MM_findObj(x).act ){		
		MM_findObj(x).act = true; 
		if(option_2_select!="") MM_findObj(option_2_select).act = false; 
		sub_over(x); 
		temp = option_2_select; 
		option_2_select = x; 
		if(temp!="") sub_out(temp);
	}
}

// option_1_active
function option_3_active(x){  
	// Button intern aktivieren
	if (!MM_findObj(x).act ){	
		MM_findObj(x).act = true; 
		if(option_3_select!="") MM_findObj(option_3_select).act = false; 
		sub_over(x);
		temp = option_3_select;
		option_3_select = x;
		if(temp!="") sub_out(temp);
	}
}

// Button Mouseover
function sub_over(x){ if (!MM_findObj(x).act) css("add",MM_findObj(x),"active_btn"); }
function sub_out(x){ if (!MM_findObj(x).act) css("remove",MM_findObj(x),"active_btn"); }

// generate_button
function generate_button(id, name, action){
	return "<div class='btn_info'  id='"+id+"' onmouseover='javascript:sub_over(this.id)' onmouseout='javascript:sub_out(this.id)' onclick='javascript:"+action+";' >"+name+"</div>"	
}

// ------------------------------------------------------------------------------
// CSS
// ------------------------------------------------------------------------------
function css(a,o,c1,c2)
{
  switch (a){
    
	case 'swap':
      o.className=!css('check',o,c1)?o.className.replace(c2,c1): // "<-" entfernt
      o.className.replace(c1,c2);
    break;
    case 'add':
      if(!css('check',o,c1)){o.className+=o.className?' '+c1:c1;}
    break;
    case 'remove':
      var rep=o.className.match(' '+c1)?' '+c1:c1;
      o.className=o.className.replace(rep,'');
    break;
    case 'check':
      return new RegExp('\\b'+c1+'\\b').test(o.className)
    break;
  }
}

// ------------------------------------------------------------------------------
// MM
// ------------------------------------------------------------------------------
// Macromedia ImageSwap
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
