var timer = '';

$(document).ready(function() {
	
	if ($(".previewColumn").length < 1) {
		// Accessibility: Zorg ervoor dat cufon niet wordt gerenderd als css uitstaat
		if ($("#cufonload").css("display") == "none" && $(".previewColumn").length != 1) { 
			$(".ndtrcContainer h1").addClass("nocufon");
			$(".houseContainer h1").addClass("nocufon");
			$(".uitAgendaSearch h3").addClass("nocufon");
			Cufon.replace('.topNav, h1:not(.nocufon), h2:not(.nocufon), #left h3:not(.nocufon), .uitAgendaItem h3, #right h3, .h3 strong, .h2 strong');
			Cufon.replace('#carrouselContainer .cufon', { textShadow: '2px 1px rgba(0,0,0,0.8)' });
		} else { 
			// Thickbox uitschakelen als CSS uitstaat
			$('.thickbox').each(function(i) {
	            $(this).unbind('click');
	        });
		}
		$(".description").css("overflow", "hidden"); 
	}
	
	if ($(window).width() < 1400) {
		$("#carrouselGradient").addClass("small");
	} else if ($(window).width() >= 1400) {
		$("#carrouselGradient").removeClass("small");
	}
	
	$(window).resize(function() {
		if ($(window).width() < 1400) {
			$("#carrouselGradient").addClass("small");
		} else if ($(window).width() >= 1400) {
			$("#carrouselGradient").removeClass("small");
		}
	});
	
	if ($("#carrouselContainer ul li").size() > 1){
		randomElements = jQuery("#carrouselContainer ul li:first").get().sort(function(){ 
		return Math.round(Math.random())-0.5
		}).slice(0,1);
			
		var item = jQuery(randomElements).attr('id');
		showSlogan(item); 
		
		timer = setTimeout( "loopSlogans()", 6000 );
	  
		$("#homeCarrouselNav li a").each(function() { 
			$(this).bind("click", function(){ 
				
				clearTimeout(timer);
				var className = $(this).attr("class"); 
				showSlogan(className.replace(' active', ''));
				timer = setTimeout( "loopSlogans()", 10000 );	
				return false;
			
			});
		});
	};
	
	$(".newWindow").click(function(){ window.open(this.href); return false; });
	
});

function loopSlogans() {
	clearTimeout(timer);
	var found = false;
	var changed = false;
	var current = '';
	var next = '';
	jQuery("#carrouselContainer ul li").each(function() {		
		if(!changed){
			if(found){				
				changed = true;
				next = this;
			}
			if ( jQuery(this).hasClass('active') ) {
				found = true;
				current = this;
			}
		}
		
	});
	
	if(!changed){
		next = jQuery("#carrouselContainer ul li:first");
	}	
	
	hideSlogan(jQuery(current).attr('id'), function(){
		showSlogan(jQuery(next).attr('id'));
	});
	
	timer = setTimeout( "loopSlogans()", 6000 );	
}


function showSlogan(id)
{	
	jQuery('#carrouselContainer ul li.active').removeClass('active');
	jQuery('#carrouselContainer ul li').css({zIndex:99}).fadeOut(0);
	jQuery('#homeCarrouselNav li a.active').removeClass('active');
	jQuery('#homeCarrouselNav li a.'+id).addClass('active');
	jQuery('#' + id).css({zIndex:90}).fadeIn(1000,function(){
		jQuery('#' + id).removeClass('hidden'); 
		jQuery('#' + id).addClass('active'); 
	});
}

function hideSlogan(id, callback)
{
	var returnCall = callback;
	jQuery('#homeCarrouselNav li a.active').removeClass('active');
	jQuery('#' + id).css({zIndex:90}).fadeOut(1000,function(){
		jQuery('#' + id).addClass('hidden'); 
		jQuery('#' + id).removeClass('active'); 
		returnCall();
	});	
}
