$(document).ready(function() {
	if ($.browser.msie) {
		var v=$.browser.version; v=v.substr(0,1);
		if (v<7) {$('#introtekst').append('<p><br/>LET OP: U gebruikt geen moderne browser. Dat kan veiligheidsproblemen geven. Ook kan dat sites minder mogelijkheden geven. Zo mist u hier een mooie panoramaviewer, meerdere fotos, en een mooiere layout.</p>');}
	}
	if ($.browser.safari) {
		$('#klokkader').add('#middenkader').css({'margin-right':'-2px'});
	}
	
	if ($('body').hasClass('timemachine')) {
		$('#gegevenskader div:not(#introtekst):not(#resettekst)').hide();
		$('#resettekst').show();
	}
	
	// extra > level3
	$('#menu li.lev3 a').prepend('&gt; ');
	// hover sub sub
	$('#menu li.lev2:not(.active)').each(function(){
		// has ul?
		if ($(this).children('ul').length>0) {
			// remove link
			var link=$(this).children('a:first').html();
			$(this).children('a:first').remove();
			$(this).prepend('<span>'+link+'</span>');
			// click is fold in/out
			$(this).click(function(e){
				if ($(this).hasClass('foldout')) {
					$(this).children('ul').hide();
					$(this).removeClass('foldout');
				}
				else {
					$(this).children('ul').show();
					$(this).addClass('foldout');
				}
			});
		}
	});
	// has sub? not clickable
	$('#menu li.sub>a').click(function(event){
	  event.preventDefault();
	});
	
	
	
	// blog
	$('#blog .item .tekst').hide();
	$('#blog #item0 .tekst').show();
	$('#blog .item').css('cursor','pointer').click(function(){
		// hide all
		$('#blog .item .tekst').hide();
		// show this one
		$(this).children('.tekst').show();
		// select fotos
		var fotos=$(this).children('.fotos');
		var fotosHtml=$(fotos).html();
		if (fotosHtml.length>10) {
			fotosHtml=fotosHtml.replace(/_small_/ig,'');
			// replace current fotos with these
			$('#foto').html(fotosHtml);
			var name=$(fotos).children('img:first').attr('alt');
			$('#fotosdatum').html(name);
			setFotos();
		}
	});
	
	// assortiment foto's
	setAssortimentFotos();
	function setAssortimentFotos() {
		$('#assortiment .item .fotos').click(function(){
			var fotos=$(this).html();
			fotos=fotos.replace(/_small_/ig,'');
			// replace current fotos with these
			$('#foto').html(fotos);
			var name=$(this).children('img:first').attr('alt');
			$('#fotosdatum').html(name);
			setFotos();
			rePosFoto();
		});
		if ($('body').hasClass('assortiment')) {
			var name=$('ul#assortiment li:first img').attr('alt');
			$('#fotosdatum').html(name);
		}
	}
	
	// assortiment filter/sorteer
	$('#taalkeus li a').click(function(){
		$('#taalkeus li a').removeClass('current');
		$(this).addClass('current');
		var l=$(this).attr('id');
		sortOn(l);
		$('#alfabet li a').removeClass('current');
		$('#alfabet li.alles a').addClass('current');
		$('#assortiment li').show();
	});
	$('#alfabet li').click(function(){
		$('#alfabet li a').removeClass('current');
		$(this).children('a').addClass('current');
		var l=$('#taalkeus li a.current').attr('id');
		var c=$(this).attr('class');
		if (c=='alles') {
			$('#assortiment li').show();
		}
		else {
			$('#assortiment li').hide();
			$('#assortiment li.'+l+'_'+c).show();
		}
	});

	function sortNL(a,b) {
		var x=$(a).attr('naamNL');
		var y=$(b).attr('naamNL');
		var z=((x<y)?-1:((x > y)?1:0));
		return z;
	}
	function sortLAT(a,b) { 
		var x=$(a).attr('naamLAT');
		var y=$(b).attr('naamLAT');
		var z=((x<y)?-1:((x > y)?1:0));
		return z;
	}
	function sortOn(at) {
		var list=$('#assortiment li');
		if (at=='NL') list.sort(sortNL);
		if (at=='LAT') list.sort(sortLAT);
		$('#assortiment').html(list);
		setAssortimentFotos();
	}
	
});


$(window).load(function(){
	setFotos();
	rePosFoto();
	
	// Panorama
	
	if ($('#pans img').length>1) {
		$('#panoramavooruit a').add('#panoramaachteruit a').show();
		$('#panoramavooruit a').click(function(){showPan(-1)});
		$('#panoramaachteruit a').click(function(){showPan(1)});
	}
	
	function showPan(add,big) {
		if (big) {
			var current=$(pan).children('img:first').attr('class');
			current=Number(current.replace('nr',''));
		}
		else {
			var current=$('#panorama div:first img').attr('class');
			current=Number(current.replace('nr',''));
		}
		var total=$('#pans img');
		total=total.length;
		var next=current+add;
		if (next<1) next=total-1;
		if (next>total-1) next=1;
		if (big) {
			var i=$('#pans img.nr'+next).attr('src');
			var alt=$('#pans img.nr'+next).attr('alt');
			i=i.replace(/_small_/g,'');
			// preload first
			$.get('img.php?i='+i);
			$(pan).fadeOut('slow',function(){
				// preload again, wait
				$.get('img.php?i='+i, function(data){
					// ok ready (in cache)
					$('#foto').html('');
					$('#fotosdatum').html(alt);
					i='<div id="panScroller"><img class="nr'+next+'" src="'+i+'" alt="'+alt+'"/></div>';
					$('#foto').append(i);
					pan=$('#panScroller');
					$(pan).children('img:first').hide();
					$(pan).css({left:'0px'});
					$(pan).children('img:first').fadeIn('slow',function(){
						startPanorama();
					});
				});
			});
		}
		else {
			$('#panorama div:first img').remove();
			next=$('#pans img.nr'+next).clone();
			$('#panorama div:first').html(next);
			// name
			var alt=$('#panorama div:first img').attr('alt');
			$('#panoramadatum').text(alt);
			startPanorama();
		}
	}
	
	var speed=0;
	var pan=$('#panScroller');
	var foto=$(pan).html();
	var w=$(pan).children('img:first').width();

	startPanorama();
	function startPanorama() {
		$(pan).unbind();
		// panoramafoto
		foto=$(pan).html();
		w=$(pan).children('img:first').width();
		$(pan).append(foto).prepend(foto);
		$(pan).children('img:first').css({marginLeft:-w});
		$(pan).children('img:last').css({marginLeft:w});

		$(pan).hover(function(e){
			// start moving
			$(this).mousemove(function(e){
				var middle=$(pan).width()/2;
				var m=(e.pageX-middle)/(middle/2);
				speed=Math.pow(m,4)*m;
			});
		},function(e){
			speed=0;
			$(this).unbind('mousemove');
		});
		$('#panorama #panScroller').click(function(){
			// place panorama at foto when clicked
			var i=$('#panorama #panScroller img:first').attr('src');
			var nr=$('#panorama #panScroller img:first').attr('class');
			var alt=$('#panorama #panScroller img:first').attr('alt');
			i=i.replace(/_small_/g,'');
			// preload first
			$('#panScroller').attr('id','panOriginal').unbind();
			$('#fotoachteruit').add('#fotovooruit').unbind();
			$('#fotosdatum').html('Laden...');
			$('#panoramaachteruit').add('#panoramavooruit').html('');
			// preload
			$.get('img.php?i='+i);
			$('#foto *:first').fadeOut('slow',function(){
				// preload again, wait
				$.get('img.php?i='+i, function(data){
					// ok ready (in cache)
					$('#foto *').remove();
					i='<div id="panScroller"><img class="'+nr+'" src="'+i+'" alt="'+alt+'" /></div>';
					$('#foto').append(i);
					pan=$('#panScroller');
					$(pan).children('img:first').hide();
					$(pan).css({left:'0px'});
					$(pan).children('img:first').fadeIn('slow',function(){
						$('#fotosdatum').html(alt);
						$('#fotoachteruit *').add('#fotovooruit *').show();
						$('#fotoachteruit').click(function(){showPan(1,true)});
						$('#fotovooruit').click(function(){showPan(-1,true)});
						startPanorama();
					});
				});
			});
		});

		$.fjTimer({interval: 50, repeat:true, tick: movePan });
	}
	
	function movePan(counter,id) {
		if (speed!=0) {
			if (w>0) {
				if ($(pan).children('img').length>1) {
					var l=$(pan).position().left-speed;
					while (l<0) {l+=w;}
					while (l>w) {l-=w;}
					$(pan).css({left:l});
				}
			}
			else {
				w=$('#panScroller img:first').width();
			}
		}
		else {clearInterval(id);}
	}
});

function setFotos() {
	$('#fotoachteruit').unbind();
	$('#fotovooruit').unbind();
	var fotos=$('#foto img');
	$('#fotoachteruit *').add('#fotovooruit *').hide();
	if (fotos.length>1) {
		$('#fotoachteruit *').add('#fotovooruit *').show();
		$('#fotoachteruit').click(function(){showFoto(-1);});
		$('#fotovooruit').click(function(){showFoto(1);});
		$('#foto img').click(function(){showFoto(1);}).css({cursor:'pointer'});
	}
}

function showFoto(add) {
	var current=$('#foto img.current').attr('id');
	current=Number(current.replace('foto',''));
	var total=$('#foto img');
	total=total.length;
	var next=current+add;
	if (next<0) next=total-1;
	if (next>total-1) next=0;
	$('#foto img').removeClass('current').fadeOut('slow');
	// preload first
	var i=$('#foto img#foto'+next).attr('src');
	$.get('img.php?i='+i, function(data){
		$('#foto img#foto'+next).addClass('current').fadeIn('slow',function(){
			// name
			var alt=$('#foto img.current').attr('alt');
			$('#fotosdatum').text(alt);
			rePosFoto();
		});
	});
}



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

function rePosFoto() {
	var f=$('#foto img.current');
	if (f.length>0) {
		var h=$(f).height();
		var maxh=$('#foto').height();
		w=$(f).width();
		var maxw=$('#foto').width();
		if (w>maxw) {
			$(f).css({height:'auto',width:'100%'});
		}
		if (h>maxh) {
			$(f).css({height:'100%',width:'auto'});
		}
		nw=$(f).width();
		if (nw>0) w=nw; 
		$(f).css({left:'50%',marginLeft:-(w/2)});
	}
	else {
		f=$('#foto iframe');
		if (f.length>0) {
			var maxh=$('#foto').height();
			var maxw=$('#foto').width();
			$(f).css({width:maxw,height:maxh});
		}
	}
}


