// JavaScript Document
google.setOnLoadCallback(function() {

	$(function() {

		// prevent fading with active class
		$('#voorwoord #but1').addClass('active');
		$('#voorwerk #but2').addClass('active');
		$('#voorstellen #but3').addClass('active');
		$('#voortraject #but4').addClass('active');
			
		// fade in main navigation on mouse over
		$('.navbut').mouseover(function (e) {
			this.style.cursor='auto';
			if ($(this).hasClass('active')) { }
			else { 
				$(this).children('a').animate({ opacity:0.5 }, { queue:false, duration:500 }).removeAttr('filter');
				this.style.cursor='pointer';
			}
		});
		// fade out main navigation on mouse out
		$('.navbut').mouseout(function (e) {
			this.style.cursor='auto';
			if ($(this).hasClass('active')) { }
			else { 
				$(this).children('a').animate({ opacity:0 }, { queue:false, duration:500 });
				this.style.cursor='pointer';
			}
		});
	
		// scroll de tekst
		var contentYloc = $('#content').offset().top;
		var contentHeight = function() {
			return $('#content').height();
		};
		var scrollboxHeight = function() {
			return $('#scrollbox').height();
		};
		$('#scrollbox').live('mousemove', function (e) {
			var mousePosY = e.pageY - contentYloc;
			var dY = contentHeight() - scrollboxHeight();
			var offset = Math.floor((dY) * mousePosY / contentHeight()) + "px";
			if (dY < 0) {
				$('#scrollbox').animate({top: offset}, {duration: 0, queue: false}); 
			}
		});
		
		function resetScrollboxTop(e) {
			$("#scrollbox").css("top", 0);
		}	
		$(window).bind("resize", resetScrollboxTop);
	
		function delayGotoHref(goHere) {
			setTimeout(function() { window.location.href = goHere; }, 1000);
		}
		
		function longDelayGotoHref(goHere) {
			setTimeout(function() { window.location.href = goHere; }, 3000);
		}
		
		// Logo naar linksonder
		function animateToDL() {
			$('#but5').animate({
				left: "0%",
				top: "50%",
				width: "50%"
			}, 1000);
		}

		// Logo naar linksboven
		function animateToTL() {
			$('#but5').animate({
				left: "0%",
				top: "10%",
				width: "50%"
			}, 1000);
		}

		// Logo naar rechtsonder
		function animateToDR() {
			$('#but5').animate({
				left: "50%",
				top: "50%",
				width: "50%"
			}, 1000);
		}
	
		// Logo naar midden links
		function animateToML() {
			$('#but5').animate({
				left: "0%",
				width: "50%",
				top: "40%"
			}, 1000);
		}
		
		// Logo naar midden
		function animateToM() {
			$('#but5').animate({
				left: "33%",
				width: "33%",
				top: "40%"
			}, 1000);
		}
	
		// Gordijn1 dicht
		function sluitGordijn1() {
			$('#but1 .gordijn').animate({ top:30, height:'100%' }, { queue:false, duration:1000 });
		}
		// Gordijn2 dicht
		function sluitGordijn2() {
			$('#but2 .gordijn').animate({ right:30, width:'100%' }, { queue:false, duration:1000 });
		}
		// Gordijn3 dicht
		function sluitGordijn3() {
			$('#but3 .gordijn').animate({ bottom:30, height:'100%' }, { queue:false, duration:1000 });
		}
		// Gordijn4 dicht
		function sluitGordijn4() {
			$('#but4 .gordijn').animate({ left:30, width:'100%' }, { queue:false, duration:1000 });
		}
		// Gordijn1 open
		function openGordijn1() {
			$('#but1 .gordijn').animate({ top:0, height:30 }, { queue:false, duration:1000 });
		}
		// Gordijn2 open
		function openGordijn2() {
			$('#but2 .gordijn').animate({ right:0, width:30 }, { queue:false, duration:1000 });
		}
		// Gordijn3 open
		function openGordijn3() {
			$('#but3 .gordijn').animate({ bottom:0, height:30 }, { queue:false, duration:1000 });
		}
		// Gordijn4 open
		function openGordijn4() {
			$('#but4 .gordijn').animate({ left:0, width:30 }, { queue:false, duration:1000 });
		}
		function sluitAlle() {
			sluitGordijn1();
			sluitGordijn2();
			sluitGordijn3();
			sluitGordijn4();
		}
		function openAlle() {
			openGordijn1();
			openGordijn2();
			openGordijn3();
			openGordijn4();
		}
		function hideStuff() {
			$('#content').fadeOut(500);
			$('#subnav').fadeOut(500);
			$('#zoombox').fadeOut(500);
			$('#adres').fadeOut(500);
			$('#but4').css('backgroundColor','transparent');
		}
		
		// Achtergrondkleur toevoegen/verwijderen voor rendering in IE:
		$('#voorwoord #but1 a').css('backgroundColor','#0033cc');
		$('#voorwerk #but2 a').css('backgroundColor','#ffdd00');
		$('#voorstellen #but3 a').css('backgroundColor','#cc0000');
		$('#voortraject #but4 a').css('backgroundColor','#009900');
		function removeBgColor(elem) {
			elem.css('backgroundColor','transparent');
		}
		$('.navbut, .navbut a').click(function (e) {
			removeBgColor($('.navbut a'));
			return false;
		});
		
		// Alle pagina's behalve Mental Preference
		$('#but1 a').not($('#voorwoord #but1 a')).click(function (e) {
			animateToDL();
			openGordijn2();
			openGordijn3();
			openGordijn4();
			sluitGordijn1();
			hideStuff();
			delayGotoHref(this.href);
			return false;
		});
		$('#but1').not($('#voorwoord #but1')).click(function (e) {
			animateToDL();
			openGordijn2();
			openGordijn3();
			openGordijn4();
			sluitGordijn1();
			hideStuff();
			delayGotoHref($(this).children('a').attr('href'));
			return false;
		});
		$('#but2 a').not($('#voorwerk #but2 a')).click(function (e) {
			animateToML();
			openGordijn1();
			openGordijn3();
			openGordijn4();
			sluitGordijn2();
			hideStuff();
			delayGotoHref(this.href);
			return false;
		});
		$('#but2').not($('#voorwerk #but2')).click(function (e) {
			animateToML();
			openGordijn1();
			openGordijn3();
			openGordijn4();
			sluitGordijn2();
			hideStuff();
			delayGotoHref($(this).children('a').attr('href'));
			return false;
		});
		$('#but3 a').not($('#voorstellen #but3 a')).click(function (e) {
			animateToTL();
			openGordijn1();
			openGordijn2();
			openGordijn4();
			sluitGordijn3();
			hideStuff();
			delayGotoHref(this.href);
			return false;
		});
		$('#but3').not($('#voorstellen #but3')).click(function (e) {
			animateToTL();
			openGordijn1();
			openGordijn2();
			openGordijn4();
			sluitGordijn3();
			hideStuff();
			delayGotoHref($(this).children('a').attr('href'));
			return false;
		});
		$('#but4 a').not($('#voortraject #but4 a')).click(function (e) {
			animateToDR();
			openGordijn1();
			openGordijn2();
			openGordijn3();
			sluitGordijn4();
			hideStuff();
			delayGotoHref(this.href);
			return false;
		});
		$('#but4').not($('#voortraject #but4')).click(function (e) {
			animateToDR();
			openGordijn1();
			openGordijn2();
			openGordijn3();
			sluitGordijn4();
			hideStuff();
			delayGotoHref($(this).children('a').attr('href'));
			return false;
		});
		$('#voortraject #but5, #voorwoord #but5').click(function (e) {
			delayGotoHref(this.href);
			$('#content').show().hide('normal');
			$('#subnav').show().hide('normal');
			removeBgColor($('.navbut a'));
			animateToM();
			openAlle();
			return false;
		});
		$('#but5').not($('#home #but5, #mental #but5')).click(function (e) {
			removeBgColor($('.navbut a'));
			openAlle();
			animateToM();
			hideStuff();
			delayGotoHref(this.href);
			return false;
		});		
		$('#home #but5').click(function (e) {
			sluitAlle();
			hideStuff();
			delayGotoHref(this.href);
			$('#but5 img').fadeOut(500);
			return false;
		});

		// Voortraject pagina
		$('#voortraject #subnav a').not($('.ajaxoff')).click(function (e){
			$('#voortraject #subnav a').removeClass('active');
			$(this).addClass('active');
			$('#voortraject #content').load(this.href);
			return false;
		});

		// Voorstellen pagina
		$('#voorstellen #subnav a').click(function (e){
			var naam='#'+$(this).attr('class');
			var verbergen=$('#zoombox img').not($(naam));
			var tonen=$(naam);
			
			$('#voorstellen #subnav a').removeClass('active');
			$(this).addClass('active');
			$('#voorstellen #content').load(this.href);
			
			verbergen.animate({
				left: "100%"
			}, 500);
			tonen.animate({
				left: "20%"
			}, 500);
			
			return false;
		});

		// Voorwerk pagina
		$('#voorwerk #subnav a').click(function (e){
			$('#but5').fadeOut(500);
			$('#content').fadeOut(500);
			$('#subnav a').removeClass('active');
			$(this).addClass('active');
			delayGotoHref(this.href);
			return false;
		});
		
		function resizeZoomImg() {
			var mw = $('#zoombox').width();
			var mh = $('#zoombox').height();
			$('#zoombox img').css({'maxWidth':'mw','maxHeight':'mh'});
			//$('#zoombox img').jScale({h:$('#zoombox').height()});
		}
		
		$(window).bind("resize", resizeZoomImg);
		
		$('#voorwerk #thumbs a img').click(function (e){
			var ext=$(this).attr('src').slice(-4);			// de extensie bepalen
			var pad=$(this).attr('src').slice(0,-9) + ext;  // 'klein' uit het pad verwijderen
			var url=$(this).parent().attr('href');
			var moviepad=$(this).parent().attr('class');
			var mediastring="<a href='http://www.voor.cc/swf/"+moviepad+"' class='media'></a>";
			var imagestring="<img src='"+pad+"' alt='' />";
			var oms='<p>'+$(this).attr('alt')+'</p>';
			if ($(this).hasClass('media')) {
				$('#zoombox').html(mediastring);
				$('#zoombox .media').media();
			} else {
				$('#zoombox').html(imagestring);
			}
			$('#story').html(oms);
			$('body.internet #story p').wrap("<a href='' target='_blank'></a>");
			$('#story>a').attr('href',url);
			return false;
		});
		
		// Mental preference pagina
		$('#mental .navbut a').click(function (e) {
			$('#but5').fadeOut(500);
			delayGotoHref(this.href);
			$('#but5').stop();
			return false;
		});
		$('#mental .navbut').click(function (e) {
			$('#mental #but5').fadeOut(500);
			delayGotoHref($(this).children('a').attr('href'));
			$('#but5').stop();
			return false;
		});
		$('#mental #but5').click(function (e) {
			$(this).load('inc/mental_uitleg.php').css('backgroundColor','#ffffff').css('textDecoration','none');
			$(this).removeAttr('href');
			return false;
		});
				
	});
});
