
;(function($){

	$.prefetch = function () {
		for (var i=0; i<arguments.length; i++)  {
			$('<img>').attr('src', arguments[i]);
		}
	}

	$.fn.imghover = function () {
		return this.each (function () {
			$(this).data('__img__original', this.src);
			$.prefetch($(this).attr('data-hover'));
		}).hover(function(){
			this.src = $(this).attr('data-hover');
		},function(){
			this.src = $(this).data('__img__original');		
		});
	};

})(jQuery);

function getNextFrame(currentFrame, totalFrames) {
	return currentFrame < totalFrames-1 ? currentFrame+1 : 0;
}

var tipsAnimationFrameCount = 1;
var tipsAnimationCurrentFrame = 0;
var tipsAnimationTimer = null;
var tipsAnimationRunning = false;

function tipsAnimateTo(idx) {
	var new_tip = $('#tips li:eq('+idx+')');
	var current_tip = $('#tips li:visible');

	if (tipsAnimationRunning || new_tip.length == 0 || new_tip.is(':visible')) return;

	tipsAnimationRunning = true;

	current_tip.fadeOut(500,function(){
		new_tip.fadeIn(500,function(){
			tipsAnimationCurrentFrame = idx;
			tipsAnimationRunning = false;
		});
	});
}

var fotoAnimationFrameCount = 1;
var fotoAnimationCurrentFrame = 0;
var fotoAnimationTimer = null;
var fotoAnimationRunning = false;

function fotoAnimateTo(idx) {
	var new_foto = $('#detailfotos li:eq('+idx+')');
	var current_foto = $('#detailfotos li:visible');

	if (fotoAnimationRunning || new_foto.length == 0 || new_foto.is(':visible')) return;

	fotoAnimationRunning = true;

	current_foto.css('zIndex',2);
	new_foto.css('zIndex',1).show();
	
	var ind = $('#detailfotos div.indicator');
	ind.find('div').removeClass('active');
	ind.find('div:eq('+idx+')').addClass('active');

	current_foto.fadeOut(500,function(){
		fotoAnimationCurrentFrame = idx;
		fotoAnimationRunning = false;
		current_foto.hide();
	});
}


$(window).load(function(){
	if (tipsAnimationFrameCount > 1) {
		tipsAnimationTimer = setInterval(function(){
			tipsAnimateTo(getNextFrame(tipsAnimationCurrentFrame, tipsAnimationFrameCount));
		},6000);
	}

	if (fotoAnimationFrameCount > 1) {
		fotoAnimationTimer = setInterval(function(){
			fotoAnimateTo(getNextFrame(fotoAnimationCurrentFrame, fotoAnimationFrameCount));
		},6000);
	}
});

$(function(){
	
	$('img[data-hover],input[data-hover]').imghover();

	

	tipsAnimationFrameCount = $('#detailfotos li').length;
	fotoAnimationFrameCount = $('#detailfotos li').length;

	// Menu
	$('#mainmenu>li>a').hover(function(){
		closeMenus();
		$(this).parents('li').addClass('active');
	});

	function closeMenus(){		
		$('#mainmenu li.active').removeClass('active');
	}

	var closeTimer;
	$('#mainmenu>li').hover(function(e){
		if (e.type === 'mouseenter') {
			clearTimeout(closeTimer);
		} else {
			closeTimer = setTimeout(closeMenus,400);
		}
	});


	// Volledig programma toggle
	$('#showfullprogramme').click(function(){		
		toggleProgramme($(this).attr('checked'));
	});

	function toggleProgramme(toggle) {
		var r = $('#resultlisting');
		if (toggle) {
			r.find('div.programme_short').hide();
			r.find('div.programme_long').show();
		} else {
			r.find('div.programme_short').show();
			r.find('div.programme_long').hide();
		}
	}

	toggleProgramme($('#showfullprogramme').attr('checked'));


	// Filter toggles
	$('div.filterbox div.filtertoggle').click(function(){
		$(this).parents('div.filterbox').toggleClass('active');
	}).each(function(){
		//prevent text selection
		this.onselectstart = this.onmousedown = function(){return false};
	});
	

});
