$(document).ready(function()
{
	sifrThis();
	
	$('#varattu').click(function() {
		$(this).css('display','none');
	});
	$('#myyty').click(function() {
		$(this).css('display','none');
	});
	$('#reserverad').click(function() {
		$(this).css('display','none');
	});
	$('#sald').click(function() {
		$(this).css('display','none');
	});

	
	if(document.getElementById("googleMaps")){
	googleMapsInit();
	}
	
	/**
	 * Mandatory form fields
	 */
	$('form').each(function()
	{
		$(this).submit(function(e)
		{
			$(this).find('.mandatory').each(function()
			{
				var val = $(this).val();
				if ( ! val.length)
				{
					e.preventDefault();
					$(this).focus();
					$(this).addClass('warn_input');
					if($('body').is('.sve'))
					{
					alert('Vänligen fyll i  alla obligatoriska fält.');
					} 
					else
					{
					alert('Ole hyvä ja täytä kaikki pakolliset kentät.');
					}
					setTimeout(clear_warn_inputs, 1000);
					return false;
				}
			});
		});
	});

	function clear_warn_inputs()
	{
		$('input, textarea').removeClass('warn_input');
	}



	$('#focus').focus();

	/**
	 * Clear certain inputs on focus
	 */
	$('.emptyme').each(function()
	{
		$(this).addClass('inactive_input');
		$(this).attr('original_value', $(this).val());

		$(this).focus(function()
		{
			var my_original_value = this.getAttribute('original_value');
			if(this.value == my_original_value)
			{
				this.value = '';
				$(this).removeClass('inactive_input');
			}
		});

		$(this).blur(function()
		{
			var my_original_value = this.getAttribute('original_value');
			if(this.value == '')
			{
				this.value = my_original_value;
				$(this).addClass('inactive_input');
			}
		});
	});



	/**
	 * Blank links
	 */
	$('a.blank').click(function()
	{
		$(this).attr('target', '_blank');
	});



	/**
	 * Background image
	 */
	function set_bg()
	{
		var bg_img = $('#content').css('backgroundImage').replace('"', '').replace('url(', '').replace(')', '').replace('"', '');
		$('#content').css('background', 'transparent');
		$.backstretch(bg_img);
	}
	set_bg();



	/**
	 * Footer to the bottom of the page
	 
	function do_footer()
	{
		$(window).bind("load", function()
		{
			var footerHeight = 0,
				footerTop = 0,
				$footer = $("#footer");

			positionFooter();

			function positionFooter()
			{
				footerHeight = $footer.height();
				footerTop = ($(window).scrollTop()+$(window).height()-footerHeight)+"px";
				if(($(document.body).height()+footerHeight) < $(window).height())
				{
					$footer.css({
					position: "absolute"
					}).animate({
					top: footerTop
					});
				}
				else
				{
					$footer.css({
						position: "static"
					});
				}
			}
			$(window).scroll(positionFooter).resize(positionFooter);
		});
	}
	//do_footer();

	function attach_footer()
	{
		$total_height = $(document).height();
		$roof = $total_height - $('#footer').height();

		if($('#footer').offset().top < $roof)
		{
			$('#footer').offset({ top: $roof, left: 0 });
		}
	}
	attach_footer();

	function attach_gallery_footer()
	{
		if( ! document.getElementById('pic_footer_wrap'))
		{
			return;
		}

		$total_height = $(document).height();
		$roof = (($total_height - $('#pic_footer_wrap').height()) - $('#pic_footer_wrap').height()) - 20;

		if($('#pic_footer_wrap').offset().top < $roof)
		{
			$('#pic_footer_wrap').offset({ top: $roof, left: 0 });
		}
	}
	attach_gallery_footer();
	*/


	/**
	 * Subnavs
	 */
	$('#navigation .subnav').hide();
	$('#navigation .dd a').mouseenter(function()
	{
		$sub = $(this).parent().find('.subnav');

		if( ! $sub.attr('has_image'))
		{
			var seg = $(this).attr('href').split('/');
				seg = seg[seg.length - 1];
			var subimg = document.createElement('img');
				subimg.src = _root + 'assets/img/subnav/' + seg + '.jpg';
			$sub.append(subimg);
			$sub.attr('has_image', true);
		}

		$sub.fadeIn(200, function()
		{
			$('.subnav').not($(this)).hide();
		});
		$sub.mouseleave(function(e)
		{
			$(this).fadeOut(350);
		});
	});
	
	/**
	 * Share Links
	 */
	$('#sharelinks').hide();
	$('#footer .jaa').mouseenter(function()
	{
		$sub = $(this).parent().find('#sharelinks');

		$sub.fadeIn(200, function()
		{
			$('#sharelinks').not($(this)).hide();
		});
		$sub.mouseleave(function(e)
		{
			$(this).fadeOut(350);
		});
	});



	/**
	 * Togglers
	 */
	$('.hide').hide();
	$('.toggler').click(function(e)
	{
		e.preventDefault();
		$targ = $('#' + $(this).attr('rel'));
		if($targ.is(':visible'))
		{
			$targ.fadeOut('slow');
		}
		else
		{
			$targ.fadeIn('slow');
		}
	});



	/**
	 * Search
	 */
	$('.apartment_list tr').click(function(e)
	{
		e.preventDefault();
		$ap_url = $(this).attr('rel');
		if( ! $ap_url)
		{
			return;
		}
		else
		{
			window.location = $ap_url;
		}
	});


	/**
	 * Facebook sharing
	 */
	$('.facebook_share, #share_on_facebook').click(function(e)
	{
		e.preventDefault();
		var my_url = window.location.href;
		share_on_facebook(my_url);
	});

	// Init functions
	check_open_form();

});



// -------------------------------- 8< --------------------------------

/**
 * Contact form
 */
function check_open_form()
{
	if(window.location.href.indexOf('#lomake') > -1)
	{
		open_contact_form();
	}
}

function open_contact_form()
{
	$('#contact_form').show();
}


/**
 * Image gallery
 */
function set_bg_gallery(image_url)
{
	$('#content').css('background', 'transparent');
	$('#backstretch').empty().remove();
	$.backstretch(image_url, {speed: 550});

	$('#gallery_nav li').removeClass('active');
}


function share_on_facebook(url)
{
	var title = document.title;
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(url)+'&t='+encodeURIComponent(title),'sharer','toolbar=0,status=0,width=626,height=436');
}

