var searchProc = null;
var searchProc_sphinx = null;
var se_rechts_aktiv = "";

function toggle_suche()	{
	
}

function startSearch()	{
	document.body.style.cursor="wait;";
	if(searchProc!=null)	{
		try{
			searchProc.transport.abort();
			searchProc = null;
		}	catch(e)	{}
	}

	if(searchProc_sphinx!=null)	{
		try{
			searchProc_sphinx.transport.abort();
			searchProc_sphinx = null;
		}	catch(e)	{}
	}
	
	var keyword = $('schnellsuche_input').value;
	var rowCount = null;
	var sum = 0;
	var sumShow = 0;
	
	if(keyword!="")	{	
//		Sphinx-Suche
//		if(!$('suchcontent_sphinx').visible())	$('suchcontent_sphinx').toggle();
//		$('suchcontent_sphinx').update("");
//		keyword = keyword.replace(" ","/");
//		searchProc_sphinx = new Ajax.Request('/sphinx/schnellsuche/'+keyword,{	
//			onSuccess: function(transport)	{
//				var erg = transport.responseText;
//				$('suchcontent_sphinx').update(erg);
//			}
//		})
	}
}

function such_kleiner_such()	{
	var searchPhrase = "";
	var keywords = $('schnellsuche_input').value;
	if (keywords.length > 0) {
		keywords.scan(/\"([^\"]*?)\"/, function(phrase){
			searchPhrase += phrase[1] + "/";
			keywords = keywords.gsub(phrase[0], "");
		});
		keywords = keywords.strip().gsub(" ", "/");
		keywords = keywords.gsub("\"", "");
		
		searchPhrase += keywords;
		searchPhrase = searchPhrase.gsub("//", "/");
		
		location.href = "/" + com.videotaxi.kunden.vtid + "/suche/liste/" + searchPhrase;
	}	else	{
		alert("Bitte gib einen Suchbegriff ein");
	}
}

function changeRequestState(keyword,transport)	{
//	if(!rowCount || rowCount.length<4)	{
		rowCount=transport.responseText.split("</rc>");
		var rc = rowCount[0];
		
//		$('searchCount_titel').update(rc+" Einträge bei der Titelsuche gefunden<br>");
		$('searchCount_titel').update("");
//		if(rowCount.length==2 && !$('se_links_filme'))	{
			str = "Die Suche nach <i>"+keyword+"</i> ergab folgende Treffer:<br />";
			if(rc>0)	{
				str = str+"<div id=se_links_filme style='padding:2px;cursor:pointer;' "
				+"onmouseover='this.style.backgroundColor=\"lightblue\";' "
				+"onmouseout='this.style.backgroundColor=\"white\";' "
				+"onclick=\"showContent('Filme');\">Artikel ["+rc+"]</div>";
			}
//			new Insertion.Bottom($('se_links'),str);
			$('se_links').update(str);
//		}
		
		$('searchCount_person').update("Darsteller werden gesucht...");
		if(rowCount.length>2)	{
			rc = rowCount[1];			
//			$('searchCount_person').update(rc+" Einträge bei der Darstellersuche gefunden<br>");
			$('searchCount_person').update("");
//			if(rowCount.length==3 && rc>0 && !$('se_links_darsteller'))	{
			if(rc>0){
				str = str+"<div id=se_links_darsteller style='padding:2px;cursor:pointer;' "
					+"onmouseover='this.style.backgroundColor=\"lightblue\";' "
					+"onmouseout='this.style.backgroundColor=\"white\";' "
					+"onclick=\"showContent('Darsteller');\">Darsteller ["+rc+"]</div>";
//				new Insertion.Bottom($('se_links'),str);
				$('se_links').update(str);
			}
			$('searchCount_regie').update("Regiesseure werden gesucht...");
		}
		if(rowCount.length>3)	{
			rc = rowCount[2];
//			$('searchCount_regie').update(rc+" Einträge bei der Regiesuche gefunden");
			$('searchCount_regie').update("");
//			if(rowCount.length==4 && rc>0 && !$('se_links_regie'))	{
			if(rc>0)	{
				str = str+"<div id=se_links_regie style='padding:2px;cursor:pointer;' "
					+"onmouseover='this.style.backgroundColor=\"lightblue\";' "
					+"onmouseout='this.style.backgroundColor=\"white\";' "
					+"onclick=\"showContent('Regie');\">Regie ["+rc+"]</div>";
//				new Insertion.Bottom($('se_links'),str);
				$('se_links').update(str);
			}						
		}					
//	}	
}

function showContent(val,box)	{
	se_rechts_aktiv = "";
	if(box=="rechts")	{
		var children = $('se_rechts').childElements();
		children.each(function(child)	{
			if(child.style.display!="none")	{
				child.toggle();
			}
		});

		var elArr = $('se_rechts').select('[name="'+val+'"]');
//		var elArr = document.getElementsByName(val);
		for(i=0;i<elArr.length;i++)	{
			el = elArr[i].id;
			$(el).toggle();
		}
		se_rechts_aktiv = val;
	}	else	{
		var children = $('se_mitte').childElements();
		children.each(function(child)	{
			if(child.style.display!="none")	{
				child.toggle();
			}
		});

		var children = $('se_rechts').childElements();
		children.each(function(child)	{
			if(child.style.display!="none")	{
				child.toggle();
			}
		});
		
		var content = val+'div';
		if($(content))	{
			$(content).toggle();
		}	else	{
			$('se_mitte').update(val+" werden geladen");
		}
	}
}

function hcToggle()	{
	if($('Filmediv').style.display!="none")	{
		var hc = $('Filmediv').select('[class="notshown"]');
		hc.each(function(el)	{
			el.toggle();
		});
	}
	
	if($('Darstellerdiv').style.display!="none")	{
		var hc = $('Darstellerdiv').select('[class="notshown"]');
		hc.each(function(el)	{
			el.toggle();
		});
	}
	
	if($('Regiediv').style.display!="none")	{
		var hc = $('Regiediv').select('[class="notshown"]');
		hc.each(function(el)	{
			el.toggle();
		});
	}
	
	if(se_rechts_aktiv!="")	{
		var hc = $('se_rechts').select('[class="notshown"]');
		hc.each(function(el)	{
			if(el.id.substring(0,se_rechts_aktiv.length)==se_rechts_aktiv)	{
				el.toggle();
			}
		});	
	}
}

new PeriodicalExecuter(function(pe)	{
	if($('body'))	{
		if ($("schnellsuche_input")) {
			$("schnellsuche_input").observe('focus', function(){
				if (com.videotaxi.kunden.vtid > 0) {
					document.observe('keypress', function(e){
						e = !e ? event : e;
						tastenCode = e.keyCode ? e.keyCode : e.which;
						if (tastenCode == 13) {
							such_kleiner_such();
						}
					})
				}
				else {
					com.videotaxi.kunden.initFinder($("schnellsuche_input"));
				}
			});
			$("schnellsuche_input").observe('blur', function(){
				document.stopObserving('keypress');
			});
			pe.stop();
		}
	}
},1)