// dhtmlwin
var dragapproved=false;
var minrestore=0;
var initialwidth,initialheight;
var ie5=document.all&&document.getElementById;
var ns6=document.getElementById&&!document.all;

function iecompattest(){
  return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

function drag_drop(e){
  if (ie5&&dragapproved&&event.button==1){
    document.getElementById("dwindow").style.left=tempx+event.clientX-offsetx+"px";
    document.getElementById("dwindow").style.top=tempy+event.clientY-offsety+"px";
  } else if (ns6&&dragapproved){
    document.getElementById("dwindow").style.left=tempx+e.clientX-offsetx+"px";
    document.getElementById("dwindow").style.top=tempy+e.clientY-offsety+"px";
  }
}

function initializedrag(e){
  offsetx=ie5? event.clientX : e.clientX;
  offsety=ie5? event.clientY : e.clientY;
  document.getElementById("dwindowcontent").style.display="none" ;//extra
  tempx=parseInt(document.getElementById("dwindow").style.left);
  tempy=parseInt(document.getElementById("dwindow").style.top);
  
  dragapproved=true;
  document.getElementById("dwindow").onmousemove=drag_drop;
}

function loadwindow(url,width,height){
  if (!ie5&&!ns6)window.open(url,"","width=width,height=height,scrollbars=1");
  else{
    document.getElementById("dwindow").style.display=''
    document.getElementById("dwindow").style.width=initialwidth=width+"px"
    document.getElementById("dwindow").style.height=initialheight=height+"px"
    document.getElementById("dwindow").style.left="50px"
    document.getElementById("dwindow").style.top=ns6? window.pageYOffset*1+50+"px" : iecompattest().scrollTop*1+50+"px"
    
    document.getElementById("cframe").style.width=(width-2)+"px"
    document.getElementById("cframe").style.height=(height-2)+"px"
    document.getElementById("cframe").src=url
  }
}

function closeit(){
  document.getElementById("dwindow").style.display="none"
}

function stopdrag(){
  dragapproved=false;
  document.getElementById("dwindow").onmousemove=null;
  document.getElementById("dwindowcontent").style.display="" //extra
}

// Ajaxrequest
var resObjekt, level, objID, activLevel = new Array();

if(navigator.appName.search("Microsoft") > -1) resObjekt = new ActiveXObject("MSXML2.XMLHTTP");
else resObjekt = new XMLHttpRequest();

function sndReq(object, sort, region){
	
	objID = object;
	
	
	
	if(region) addreq='&region='+region;
	else addreq='';
	
	// console.log('bridge.php?root='+object+'&sort='+sort+addreq);
	//alert(root+'bridge.php?root='+object+'&sort='+sort+addreq);
	resObjekt.open('get', root+'bridge.php?root='+object+'&sort='+sort+addreq, true);
	
	resObjekt.onreadystatechange = handleResponse;
	resObjekt.send(null);
}
function srchReq(searchstr, order){
	resObjekt.open('get', root+'bridge.php?root='+txtObject.level[0].id+'&search='+searchstr+'&order='+order, true);
	resObjekt.onreadystatechange = handleResponse;
	resObjekt.send(null); 
}
function linkReq(){
	resObjekt.open('get', root+'bridge.php?linklist=1', true);
	resObjekt.onreadystatechange = txtreplace;
	resObjekt.send(null); 
}
function genContact(){
	outdoc = document.getElementById("output");
	outdoc.innerHTML = '<a href="javascript:document.location.href=\'{/literal}{php} echo $_SERVER["PHP_SELF"]."?".$_SERVER["QUERY_STRING"]; {/php}{literal}\'">Zur&uuml;ck zur &Uuml;bersicht</a>';
}

function txtreplace(){
	if(resObjekt.readyState == 4){
		// document.(resObjekt.responseText);
		document.getElementById('text-link').innerHTML = '<b>Linkübersicht</b><br /><br />'+resObjekt.responseText;
	}
}

function handleResponse(){
	
	outdoc = document.getElementById("output");
	outlevel = document.getElementById("output_level");
	
	loader = document.getElementById("loaderlay");
	if(resObjekt.readyState < 4){
		loader.style.display = 'inline';
	}else if(resObjekt.readyState == 4){
		
		setTimeout("loader.style.display = 'none';", 500);
		

		var innerRes='';
		var innerSel='';
		var JSONData = resObjekt.responseText;
		
		txtObject = JSONData.parseJSON();

		
		// switch colorclass
		if(document.getElementById("nav"+objID)){
			try {
				document.getElementById("nav"+activLevel[txtObject.level.length - 1]).className = 'nav'+(txtObject.level.length - 1);
			}catch(e){}
			// document.getElementById("nav"+objID).className = 'navA'+(txtObject.level.length - 1);
			document.getElementById("nav"+objID).className = 'navA1';
		}
		activLevel[txtObject.level.length - 1] = objID;
		//
		
		if(txtObject.results!=undefined) textVal = txtObject.results[0].label.search(/-o-/);
		
		unsetLevel(txtObject.level.length, textVal);
		
		if(txtObject.level.length==1 && textVal==-1){
			
			// Hauptnav
			innerNav = genTable(txtObject);
						
			navDiv = document.getElementById("nav");
			navDiv.innerHTML = innerNav;
			
		}else if(txtObject.level.length==2 && textVal==-1){
			
			// Unav
			var inneruNav='';
			inneruNav = genTable(txtObject);
			
			unavDiv = document.getElementById("unav");
			unavDiv.innerHTML = inneruNav;
			
		}else if(txtObject.level.length==3 && textVal==-1){
			
			// Uunav
			var inneruuNav='';
			inneruuNav = genTable(txtObject);
			
			uunavDiv = document.getElementById("uunav");
			uunavDiv.innerHTML = inneruuNav;
		}
		if(textVal!=-1){
			
			$('text').setStyle('display','none');
			$('content').setStyle('background-color','#ffffff');
			
			searchAdd = '<br /><br /><!-- Volltextsuche:<br><input type="text" id="suchfeld" name="suchfeld" value="';
			if(txtObject.search) {
				searchAdd += txtObject.search;
				//txtObject.level[0].id = 
			}
			searchAdd += '"> <a href="javascript:srchReq(document.getElementById(\'suchfeld\').value,\'gruppe\');"><u>GO</u></a> //-->';
 			innerSel = '<table border="0"><tr><td valign="top">Bitte wählen Sie den Kanton<br>oder Ihre gew&uuml;nschte Region:<br><br><a href="#" onclick="javascript:sndReq('+txtObject.level[0].id+');" style="text-decoration:underline;color:#4E4E4F;font-size:11px;">ganze Schweiz</a> | <a href="javascript:window.scrollTo(0, 0);" style="text-decoration:underline;color:#4E4E4F;font-size:11px;">neue Suche</a><br><br>';
 			innerSel += '<select id="kanton" name="kanton" onchange="sndReq(\''+txtObject.level[0].id+'\',\'undefined\',encodeURI(this.value));"><option value="">';
 			if(txtObject.region) innerSel += txtObject.region;
 			else innerSel += "Kantone...";
 			innerSel += '</option><option value="Aargau">Aargau</option><option value="Appenzell Ausserrhoden">Appenzell Ausserrhoden</option><option value="Appenzell Innerrhoden">Appenzell Innerrhoden</option><option value="Basel-Land">Basel-Land</option><option value="Basel-Stadt">Basel-Stadt</option><option value="Bern">Bern</option><option value="Freiburg">Freiburg</option><option value="Genf">Genf</option><option value="Glarus">Glarus</option><option value="Graubünden">Graub&uuml;nden</option><option value="Jura">Jura</option><option value="Luzern">Luzern</option><option value="Neuenburg">Neuenburg</option><option value="Nidwalden">Nidwalden</option><option value="Obwalden">Obwalden</option><option value="Schaffhausen">Schaffhausen</option><option value="Schwyz">Schwyz</option><option value="Solothurn">Solothurn</option><option value="St. Gallen">St. Gallen</option><option value="Tessin">Tessin</option><option value="Thurgau">Thurgau</option><option value="Uri">Uri</option><option value="Waadt">Waadt</option><option value="Wallis">Wallis</option><option value="Zug">Zug</option><option value="Zürich">Z&uuml;rich</option></select><br><br>'+searchAdd+'</td>'+
 			//innerSel = searchAdd + '<table border="0"><tr><td valign="top"><h1>Ihre Resultate:</h1>Bitte wählen Sie Ihre gew&uuml;nschte <b>Region</b>.<br><br><a href="#" onclick="javascript:sndReq('+txtObject.level[0].id+');" style="text-decoration:underline;">Ganze Schweiz</a><br><br><br><a href="javascript:window.scrollTo(0, 0);" style="text-decoration:underline; font-weight:bold;">Neue Suche</a></td>'+
			      '<td valign="top"><a href="#"><img src="'+root+'images/map.gif" width="298" height="186" usemap="#1" border="0"></a></td></tr></table>'+
			      '<map name="1">'+
			      '<area shape="poly" style="cursor:pointer;" onclick="sndReq(\''+txtObject.level[0].id+'\',\'undefined\',\'ost\')" coords="213,155,221,141,218,130,223,122,228,126,231,123,232,136,240,144,248,144,251,138,265,136,271,150,277,149,279,146,274,138,278,133,276,130,270,130,270,119,282,110,286,119,298,119,298,110,292,110,298,82,292,76,289,76,287,82,281,83,282,88,275,92,258,82,258,74,232,72,234,67,234,59,241,41,244,42,244,33,238,30,237,25,214,14,200,13,187,8,187,11,182,11,182,13,191,14,191,18,190,22,195,26,202,45,201,50,194,57,200,58,200,69,194,75,200,81,199,90,182,108,182,113,188,116,194,114,200,111,206,115,204,123,208,127,208,141,206,146">'+
			      '<area shape="poly" style="cursor:pointer;" onclick="sndReq(\''+txtObject.level[0].id+'\',\'undefined\',\'west\')" coords="0,146,15,132,10,126,14,113,37,95,34,80,63,60,76,67,74,78,88,75,85,86,95,87,91,102,85,139,162,116,166,123,141,147,146,152,145,158,135,171,127,174,126,179,106,172,94,180,87,179,78,184,63,163,63,156,55,157,60,142,56,133,38,132,17,147">'+
			      '<area shape="poly" style="cursor:pointer;" onclick="sndReq(\''+txtObject.level[0].id+'\',\'undefined\',\'nord\')" coords="75,43,64,41,72,32,71,28,84,28,84,33,84,34,99,33,115,18,118,25,127,21,134,24,151,18,159,23,167,22,165,10,176,1,183,9,180,11,180,16,189,15,189,19,184,20,184,20,193,27,200,45,196,53,183,58,180,61,162,53,163,63,159,63,153,50,150,54,133,50,126,56,130,76,134,76,134,79,127,88,134,97,146,99,156,100,160,96,165,99,166,106,162,107,162,115,86,137,96,86,86,85,90,72,76,76,78,65,64,58">'+
			      '<area shape="poly" style="cursor:pointer;" onclick="sndReq(\''+txtObject.level[0].id+'\',\'undefined\',\'zentral\')" coords="128,56,134,52,150,56,153,54,158,64,164,65,164,55,179,62,182,62,188,57,198,59,198,68,192,74,192,76,198,82,197,89,181,107,180,114,166,120,164,115,164,108,168,106,167,98,160,93,156,98,135,95,129,88,136,80,136,74,131,74">'+
			      '<area shape="poly" style="cursor:pointer;" onclick="sndReq(\''+txtObject.level[0].id+'\',\'undefined\',\'sued\')"coords="202,187,196,187,195,178,187,172,192,165,184,160,178,160,166,149,163,143,167,140,166,129,168,120,178,117,193,118,201,112,204,116,203,124,207,128,207,140,205,145,212,155,202,168,202,174,206,181">'+
			      '<area shape="default" nohref>'+
			      '</map><h1 style="padding: 0 0 5px 0; margin:0px; color:#DB097B;">Ihr Suchresultat:</h1>';
			
			if (txtObject.total == 0 && !txtObject.search){
				
				innerRes = '<br>Auf Ihre Suche wurde kein Resultat gefunden.<br><br>&nbsp;<a href="javascript:sndReq('+txtObject.level[0].id+')">Zur&uuml;ck</a>';
			}else{
				
				// Content
				addHtml = '';
				for (i = 0; i < txtObject.results.length; ++i){
					
					if(i%2) addClass = ' class="line1"';
					else  addClass = ' class="line2"';
					
					newtxt = txtObject.results[i].label.replace(/ -o- /g, '&nbsp;</td><td valign="top"'+addClass+'>');
					addHtml += '<tr onmouseover="this.style.backgroundColor=\'#f2f1f0\'" onmouseout="this.style.backgroundColor=\'\'"><td valign="top"'+addClass+'>'+newtxt+'&nbsp;</td><td width="5%" valign="top"'+addClass+'>&nbsp;&nbsp;<a style="color:#000000;font-size:11px;" href="javascript:loadwindow(\''+root+'bridge.php?popup=1&root='+txtObject.level[0].id+'&id='+txtObject.results[i].id+'&level='+objID+'\',690,500);">Details</a></td></tr>';
				}
				
				if(txtObject.results && txtObject.results[0].leaf==true){
					if(!txtObject.search){
						innerRes += '<table width="500" border="0" cellpadding="3" cellspacing="0"><tr>'+
							'<td width="350" align="left" nowrap><b>Bezeichnung</b><a style="cursor:pointer;" onclick="sndReq(\''+txtObject.level[0].id+'\', \'bezeichnung ASC\',\''+txtObject.regions+'\')" title="Bezeichnung"><img src="'+root+'images/pfeil.gif" width="19" height="10" alt="" border="0"></a><a style="cursor:pointer;" onclick="sndReq(\''+txtObject.level[0].id+'\', \'bezeichnung DESC\',\''+txtObject.regions+'\')" title="Bezeichnung"><img src="'+root+'images/pfeil_d.gif" width="11" height="10" alt="" border="0"></a></td>'+
							//'<td align="left" nowrap><a style="cursor:pointer;" onclick="sndReq(\''+txtObject.level[0].id+'\', \'gruppe\',\''+txtObject.regions+'\')" title="Gruppe"><b>Gruppe</b><img src="images/pfeil.gif" width="19" height="10" alt="" border="0"></a></td>'+
							//'<td align="left" nowrap><a style="cursor:pointer;" onclick="sndReq(\''+txtObject.level[0].id+'\', \'name\',\''+txtObject.regions+'\')" title="Name Vorname"><b>Name Vorname</b><img src="images/pfeil.gif" width="19" height="10" alt="" border="0"></a></td>'+
							//'<th width="20%" align="left"><a href="#" onclick="sndReq(\''+txtObject.level[0].id+'\', \'zusatz\')" title="Zusatz">Zusatz</a></td>'+
							'<td widht="150" align="left" nowrap><a style="cursor:pointer;" onclick="sndReq(\''+txtObject.level[0].id+'\', \'ort ASC\',\''+txtObject.regions+'\')" title="Ort"><b>Ort</b><img src="'+root+'images/pfeil.gif" width="19" height="10" alt="" border="0"></a><a style="cursor:pointer;" onclick="sndReq(\''+txtObject.level[0].id+'\', \'ort DESC\',\''+txtObject.regions+'\')" title="Ort"><img src="'+root+'images/pfeil_d.gif" width="11" height="10" alt="" border="0"></a></td>'+
							//'<td align="left" nowrap><a style="cursor:pointer;" onclick="sndReq(\''+txtObject.level[0].id+'\', \'kanton\',\''+txtObject.regions+'\')" title="Kanton"><b>Kanton</b><img src="images/pfeil.gif" width="19" height="10" alt="" border="0"></a></td>'+
							'<td width="50" nowrap></td>'+
							'</tr>'+addHtml+'</table>';
					}else{
						innerRes += '<table width="500" border="0" cellpadding="3" cellspacing="0"><tr>'+
							'<td width="350" align="left" nowrap><b>Bezeichnung</b><a style="cursor:pointer;" onclick="srchReq(\''+txtObject.search+'\', \'bezeichnung ASC\');" title="Bezeichnung"><img src="'+root+'images/pfeil.gif" width="19" height="10" alt="" border="0"></a><a style="cursor:pointer;" onclick="srchReq(\''+txtObject.search+'\', \'bezeichnung DESC\');" title="Bezeichnung"><img src="'+root+'images/pfeil_d.gif" width="11" height="10" alt="" border="0"></a></td>'+
							'<td widht="150" align="left" nowrap><a style="cursor:pointer;" onclick="srchReq(\''+txtObject.search+'\', \'ort ASC\');" title="Ort"><b>Ort</b><img src="'+root+'images/pfeil.gif" width="19" height="10" alt="" border="0"></a><a style="cursor:pointer;" onclick="srchReq(\''+txtObject.search+'\', \'ort DESC\');" title="Ort"><img src="'+root+'images/pfeil_d.gif" width="11" height="10" alt="" border="0"></a></td>'+
							'<td width="50" nowrap></td>'+
							'</tr>'+addHtml+'</table>';
						/*
						innerRes += '<table width="500" border="0" cellpadding="3" cellspacing="0"><tr>'+
							'<td align="left" nowrap><a style="cursor:pointer;" onclick="srchReq(\''+txtObject.search+'\', \'bezeichnung ASC\')" title="Bezeichnung"><b>Bezeichnung</b><img src="images/pfeil.gif" width="19" height="10" alt="" border="0"></a></td>'+
							'<td align="left" nowrap><a style="cursor:pointer;" onclick="srchReq(\''+txtObject.search+'\', \'ort\')" title="Ort"><b>Ort</b><img src="images/pfeil.gif" width="19" height="10" alt="" border="0"></a></td>'+
							'<td align="left" nowrap><a style="cursor:pointer;" onclick="srchReq(\''+txtObject.search+'\', \'kanton\')" title="Kanton"><b>Kanton</b><img src="images/pfeil.gif" width="19" height="10" alt="" border="0"></a></td>'+
							'</tr>'+addHtml+'</table>';
						*/
					}
				}else if(!testObject.search){
					innerRes = addHtml; //+searchAdd;
				}
			}
		}
		
		for(i = 0; i < txtObject.level.length; i++){
			document.getElementById(i);
		}
		
		resultDiv = document.getElementById("result");
		resultDiv.innerHTML = innerRes;
		
		selectDiv = document.getElementById("select");
		selectDiv.innerHTML = innerSel;
		
		if(textVal!=-1) window.scrollTo(0, 320);
		
		/*
		if(document.getElementById("text").offsetHeight < 40){
			document.getElementById("content").style.height = 250+"px";
		}else{
			document.getElementById("content").style.height = document.getElementById("text").offsetHeight+15+"px";	
			alert("bb");
		}*/
  	}
  	
  	
}

function genTable(txtObject){
	
	var str='';
	
	for (i = 0; i < txtObject.results.length; ++i){
		if(txtObject.results[i].id == 53)  str += '<table cellpading="0" cellspacing="0" border="0"><tr class="nav'+txtObject.level.length+'" id="nav' + txtObject.results[i].id + '"><td valign="top">><img src="'+root+'images/0.gif" width="14" height="1" alt="" border="0"></td><td><a href="/index.php?id=2">' + txtObject.results[i].label + "</a></td></tr></table>\n";
		else str += '<table cellpading="0" cellspacing="0" border="0"><tr class="nav'+txtObject.level.length+'" id="nav' + txtObject.results[i].id + '"><td valign="top">><img src="'+root+'images/0.gif" width="14" height="1" alt="" border="0"></td><td><a href="javascript:sndReq(' + txtObject.results[i].id + ')">' + txtObject.results[i].label + "</a></td></tr></table>\n";
	}
	return str;
}

function unsetLevel(level, output){
	
	uBox = document.getElementById("unav_box");
	uuBox = document.getElementById("uunav_box");
	resBox = document.getElementById("text_result");
	claimBox = document.getElementById("claim");
	
	if(level >= 2){
		
		uBox.style.display = 'inline';
	}else{
		uBox.style.display = 'none';
	}
	
	
	if(level == 4 || (level >= 3 && output==-1)){
		uuBox.style.display = 'inline';
	}else{
		uuBox.style.display = 'none';
	}
	
	if(output!=-1){
		resBox.style.display = 'inline';
		/*
		claimBox.style.display = 'none';
		for(i=1; i<5; i++){
			 document.getElementById("claim"+i).style.display = 'none';
		}
		*/
	}else{
		resBox.style.display = 'none';
		/*
		claimBox.style.display = 'inline';
		for(i=1; i<5; i++){
			 document.getElementById("claim"+i).style.display = 'inline';
		}
		*/
	}
}

function textShow(target){
	sndReq(0);
	
	document.getElementById('text_result').style.display = 'none';
	document.getElementById('text').innerHTML = document.getElementById('text-'+target).innerHTML;
	
}

// JSON
(function (s) {var m = {'\b': '\\b','\t': '\\t','\n': '\\n','\f': '\\f','\r': '\\r','"' : '\\"','\\': '\\\\'};s.parseJSON = function (filter) {try {if (/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(this)) {var j = eval('(' + this + ')');if (typeof filter === 'function') {function walk(k, v) {if (v && typeof v === 'object') {for (var i in v) {if (v.hasOwnProperty(i)) {v[i] = walk(i, v[i]);}}}return filter(k, v);}j = walk('', j);}return j;}} catch (e) {}throw new SyntaxError("parseJSON");};}) (String.prototype);