/* 
	
	ABBYS Hoofd File
	
-------------------------------------------------------------------------------------------------- */
var activeMenuLevel = 1;
var l2target;
var view = $(window);
var debug = false;

/* Bind h1
-------------------------------------------------------------------------------------------------- */
$('h1').click(function(){
	window.location = '#';
	target = $('a.active.on');
	$('div.nav02.active').removeClass('active').fadeOut('fast',function(){
		window.location='#';
		$('#level3 div.article.active').fadeOut('fast');
		$(target).removeClass('active on').parents('#container').animate({'width':192});
		activeMenuLevel = 1;
	});
	resetCycle();
});

/* Cufon calls
-------------------------------------------------------------------------------------------------- */
Cufon.replace('#footer', { hover:true });
Cufon.replace('#level2 .cfn strong, #level2 .arrow');

/* Window load
-------------------------------------------------------------------------------------------------- */

// Force show
function forceShow(elem){
	if(elem.css('opacity') < 1){
		elem.css('opacity',1);
	}
}

$(document).ready(function (){
    $('#nieuws_nieuwsblok').cycle({
        fx:      'scrollHorz',
		timeout: 0, 

        prev:    '#nieuws_nieuwsnavigatie #prev',
        next:    '#nieuws_nieuwsnavigatie #next',
        pager:   '#nieuws_nieuwsnavigatie #nav',
        pagerAnchorBuilder: function(idx, slide) { 
         var s = idx > 9 ? ' style="display:none"' : '';
        return '<li'+s+'><a href="#"><img src="images/item.gif" alt="" /><span class="pijl"><img src="images/item_actief.gif" alt="" /></span></a></li>';
    }
    });
    $('#agenda_nieuwsblok').cycle({
        fx:      'scrollHorz',
		timeout: 0, 

        prev:    '#agenda_nieuwsnavigatie #prev2',
        next:    '#agenda_nieuwsnavigatie #next2',
        pager:   '#agenda_nieuwsnavigatie #nav2',
        pagerAnchorBuilder: function(idx, slide) { 
         var s = idx > 9 ? ' style="display:none"' : '';
        return '<li'+s+'><a href="#"><img src="images/item.gif" alt="" /><span class="pijl"><img src="images/item_actief.gif" alt="" /></span></a></li>';
    }
    });

});


/* EDIT BY YAPPO */
function dlog(string) {
	if (debug) console.log(string);
}

function resizeBG() {
	var targetHeight = false;
	var position = 0;
	$('#background img').width($(window).width()).css('height','auto');;
	$('#background img:visible').each(function() {
		//als hij niet hoog genoeg is dan hoger maken:
		if ($(this).height() < $(window).height()) {
			//$(this).height($(window).height());
			targetHeight = $(window).height()
			//$(this).css('top',0).css('width','auto');
		} else { //centreren
			
			position = $(window).height() - $(this).height();
			if (position < 0) position = position / 2; 
		}
	});
	if (targetHeight) {
		$('#background img').height(targetHeight).css('top',0).css('width','auto');
	} else {
		$('#background img').css('top', position / 2);
	}
}

view.resize(function(){
	$('#background, #background ul.homepage').height($(window).height()).width($(window).width());
	$('#level3 div.article, #inner, #container, #content').height(view.height() - 52);
	initScrollBars();
	resizeBG();	
});
	
$(document).ready(function (){
	$('#background li img:first').load(function() {
		view.trigger('resize');
		$('#background ul.homepage').cycle({
			containerResize: 0,
			slideResize: 0
		});
	});
	
	$.address.change(function(e){
		if (e.pathNames[1]) { //dan naar landing gaan, anders niets doen, omdat enkel het menu wordt uitgevouwen.
			//als er nog geen menu level 2 is geopend, dan die weergeven:
			showContent(e.pathNames[0] + '_' + e.pathNames[1]);
			//plaatje inladen
			var img = new Image;
			dlog('ul.landing li[data-tag=' + e.pathNames[0] + '_' + e.pathNames[1] + ']');
			img.src = $('ul.landing li[data-tag=' + e.pathNames[0] + '_' + e.pathNames[1] + ']').attr('data-src');
			img.onload = function() {
				$('#background ul.homepage').cycle('pause');
				if ($('#background ul.homepage li.landing').length > 0) {
					//appende aan bestaande landing images	
					$('#background ul.homepage li.landing').parent().append('<li class="landing" style="z-index:100;"></li>');
					$('#background ul.homepage li.landing:last').append(img);
					resizeBG();
					$('#background ul.homepage li.landing:last').fadeIn(function(){
						$('#background ul.homepage li.landing').not($(this)).remove();
					});
				} else {
					//prependen aan background images
					$('#background ul.homepage').prepend('<li class="landing" style="z-index:100;"></li>');
					$('#background ul.homepage li:first').append(img);
					resizeBG();
					$('#background ul.homepage li:first').fadeIn();
				}
				
			};
			if ($('#level2 > div.active').length < 1) {
				$('#nav01 a[href="#!/' + e.pathNames[0] + '"]').addClass('active on');
				var target = $('div#' + e.pathNames[0]);
				showMenu(2, target);
			}
		} else if (e.pathNames[0]){
			var target = $('#nav01 > ul > li > a').filter(':[href=#!' + e.value + ']');
			slideMenu(target);
		}
	});
	
	$('div.nav02').hide();
	
	//menu level 1 uitvouwen:
	$('#nav01 > ul > li > a').click(function() {
		slideMenu($(this));
	});

});

function initScrollBars() {
	$('#level3 div.article').not('#contact_route').jScrollPane({contentWidth:387, verticalGutter:20, verticalDragMaxHeight:300, verticalDragMinHeight:100, mouseWheelSpeed:50});
}

function showContent(identifier) {
	if (identifier == 'restaurant_overons') $('#restaurant_overons iframe').show();
	$('#container').animate({'width':790},'fast',function(){});
	if ($('#level3 div.article.active').length > 0) {
		//er zijn nog articles zichtbaar
		$('#level3 div.article.active').fadeOut('fast',function() {
			$(this).removeClass('active');
			$('#' + identifier).addClass('active').fadeIn(function(){
				initScrollBars();
				activeMenuLevel = 3;
				//swapImage(identifier);
			});
		});
	} else {
		$('#' + identifier).addClass('active').fadeIn(function(){
			initScrollBars();
			activeMenuLevel = 3;
			dlog(identifier);
			//swapImage(identifier);
		});
	}
}


function slideMenu(target) {
	if ($(target).length == 0) return false;
	l2target = $('div#' + $(target).attr('href').substring(3));
	if (!$(target).hasClass('active')) {
 		//content dichtvouwen als die geopend is
 		if (activeMenuLevel == 3) {
			$('#level3 div.article.active').fadeOut('fast',function() {
				$(this).removeClass('active');
				$('#container').animate({'width':400},'fast');
			});
			activeMenuLevel = 2;
		}
		
		$('#nav01 > ul > li > a').not(target).removeClass('active on');
		if (activeMenuLevel == 1){
			$(target).addClass('active on').parents('#container').animate({'width':400},'fast',function(){
				showMenu(2,l2target);
				activeMenuLevel = 2;
			}); 
		} else {
			$('#nav01 > ul > li > a').not(target).removeClass('active on');
			$(target).addClass('active on');
				showMenu(2,l2target);				
		}
	} else {
		//deze dichtvouwen
		
		//eerst foto verbergen
		$('#background ul.homepage li.landing').fadeOut(function() { 
			$(this).remove();
			$('#background ul.homepage').cycle('resume');
		});
		
		
		$('div.nav02.active').removeClass('active').fadeOut('fast',function(){
			window.location='#';
			$('#level3 div.article.active').fadeOut('fast');
			$(target).removeClass('active on').parents('#container').animate({'width':192});
			activeMenuLevel = 1;
			//resetCycle();
		});
	}
}

function showMenu(level, target) {
	if (activeMenuLevel > 1){
		$('div.nav02.active').removeClass('active').fadeOut('fast',function() {
			$(target).fadeIn('fast').addClass('active');
		});	
	} else {
		$(target).fadeIn('fast').addClass('active');		
	}
}
