$(document).ready(function()
{
	
	// Slideshow
	
	$('.slideshow').cycle({
		fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});
	
	// Main-Navigation
	
	$('.main-navigation li:last').addClass('last');
	
	// Quick-Search

	$('.map ul li a').click(function()
	{
		var this_href = $(this).attr('href');
		$('#ort option').each(function()
		{
			if($(this).val() == this_href)
			{
				$(this).attr('selected','selected');
				return false;
			}
		});
		return false;
	});
	
	$('#preis, #preis_verkauf, #preis_vermietung').each(function()
	{
		if($(this).attr('value') == '')
		{
			$('input#minPreis, input#verkauf_minPreis, input#vermietung_minPreis').attr('value','');	
		}
		else if($(this).attr('value') == '5000')
		{
			$('input#minPreis, input#verkauf_minPreis, input#vermietung_minPreis').attr('value','0');
		}
		else
		{
			$('input#minPreis, input#verkauf_minPreis, input#vermietung_minPreis').attr('value','5000');
		}
	});
	
	$('#preis, #preis_verkauf, #preis_vermietung').change(function()
	{
		if($(this).attr('value') == '')
		{
			$('input#minPreis, input#verkauf_minPreis, input#vermietung_minPreis').attr('value','');	
		}
		else if($(this).attr('value') == '5000')
		{
			$('input#minPreis, input#verkauf_minPreis, input#vermietung_minPreis').attr('value','0');
		}
		else
		{
			$('input#minPreis, input#verkauf_minPreis, input#vermietung_minPreis').attr('value','5000');
		}
	});
	
	// Objectnummber-Search
	
	$('.objectnumber-search .objectnumber').each(function()
	{
		$(this).focus(function()
		{
			$('.objectnumber-search label').hide();
		});
	
		$(this).blur(function()
		{
			if($(this).val() == '')
			{
				$('.objectnumber-search label').show();
			}
		});
	});
	
	// Service-Navigation
	
	$('.service-navigation li:last').addClass('last');
	
	// Contentbox
	
	$('.content .content-box:last').addClass('last-content-box');
	
	$('.content .content-box').each(function()
	{
		$(this).find('p:last').addClass('last');	 
	});
	
	// Frame-Header
	
	$('.frame-header li').each(function()
	{
		var url = document.URL;
		url = url.split('?');
		
		if(url[1] == undefined)
		{
			var this_href = $(this).find('a').attr('href');
			this_href = this_href.split('&');
			$(this).find('a').attr('href',this_href[0] + '?' + this_href[1]);
		}
	});
	
	// Frame-Footer
	
	$('.frame-footer > a:first').addClass('prev');
	$('.frame-footer > a:last').addClass('next');
	
	$('.frame-footer').each(function()
	{
		var site_counter = 0;
		$(this).find('span a').each(function()
		{
			site_counter++;
		});
		if(site_counter == '1')
		{
			$(this).hide();
		}
	});
	
	// Listobject
	
	$('.listobject').each(function()
	{
		$(this).find('.information table tr:last').addClass('last');
		$(this).find('.information ul li a').each(function()
		{
			$(this).addClass('button');		  
		});
	});
	
	// Details
	
	$('.details .details-navigation a:first').addClass('first');
	
	$('.details .gallery li:nth-child(7n)').addClass('row-end');
	
	$('.details .tabs ul a:first').addClass('active');
	$('.details .tabs .tabs-content div:first').show(); 
	
	$('.details .tabs ul a').click(function()
	{
		var this_hash = $(this).attr('href');
		$('.details .tabs ul a.active').removeClass('active');
		$(this).addClass('active');
		$('.details .tabs .tabs-content div').hide();
		$('.details .tabs .tabs-content div#' + this_hash).show();
		return false;
	});
	
	$('.details .tabs .tabs-content table tr:last').addClass('last');
	
	$('.details .tabs .tabs-content p').each(function()
	{
		if($.trim($(this).html()) == '')
		{
			$(this).remove();	
		}
	});
	
	$('.details .tabs .tabs-content div').each(function()
	{
		var this_h3 = $(this).find('h3').text();

		if($.trim($(this).html()) == '<h3>' + this_h3 + '</h3>' || $.trim($(this).html()) == '<H3>' + this_h3 + '</H3>')
		{
			var this_id = $(this).attr('id');
			$('.details .tabs ul a[href="#' + this_id + '"]').hide();
		}
	});
	
	if($.trim($('.details .plan-check').html()) == '')
	{
		$('.details-navigation a:eq(1)').hide();	
	}
	
	// More Objects and Top Object
	
	$('.more-objects-list-object:nth-child(3n+4)').addClass('row-end');
	
	$('.more-objects-list-object').each(function()
	{
		$(this).find('table tr:last').addClass('last');
	});
	
	// Object of the Month
	
	$('.object-month .image li img').each(function()
	{
		var img_margin = $(this).height() / 11;
		$(this).css('margin-top','-' + img_margin + 'px');
	});
	
	$('.object-month .image ul').cycle({
		fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});
	
	$('.object-month table tr:last').addClass('last');
	
	$('.object-month table .last td:last').addClass('last');
	
	// Directory

	var count = 0;

	$('#main .content *').each(function()
	{
		
		if($(this).is('h3') || $(this).is('h4') || $(this).is('h5') || $(this).is('h6'))
		{
			var anker = $(this).text().split('. ');
			if(anker[1] != undefined)
			{
				anker = $.trim(anker[1]);
			}
			else
			{
				anker = $.trim(anker[0]);	
			}
			
			$(this).attr('id','h' + count);
			
			if($(this).is('h3'))
			{
				$('#main .directory').append('<li><a href="#h' + count + '">' + anker + '</a><ol></ol></li>');	
			}
			else if($(this).is('h4'))
			{
				$('#main .directory > li:last > ol').append('<li><a href="#h' + count + '">' + anker + '</a><ol></ol></li>');		
			}
			else if($(this).is('h5'))
			{
				$('#main .directory > li:last > ol > li:last > ol').append('<li><a href="#h' + count + '">' + anker + '</a><ol></ol></li>');		
			}
			else if($(this).is('h6'))
			{
				$('#main .directory > li:last > ol > li:last > ol > li:last > ol').append('<li><a href="#h' + anker + '">' + anker + '</a><ol></ol></li>');		
			}
			
			count++;
			
		}
	});
	
	$('#main .directory li ol').each(function()
	{
		if($.trim($(this).html()) == '')
		{
			$(this).remove();	
		}
	});
	
	$('#main .directory a').bind('click',function(event)
	{					 
		event.preventDefault();
		var target = $(this).attr('href');
		
		$('html,body').animate(
		{
			scrollTop: $(target).offset().top
		},2000,function()
		{
			location.hash = target;
		});
		
		return false;
	});
	
	$('input[type="text"]').each(function()
	{
		$(this).focus(function()
		{
			if($(this).val() == $(this).attr('defaultValue')) 
			{
				$(this).val(''); 
			}
		});   
		$(this).blur(function()
		{
			if($(this).val() == '') 
			{
				$(this).val($(this).attr('defaultValue'));
			}
		});  
	});
	
	//
	
	$('form').submit(function()
	{
		$('input[type="text"]').each(function()
		{
			if($(this).val() == $(this).attr('defaultValue'))
			{
				$(this).val('');
			}
		});
	});
	
	// Team
	
	$('.team h3').addClass('slogan');
	
	$('.team').each(function()
	{						 
		$(this).find('.team-lightbox').fancybox();
	});
	
});
