$(function(){
	$(window).trigger('resize')
	Cufon.replace('#header ul li a, #language', {
		hover: true,hoverables:{a:true}
	});
	if($.browser.msie && $.browser.version=="6.0"){
		
	} else {
		Cufon.replace('#sidebar ul li a', {
			hover: true,hoverables:{a:true}
		});
	} 
	$('.youtube').colorbox({
		iframe: true,
		innerWidth:425, 
		innerHeight:344,
		opacity:0.5
	})
	if($('.home').length){
		$('.images img').fullBg();	
		$('.images img:not(:first), .captions .caption:not(:first)').addClass('hide')
		if($('.images img:last').index() > 0){
			$(document).everyTime(5000, function(i) {
				var el = $('.images img:not(.hide)')		
				var nowindex = el.index()
				if(el.next().length){
					var nextindex = el.next().index();
				} else {
					var nextindex = $('.images img:first').index();
				}
				$('.images img:eq('+nowindex+')').css({
					'z-index': 1
				})
				$('.captions .caption:eq('+nowindex+')').fadeOut(500, function(){
					$('.captions .caption:eq('+nextindex+')').fadeIn(500)
				})
				$('.images img:eq('+nextindex+')').css({
					'z-index': 2,
					'opacity': 0
				}).removeClass('hide').animate({
					opacity: 1
				}, 800, function(){
					$('.images img:eq('+nowindex+')').css({
						'opacity': 0
					}).addClass('hide')	
				})
			});			
		}
	}
	
	// remove submenu
	
	if(!$('#sidebar ul li').length){
		$('#sidebar').remove();
	}
	
	// images
	if($('#images .image:last').index() > 0){
		$('#images').addClass('more');
		$('#images .image:not(:first)').addClass('hide')
		var amountImages = $('.image:last').index();

		$('#images').append('<div class="buttons"></div>')
		
		var i = 0;
		for (i=0;i<=amountImages;i++){
			if(i==0){
				$('#images .buttons').append('<span class="button active"></span>')
			} else {
				$('#images .buttons').append('<span class="button"></span>')		
			}
		}
		$('.button').live('click', function(){
			var newImage = $('.image').eq($(this).index())
			$(this).addClass('active').siblings('.button').removeClass('active')
			newImage.removeClass('hide').siblings('.image').addClass('hide')
			$('#images').animate({
				width: newImage.find('img').width()
			}, 200)
		})
		$('#images .image').live('click', function(){
			if($('.button.active').next().length){
				$('.button.active').next().trigger('click')		
			} else {
				$('.button:first').trigger('click')					
			}
		})		
	}
	
	// meer nieuwsberichten
	$('a.more').live('click', function(){
		$(this).parent().addClass($(this).attr('class'))
	})
	$('a.less').click(function(){
		$(this).parent().removeClass('more')
	})
	$('#footer').width($(window).width() - 40)
});

$(window).resize(function(){
	minWidth = parseFloat($('body').css('min-width'));
	if($(window).width() < minWidth){
		var documentWidth = minWidth
	} else {
		var documentWidth = $(window).width();
	}
	if($('#images').length){
		var extraHeight = 70;
		if($('.page').length){
			var availableWidth = Math.floor(documentWidth - $('#sidebar').outerWidth() - $('#page_content').outerWidth() - 80);
			var availableHeight = Math.floor($(window).height() - $('#header').outerHeight() - extraHeight);
		} else {
			var availableWidth = Math.floor(documentWidth - $('#sidebar').outerWidth() - $('#sideright').outerWidth() - 80);
			var availableHeight = Math.floor($(window).height() - $('#header').outerHeight() - extraHeight);
		}
		imageSizePortfolio(availableWidth, availableHeight);		
	}
	$('#footer').width($(window).width() - 40)

})
function imageSizePortfolio(availableWidth, availableHeight){	
	$('#images img').each(function(){
		scalex = availableWidth / $(this).attr('width');
		scaley = availableHeight / $(this).attr('height');
		if(scalex<scaley) scale = scalex; else scale = scaley;	
		newx = scale * $(this).attr('width');
		newy = scale * $(this).attr('height');

		if(newx==0||newy==0) newx = 20;

		$(this).width(newx);	
 		$(this).height(newy);
	})
	$('#images').width($('.image:visible img').width())
	$('body').addClass('initialized')
}
