$(function(){
/*
	$.ajax({
		url: '/site/ajax/youtube.php',
		cache: false,
		success: function(html){
			$('#youtube .loading').remove();
			$('.kop_youtube').after(html)
		},
		error: function(){
			$('.kop_youtube').after('<p>Youtube lijkt momenteel niet beschikbaar.</p>')
		}
	})
	$.ajax({
		url: '/site/ajax/youtube2.php',
		cache: false,
		success: function(html){
			$('#youtube').append(html)
		},
		error: function(){
			$('.kop_youtube').after('<p>Youtube lijkt momenteel niet beschikbaar.</p>')
		}
	})
*/
	$('#tweets-container').jTweetsAnywhere({
		username: $('#twitter').data('twitterusername'),
	    count: $('#twitter').data('ammounttweets'),
	    showFollowButton: false,
	    showInReplyTo: false,
	    tweetFilter: function(tweet, options){
	    	if(!tweet.in_reply_to_screen_name){
	    		return true;
	    	}
	    },
	    tweetDecorator : function(tweet, options){
			// the default tweet is made of the optional user's profile image and the
			// tweet body inside a list item element
			var html = '';
	
			if (options._tweetFeedConfig.showProfileImages)
			{
				html += options.tweetProfileImageDecorator(tweet, options);
			}
	
			if (options.tweetBodyDecorator)
			{
				html += options.tweetBodyDecorator(tweet, options);
			}
	
			html += '<div class="jta-clear">&nbsp;</div>';
	
			return '<li class="tweet">' + html + '<div class="roundedcorner"></div></li>';
		}, 
		loadingDecorator : function(){
			return '<li class="jta-loading">Laden ...</li>';
		},
		tweetTimestampFormatter : function(timeStamp){
			var now = new Date();
	
			var diff = parseInt((now.getTime() - Date.parse(timeStamp)) / 1000);
	
			var tweetTimestamp = '';
			if (diff < 60)
			{
				tweetTimestamp += diff + ' seconde' + (diff == 1 ? '' : 'n') + ' geleden';
			}
			else if (diff < 3600)
			{
				var t = parseInt((diff + 30) / 60);
				tweetTimestamp += t + ' minute' + (t == 1 ? '' : 'n') + ' geleden';
			}
			else if (diff < 86400)
			{
				var t = parseInt((diff + 1800) / 3600);
				tweetTimestamp += t + ' uur geleden';
			}
			else
			{
				var d = new Date(timeStamp);
				var period = 'AM';
	
				var hours = d.getHours();
				if (hours > 12)
				{
					hours -= 12;
					period = 'PM';
				}
	
				var mins = d.getMinutes();
				var minutes = (mins < 10 ? '0' : '') + mins;
	
				var monthName = [ "Januari", "Februari", "Maart", "April", "Mei", "Juni", "Juli", "Augustus", "September", "October", "November", "December" ];
	
				tweetTimestamp += d.getDate() + ' ' + monthName[d.getMonth()];
	
				if (d.getFullYear() < now.getFullYear())
				{
					tweetTimestamp += ', ' + d.getFullYear();
				}
	
				var t = parseInt((diff + 43200) / 86400);
				tweetTimestamp += ' (' + t + ' dag' + (t == 1 ? '' : 'en') + ' geleden)';
			}
	
			return tweetTimestamp;
		},
		tweetRetweeterDecorator : function(tweet, options){
			return '';
		}
	});

	if($('ul#images li:last').index() == 0){
		$('#pager').hide();
	}
	// dropdown
	// if ul has .internal it doesn't leave the page
	$('.dropdown').each(function(){
		var ulWidth = $(this).outerWidth();
		if(ulWidth <= 150){
			ulWidth = 160;
		}
		$(this).width(ulWidth).find('li:last').addClass('last')
	});
	$('.dropdown li a').each(function(){
		var imglink = $(this).find('img').attr('src')
		$(this).css({'background-image' : 'url('+imglink+')'})
	})
	$('.dropdown.internal li a').click(function(){
		$(this).parents('ul').next().text($(this).attr('href')).effect("highlight", {}, 2000);
		$(document).trigger('click');
		return false;
	})
	$(document).click(function(event) {
		var $target = $(event.target);
        if($target.parents('.dropdown').length){
			$('.dropdown').not($target.parents('.dropdown')).removeClass('open')
			$target.parents('.dropdown').toggleClass('open')
		} else if ($('.dropdown.open').length){
			$('.dropdown').removeClass('open')
		}              
    });

	// open video
	$('#video, .close_video').click(function(){
		$('#video_block').slideToggle();
	})
	
	
	$('#images').cycle({
		fx: 'fade',
		next: $('.next'),
		prev: $('.prev'),
		timeout:0,
		speed: 400,
		height: 256
	})
	if($('#images li:last').index() == 0){
		$('#pager').remove();
	}

})

