$(function () {
			
   /* Abschicken von Formularen durch 'Enter' */
   $('#mainSearch input').keydown(function(e){
     if (e.keyCode == 13) {
         $(this).parents('form').submit();
         return false;
     }
   });
   

   
//   $('#header').click(function(){window.location.href="index.php"});
   
   /* Searchform */
	if($('#whoSearch').length > 0)
		initSearch();
	
	if($('.smallmap').length > 0)
		loadMaps();
		
	if($('#bigmap').length > 0)
		loadBigMap();		

	if($('#radiusmap').length > 0)
		loadRadiusMap();	

	if($('.tx-adressbuch-pi1-branchengruppen').length > 0)
		initBranchenGruppenOverview();
		
	xajax_loadBranchenMenu();
	$('#header ul li:eq(1)').addClass('submenu');
	
	$('#quickcontact form input[type="text"], #quickcontact form textarea').focus(function(){
		
		if($(this).val().substr(0,1) == ">")
			$(this).val("");											 
	});
});

function loadBranchenMenu_result(result)
{
	element = $('#header ul li:eq(1)');
	if(result.length > 0) element.append(result);
}


function initSearch()
{
   
	$('#submitSearch').hover(
	   function () {
		  $('div#mainSearch form').css('background-position','176px -227px');
	   },
	   function () {
		  $('div#mainSearch form').css('background-position','176px 33px');
	   }
	);
	
	$("#whoSearch").keyup(
		function()
		{
			if($(this).val().length > 2)
				xajax_searchBranch($(this).val());
			else{
				$("#suggestResult").find("li").remove();
				closeSearchSuggest();
			}
		}
	);	
		
	$("#whoSearch").click(checkSearchForm).attr("autocomplete","off");
	$("#whereSearch").click(checkSearchForm).attr("autocomplete","off");	
      
	$('div#mainSearch form').submit(checkSearchForm);
	
	$("#suggestClose").click(closeSearchSuggest);
   
	$("#suggestResult").find("li").live('click',
		function()
		{	
			$("#whoSearch").val(StripTags($(this).html())); 
			closeSearchSuggest();
			$("#whereSearch").click();
			$("#whereSearch").focus();			
		}
	);	
	
	$("#suggestResult").find("li").live('mouseover',
		function()
		{	
			$(this).addClass('hover');
		}
	);			

	$("#suggestResult").find("li").live('mouseout',
		function()
		{	
			$(this).removeClass('hover');
		}
	);			
}

function initBranchenGruppenOverview()
{
	list = $('.tx-adressbuch-pi1-branchengruppen>ul>li h2');
	list.each(function(){
		$(this).toggle(
			function(){
				$(this).parent().addClass('active');	
				$(this).next('ul').fadeIn('fast');
			},
			function(){
				$(this).parent().removeClass('active');	
				$(this).next('ul').fadeOut('fast');				
			}			
		);
	});
}

function searchBranch_result(result)
{
	if($("#suggestResult").find("li").length > 0)
		$("#whoSearchSuggest").fadeIn("fast");
	else
		$("#whoSearchSuggest").fadeOut("fast");
}

function StripTags(strMod){
    if(arguments.length<3) strMod=strMod.replace(/<\/?(?!\!)[^>]*>/gi, '');
    else{
        var IsAllowed=arguments[1];
        var Specified=eval("["+arguments[2]+"]");
        if(IsAllowed){
            var strRegExp='</?(?!(' + Specified.join('|') + '))\b[^>]*>';
            strMod=strMod.replace(new RegExp(strRegExp, 'gi'), '');
        }else{
            var strRegExp='</?(' + Specified.join('|') + ')\b[^>]*>';
            strMod=strMod.replace(new RegExp(strRegExp, 'gi'), '');
        }
    }
    return strMod;
}


function checkSearchForm()
{
	if($(this).val().substr(0,1) == '>')
		$(this).val("");
	else
	{
		if($("#whoSearch").val().substr(0,1) == '>')
			$("#whoSearch").val("");
		if($("#whereSearch").val().substr(0,1) == '>')
			$("#whereSearch").val("");			
	}
}

function closeSearchSuggest()
{
	$("#whoSearchSuggest").fadeOut("fast");
}