$(document).ready(function()
{
	// Menu und Borders
	$('#menu_main > ul > li:last').addClass('border_bottom');
	
	// Objektdetailtabelle	
	$('#details td:even').addClass('td_links');
	$('#details td:odd').addClass('td_rechts');
	
	$('.besichtigung td:even').addClass('td_links');
	$('.besichtigung td:odd').addClass('td_rechts');
		
	// Menu span erzeugen
	$('#menu_main ul li a').append('<span></span>');
	
	//Formularfelder leeren						   
		$('input[type="text"], input[type="password"]').each(function(){
		 $(this).focus(function()
			{
				if($(this).val() == $(this).attr('defaultValue')) 
				{
					$(this).val('');	
				}
			});			
			$(this).blur(function()
			{
				if($(this).val() == '') 
				{
					$(this).val($(this).attr('defaultValue'));
				}
			});		
		});	
		$('.schnellsuche-form, .detailsuche-form').submit(function()
		{
			$('input[type="text"]').each(function(){								  
				if($(this).val() == $(this).attr('defaultValue'))
				{
					$(this).val('');
				}
			});
		});
		
	
	//Tab Navigation - Anfang
	
	//Initialisierung
	$('#tabs > ul li').eq(0).addClass('active');
	$('#tabs > div').hide();
	$('#tabs > div').eq(0).show();
	
	//Events
	$('#tabs ul li a').click(function(){
	
		$('#tabs > ul li').removeClass('active');
		$(this).parent().addClass('active');
		
		zwischenwert = $(this).parent().attr('id');
		zwischenwert = zwischenwert.substring(4);
		
		$('#tabs > div').hide();
		$('#content-'+zwischenwert).show();
		
		//behebt den Sprung ans Seitenobere
		return false;
		
	});
	
	//Tab Navigation - Ende

	//Formularmeldungen als Alerts
	$('.formularmeldungen .success').hide();
	var success = 0;
	$('.formularmeldungen').each(function()
	{
		$(this).each(function()
		{
			if($.trim($(this).find('p').html()) != '')
			{
				alert($.trim($(this).text()));
				
				success = 1; 
				return false;
			}
		});
		if(success == 1)
		{
			return false;
		}
	});
	
	//Autocomplete
	var availableTags = [];
	
	$('.autocomplet option').each(function()
	{
		availableTags.push($(this).text());
	});

	$('#obnr').autocomplete(
	{
		source: availableTags
	});
		

	//Besichtungstermine beim Objekt
	$('#terminhinweis ul li').each(function() {
			var termine = $(this).html();
			if (termine == 'Keine Termine vorhanden.')
			{
				$(this).parent().hide();
			}
		});
	
	
	$('#terminhinweis ul li').prepend('Besichtigungstermin am:  ');
	$('#terminhinweis ul li').append('. <a href="besichtigungstermine.xhtml">Hier anmelden</a>.');
	
});
