(function($) {
	
	// jQuery fixCheckBoxes 1.1
	$.fn.fixCheckBoxes=function(){return this.each(function(){var a=$(this);var b=$('<input type="hidden" />');b.attr({'id':a.attr('id')+'_hidden','name':a.attr('name')});if(a.attr('checked'))b.val('Ja');else b.val('Nee');a.after(b);a.removeAttr('name');a.click(function(){if(b.val()=='Nee')b.val('Ja');else b.val('Nee')})})};
	
	// jQuery imageHover 1.0
	$.fn.imageHover=function(d){var d=$.extend({className:'overlay',format:'overlay',opacity:1,inSpeed:1000,outSpeed:1000},d);function replaceFormat(a,b){return a.substring(0,a.indexOf('format=')+7)+b};function hide(a){a.hide();a.css('opacity',0)};function fadeIn(a){a.stop();a.show();a.fadeTo(d.inSpeed,d.opacity)};function fadeOut(a){a.stop();a.fadeOut(d.outSpeed,function(){hide(a)})};return this.each(function(){var a=$(this);var b=a.children('img');var c=b.clone();c.attr('src',replaceFormat(c.attr('src'),d.format));c.addClass(d.className);hide(c);a.hover(function(){fadeIn(c)},function(){fadeOut(c)});a.append(c)})};

	// jQuery classHandler 1.0
	$.fn.classHandler=function(a,b){var c={'mouseover':'mouseout','mouseenter':'mouseleave','focus':'blur'};var d=$(this);d.bind(a,function(){$(this).addClass(b)});d.bind(c[a],function(){$(this).removeClass(b)});return this};
	
	// jQuery miniGallery 1.0
$.fn.miniGallery=function(e,f){var f=$.extend({speed:'normal'},f);var g=$(this),e=$(e),images=[];return g.each(function(){var c=$(this);var d=$('<img src="'+c.attr('href')+'" alt="" />').hide().appendTo(e);images.push(d);c.click(function(a){var b=g.index(c),d=images[b];if(d.is(':hidden')){$.each(images,function(){$(this).hide()});d.show()};a.preventDefault()})})};
	
})(jQuery);

function replaceFormat(source, format) {
	return source.substring(0, source.indexOf('format=') + 7) + format;
};

function projectImages() {
	
	var elements = $('#elements .element'),
		target = $('.project-image-large'),
		overlay = $('<div class="overlay" />'),
		images = [];
	
	elements.children('img').each(function() {
		
		var image = $(this).clone();
		
		image.attr('src', replaceFormat(image.attr('src'), 'projectimage'))
			.hide()
			.appendTo(target);
			
		images.push(image);

	});
	
	elements.click(function(event) {

		var currentIndex = elements.index(this),
			image = images[currentIndex];

		if(image.is(':hidden')) {
			$.each(images, function() {
				$(this).hide();
			});
			image.show();
		};
		
		event.preventDefault();

	});

};

function showFlowPlayer(selector) {
	var movie = $('#news-message .movie');
	movie.children('a').attr('href') == '/' ? movie.remove() : movie.show();		
};

$(window).load(function() {

	$('.movie').each(function(i) {
							  
		// Set up variables
		var fpContainer = $(this),
			fpMovie = fpContainer.children('.movie-player');
			
		if(fpMovie.attr('href') == '/') {
			fpContainer.remove();
			return false;
		};
		
		var fpMovieId = fpMovie.attr('class') + '-' + i,
			fpControls = fpContainer.children('.movie-controls'),
			fpControlsId = fpControls.attr('class') + '-' + i;
			
		// Assign attributes
		fpMovie.attr('id', fpMovieId);
		fpControls.attr('id', fpControlsId);
		
		// Set up flowplayer
		flowplayer(fpMovieId, {
			src : 'style/peppermint/flowplayer/flash/flowplayer.swf',
			wmode : 'opaque'
		}, {
			canvas : {
				backgroundColor : '#ffffff'
			},
			clip: {
				autoPlay: false,
				autoBuffering: true
			},
			plugins: {
				controls : null
			}
		}).controls(fpControlsId);

		// Set up initial state
		var initStyle = {
			opacity : 0,
			bottom : 10
		};
		fpControls.css(initStyle);
			
		// Set up events
		fpContainer.hover(function() {
			fpControls.stop().animate({
				opacity : 1,
				bottom: 20
			}, 250);
		}, function() {
			fpControls.stop().animate(initStyle, 500);
		});
		
		if(fpContainer.is(':hidden'))
			fpContainter.show();

	});
		
});

$(function() {
	
	// Portfolio thumbnails hover
	$('#portfolio .thumbnail').imageHover({
		format : 'portfolio_hover',
		inSpeed : 600,
		outSpeed : 300
	});

	// Assign classHandler to form fields
	$('.form input[type=text], .form textarea').classHandler('focus', 'focus');
	
	// Set up miniGallery
	$('#item-images a').miniGallery('#news-message .thumbnail');
	
});