// JavaScript Document

$(document).ready(function() {

	var imgPosition			=	0;
	var viewPort			=	223;
	var speedFactor			=	10;
	var scrolling			=	false;
	/**
	$('#topbar #items').cycle({
		fx			:	'scrollUp',
		duration	:	1000,
		timeout		:	7500
	});
	*/
	$('#scrollDown').mousemove(function() {
		if(!scrolling) {
			scrolling		=	true;
			var currImage	=	$('#projects .img_'+imgPosition);
			var imgHeight 	= 	parseInt(currImage.height()) - viewPort;
			var topPosition	=	parseInt(currImage.position().top) + viewPort;
			var speed		=	(imgHeight - viewPort) * speedFactor;
			if(topPosition != 0) {
				var tmpPos	=	topPosition.toString()
				var pos		=	parseInt(topPosition.toString().substr(1, (tmpPos.length-1)));
				speed		=	(imgHeight - pos) * speedFactor;
			}
			currImage.animate({
				top:'-'+imgHeight+'px'
			}, speed, 'linear');
		}
	}).mouseleave(function() {
		scrolling			=	false;
		$('#projects .img_'+imgPosition).stop();
	});
	
	$('#scrollUp').mouseover(function() {
		if(!scrolling) {
			scrolling		=	true;
			var currImage	=	$('#projects .img_'+imgPosition);
			var imgHeight 	= 	parseInt(currImage.height()) - viewPort;
			var topPosition	=	parseInt(currImage.position().top) + viewPort;
			var speed		=	(imgHeight - viewPort) * speedFactor;
			if(topPosition != 0) {
				var tmpPos	=	topPosition.toString()
				var pos		=	parseInt(topPosition.toString().substr(1, (tmpPos.length-1)));
				speed		=	(imgHeight - pos) * speedFactor;
			}
			currImage.animate({
				top:'0px'
			},speed,'linear');
		}
	}).mouseleave(function() {
		scrolling			=	false;
		$('#projects .img_'+imgPosition).stop();
	});
	
	$('#paging span').click(function() {
		var currPage		= 	parseInt($('#paging span.active').text());
		if(!$(this).hasClass('active')) {
			var newPage		= 	parseInt($(this).text());
			imgPosition 	= 	newPage - 1;
			$('#paging span').removeClass();
			$(this).addClass('active');
			$('#projects').children().animate({left:'-'+((newPage - 1)*55)+'0px'},550);
			window.setTimeout(function() {
				$('#projects .img_'+(currPage-1)).css({top:'0px'});
			},550);
		}
		
	});
	
	$('#menu .item').mouseover(function() {
		$(this).children('.submenu').show();
	}).mouseout(function() {
		$(this).children('.submenu').hide();
	});
	
	$('.faq li').click(function() {
		if($(this).children('div').css('display') == 'none') {
			$(this).children('div').show();
		}
		else {
			$(this).children('div').hide();
		}
	});
	if(!$('#presentatie').hasClass('off')) {
		$('#presentatie').animate({height:'250px'}, 2500);
	}
	else {
		$('#impressie').show();
	}
	window.setTimeout(function() {
		$('#presentatie #middle').fadeIn(500);
		startPres(1500, parseInt($('.group .prj').length), 6000);
	}, 2000);
	$('#presentatie #sluiten').click(function() {
		$('#presentatie #middle').fadeOut(500);
		$('#presentatie').animate({height:'0px'}, 1500);
		window.setTimeout(function() {
			$('#impressie').fadeIn(500);
		},1500);
		$.ajax({url:'includes/request.php?presentatie=1'});
	});
	$('#impressie').click(function() {
		$(this).fadeOut(500);		
		$('#presentatie #middle').fadeIn(500);
		$('#presentatie').animate({height:'250px'}, 1500);
		$.ajax({url:'includes/request.php?presentatie=0'});
	});

});

var count = 0;
function startPres(tm, projects, dr) {
	var ti = false;
	if(count == 0 || count == 4 || count == 6) {
		dr = 3000;
		ti = true;
	}
	else if(count == 5) {
		dr = 9000;
	}
	if(count < projects) {
		if(ti) {
			$('#presentatie #projecten .group').animate({marginTop: '-'+(count*250)+'px'}, 500);
			count++;
			startPres(tm, projects, 6000);
		}
		else {
			window.setTimeout(function() {
				$('#presentatie #projecten .group').animate({marginTop: '-'+(count*250)+'px'}, 500);
				if(count == 4) {
					$('.social').children().each(function() {
						window.setTimeout(function() {
							$('.social .twitter').fadeIn(500);		   
						}, 500);
						window.setTimeout(function() {
							$('.social .linkedin').fadeIn(500);		   
						}, 1000);
						window.setTimeout(function() {
							$('.social .facebook').fadeIn(500);		   
						}, 1500);
						window.setTimeout(function() {
							$('.social .twitter').fadeOut(250);		   
						}, 8250);
						window.setTimeout(function() {
							$('.social .linkedin').fadeOut(250);		   
						}, 8500);
						window.setTimeout(function() {
							$('.social .facebook').fadeOut(250);		   
						}, 8750);
					});
				}
				else {
					$('#presentatie .show').cycle({
						fx			:	'fade',
						duration	:	1000,
						timeout		:	2500
					});
				}
				window.setTimeout(function() {
					count++;
					$('#presentatie .show').cycle('stop');
					startPres(tm, projects, 6000);
				}, dr);
			}, tm);
		}
	}
	else {
		$('#presentatie #projecten').fadeOut(1500);
		window.setTimeout(function() {
			count = 0;
			$('#presentatie #projecten .group').css({marginTop:'0px'});
			$('#presentatie #projecten').fadeIn(500);
			startPres(tm, projects, 6000);
		}, 1500);
	}
}
