var newSearch, searchReqObj, searchdone;

window.addEvent('domready', function(){
	
	if(typeof(searchOptions) != "undefined" && searchOptions.template) newSearch = 1;
	
	if(newSearch && !$defined(String.toDOM)){
		new Element('div', {
			'html': '<b>DESK-JS Fehler:</b> Verzeichnissuche - desk.js wurde nicht eingebunden (String.toDOM fehlt)'
			,'class': 'text'
			,'styles': {
				'position': 'absolute'
				,'width': '100%'
				,'left': 0
				,'top': 0
				,'color': '#67726A'
				,'background-color': '#DFDEDA'
				,'border-bottom': '1px solid #67726A'
				,'padding': '10px'
				,'z-index': 9999
			}
		}).inject(document.body, 'top');
	}
	
	if($('search')){
		
		if($('search_title')) searchdone = true;
		
		$('search').addEvent('keyup', function(){
			
			// Refresh nach Sucheingabe
			if(!$('search_title')){
				
				if(searchReqObj) searchReqObj = $clear(searchReqObj);
				
				searchReqObj = (function(){
					document.location.href = ROOT + "index.php?mode=search&value="+$('search').value + '&lang=' + LANG;
				}).delay(500);
			}else{
				
				if(searchReqObj) searchReqObj = $clear(searchReqObj);
				
				// Text
				searchReqObj = (function(){contentReq()}).delay(1000);
			}
		});
	}
});

function contentReq(){
	
	if(newSearch) addNewTag = '&newSearch=1'
	else addNewTag = '';
	
	a = new Request({
		url: 'index.php'
		,method: 'post'
		,data: 'mode=search&value='+$('search').value + '&lang=' + LANG + addNewTag
		,noCache: true
		,onComplete: function(response){
			
			// JSON-decode
			var req = JSON.decode(response);
			if(!req) return;
// DEBUG!
			if(req.result/* newSearch */) res = req.result
			else res = req;
			
			// Loader zurücksetzten
			end = (function(){$('search_load').setStyle('display','none');});
			end.delay(500);
			
			// Ausgabe
			if(res && res[0] != ""){
				$each(res, function(txtobj, i){
					
					if(newSearch && searchOptions.template){
												
						txtobj.a_start = '<a href="index.php?id=' + txtobj.id + '">';
						txtobj.a_end = '</a>';
						
						buildSearchTpl(txtobj);
					}else{
						var el_main = new Element('div',{
							'class':'page-icon'
						});
						var el_titel = new Element('div',{
							'html':'<div style="margin-right:100px;"><b><a href="index.php?id=' + txtobj.id + '">' + txtobj.title + '</a></b><br />' + txtobj.text + '</div>'
						});
						var el_date = new Element('div', {
							'html':txtobj.datum,
							'styles':{
								'position':'relative',
								'margin-left':'10px',
								'float':'right',
								'top':'0px'
							}
						});
						var el_break = new Element('br', {
							'styles':{
								'clear':'both'
							}
						});
						
						el_main.adopt(el_date, el_titel);
						$('search_text').adopt(el_main, el_break);
					}
				});
			}else if(!newSearch){
// DEBUG!
				// $('search_text').set('text', notfound);
			}
		}.bind(this)
		,onRequest: function(){
			$('search_load').setStyle('display', '');
			$('search_text').empty();
		}.bind(this)
	}).send();
	
	if(newSearch && $defined(searchOptions.verzDB)){
		
		searchReqStr = {
			type: 'search'
			,user: deskJS.options.verzUser
			,searchStr: $('search').value
			,searchDB: searchOptions.verzDB
			,language: LANG
		}
		
		if(deskJS.options.verzDebug == 1) debugStr = '&debug=1';
		else debugStr = '';
		
		new Request({
			url: 'index.php'
			,method: 'post'
			,data: 'mode=verzeichnis&local=' + deskJS.options.verzLocal + debugStr +'&request=' + encodeURIComponent(JSON.encode(searchReqStr))
			,noCache: true
			,onComplete: function(response){
				
				// JSON-decode
				var req = JSON.decode(response);
				res = req.result
				
				if($type(res[0]) == 'object') res = [res];
				
				// Ausgabe
				if(res && res[0] != ""){
					$each(res, function(txtobj, i){
						if(txtobj != ''){
							
							$each(txtobj, function(subobj, subi){
								
								if(subi == 0){
									
									// Setzten des Bereiches in der Suchausgabe
									if($defined(searchOptions.verzDB[i].dbTitle) && searchOptions.verzDB[i].dbTitle != ''){
										verzTitle = '<h2 class="title searchtitle">' + searchOptions.verzDB[i].dbTitle + '</h2><br /><br />';
										injTpl = verzTitle.toDOM();
										injTpl.inject($('search_text'));
									}
								}
								
								subobj.a_start = '<a href="' + ROOT  + LANG + '/verz/' + req.tblName[0] + '_' + '_' + subobj.id + '/" onclick="javascript: deskJS._directLink(\'' + req.tblName[0] + '\',\'' + '\',\'' + subobj.id + '\'); return false">';
								subobj.a_end = '</a>';
								
								buildSearchTpl(subobj);
							});
						}
					});
				}else if($('search_text').get('html') == ''){
// DEBUG!
					// $('search_text').set('text', notfound);
				}
			}.bind(this)
		}).send();
	}
}

function buildSearchTpl(txtnode){
	// Templates var Replaces
	tpl = searchOptions.template;
	tpl = tpl.substitute($H(txtnode));
	
	injTpl = tpl.toDOM();
	injTpl.inject($('search_text'));
}
