/* Author: Hinderling Volkart
*/
$(window)._scrollable();


/* DOM is loaded */
$(document).ready(function() {
	
	var HEADER_HEIGHT = 75;


	$('div.video .image').click(function() {
		$('iframe#vimeo').attr('src', 'http://player.vimeo.com/video/' + $(this).parent().attr('id') + '?title=0&byline=0&portrait=0&autoplay=1');
	});

	var $buttons = $('#buttons .button');
	var $infos = $('.info');
	$infos.hide();

	function showInfo(target) {
		
		$buttons.removeClass('active');

		if ( target ) {
			var $open = $infos.not(target).filter(':visible').slideUp();

			var $info = $infos.filter(target);
			var $btn = $buttons.filter( '#btn-'+ $info.attr('id') );

			$btn.addClass('active');

			setTimeout(function(){
				$info.slideDown('normal', function() {
					clearInterval(scrolling);
					if ( target == '#contact' && $('#map').attr('src') == '' ) {
						$('#map').attr('src','http://maps.google.ch/maps?f=q&source=s_q&hl=de&geocode=&q=Hinderling+Volkart+AG,+Pflanzschulstrasse,+Z%C3%BCrich&aq=0&oq=Hinderling&sll=46.362093,9.036255&sspn=7.483085,11.030273&vpsrc=6&ie=UTF8&hq=Hinderling+Volkart+AG,&hnear=Pflanzschulstrasse,+8004+Z%C3%BCrich&t=p&cid=17279785588246669483&ll=47.382602,8.52273&spn=0.029058,0.058279&z=14&iwloc=A&output=embed');
					}
				});

				var position = $info.offset();
				var height = position.top - HEADER_HEIGHT;

				var scrolling = window.setInterval(function() {
					$(window).scrollTop(height);
				}, 100/6 );
			}
			, $open.length ? 400 : 0
			);
		}
		else
		{
			$infos.slideUp();
		}

	}

	function toggleInfo(target) {
		var $info = $infos.filter(target);
		var $btn = $buttons.filter( '#btn-'+ $info.attr('id') );
		if ( $btn.is('.active') ) {
			showInfo(false);
		} else {
			showInfo(target);
		}
	}

	$buttons.click(function(event) {
		event.preventDefault();
		
		var id = $(this).attr('href');
		toggleInfo(id);
	});

	$infos.find('.close').click(function() {
		showInfo(false);
	});


});

/* content is loaded */
$(window).load(function() {

	// get access to the API
	var intStep = 3;
	var intSpeed = 400;
	var intCountVisibleItems = 4;
	var intCountItems = $('div.video').length;

	$('.scrollable').scrollable({
		circular: false,
		onSeek: checkAndHideNextButton,
		easing: 'linear',
		time: 200
	});

	// show buttons if more than 4 items available
	if (intCountItems > intCountVisibleItems) {
		$('a.next').show();
		$('a.prev').show();
	}

	$('a.next').click(function() {
		$('.scrollable').data("scrollable").move(intStep, intSpeed);
	});

	$('a.prev').click(function() {
		$('.scrollable').data("scrollable").move(-intStep, intSpeed);
	});

	//$('a.next').move(4, 200);
	// bugfix to hide next button if no items left
	function checkAndHideNextButton(event, i) {
		if (intCountItems > intCountVisibleItems) {
			var intVideoWidth = $('div.video').outerWidth(true);
			var intContainerWidth = $('div.items').position();
			var intMaxContainerWidth = (intCountItems - intCountVisibleItems) * intVideoWidth;
			if (intMaxContainerWidth + intContainerWidth.left <= 0) {
				$('a.next').addClass('disabled');
			}
		}
	}

});

