$(window).resize(function(){
	if (($('.wrapper').height()+80)>$(window).height()) $('.wrapper').addClass('maxed');
	else $('.wrapper').removeClass('maxed');
}).resize();
$('.thought').find('.controls a').click(function(){
	if ($(this).hasClass('close')) {
		$(this).closest('.thought').addClass('closed').animate({top: -260});
		$(this).hide();
		$(this).parent().parent().find('a.show').show();
	}
	else {
		$(this).closest('.thought').removeClass('closed').animate({top: 20, right: 20});
		$(this).hide();
		$(this).parent().parent().find('a.close').show();	
	}
	return false;
	
});
$('ul.media').addClass('go-go-go-go');
$('.media a').click(function(){
	var a = $(this);
	
	
	if (!$('#fader').size()) $('<div id="fader"/>').appendTo('body');
	if (!$('#popup').size()) $('<div id="popup"/>').appendTo('body');
	
	
	$('#fader').fadeTo(300,0.9);
	$.ajax({
		url: a.attr('href'),
		type: 'get',
		success: function(data){
			
			$('#popup').html(data);
			if (a.parent().next().size()) $('<a class="next" href="#_next">volgende foto</a>').click(function(){
				a.parent().next().find('> a').click();
				return false;
			}).appendTo('#popup');
			if (a.parent().prev().size()) $('<a class="prev" href="#_prev">vorige foto</a>').click(function(){
				a.parent().prev().find('> a').click();
				return false;
			}).appendTo('#popup');
			$('#popup').css({
				marginLeft: -($('#popup').width())/2,
				marginTop: -($('#popup').height())/2
			});
			$('#popup').fadeTo(200,1);
		}
	});
	return false;
});



$('#fader,  #popup .close a, a.close').live('click',function(e){
	var origin = $(e.target);
	
	$('#popup, #fader').fadeOut(200,function(){
		$('#popup, #fader').remove();
	})
	return false;
});
$('a.soundcloud').live('click',function(){
	var cloud = '<a href="#_close" class="close">sluiten</a><div class="inner"><object height="225" width="100%"> <param name="movie" value="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Fplaylists%2F899584"></param> <param name="allowscriptaccess" value="always"></param> <embed allowscriptaccess="always" height="225" src="http://player.soundcloud.com/player.swf?url=http%3A%2F%2Fapi.soundcloud.com%2Fplaylists%2F899584" type="application/x-shockwave-flash" width="100%"></embed> </object>  <span><a href="http://soundcloud.com/wereldband/sets/mr-kopm-n-1">Mr. Kopm�n</a> by <a href="http://soundcloud.com/wereldband">Wereldband</a></span></div>';
	$('#popup, #fader').remove();
	$('<div id="fader"/>').appendTo('body');
	$('<div id="popup"/>').appendTo('body');
	$('#popup').html(cloud);
	$('#popup').css({
		marginLeft: -($('#popup').width())/2,
		marginTop: -($('#popup').height())/2
	});
	$('#fader').fadeTo(300,0.9);
	$('#popup').fadeTo(200,1);
	return false;

});

$('.content-container a[href*="youtube.com"], .content-container a[href*="youtu.be"], .content-container a[href*="vimeo.com"]').each(function(){
	var a = this;
	// youtube 
	var id = a.href.match(/\?v=([^&]+)?/);
	if (!id) id =a.href.match(/youtu\.be\/(.+)/);
	if (id && id[1]) {
		var str = '<iframe width="560" height="349" src="http://www.youtube.com/embed/'+id[1]+'" frameborder="0" allowfullscreen></iframe>';
		var descr = $(this).html();
		$(this).html('<img src="http://i4.ytimg.com/vi/'+id[1]+'/default.jpg"/><span>'+descr+'</span>');
	}
	if (!id) { 
		id =a.href.match(/vimeo.com\/(.+)/);
		if (id && id[1]) {
			var str = '<iframe src="http://player.vimeo.com/video/19024160?title=0&amp;byline=0&amp;portrait=0" width="400" height="215" frameborder="0"></iframe><p>';
			var descr = $(this).html();
			$.ajax({
	            url: 'http://vimeo.com/api/v2/video/' + id[1] + '.json',
	            dataType: 'jsonp',
	            success: function(data) {
					$(a).html('<img src="'+data[0].thumbnail_medium+'"/><span>'+descr+'</span>');
				
	            }
	        });

		
			
		}
		
	}
	
	$(this).click(function(){
		$('#popup, #fader').remove();
		$('<div id="fader"/>').appendTo('body');
		$('<div id="popup"/>').appendTo('body');
		$('#popup').html('<a href="#_close" class="close">sluiten</a>'+str);
		$('#popup').css({
			marginLeft: -($('#popup').width())/2,
			marginTop: -($('#popup').height())/2
		});
		$('#fader').fadeTo(300,0.9);
		$('#popup').fadeTo(200,1);
		return false;
	});

	
});
var ratio;

//$(window).scroll(function(){
//	if (ratio>0) $('#bg').css({top: -$(window).scrollTop()*ratio});
//})

$(window).resize(function(){
	ratio = ($('#bg img').height()-$(window).height())/(document.documentElement.scrollHeight - document.documentElement.clientHeight);
	
}).resize();
var socialHover = false;
$('div.social').hover(function(){
	socialHover = true;
},function(){
	socialHover = false;;
});
var socialInterval = setInterval(function(){
	if (socialHover) return;
	var current = $('ul.twitter li:visible');
	$('ul.twitter').animate({marginLeft: -current.width()},300,'linear',function(){
		var ul = $(this);
		var next = current.next();
		if (!next.length) next = $('li:first', this); 
		current.hide();
		next.show();
		ul.css({marginLeft: -next.width()}).animate({marginLeft: 0},1000);
	});
},10000);

$('form#comment').live('submit',function(){
	var form = $(this);
	$('.error',form).removeClass('error');
	$('.error-message', form).remove();
	$('.required', form).each(function(){
		if ($(this).val()=='') {
			
			$(this).closest('label').addClass('error');
			//.append('<p class="error-message">Dit veld is verplicht!</p>');
		}
	});
	if ($('.error',this).length) {
		$('<p class="error-message">Niet alle velden zijn ingevuld!</p>').prependTo(form);
	}
	else {
		$.ajax({
			url: form.attr('action'),
			type: 'post',
			data: form.serialize(),
			success: function(data){
				if (data.match(/<title>Fout<\/title>/gi)) {
					var str = '';
					$(data).find('li').each(function(){
						str+=$(this).html()+'<br/>';
					});
					$('<p class="error-message">'+str+'</p>').prependTo(form);
				}
				else {
					$(window).scrollTop(0);
					$.ajax({
						url: '/gastenboek',
						success: function(data){
							form.replaceWith($(data).find('form#comment'));
						}
					});
					$('p.do-it-too').before('<p class="ok-message">Je bericht is verstuurd!<br/>We moeten het enkel nog even goedkeuren.</p>');
				}
			}
		});
	}
	return false;
});

$('form#shop').live('submit',function(){
	var form = $(this);
	$('.error',form).removeClass('error');
	$('.error-message', form).remove();
	$('.required', form).each(function(){
		if ($(this).val()=='') {
			
			$(this).closest('label').addClass('error');
			//.append('<p class="error-message">Dit veld is verplicht!</p>');
		}
	});
	if ($('.error',this).length) {
		$('<p class="error-message">Niet alle velden zijn ingevuld!</p>').prependTo(form);
		return false;
	}
	else {
		return true;
	}

});
