var pageWrapPadding = 30;
var subMenuHeight = 95;
var menuHeight = 110;
var imgs = [];
var imgsorig = [];
var apis = [];

// remap jQuery to $
(function($){})(window.jQuery);


/* trigger when page is ready */
$(document).ready(function (){
	$tmp = ($('#page-wrap').attr('data-background').toString().split(','));
	imgs.push($tmp[Math.floor(Math.random()*$tmp.length)]);
	
	/* medewerkers */
	if ($('#teamleden').length > 0) initMedewerkers(1);
	
		
	if ($('#teamleden').length > 0) {
		$('#submenu').children('div').click(function() {
			var target = parseInt($(this).attr('data-group'));
			if (!$('#team-container ul').eq(target).hasClass('active')) {
				$('#teamleden-content').fadeOut('normal',function() {
					$(this).empty();
					initMedewerkers(target+1);
					$(this).fadeIn();
				});
			}
			if (target > -1) {
				//content terugzetten
				if ($('#content-copy div.entry').length > 0) {
					$('article div.entry').hide().remove();
					$('#content-copy div.entry').clone().appendTo('#article-wrap article');
					//$('#content-copy').empty();
					$('article div.entry').fadeIn(function(){resizeElements();});
				}
			}
			
			//persoon uit achtergrond halen
			$('#bgstretcher ul li img').not(':first').fadeOut('normal',function() {
				$(this).remove();	
			});

			return false;
		});
		
		$('#scroll-left').click(function() {
			$('#teamleden-content li:last').prependTo('#teamleden-content ul');
			return false;
		});

		$('#scroll-right').click(function() {
			$('#teamleden-content li:first').appendTo('#teamleden-content ul');
			return false;
		});
	}
	
	/* resizen */

	resizeElements();

        //  Initialize Backgound Stretcher	   
		$(document).bgStretcher({
			images: imgs,
			imageWidth: 1600, imageHeight: 900
		});

		
	//$('div.entry').jScrollPane({verticalDragMaxHeight: 82});

	// CUFON replacements
	Cufon.replace('article h5, #content-left h5', { fontFamily:'dinpro-light'});
	Cufon.replace('#submenu h3', { fontFamily:'dearjoe'});
	Cufon.replace('#submenu span', { fontFamily:'dinpro-black'});
	
});


/* optional triggers

$(window).load(function() {
	
});*/

$(window).resize(function() {
	resizeElements();
});

function resizeElements() {
	
	//console.log('triggered: resizeElements');
	var ww = $(window).width();
	var wh = $(window).height();
	
	var th = wh-pageWrapPadding-subMenuHeight-menuHeight;
	
	//if (th > 444) th = 444;
	if (th > 600) th = 600;
	
	if (th < 300) th = 300;
	
	$('#article-wrap, #article-left').height(th);
	
	$('#article-inner').height(th-40);

	$('div.entry').height(th-190);
	
	$('#teamleden').css('top',th);
		
	$('article div.entry').jScrollPane({verticalDragMaxHeight: 82, verticalDragMinHeight: 82});

}

function reInitScrollPane() {
	$('div.entry').each(function(){
		apis.push($(this).jScrollPane().data().jsp);
	});	
	
	if (apis.length) {
		$.each(apis, function(i) {
			this.destroy();
		})
	apis = [];
	}
}

function initMedewerkers(i) {
	if ($('#team-container')) {
		$('#team-container ul').removeClass('active');
		$('#team-container ul').eq(i-1).addClass('active').clone().appendTo('#teamleden-content');	
		
		$('#teamleden-content li').on('mouseenter', null, null, function() {
			$(this).find('div.naam').show();
		});
		$('#teamleden-content li').on('mouseleave', null, null, function() {
			$(this).not('.active').find('div.naam').hide();
		});

		$('#teamleden-content li').on('click', null, null, function() {
			if (!$(this).hasClass('active')) {
				$('#teamleden-content li').removeClass('active');
				$(this).addClass('active');
				
				//achtergrond laden:
				var img = new Image;
				img.src = $(this).attr('data-foto');
				$(img).css('display','none');
				$(img).load(function() {
					$('#bgstretcher ul li').append(img);
					$(window).trigger('resize');
					$(this).fadeIn('normal',function(){
						$('#bgstretcher ul li img').not(':first').not(':last').remove();
					});
				});

				reInitScrollPane();


				if ($(this).find('div.bio').length > 0){
					//deze persoon heeft een bio, die weergeven:
					if ($('#content-copy').html() == '') {
						//eerst copy maken van originele content
						$('article div.entry').clone().appendTo('#content-copy');
					}
					//$('article div.entry').empty().hide();
					$('article div.entry').empty();
					$(this).find('div.bio').find('*').clone().appendTo('#article-wrap article div.entry');
					$('article div.entry').fadeIn(function(){resizeElements();});
				} else {
					//deze persoon heeft geen bio. Als er een copy is van de content, die dan terugzetten:
					if ($('#content-copy div.entry').length > 0) {
						$('article div.entry').hide().remove();
						$('#content-copy div.entry').clone().appendTo('#article-wrap article');
						//$('#content-copy').empty();
						$('article div.entry').fadeIn();
					}
				}
			$(this).parent().find('li').not('.active').find('div.naam').hide();

			}
			
		});
	}
}

