/*
 * gisle@gisle.net
 */

(function() {

kk.Ui.fullView('.fullViewImg', '.fullView', {fadeFirst: 800, wrap: true});
kk.interactor('a:not(.custom)', { popupRemote: true, excludeElements: 'gallery', elements: 'p' });

if (kk.touchSupport) kk.loader({ position: 'center' });
else kk.loader();

var clickEvent = 'click'; 

kk.ga('UA-114961-22');

kk.bind('postActivate', function() {
	if ($('a.nav._active:not(.front)').length > 0) {
		$('a.nav.front').removeClass('_active');
	}
});

/* Keyboard interaction */
$(document).keydown(function(e) {
	if ( e.keyCode == 37 && kk.root.findElement('gallery') ) kk('gallery').previous();
	else if ( e.keyCode == 39 && kk.root.findElement('gallery') ) kk('gallery').next(); 
});

kk.interactor('.link.right', function(i) {
	if (kk.root.findElement('gallery')) kk('gallery').next();
});
kk.interactor('.link.left', function(i) {
	if (kk.root.findElement('gallery')) kk('gallery').previous();
});

/* New data loaded */
kk.bind('load', function() {
	/* Follow mouse cursor */
	$('.link.left .cursor:not(.bFoll)').followCursor({container: '.link', xDiff: -23, yDiff: -25}).addClass('bFol');
	$('.link.right .cursor:not(.bFol)').followCursor({container: '.link', xDiff: 15, yDiff: -25, gravity: 'NE'}).addClass('bFol');
	$('.galleryControl .link:not(.bHov)').hover(function() {
		$(this).find('.cursor').stop().fadeTo(100, 1.0);
	}, function() {
		$(this).find('.cursor').stop().fadeOut(100);
	}).addClass('bHov');

	/* Add _pressed class to links */
	$('a:not(.bPress), .a:not(.bPress)').mousedown(function() {
		var $that = $(this);
		$that.addClass('_pressed');	
		$('body').one('mouseup', function() {
			$that.removeClass('_pressed');
		});
	}).addClass('bPress');

	/* Touch-scrollable pages */
	if (kk.touchSupport) {
		$('.scrollable:not(.bScroll)').each(function() {
			var $this = $(this);
			var prevPos, prevTime, posDiff, timeDiff, stopped=false, max=0, min, top;

			$this.bind('touchstart', function(ev) {
				stopped = true;
				var e = ev.originalEvent;
				if (e.targetTouches.length > 1) return false;
				prevPos = {
					x: e.targetTouches[0].clientX,
					y: e.targetTouches[0].clientY
				};
				prevTime = new Date();
			})
			
			.bind('touchmove', function(ev) {
				var e = ev.originalEvent;
				if (e.targetTouches.length > 1) return false;

				// Get current position
				var pos = {
					x: e.targetTouches[0].clientX,
					y: e.targetTouches[0].clientY
				}

				// If (for some reason) starttouch hasn't been triggered, update prevpos
				if (!prevPos) {
					prevPos = pos;
					return;	
				}

				if (typeof(min) === 'undefined') {
					var bottomOffset = $this.parent().outerHeight()-$this.height();
					var topOffset = $this.offset().top;
					min = $('body').height() - (bottomOffset+topOffset+$this.height());
				}

				// Calculate diff
				posDiff = prevPos.y - pos.y;
				if (typeof(top) === 'undefined') top = parseInt( $this.css('top').replace(/[^0-9\-]+/, '') ) || 0;
				top -= posDiff;
				if (top < min) top = min;
				if (top > 0) top = 0;

				$this.translate({ y: top });

				prevPos = pos;

				var thisTime = new Date();
				timeDiff = thisTime - prevTime;
				prevTime = thisTime;

				return false;
			})
			
			.bind('touchend', function() {
				prevPos = undefined;	
				stopped = false;

				// Animate remaining movement
				var ppms = posDiff / timeDiff;
				var end = 1000*ppms;	

				var top = parseInt( $this.css('top').replace(/[^0-9\-]+/, '') ) || 0;
				top -= posDiff;

			});
		}).addClass('bScroll');
	}

	/* Hover-effect for dynamic colored links */
	$('a.dynamicColor:not(.bound)').hover(function() {
		$(this).not('._active').stop().animate({ color: kk.activeColor }, 50);
	}, function() {
		$(this).not('._active').stop().animate({ color: kk.currentFg }, 100);
	}).addClass('bound');

	/* Hide-info button */
	$('.hidePage:not(.bound)').bind(clickEvent, function() {
		$(this).closest('.pageContent').addClass('hidden').stop().fadeOut('fast');
		$('._active').addClass('notActive');
	}).addClass('bound');

	// Hide info on gallery-interaction
	$('.galleryControl .link:not(.bHide), .galleryWrapper:not(.bHide)').bind(clickEvent, function() {
		$('.hidePage').click();
	}).addClass('bHide');

	/* Show page */
	$('.navigation a:not(.bShow)').bind(clickEvent, function() {
		$(this).removeClass('notActive');
//		$('.pageContent.hidden').fadeTo(150, 1.0).removeClass('hidden');
	}).addClass('bShow');
	
});

/* Display hidden pages before showing */
kk('p').bind('preShowState', function(e, s) {
	if (!s || !s.$state) return;
	s.$state.find('.pageContent').show();
});

/* Update colors after changing gallery image */
kk.updateColors = function(e, s) {
	if (!s || !s.property) return;
	var bg = s.property('bg') || kk.bgColor;
	var fg = s.property('fg') || kk.fgColor;
	kk.currentBg = bg;
	kk.currentFg = fg;
	try {
		$('.dynamicColor:not(._active)').stop().animate({ color: fg }, 800);
		$('body').stop().animate({ backgroundColor: bg }, 800);
	} catch(e) {}
};

// Mark active links 
kk.bind('preActivate', function($elements) {
	/*
	var $active = $elements.filter('a');
	var $inactive = $('a._active').not($active);

	$active.filter('a').stop().animate({ color: kk.activeColor }, 150);
	$inactive.filter('a').stop().animate({ color: kk.currentFg || kk.fgColor });
	$('.galleryInfo').find($active).closest('.image').addClass('active');
	$('.galleryInfo').find($inactive).closest('.image').removeClass('active');
	*/
});



kk.bind('load', function() {
	// Handheld dropdown-menu
	$('.handheld .menu:not(.bound)').change(function() {
		var val = $(this).val();

		if (val === '_en') return Kveik.goTo('http://en.cafeherman.no/');
		if (val === '_no') return Kveik.goTo('http://cafeherman.no/');
		Kveik.goTo('/' + $(this).val() + '/');
	}).addClass('bound');

	$('._touch ._isElement:not(.touchBound)').bind('touchstart', function() {
		$('.slideInfo').fadeOut();
	}).addClass('touchBound');
});


})();

