function checkContentHeight()
{
	if( $(window).height() > originalContentHeight )
	{
		var heightDiff = $(window).height() - originalContentHeight;
		$('#content_wrap').css('height', originalContentHeightInner+heightDiff+'px');
		$('#content_footer').css('height', originalFooterHeight+heightDiff+'px');
	}
	else
	{
		$('#content_wrap').css('height', 'auto');
		$('#content_footer').css('height', 'auto');
	}
}


function trimContact()
{
	var contact = $('#contact_us').find('span').html();
	contact = $.trim(contact);
	$('#contact_us').find('span').html(contact);
}

function hideFeatureFields()
{
	var fieldEmpty = 0;
	
	$('ul.featurelist').find('li').each(function(){
		var this_field = $(this).html();
		this_field = $.trim(this_field);
		
		if( this_field.length < 1 )
		{
			$(this).remove();
			fieldEmpty++;
		}		
	});
	
	if( fieldEmpty == 4 )
	{
		$('ul.featurelist').remove();
	}
}

$(function(){
	
	hideFeatureFields();
	trimContact();
	
	// INITAL CHECKS
	// Adjust height if window height is bigger than content height
	if( $('#content_footer').find('.footer_block').length == 0 )
	{
		$('#content_footer').remove();
	}
	originalContentHeight = $('#content_wrap').outerHeight();
	originalContentHeightInner = $('#content_wrap').height();
	originalFooterHeight = $('#content_footer').height();
	checkContentHeight();
	
	// Check this again when window is resized
	$(window).resize(function(){
		checkContentHeight();
		setTimeout(checkContentHeight, 50);
	});
	// Check if info in properties detail box on listing_alt page is set and delete if it isn't
	$('li.bath, li.bed, li.car').each(function(){
		var tmpString = $(this).text();
		tmpString = tmpString.replace(" ","");
		if (parseInt(tmpString) == 0) {
			$(this).remove();
		};
	});

	// MENU
	
	$('ul#primary > li > a').click(function(){
		
		if( $(this).hasClass('folder') )
		{
			if( $(this).siblings('ul.secondary').hasClass('open') )
			{
				$(this).siblings('ul.secondary')
					.removeClass('open')
					.slideUp('normal');
			}
			else
			{
				$('ul#primary ul:visible').slideUp(500, function(){
					$(this).removeClass('open');
				});
				
				$(this).siblings('ul.secondary').addClass('open').slideDown(1000);
			}
			
			return false;
		}
		else
		{
			return true;
		}
	});
	
	// $('ul.secondary a').click(function(){
	// 		
	// 		var linking_to = $(this).attr('href');
	// 		
	// 		$('#menu_wrap').fadeOut(100);
	// 		$('#content_wrap').fadeOut(100, function(){
	// 			window.location = linking_to;
	// 		});
	// 		$('#footer_bar').fadeOut(100);
	// 		
	// 		return false;
	// 	});

	
	// Show Social Bookmark window
	$('#bookmark_it').hoverIntent(
		function(){
			if( !$.browser.msie )
			{
				$('#bookmark_services').fadeIn(300);
			}
			else
			{
				$('#bookmark_services').show();
			}
			
		},
		function(){
			if( !$.browser.msie )
			{
				$('#bookmark_services').fadeOut(200);
			}
			else
			{
				$('#bookmark_services').hide();
			}
		}
		);
	
	// TEASER BOXES
	// Hero
	
	// TEASER HERO SLIDER
	if( $('#hero_wrapper').length > 0 )
	{
		// check slide headings, whether 1 or 2 lines
		$('#hero_wrapper .overlay_headline').children().each(function(){
			if( $(this).height() > 30 )
			{
				$(this).css('margin-top','0px');
			}
		});
		
		
		var item_count = $('#hero_cycle .teaser_hero').length;
		var current_item = 0;
		var item_width = $('#hero_cycle .teaser_hero').outerWidth();
		var pickUpClick = true;
		var slideAuto = true;
		
		function heroSlide()
		{
			if( pickUpClick )
			{
				pickUpClick = false;
				
				var go_to = current_item + 1;

				$('#hero_cycle').animate({left: (-1*go_to*item_width)+'px'}, 1000, 'easeOutExpo', function(){
					if( go_to >= item_count )
					{
						$('#hero_cycle').css('left','0px');
						current_item = 0;
					}
					else
					{
						current_item = go_to;
					}
					
					pickUpClick = true;
				});
			}
		}
		
		function heroTeaserAutoplay()
		{
			if( slideAuto )
			{
				heroSlide();
				setTimeout(heroTeaserAutoplay, 4500);
			}
		}
		
		$('.teaser_hero').eq(0).clone().appendTo('#hero_cycle');
		$('#hero_cycle').width((item_count+1) * item_width); // Correct width of container
		setTimeout(heroTeaserAutoplay, 5000);
		$('#hero_cycle').click(function(){
			slideAuto = false;
		});
		
		$('#hero_nav div').click(function(){
			slideAuto = false;
			heroSlide();
		});
		
		$('#hero_wrapper').hoverIntent(
			function(){
				$('#hero_next').fadeIn(600);
				$(this).find('.overlay_slideout').fadeIn(500);
				$(this).find('.teaser_overlay').css('background-position','left top');
			},
			function(){
				$('#hero_next').fadeOut(300);
				$(this).find('.overlay_slideout').fadeOut(500);
				$(this).find('.teaser_overlay').css('background-position','right top');
			});
		
		$('.teaser_hero .teaser_overlay').each(function(){
			
			if( $(this).find('.overlay_link').find('a').length > 0 )
			{
				var link_to = $(this).find('.overlay_link').find('a').attr('href');
				
				if( link_to != '#' )
				{
					$(this).css('cursor','pointer')
					.click(function(){
						window.location = link_to;
					});
				}
			}
		});
			
		// $('.teaser_hero .teaser_overlay').hoverIntent(
		// 	function(){
		// 		slideAuto = false;
		// 		$(this).find('.overlay_slideout').fadeIn(500);
		// 	},
		// 	function(){
		// 		slideAuto = true;
		// 		$(this).find('.overlay_slideout').fadeOut(500);
		// 	});
	}

	// Square
	// $('.teaser_square .teaser_overlay').hide();
	
	$('.teaser_square').hoverIntent(
		function(){
			$(this).children('.teaser_overlay').fadeTo(1000, 0.8);
		},
		function(){
			if( $.browser.msie && $.browser.version > 7 )
			{
				// do in IE8
				var _this = $(this);
				_this.children('.teaser_overlay').fadeOut(500, function(){
					_this.children('.teaser_overlay').hide();
				});
			}
			else
			{
				$(this).children('.teaser_overlay').fadeTo(500, 0);
			}
		});

	$('.overlay_image').hoverIntent(
		function(){
			if( $(this).find('.overlay_text > h3').html().length > 2 )
			{
				$(this).children('.overlay_text').fadeIn(1000);
				$(this).children('.overlay_text').animate({bottom: '0px'}, {duration: 500, easing: 'easeOutCirc', queue: false});
			}
		},
		function(){
			if( $(this).find('.overlay_text > h3').html().length > 2 )
			{
				var overlay_height = $(this).children('.overlay_text').outerHeight();
				$(this).children('.overlay_text').fadeOut(1000);
				$(this).children('.overlay_text').animate({bottom: '-'+overlay_height+'px'}, {duration: 500, easing: 'easeInCirc', queue: false});
			}
		});
	
	
	// Link elements
	// teaser square
	if( $('.teaser_square').length > 0 )
	{
		$('.teaser_square').click(function(){
			if( $(this).find('.teaser_image_subline').children('a').length == 1 )
			{
				var link_to = $(this).find('.teaser_image_subline').children('a').attr('href');
				window.location = link_to;
			}
		});
	}
	// teaser portraits
	if( $('.teaser_portrait').length > 0 )
	{
		$('.teaser_portrait .teaser_image, .teaser_portrait .teaser_link').click(function(){
			if( $(this).parent().children('.teaser_image_subline').children('a').length == 1 )
			{
				var link_to = $(this).parent().children('.teaser_image_subline').children('a').attr('href');
				window.location = link_to;
			}			
		});
	}
	// teaser portraits alt
	if( $('.teaser_portrait_alt').length > 0 )
	{
		$('.teaser_portrait_alt .teaser_image, .teaser_portrait_alt .teaser_link').click(function(){
			if( $(this).parent().children('.teaser_image_subline').children('a').length == 1 )
			{
				var link_to = $(this).parent().children('.teaser_image_subline').children('a').attr('href');
				window.location = link_to;
			}			
		});
	}
	// imagelisting
	if( $('.imagelisting_item').length > 0 )
	{
		$('.imagelisting_item .imagelisting_img').click(function(){
			if( $(this).parent().find('.imagelisting_link').children('a').length == 1 )
			{
				var link_to = $(this).parent().find('.imagelisting_link').children('a').attr('href');
				window.location = link_to;
			}
		});
	}
	
	
	// Search form
	$('#search_site_form input').focus(function(){
		if( $(this).val() == "Search this site" )
		{
			$(this).val('');
		}
	})
	.blur(function(){
		if( $(this).val() == "Search this site" || $(this).val() == "" )
		{
			$(this).val('Search this site');
		}
	});
	
	
	$.localScroll();
	
	// HIDE teaser rows if hide_value is populated
	$('.teaser_row').each(function(){
		var hideVal = $(this).find('.hide_value').html();

		if( hideVal.length > 3 )
		{
			$(this).remove();
			originalContentHeight = $('#content_wrap').css('height','auto').outerHeight();
			originalContentHeightInner = $('#content_wrap').height();
			checkContentHeight();
		}
	});
	
	
	// PRINT BUTTON
	$('#print_style').click(function(){
		window.print();
		return false;
	});
});
