$(document).ready(function() {
		$('.mna').mouseover(function(){
			$(this).parent().find('.mnor').show().animate({width: '235px'});
			$(this).animate({color: '#ffffff'});
		});
	
		$('.mna').mouseout(function(){ 
				$(this).parent().find('.mnor').animate({width: '1px'}, 250, function() {
					$(this).parent().find('.mnor').hide();						
				});
				$(this).animate({color: '#585d63'});
		});
	
		$('.main_img').hide().fadeIn('slow');
		$('.menu_sbt').hide().fadeIn();
		
		
		if ($('#lng').length>0) {
			/*$('#up').mouseover(function(){
				scrollUp('lng');
			});*/
			$('#up').click(function(){
				scrollUp('lng');
			});
			/*$('#down').mouseover(function(){
				scrollDown('lng')
			});*/
			$('#down').click(function(){
				scrollDown('lng');
			});
		}
		if ($('#olng').length>0) {
			/*$('#up').mouseover(function(){
				scrollUp('olng');
			});*/
			$('#up').click(function(){
				scrollUp('olng');
			});
			/*$('#down').mouseover(function(){
				scrollDown('olng')
			});*/
			$('#down').click(function(){
				scrollDown('olng');
			});
		}
		
				
		if ($('#prjs').length>0) {
			$("#prjs").each(function(){
			  var obj = this;
			  if ($("li", obj).length > 3) {
				$(obj).wrapInner("<div class=container/>");
				obj.container = $(".container", obj);
				obj.previous = $("<span class=previous/>");
				obj.next = $("<span class=next/>");
				$(obj).prepend(obj.next).prepend(obj.previous);
				obj.timer = null;
				obj.sleep = 2000;
				obj.animRun = false;
				//obj.start = function(){obj.timer = setTimeout(obj.showNext, obj.sleep)};
				obj.showNext = function(single){
				  //clearTimeout(obj.timer);
				  if (obj.animRun) {setTimeout(obj.showNext, 50);return;}
				  obj.animRun = true;
				  $("li:first-child", obj).animate({marginLeft: "-313px"}, 1000, "linear", function(){
					$("li:first-child", obj).appendTo("#prjs ul").css({marginLeft: "0px"});
					//if (single !== true) obj.start();
					obj.animRun = false;
				  });
				};
				obj.showPrevious = function(single){
				  //clearTimeout(obj.timer);
				  if (obj.animRun) {setTimeout(obj.showPrevious, 50);return;}
				  obj.animRun = true;
				  $("li:last-child", obj).prependTo("#prjs ul").css({marginLeft: "-313px"});
				  $("li:first-child", obj).animate({marginLeft: "0px"}, 1000, "linear", function(){
					//if (single !== true) obj.start();
					obj.animRun = false;
				  });
				};
				$(obj).mousemove(function(){clearTimeout(obj.timer)});
				$(obj).mouseout(obj.start);
				obj.previous.click(function(){obj.showPrevious(true)});
				obj.next.click(function(){obj.showNext(true)});
				obj.previous.mouseover(function(){obj.showPrevious(true)});
				obj.next.mouseover(function(){obj.showNext(true)});
				//obj.start();
			  }
			});
		}
		
			
		if ($('#trc').length>0) { 
			$('#pa1').css('background','url(resources/pa.png) center top no-repeat');
			$("#trc").each(function(){ 
			  var obj = this;
			  
			  if ($("li", obj).length > 1) {
				$(obj).wrapInner("<div class=container/>");
				obj.container = $(".container", obj);
				obj.previous = $('<span class="previous" title="Vorige afbeelding" />');
				obj.next = $('<span class="next" title="Volgende afbeelding" />');
				$(obj).prepend(obj.next).prepend(obj.previous);
				obj.timer = null;
				obj.sleep = 2000;
				obj.animRun = false;
				
				obj.showNext = function(single,time){
				 
				  if (obj.animRun) {setTimeout(obj.showNext, 50);return;}
				  obj.animRun = true;
				  $("li:first-child", obj).animate({marginLeft: "-700px"}, time, "linear", function(){
					$("li:first-child", obj).appendTo("#trc ul").css({marginLeft: "0px"});
					//if (single !== true) obj.start();
					obj.animRun = false;
				  });
				  var num = $('#num').val();
				  prjNum(parseInt(num)+1);
				  
				};
				obj.showPrevious = function(single,time){
				  
				  if (obj.animRun) {setTimeout(obj.showPrevious, 50);return;}
				  obj.animRun = true;
				  $("li:last-child", obj).prependTo("#trc ul").css({marginLeft: "-700px"});
				  $("li:first-child", obj).animate({marginLeft: "0px"}, time, "linear", function(){
					//if (single !== true) obj.start();
					obj.animRun = false;
				  });
				  var num = $('#num').val();
				  prjNum(parseInt(num)-1);
				};
				$(obj).mousemove(function(){clearTimeout(obj.timer)});
				$(obj).mouseout(obj.start);
				obj.previous.click(function(){obj.showPrevious(true,500)});
				obj.next.click(function(){obj.showNext(true,500)});
				
			  }
			});
			
			$('.pa').each(function(){
				$(this).click(function(){
					
					$(this).css('background','url(resources/pop.png) center top no-repeat');
					var num = $('#num').val();
					num 	= parseInt(num); 
					var ths = $(this).attr('name'); //alert('num='+num + ', en ths = ' + ths);
					ths 	= parseInt(ths);
					if (ths<num) {
						if (this.animRun) {setTimeout(this.showPrevious, 50);return;}
						var nxt = num-ths; 
						for (i=0;i<nxt;++i) {
							$('#trc').each(function(){
								this.showPrevious(true,250);
							});	
						}
					} else if (ths>num) {
						if (this.animRun) {setTimeout(this.showNext, 50);return;}
						var nxt = ths-num;
						
						for (i=0;i<nxt;++i) {
							$('#trc').each(function(){
								this.showNext(true,250);
							});
						}
					}
				});
			});
		}
		
		$('#ytb').click(function(){
			youTube($(this).attr('name'));
		});
		
	$('#cycle').cycle({
		fx: 'fade',
		timeout:	2000,
		speed:      2000
	});
});

function prjNum(num) {
	var tot = $('#tot').val();
	if (num<1) { num = tot; } else if (num > tot) { num = 1; }
	var curn = $('#num').val();
	$('#trc_nm').find('#pa'+curn).css('background','url(resources/pia.png) center top no-repeat');	
	$('#num').val(num);
	$('#pa'+num).css('background','url(resources/pa.png) center top no-repeat');
}

function scrollUp(divnm){
	$('#'+divnm).stop().scrollTo({top:'-=120px',left:'0'}, 500);	
}
function scrollDown(divnm){
	$('#'+divnm).scrollTo({top:'+=120px',left:'0'}, 500);
		
}

function posWrap() {
	var wh = $(window).height();
	var wrw = (wh-578)/2;
	var wrb = -213 + wrw;
	$('#wrapper').css('margin-top',wrw);
	$('body').css('background-position','center '+wrb+'px');
}

function youTube(embedcode) {
	if (embedcode!='') {
		$('#tlc').append('<div id="youtube"><iframe width="710" height="438" src="http://www.youtube.com/embed/'+embedcode+'?rel=0&autoplay=1" frameborder="0" allowfullscreen></iframe></div>');	
		$('#logo').prepend('<img src="resources/close.png" alt="Video sluiten" title="Sluit deze video af" id="ytc" />');
		$('#ytc').click(function(){
			$('#ytc').remove();
			$('#youtube').hide().remove();
		});
	}
}
