var showtime;
var i =1;

$(document).ready(function() {
	$('.homeblock:gt(0)').hide();
	$('#topbar_wrapper').animate({ backgroundColor: "white" }, 1500);
	if($('.donthidethebar').length == 0){
		$('#topbar_wrapper:visible').toggle("blind", {}, 1800 );
	}
	
	$("a[rel=fancy]").fancybox({
		'type'			: 'image',
		'zoomSpeedIn'	: 300,
		'zoomSpeedOut'	: 300,
		'overlayShow'	: false
	});
	
	if ($('#home_left_block').length > 0) {
		startSequence();
		
	}
	
	$("#footer #carousel").carouFredSel({
		width           : 870,
		height			: 70,
		prev			: $('.back'),
		next			: $('.forward')
	}); 
	//$('#footer').infiniteCarousel();
	
	$('.likelink').click(function(){
		var link = $(this).attr('href');
		link = link.replace('/like','/like/ajax');
		$.ajax({
			type: "GET",
			url: link,
			success: function(msg){
				if(msg == 'true'){
					likes = parseInt($('.likes').html());
					$('.likes').html(likes+1);
				}
				else {
					alert('Je vindt deze pagina al leuk');	
				}
			}
		});
		return false;
	});
	
	$('.vriendvan').click(function(){
		if($('#vriendinfo').length == 0 || $('#vriendinfo:visible').length == 0){
			$('#vriendinfo').remove();
			$('#wrapper').parent().append('<div id="vriendinfo">Een vriend ontvangt:<br />- Tweemaal per jaar het Eendracht Magazine.<br />- Vriendenpas voor deelname aan evenementen, dagtochten en reizen speciaal voor \'Vrienden van de Eendracht\'.<br /><br /><a href="/zeilschip-eendracht/in-zee-met-de-eendracht/vrienden-van-de-eendracht" target="_blank">Klik hier voor meer informatie</a></div>');
			$('#vriendinfo').show();	
		}
		var pos = $(this).position();
		$('#vriendinfo').css({'left':(pos.left-220)+'px','top':pos.top+'px'});
		return false;
	});
	$(document).click(function(){
		$('#vriendinfo').remove();
	});
	
	// Blog pagina
	$('.blogitem_header').click(function(){
		/*if($(this).parent().find('.blogitem_content:hidden').length > 0){
			$('.blogitem_content:visible').slideUp();
			$(this).parent().find('.blogitem_content:hidden').slideDown('normal',function(){
																						  var pos = $(this).parent().position();
																						  $('html, body').animate({scrollTop:(pos.top-20)}, 'slow');
																					});
			//$(this).parent().find('.blogitem_content:visible').slideUp();
		}*/
		$(this).parent().find('.blogitem_content').slideToggle();
		return false;
	});
	
	$('.reis.archive .reisinfo').click(function(){
		if($(this).find('a.hidden').length > 0){
			var link = $(this).find('a.hidden').attr('href');
			window.location = link;
		}
		return false;
	});
	
});

function toggleTopBar() {
	$('#topbar_wrapper').toggle("blind", {}, 1000);
}

function showHome(block, elem) {
	$('.homeblock:visible').hide();
	$('#'+block).show();
	$('#home_right_block a').removeClass('active');
	$(elem).addClass('active');
	clearInterval(showtime);
}

function toggleNextDiv(elem) {
	$(elem).next('div').toggle('blind');
}

function startSequence() {
	jQuery.fx.off = false;
	var fade_speed	= 0;
	var read_time	= 5000;
	var interval = (fade_speed*3)+read_time;
	showtime = setInterval("showNext("+fade_speed+")", interval);
}

function showNext(fade_speed) {
	var items			= $('.homeblock');
	var newslist		= $('#home_right_block').find('a');
	var num_items		= items.length;
	var num_newslist	= newslist.length;
	if (i == num_items) {
		i = 0;
	}	
	var x = i == 0 ? num_items-1 : i-1;
	
	$(items).hide();
	$(items[i]).show();	

	$(newslist).removeClass('active');
	$(newslist[i]).addClass('active');
	
	i++;
}

function getTravelData(id, type) {
	$.ajax({
		url: '/extra/tijdlijn.php?id='+id+'&type='+type,
		cache: false,
		success: function(text){
			$("#tijdlijndiv").html(text);
		}
	});
}

function changeBedrag(val) {
	if (val == 'Ik wil zelf een bedrag bepalen') {
		$('.anderbedrag').show();
	}
	else {
		$('.anderbedrag').hide();
	}
}

function changeTypeReis(val) {
	if (val == 'groep') {
		$('.groepscode').show();
		$('.nongroep').hide();
	}
	else {
		$('.groepscode').hide();
		$('.nongroep').show();
	}
}

function changeKorting(val) {
	if (val == 'lid') {
		$('.kortingcode').hide();
		$('.kortinglid').show();
	}
	else if (val == 'code') {
		$('.kortingcode').show();
		$('.kortinglid').hide();
	}
	else {
		$('.kortingcode').hide();
		$('.kortinglid').hide();
	}
}
