(function($){
	var scrollableOpts = {
			circular: true,
			items: '.items'
		},
		naviOpts = {
			navi: '#carousel_bullets',
			naviItem: 'a'
		},
		autoscrollOpts = {
			interval: 4000,
			autoplay: false
		};

	function buildCarousel(){
		$('#home_carousel .items > *').wrap('<div />');
		$('#home_carousel').scrollable(scrollableOpts).navigator(naviOpts).autoscroll(autoscrollOpts);
		var api = $("#home_carousel").data("scrollable");
		api.seekTo(1, 0);
		api.play();
		api.seekTo(0, 0);
	}
	
	var colTimer = null,
		colPollInterval = 1000;
	
	
	function checkColumnHeights(selector){
		if(typeof(selector) === 'undefined'){
			selector = ".home_column";
		}
		
		var highest = 0;
		
		$(selector).each(function(){
			highest = ($(this).height() > highest) ? $(this).height() : highest;
		});
		
		$(selector).height(highest);
	}
	

	$(document).ready(function(){
		buildCarousel();
		
		var fbFeedOptions = {
			maxItems: 1,
			userId: '122667304482034',
			wrappers: {
				item: '<p />',
				author: '<strong />'
			},
			filter: function(item){
				return (item.from && item.from.name.indexOf("Lehtimäen Matkat") === 0);
			},
			onFinish: function(){
				this.find('p').click(function(e){
					e.stopPropagation();
					window.open('https://www.facebook.com/pages/Lehtim%C3%A4en-Matkat/122667304482034');
				});
			}
		};
		
		$('#home_fb .body:first .content:first').fbFeed(fbFeedOptions);
		
		$('#btn_form_brochure_open').click(function(event){
			event.preventDefault();
			var $form = $('#form_brochure'),
				$emt = $(this),
				pos = $('#img_brochure').offset();
			
			$form.css({left: pos.left + 230, top: pos.top - 120});
			$form.fadeIn('fast');
		});
		
		$('#btn_form_brochure_close').click(function(event){
			event.preventDefault();
			var $form = $('#form_brochure');
			$form.fadeOut('fast');
		});
		
		if(colPollInterval > 0){
			colTimer = setTimeout(function(){checkColumnHeights();}, colPollInterval);
		}
		
		$('.home_column li').each(function(){
			$(this).click(function(e){
				window.location.href = $(this).find('a[href]:first').attr('href');
			});
		});
	});
})(jQuery);


// https://graph.facebook.com/122667304482034/feed
