function endpage()
{
	$("div.container:empty").hide();
}
$(document).ready(function(){
	
	//menu hovers
	$("ul#top li").hover(function(){
		$(this).addClass("hover");
	}, function(){
		$(this).removeClass("hover");
	});
	
	//init search actions
	InitSearchActions();
	
	$("#content .home .header,#content .home .item,ul.searchtable li.item,.addhover").hover(function(){
		$(this).addClass("hover");
	},function(){
		$(this).removeClass("hover");
	});


	$(".related h4[class!=noimg]").contents().each(function(){
		CreateTitle(this, 'h4rc',true);
	});
	
	$(".related h3").contents().each(function(){
		if($(this).hasClass("h3green"))
			CreateTitle(this, 'h3greenrc');
		else if($(this).hasClass("noimg"))
			return;
		else
			CreateTitle(this, 'h3rc');
	});
	
	$("h2").contents().each(function(){
		CreateTitle(this, 'h2');
	});
	
	$("h1").contents().each(function(){
		if($(this).parent().hasClass("noimg"))
			return;
		CreateTitle(this, 'h1');
	});
	
	$("#nav").masonry({singleMode: true,columnWidth: 153 });
});

function CreateTitle(o,t,uppercase)
{
		if(this.location.href.match(/cms.ipsum/))
			return;

		if(String(o.tagName).toLowerCase()=='a')
		{	
			var text = $(o).text();
			$(o).html('<img src=\"/titles/'+t+'/'+encodeURI((uppercase?text.toUpperCase():text))+'\" />');
		}else
		if(o.tagName && String(o.tagName).toLowerCase()!='img')
		{
			var ct=$(o).contents();
			ct.each(function() {
				CreateTitle(this,t);
			});
		}
		else
		{
			var text = o.nodeValue;
			$(o).replaceWith('<img src=\"/titles/'+t+'/'+encodeURI((uppercase?text.toUpperCase():text))+'\" />');
		}
}

function InitSearchActions(){

	$("#searchbox a:first").click(function(){
		search($("#searchbox input[name=searchtext]"),'/zoekresultaten');
	});

	//searchtext action
	/*
	$("input[name=searchtext]").keydown(function(event){
		if (event.keyCode==13)
		{ 
			search($(this).val(),'/zoekresultaten');
		}
	});
	*/
	//searchtext focus action
	$("input[name=searchtext]").focus(function(){
		if($(this).val()== $(this).attr("defaulttext"))
			$(this).val("");
	});
	
	//searchtext blur action
	$("input[name=searchtext]").blur(function(){
		if($(this).val()=="")
			$(this).val($(this).attr("defaulttext"));
	});
}

//InitSlideshow
function InitSlideshow(){
	slideshowlength = $(".largeitems:eq(0) .item").length;
	//SlideShowMove();
}

var slideshowindex = 0;
var slideshowlength = 0;
var slideshowtimer;

//SlideShowMove
function SlideShowMove(index)
{
	if(index>=0)
	{
		slideshowindex = index;
		window.clearTimeout(slideshowtimer);
	}

	var target = $(".slideshowtarget");
	
	var largeimg = $(".largeitems:eq(0) .item:eq("+slideshowindex+")")[0];
	var newtarget = $(".image img.thumbnail",largeimg);
	if($(newtarget).attr("largeimg")!='')
	{
		target.html($(largeimg).html());
		$(".image img.thumbnail",target).attr("src",$(newtarget).attr("largeimg") );
	}
	slideshowindex++;
	
	if(slideshowindex>=slideshowlength)
		slideshowindex = 0;
	
	slideshowtimer = window.setTimeout('SlideShowMove()', (index>=0?4000:2500));
}

//search action
function search(s,url)
{
	var searchtext = $(s).val();
	if(searchtext == $(s).attr("defaulttext"))
		return;
	window.location = url + (url.indexOf('page_id')>0 ? '&path=':'/')+'zoeken/' + encodeURI(searchtext);
}

function vote(e)
{
	$.post(e, '', function(data){
		window.location.href=window.location.href;
	})
}

function selectpollanswer(vara,varb){

	$("#pollvote .selected").removeClass("selected");
	$(varb).parent().addClass("selected");
	selectanswer(vara,varb);
}

function FotoZoom(img)
{
	$("#lightbox .lightboxContent").html(img);
	//$("#fotozoom").show();
	window.scrollTo(0,0);
	loadPopup($("#lightbox"));
	
}

function VideoZoom2112011(video)
{
	var videoid=null;
	String(video).replace(/v=([^&]+)/i,function(match,vidid) {videoid=vidid});
	if (videoid)
	{
		html='<object width="788" height="400">'+
	  '<param name="movie" value="http://www.youtube.com/v/'+videoid+'" />'+
	  '<param name="wmode" value="transparent" />'+
	  '<embed src="http://www.youtube.com/v/'+videoid+'" '+
			 ' type="application/x-shockwave-flash" '+
			 ' wmode="transparent" width="788" height="400" />'+
			' </object>';

		$("#lightbox .lightboxContent").html("");
		//$("#fotozoom").show();
		window.scrollTo(0,0);
		loadPopup($("#lightbox"),function()
		{
			$("#lightbox .lightboxContent").html(html);
		});
	}
	
}

function VideoZoom(video)
{
	var videoid=null;
	if(video.match(/vimeo/))
	{
		String(video).replace(/\/([0-9]+)/i,function(match,vidid) {videoid=vidid});
		
		html = '<iframe src="http://player.vimeo.com/video/'+videoid+'?title=0&amp;byline=0&amp;portrait=0" width="788" height="400" frameborder="0" webkitAllowFullScreen allowFullScreen></iframe>';
		
		$("#lightbox .lightboxContent").html("");
			//$("#fotozoom").show();
			window.scrollTo(0,0);
			loadPopup($("#lightbox"),function()
			{
				$("#lightbox .lightboxContent").html(html);
			});
		
	}else{
		String(video).replace(/v=([^&]+)/i,function(match,vidid) {videoid=vidid});
		if (videoid)
		{
			html='<object width="788" height="400">'+
		  '<param name="movie" value="http://www.youtube.com/v/'+videoid+'" />'+
		  '<param name="wmode" value="transparent" />'+
		  '<embed src="http://www.youtube.com/v/'+videoid+'" '+
				 ' type="application/x-shockwave-flash" '+
				 ' wmode="transparent" width="788" height="400" />'+
				' </object>';
		
			$("#lightbox .lightboxContent").html("");
			//$("#fotozoom").show();
			window.scrollTo(0,0);
			loadPopup($("#lightbox"),function()
			{
				$("#lightbox .lightboxContent").html(html);
			});
		}
	}
	
}

function FotoClose()
{
	$("#fotozoom").hide();
}

function ToggleDossierList(e)
{
	if($(e).hasClass("active"))
	{
		$(e).removeClass("active");
		$(".dossiers .dossierslist").hide();
	}else{
		$(e).addClass("active");
		$(".dossiers .dossierslist").show();
	}
}

var timelinemax = 5;
function twittertimeline(max)
{
	$(".tweet:gt("+max+")").hide();
	timelinemax = max;
	window.setTimeout('twittertimelineslideup()', 3500);
}

function twittertimelineslideup()
{
	$(".tweet:visible:first").slideUp("slow", function(){
		var index = $(".tweet").index(this);
		var newitem = index+timelinemax+1;
		if($(".tweet").length<=newitem)
		{
			$(".tweet").slideUp("fast");
			$(".tweet:lt("+timelinemax+")").slideDown("slow");
			
			newitem = timelinemax;
		}
		$(".tweet:eq("+(newitem)+")").fadeIn("slow", function(){
			window.setTimeout('twittertimelineslideup()', 3500);
		});
	});
}
