var searchReqObj, search_load, search_title, search_text, searchdone;

window.addEvent('domready', function(){
	
	if($('search')){
		search_el = $('search');
		search_load = $('search_load');
		
		if($('search_title')){
			search_title = $('search_title');
			search_text = $('search_text');
			
			searchdone = true;
		}
		
		search_el.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_el.value;
				}).delay(500);
			}else{
				
				if(searchReqObj) searchReqObj = $clear(searchReqObj);
				
				// Text
				searchReqObj = (function(){contentReq()}).delay(1000);
			}
		});
	}
});

function contentReq(){
	
	var a = new Request.JSON({
		url: 'index.php',
		method: 'post',
		data: 'mode=search&value='+$('search').value
	});
	a.addEvent('onRequest', function(response, xml){
		
		search_load.setStyle('display', '');
		search_text.empty();
	});
	a.addEvent('success', function(response, xml){
		
		if(response && response[0]){
			$each(response, function(txt, i){
				
				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=' + txt.id + '">' + txt.title + '</a></b><br />' + txt.text + '</div>'
				});
				var el_date = new Element('div', {
					'html':txt.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{
			search_text.set('text', notfound);
		}					
	});
	
	a.addEvent('onComplete', function(response, xml){
		
		end = (function(){search_load.setStyle('display','none');});
		end.delay(500);
	});
	
	a.send();
}