$(document).ready(function() {
	$('#banners').cycle({
		fx: 'fade',
		speed: 2000,
		after: function()
		{
			sifrbanner();
		}
	});

	$('#nieuwsslide').after('<div class="nieuwsitemfotos">').cycle({ 
		fx:     'fade', 
		speed:  2000, 
		pager:  '.nieuwsitemfotos', 
		next:   '#next',
		prev:   '#prev',
    	timeout: 0,
		// callback fn that creates a thumbnail to use as pager anchor 
		pagerAnchorBuilder: function(idx, slide) { 
			src = slide.src.replace('large', 'mini');
			return '<a href="#"><img src="' + src + '" width="30" height="30" /></a>'; 
		} 
	});
	
	$('.project').mouseover(function(event){
		$(this).find('.projectoverlay').hide();
	});
	
	$('.project').mouseout(function(event){
		$(this).find('.projectoverlay').show();
	});
/* 	function onAfter() {
    $('#output').html("Scroll complete for:<br>" + this.src)
        .append('<h3>' + this.alt + '</h3>');
} */
	$(this).find('span.active').hide();
	
	$('.categorie_header').click(function(event){		
		if($(this).find('img').attr('src') == '/images/categoriepijl_active.png' || $(this).find('img').attr('src') == '/images/pijl_active.png')
		{
			$(this).find('img').attr('src', '/images/categoriepijl.png');
			$(this).find('span.active').hide();
			$(this).find('span.normal').show();
		}
		else
		{
			if($('.faqblok').length > 0)
			{
				// Wanneer we op de FAQ pagina zijn moet er een ander pijltje geladen worden
				$(this).find('img').attr('src', '/images/pijl_active.png');
			}
			else
			{
				$(this).find('img').attr('src', '/images/categoriepijl_active.png');
			}
			
			$(this).find('span.active').show();
			//loadactivecategorie();
			$(this).find('span.normal').hide();
		}
	});
	
	/** Voor de uitklap div naar beneden **/
	$('.project_categorie').each(function()
	{
		var height = $(this).children('.categorie_content').height();

		$(this).addClass('closed').children('.categorie_content').css({ height: 0 });
		
		$(this).children('.categorie_header').click(function()
		{
			if($(this).parent().hasClass('closed'))
			{
				$('.opened').children('.categorie_content').animate({ height: 0 }, 500, function()
				{
					$(this).parent().removeClass('opened').addClass('closed');
					$(this).css('overflow', 'hidden');
					$(this).prev().find('img').attr('src', '/images/categoriepijl.png');
					$(this).prev().find('span.active').hide();
					$(this).prev().find('span.normal').show();
					
					// Onderstaande stukje om de sifr te laden voor de niet-active tekst van items die direct opengeklapt waren door submenu
					if($(this).parent().hasClass('open-nu'))
					{
						//loadnormalcategorie();
						$(this).parent().removeClass('open-nu');
					}
					
				});
				$(this).next().animate({ height: height }, 500, function()
				{
					$(this).parent().removeClass('closed').addClass('opened');
					$(this).css('overflow', 'visible');
					
					$.post('/nl/ajax', { page: 'getSectorInfo', link: $(this).prev().find('input[name=link]').val() }, function(data)
					{
						$('#content_right').html(data);
					});
				});
			}
			else
			{
				$(this).next().animate({ height: 0 }, 500, function()
				{
					$(this).parent().removeClass('opened').addClass('closed');
					$(this).css('overflow', 'hidden');
				});
			}
		});
	});
	
	
	
	$('.faqblok .faqvragen').hide();
	
	/** voor de content die rechts moet komen te staan **/
	$('.project_sector').each(function(event)
	{
		$(this).children('.categorie_header').click(function()
		{
			$('.categorie_content').hide();
			$('.'+$(this).parent().attr('id')).show();
			
			$('.opened').each(function()
			{
				$(this).removeClass('opened').addClass('closed');

				$(this).find('img').attr('src', '/images/categoriepijl.png');	
				
				$(this).find('span.active').hide();
				$(this).find('span.normal').show();
				
				// Onderstaande stukje om de sifr te laden voor de niet-active tekst van items die direct opengeklapt waren door submenu
				if($(this).hasClass('open-nu'))
				{
					//loadnormalcategorie();
					$(this).removeClass('open-nu');
				}
			});
			
			$(this).parent().removeClass('closed').addClass('opened');
			
			/*if($(this).parent().hasClass('closed'))
			{
				$('.opened').each(function()
				{
					$(this).removeClass('opened').addClass('closed');
					$(this).find('img').attr('src', '/images/categoriepijl.png');
					$(this).find('span.active').hide();
					$(this).find('span.normal').show();
				});
				
				$('#content_right').html($(this).next().html());
				$(this).parent().removeClass('closed').addClass('opened');
				$(this).css('overflow', 'visible');
			}*/
			//sifrbekijkprojecten();
			return false;
		});
	});
	
	$('.open-nu').children('.categorie_header').trigger('click');	
	
	var p = 0;
	var pijl = '';
	var mouseOverProject = false;
	var mouseOverPopup = false;
	mouseOut = function()
	{
		setTimeout(function()
		{
			if(!mouseOverProject && !mouseOverPopup)
			{
				$('.popup2').fadeOut();
			}
		}, 25);
	}

	var html = '';
	var currentProjectElement = new Object();
	
	$('#contentvoorwiesector .project').mouseover(function(event)
	{
		currentProjectElement = this;
		p = $(this).position();
		
		$('.popup2_pijl').hide();
		$('.popup2_pijl2').hide();
		
		html = '<h3>'+$(this).children('.projectcontent').children('.projecttitel').html()+'</h3>'+$(this).children('.projectcontent').children('.projecttekst').html();

		$('.popup2 .popup2_middle').html(html);
		
		$('.popup2').fadeIn().appendTo($(this).parent());
		
		if(p.left > 309)
		{
			$(this).parent().children('.popup2').css('left', p.left-320);
			pijl = '.popup2_pijl2';
		}
		else
		{	
			$(this).parent().children('.popup2').css('left', p.left+132);
			pijl = '.popup2_pijl';
		}
		if(p.top > 0)
		{			
			$(this).parent().children('.popup2').css({ top: p.top });
			$(this).parent().children('.popup2').children(pijl).css({ top: 'auto', bottom: '20px' }).show();
		}
		else
		{
			$(this).parent().children('.popup2').css({top: p.top-3, bottom: 'auto' });
			$(this).parent().children('.popup2').children(pijl).css({ top: '20px', bottom: 'auto' }).show();
		}
		
		//loadsifr();
		
		mouseOverProject = true;
		
		src = $(this).children('img').attr('src').replace('thumb', 'slideshow');
		
		if(src.indexOf('no_image_available') < 1)
		{
			var loadImage = $('<img src="' + src + '" alt="">');
			loadImage.load(function()
			{
				$('#banners').empty().append(loadImage).append('<h1 class="bannertitel">'+$(currentProjectElement).children('.projectcontent').children('.projecttitel').html()+'</h1>');
				sifrbanner();
			});
			$('#imagecontainer').append(loadImage);	
		}	
	}).mouseout(function()
	{
		//$(this).parent().children('.popup2').hide();
		mouseOverProject = false;
		mouseOut();
	});
	
	$('.project').mouseover(function(event)
	{
		currentProjectElement = this;
		p = $(this).position();
		
		$('.popup2_pijl').hide();
		$('.popup2_pijl2').hide();
		
		html = '<h3>'+$(this).children('.projectcontent').children('.projecttitel').html()+'</h3>'+$(this).children('.projectcontent').children('.projecttekst').html();

		$('.popup2 .popup2_middle').html(html);
		
		$('.popup2').fadeIn().appendTo($(this).parent());
		
		if(p.left > 309)
		{
			$(this).parent().children('.popup2').css('left', p.left-320);
			pijl = '.popup2_pijl2';
		}
		else
		{	
			$(this).parent().children('.popup2').css('left', p.left+132);
			pijl = '.popup2_pijl';
		}
		if(p.top > 0)
		{			
			rijen = $(this).parent().height()/155;
			rijnummer = rijen-(p.top/155)-1;
			
			$(this).parent().children('.popup2').css({ bottom: rijnummer*155+8, top: 'auto' });
			$(this).parent().children('.popup2').children(pijl).css({ top: 'auto', bottom: '20px' }).show();
		}
		else
		{
			$(this).parent().children('.popup2').css({top: p.top-3, bottom: 'auto' });
			$(this).parent().children('.popup2').children(pijl).css({ top: '20px', bottom: 'auto' }).show();
		}
		
		//loadsifr();
		
		mouseOverProject = true;
		
		src = $(this).children('img').attr('src').replace('thumb', 'slideshow');
		
		if(src.indexOf('no_image_available') < 1)
		{
			var loadImage = $('<img src="' + src + '" alt="">');
			loadImage.load(function()
			{
				$('#banners').empty().append(loadImage).append('<h1 class="bannertitel">'+$(currentProjectElement).children('.projectcontent').children('.projecttitel').html()+'</h1>');
				sifrbanner();
			});
			$('#imagecontainer').append(loadImage);	
		}		
	}).mouseout(function()
	{
		//$(this).parent().children('.popup2').hide();
		mouseOverProject = false;
		mouseOut();
	});
	
	$('.popup2').mouseover(function()
	{
		mouseOverPopup = true;
	}).mouseout(function()
	{
		mouseOverPopup = false;
		mouseOut();
	});
	
	jQuery.timeago.settings.strings = {
	   suffixAgo: "geleden",
	   suffixFromNow: "vanaf nu",
	   seconds: "iets minder dan een minuut",
	   minute: "ongeveer een minuut",
	   minutes: "%d minuten",
	   hour: "ongeveer een uur",
	   hours: "ongeveer %d uren",
	   day: "een dag",
	   days: "%d dagen",
	   month: "ongeveer een maand",
	   months: "%d maanden",
	   year: "ongeveer een jaar",
	   years: "%d jaar"
	};
	
	// Comments weghalen om cookies te legen
	//$.cookie('lastpost', '');
	//$.cookie('lasttweet', '');
	
	var count = 0;
	
	setInterval(function()
	{
		if(count == 0)
		{
			$.getJSON('http://search.twitter.com/search.json?callback=?&rpp=1&q=from:biblionettweets',function(data) {
				$.each(data.results, function(i, tweet) {
					if(tweet.text !== undefined) {
						if($.cookie('lasttweet') != '')
						{
							//alert(tweet.created_at+'  + '+$.cookie('lasttweet'));
							if(tweet.created_at != $.cookie('lasttweet'))
							{
								$('#twitter .popup1_middle p').text(tweet.text);
								$('#twitter a.timeago').attr('title', tweet.created_at).text(tweet.created_at);
								
								$("#twitter a.timeago").timeago();
								$('.popup1').fadeOut();
								$('#twitter').fadeIn();
							}
						}
						else
						{
							$('#twitter .popup1_middle p').text(tweet.text);
								$('#twitter a.timeago').attr('title', tweet.created_at).text(tweet.created_at);
								
							$("#twitter a.timeago").timeago();
							$('.popup1').fadeOut();
							$('#twitter').fadeIn();
						}
						
						$.cookie('lasttweet', tweet.created_at);
					}
				});
			});
			count = 1;
		}
		else if(count == 1)
		{
			url = 'https://graph.facebook.com/120970037985564/feed?callback=?&limit=1';
		
			$.getJSON(url, function(response)
			{
				$.each(response.data, function(index, post)
				{
					if($.cookie('lastpost') != '')
					{
						if(post['created_time'] != $.cookie('lastpost'))
						{
							$('#facebook .popup1_middle p').text(post['from']['name']+': '+post['message']);
							$('#facebook a.timeago').attr('title', post['created_time']).text(post['created_time']);
							
							$("#facebook a.timeago").timeago();
							$('.popup1').fadeOut();
							$('#facebook').fadeIn();
						}
					}
					else
					{
						$('#facebook .popup1_middle p').text(post['from']['name']+': '+post['message']);	
						$('#facebook a.timeago').attr('title', post['created_time']).text(post['created_time']);
							
						$("#facebook a.timeago").timeago();
						$('.popup1').fadeOut();
						$('#facebook').fadeIn();
					}
					
					$.cookie('lastpost', post['created_time']);
				});
			});
			count = 0;
		}
		
	}, 15000);
	
	setInterval(function()
	{
		$('.popup1').fadeOut();
	}, 12000);
	
	$("a#inline").fancybox({
		'hideOnContentClick': false,
		'autoDimensions': false,
		'width': 490,
		'padding': 30,
		'overlayColor': '#fff'
	});
	
	$('.popup1_close').click(function(){
		$('.popup1').fadeOut();	
	});
	
	/** Menu **/
	/*var menu1 = 0;
	var menu2 = 0;
	
	$('#hoofdmenu > li').mouseover(function(event){
		$(this).children('ul.submenu').css({ top: '24px' })//.show();
		menusifr();
		menu1 = 1;
	});
	$('.submenu').mouseover(function(){
		menu2 = 1;
	});
	$('.submenu').mouseout(function(){
		menu2 = 0;
		if(menu1 == 0 && menu2 == 0)
		{
			$(this).children('ul.submenu').hide();
		}
	});
	$('#hoofdmenu > li').mouseout(function(){
		menu1 = 0;
		setTimeout(function(){
			if(menu1 == 0 && menu2 == 0)
			{
				$('#hoofdmenu > li').children('ul.submenu').hide();
			}
		}, 300);
	});*/
	
	/*$('#hoofdmenu > li').each(function(){
		$(this).mouseenter(function(event){
			
			$(this).children('ul.submenu').css({ top: '20px' }).each(function()
			{
				$(this).children('li').css({ width: $(this).width() - 30 });
			});
			menusifr();
			
		}).mouseleave(function(){
			
			$(this).children('ul.submenu').css({ top: '-2000px' });;

		});	
	});	*/
	
	$('#hoofdmenu > li').each(function(){
		
		$(this).mouseenter(function(event){

			$(this).children('ul.submenu').css({ top: '20px' }).each(function()
			{
				$(this).children('li').css({ width: $(this).width() - 30 });
			});
			//menusifr();
			
		}).mouseleave(function(){
			
			$(this).children('ul.submenu').css({ top: '-2000px' });;

		});	
	});	
	
	$('ul.submenu li').each(function(){
		$(this).mouseenter(function(event){
			
			$(this).children('img').attr('src', $(this).children('img').attr('src').replace('pijl', 'pijl_active'));	
			
		}).mouseleave(function(){
			
			$(this).children('img').attr('src', $(this).children('img').attr('src').replace('pijl_active', 'pijl'));	

		});		
	});
	
	/** menu end **/
	
	/** FAQ blokken **/
	$('.faqbloktitel').each(function(){
		
		$(this).click(function(){
			
			if($(this).hasClass('titelopen'))
			{
				// opened
				$(this).next().hide();
				$(this).removeClass('titelopen').addClass('titelclosed');	
				$(this).children('img').attr('src', '/images/pijl_groot2.png');	
			}
			else
			{
				$('.titelopen').each(function()
				{
					$(this).removeClass('titelopen').addClass('titelclosed');
					$(this).next().hide();
					$(this).children('img').attr('src', '/images/pijl_groot2.png');
				});
				
				// closed	
				$(this).next().show();	
				$(this).addClass('titelopen').removeClass('titelclosed');
				$(this).children('img').attr('src', '/images/pijl_groot.png');	
			}
		
		});
		
	});
	
	/** FAQ blokken end **/
	
	/** Contact pagina **/
	
	$('#contactform').validate({
		rules: 
		{
			organisatie: 'required',	
			aanhef: 'required',	
			voornaam: 'required',	
			achternaam: 'required',
			telefoon: 'required',	
			email: {
				required: true,
				email: true
			},
			opmerking: 'required'
			
		},
		messages:
		{
			organisatie: '',	
			aanhef: 'Dit veld is verplicht',	
			voornaam: '',	
			achternaam: '',
			telefoon: '',	
			email: '',	
			opmerking: ''
		}
	});
	
	$('.sendcontact').click(function(event)
	{
		event.preventDefault();	
		
		if($('#contactform').valid())
		{
			$('#contactform').submit();	
		}
		else
		{
			//$('#contacterror').html('Niet alle velden zijn volledig ingevuld.');	
		}
	});
	
	$('#aanvraagform').validate({
		rules: 
		{
			organisatie: 'required',	
			aanhef: 'required',	
			voornaam: 'required',	
			achternaam: 'required',
			telefoon: 'required',	
			straat: 'required',
			huisnummer: 'required',	
			postcode: 'required',
			plaats: 'required',	
			email: {
				required: true,
				email: true
			}
			
		},
		messages:
		{
			organisatie: '',	
			aanhef: 'Dit veld is verplicht',	
			voornaam: '',	
			achternaam: '',
			telefoon: '',	
			straat: '',
			huisnummer: '',	
			postcode: '',
			plaats: '',	
			email: ''
		}
	});
	
	$('.sendaanvraag').click(function(event)
	{
		event.preventDefault();	
		
		if($('#aanvraagform').valid())
		{
			$('#aanvraagform').submit();	
		}
	});
	
	/** Contact pagina end **/

});
