$(document).ready(function() {
	$('body').addClass('domenabled');
	$(document).pngFix();
	openInNewWindow();
	replaceText();
	
	// Navigation
	$('#navigation li.active a').after('<span class="bg"></span>');
	$('#navigation li.active').find('.bg').css({
		background: '#fff',
		opacity: 0.2
	});
	$('#navigation li:not(.active) a, #subnav ul li:not(.active) a').mouseenter(function(){
		$(this).after('<span class="bg"></span>');
		$(this).parent().find('.bg').css({
			background: '#fff',
			opacity: 0.2
		});
	}).mouseleave(function(){
		$(this).parent().find('.bg').remove();
	});

	// Decoration
	$('#contact, #contact .content').append('<div class="bg"></div>');
	$('#contact .bg, #contact .content .bg').css('opacity', .1);
	$('#subnav ul li:last').css('border','none');
	// Background music
	if($('body').hasClass('home') || $('body').hasClass('fotos')){
		$('#music').after('<a href="#" id="sound"></a>').flash({ 
			src: 'niftyplayer.swf?file=barflytune.mp3&as=1',
			name: 'bgmusic',
			width: 165,
			height: 38,
			loop: true
		});
	}
	// Pause button
	$('#sound').toggle(function() {
		$(this).css('backgroundPosition','left bottom');
		niftyplayer('bgmusic').playToggle();
	}, function() {
		$(this).css('backgroundPosition','left top');
		niftyplayer('bgmusic').playToggle();
	});
	
	// Photos
	photoGallery();
	// Slideshow
	slideshow(6);
});

function photoGallery(){
	if($(".scrollable").length > 0){
		$(".scrollable").scrollable();
		$(".items img").css('opacity', .8);
		$(".items img").mouseenter(function(){
			$(this).fadeTo(100, 1);
		}).mouseleave(function(){
			$(this).fadeTo(100, .8);
		});
		$(".items img").css('cursor','pointer').click(function() {
			if ($(this).hasClass("active")) { return; }
			var url = $(this).attr("src").replace("thumbs/", "");
			var wrap = $("#image_wrap"); //.fadeTo(1000, .2);
			var img = new Image();
			img.onload = function() {
				wrap.fadeTo(500, 1);
				wrap.find("img").attr({
					src: url
				});
			};
			img.src = url;
			$(".items img").removeClass("active");
			$(this).addClass("active");
		}).filter(":first").click();
	}
}

function slideshow(length){
	// Slideshow
	var imgWrapper = $('#slideshow ul li');
	// Show random item
	var showItem = Math.floor(Math.random()*length);
	$('#slideshow ul li:eq('+ showItem +'), #slideshownav li:eq('+ showItem +')').addClass('active');
	// Automatic slide
	autoslide();
}

function autoslide(){
	var fadeInSpeed = 500;
	var fadeOutSpeed = 1500;
	slide = setInterval(function(){
		// Check active item
		var nextItem = $('#slideshownav li.active').next();
		var nextSlideshowItem = $('#slideshow ul li.active').next();
		// Disable active item and select next item
		$('#slideshownav li.active').removeClass('active').next().addClass('active');
		// If lastitem active then select first item
		if(nextItem.length == 0){
			$('#slideshownav li:first').addClass('active');
		}
		if(nextSlideshowItem.length == 0){
			$('#slideshow ul li.active').fadeOut(fadeOutSpeed).removeClass('active');
			$('#slideshow ul li:first').fadeIn(fadeInSpeed).addClass('active');
		}else{
			$('#slideshow ul li.active').fadeOut(fadeOutSpeed).removeClass('active').next().fadeIn(fadeInSpeed).addClass('active');
		}
	}, 5000); // Every 5 seconds
}

function replaceText(){
	Cufon.replace('#navigation li a');
	Cufon.replace('h1, h2, h3');
	Cufon.replace('#contact li');	
	Cufon.now();
}

function openInNewWindow() {
	// external links
	$("a[rel=external]").each(function(){
		$(this).attr('title','Link opent in een nieuw venster');
		$(this).click(function() {
			window.open(this.href);
			return false;
		});
	});
}
