$(function(){
	
	totalblocks = 38
	wWidth = $(window).width();
	leftover = wWidth%38;
	//alert(leftover);
	blokwidth = (wWidth-leftover)/totalblocks;
	//containerwidth = 35
	if (blokwidth%2 == 0 ){
		containerwidth = wWidth-leftover-(2*blokwidth);
		extrapad = leftover/2;
		//$("#container, .row").css("width",wWidth-leftover-(2*blokwidth)+"px");
	} else {
		
		blokwidth = blokwidth - 1;
		extrapad = (wWidth - (totalblocks*blokwidth))/2;
		containerwidth = (totalblocks-2)*blokwidth;
	}
	
	//$("#container, .row").css("width",containerwidth+"px");
	extrapadding = blokwidth+extrapad;
	
	newEms = (  (containerwidth)/(totalblocks-2) )/10;
	
	$(".b[w]").each(function(){
		$(this).css("width",blokwidth*$(this).attr("w")+"px");
	});
	$(".b[h]").each(function(){
		$(this).css("height",($(this).attr("h")*blokwidth)+"px");
	});
	$(".row[h]").each(function(){
	
		if ( $(this).attr("h") == 1 ){
			
			$(this).find(".b:not(.forceheight)").css("height","auto");
			$(this).find(".b .b").css("height",.5*blokwidth+"px");
			$(this).find(".b .b").css("width","100%");
			
		} else {
			//$(this).find(".b").css("height",($(this).attr("h")*blokwidth)+"px");
			
			//$(this).css("height",($(this).attr("h")*blokwidth)+"px");
		} 	
	});
	if ($("#iframesource").length > 0 ){
		thisiframesource = $("#iframesource").text();
		//alert(thisiframesource);
		$("#holdiframe").css("display","block")
		$("#backgroundiframe").attr("src",thisiframesource);
		
	}
	//$("#container").css("margin",wWidth-leftover+"px");	
	//$("#container").css("padding",blokwidth+extrapad+"px "+blokwidth+"px");
	//$("container").css("margin-right",leftover/2+"px");
	
//	$(".elem").each(function(){
//		kliklink = $(this).find("h3 a").attr("href");
//		alert(kliklink);
//	});
	$(".elem").each(function(){
		elemlink = $(this).find("h3 a");
		
		if ( elemlink.length > 0 ){
		///alert(elemlink.length)
			kliklink = elemlink.attr("href");
		
		//alert(kliklink);
			$(this).addClass("clickelem").attr("rel",kliklink).bind('click', function() {
		
		
			window.location.href = $(this).attr("rel");
		});
		
		}
		
	});
	$(".elem[w]").each(function(){
		elemwidth = $(this).attr("w");
		$(this).css("width",elemwidth*blokwidth+"px");
	})

	
	$(".b").hover(
	  function () {
	  	if ( !$(this).attr("w") ){
	   		$(this).addClass("bh");
	    }
	    if ( $(this).hasClass("bl") ){
	   	 //$(".bl").addClass("bhh");
	    }
	  }, 
	  function () {
	   
	   		$(this).removeClass("bh");
	   
	   	 if ( $(this).hasClass("bl") ){
	   	 	// $(".bl").removeClass("bhh");
	   	 }
	  
	   
	  }
	);
	$(".leesmeerlink a[href^='http']").addClass("external");
	$("ul").find("li:first").addClass("first");
	
	var poststyle;	
	
	poststyle = " \n"
	//poststyle = poststyle + ".#navline { width:"+windowWidth+"px;} \n"
	
		
		
		//$("#container").css("margin",wWidth-leftover+"px");	
		poststyle = poststyle + ".b { width: "+blokwidth+"px; height: "+blokwidth+"px;} \n";
		poststyle = poststyle + ".bhalf .b, .forceheight, .bhalf { height: "+.5*blokwidth+"px;} \n";

		poststyle = poststyle + ".enlarged {border-bottom-width:"+.5*blokwidth+"px;} \n";
		poststyle = poststyle + "div.jMyCarousel  {margin-top: "+.5*blokwidth+"px;} \n";
		poststyle = poststyle + "div.jMyCarousel ul li  {margin-right: "+.5*blokwidth+"px;} \n";
				
		
		
		poststyle = poststyle + "#container, .row { width: "+containerwidth+"px} \n";
		poststyle = poststyle + "#container { padding: "+blokwidth+"px "+blokwidth+"px} \n";
		poststyle = poststyle + "#container { margin-top: "+extrapad+"px;margin-bottom: "+extrapad+"px} \n";
		
		poststyle = poststyle + ".row { font-size: "+newEms+"em;} \n";
		
		poststyle = poststyle + ".enlarged { height: "+((blokwidth*19)/8)*5+"px;} \n";
		
		
		poststyle = poststyle + ".elem { margin-left: "+blokwidth+"px;} \n";
		poststyle = poststyle + "div.forcemargin { margin-left: "+blokwidth+"px !important;} \n";
		
		poststyle = poststyle + ".elem { margin-bottom: "+blokwidth+"px;} \n";
		
		poststyle = poststyle + ".pad-lr { padding: 0 "+blokwidth+"px;} \n";
		poststyle = poststyle + ".pad-tb { padding: "+blokwidth+"px 0;} \n";
		poststyle = poststyle + ".pad-l { padding-left: "+blokwidth+"px;} \n";
		poststyle = poststyle + ".pad-r { padding-right: "+blokwidth+"px;} \n";
		poststyle = poststyle + ".pad2 { padding: "+.5*blokwidth+"px;} \n";
		poststyle = poststyle + ".pad2-lr { padding-left: "+.5*blokwidth+"px; padding-right: "+.5*blokwidth+"px;} \n";
		poststyle = poststyle + ".pad2-tb { padding-top: "+.5*blokwidth+"px;padding-bottom: "+.5*blokwidth+"px;} \n";
		poststyle = poststyle + ".pad2-l { padding-left: "+.5*blokwidth+"px;} \n";
		poststyle = poststyle + ".pad2-r { padding-right: "+.5*blokwidth+"px;} \n";
		poststyle = poststyle + ".pad2-t { padding-top: "+.5*blokwidth+"px;} \n";
		
		poststyle = poststyle + ".pad2-b { padding-bottom: "+.5*blokwidth+"px;} \n";
		poststyle = poststyle + ".pad4 { padding: "+.25*blokwidth+"px} \n";
		poststyle = poststyle + ".pad4-lr { padding-left: "+.25*blokwidth+"px;padding-right: "+.25*blokwidth+"px;} \n";
		poststyle = poststyle + ".pad4-tb { padding-top: "+.25*blokwidth+"px;padding-bottom: "+.25*blokwidth+"px;} \n";
		poststyle = poststyle + ".pad4-l { padding-left: "+.25*blokwidth+"px;} \n";
		poststyle = poststyle + ".pad4-r { padding-right: "+.25*blokwidth+"px;} \n";
		poststyle = poststyle + ".pad4-t { padding-top: "+.25*blokwidth+"px;} \n";
		poststyle = poststyle + ".pad4-b { padding-bottom: "+.25*blokwidth+"px;} \n";

	newStyle = "<style type='text/css'>"+poststyle+"</style>";
	
	$("head").append(newStyle);
	
	$(".elem .wp-post-image").each(function(e){
		
		curimg = $(this).attr("src");
		
		nwwidth = $(this).parents(".elem").attr("w") * blokwidth;
		nwheight = (nwwidth/8 ) * 5;
		nwimg = setNewImg(nwwidth, curimg,"thumb");
		//alert(nwheight );
		$(this).attr("width",nwwidth);
		//$(this).attr("height",nwheight);
		$(this).attr("src",newimg);
	});
	$(".bigimg .wp-post-image").each(function(e){
		
		curimg = $(this).attr("src");
		nwimg = setNewImg(blokwidth*19, curimg,"thumb");
		
		
		
		$(this).attr("src",newimg);
	});
	
	
	
	$(".jMyCarousel li a").each(function(){
		
		currel = $(this).attr("rel");
		nwwidth = 19*blokwidth;
		//alert(width);
		
		newrel = currel.replace("wl=398", "wl="+nwwidth);
		
		newrel = newrel.replace("hp=398", "hp="+((19*blokwidth)/8)*5);
		$(this).attr("rel", newrel);
		
	});
	 initiatePosterousGallery();
	//$("#gal-next").click(function(){
	//	nextSlide("next");
	//})
	
	if ($("#juitterContainer").length){
		$.Juitter.start({
				searchType:"searchWord", // needed, you can use "searchWord", "fromUser", "toUser"
				searchObject:"hetgelaatwerkt", // needed, you can insert a username here or a word to be searched for, if you wish multiple search, separate the words by comma.
		
				// The values below will overwrite the ones on the Juitter default configuration. 
				// They are optional here.
				// I'm changing here as a example only
				//lang:"en", // restricts the search by the given language
				live:"live-15", // the number after "live-" indicates the time in seconds to wait before request the Twitter API for updates.
				placeHolder:"juitterContainer", // Set a place holder DIV which will receive the list of tweets example <div id="juitterContainer"></div>
				loadMSG: "Loading messages...", // Loading message, if you want to show an image, fill it with "image/gif" and go to the next variable to set which image you want to use on 
				imgName: "loader.gif", // Loading image, to enable it, go to the loadMSG var above and change it to "image/gif"
				total: 20, // number of tweets to be show - max 100
				readMore: "Lees het op Twitter", // read more message to be show after the tweet content
				nameUser:"text", // insert "image" to show avatar of "text" to show the name of the user that sent the tweet 
				openExternalLinks:"newWindow", // here you can choose how to open link to external websites, "newWindow" or "sameWindow"
		        filter:"sex->*BAD word*,porn->*BAD word*,fuck->*BAD word*,shit->*BAD word*"  // insert the words you want to hide from the tweets followed by what you want to show instead example: "sex->censured" or "porn->BLOCKED WORD" you can define as many as you want, if you don't want to replace the word, simply remove it, just add the words you want separated like this "porn,sex,fuck"... Be aware that the tweets will still be showed, only the bad words will be removed
		});
		
	}
	
}); // end document ready



function relative_time(time_value) {
   var parsed_date = Date.parse(time_value);

   var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
   var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);

   if(delta < 60) {
       return 'minder dan een minuut geleden';
   } else if(delta < 120) {
       return 'ongeveer een minuut geleden';
   } else if(delta < (45*60)) {
       return (parseInt(delta / 60)).toString() + ' minuten geleden';
   } else if(delta < (90*60)) {
           return 'ongeveer een uur geleden';
       } else if(delta < (24*60*60)) {
       return 'ongeveer ' + (parseInt(delta / 3600)).toString() + ' uur geleden';
   } else if(delta < (48*60*60)) {
       return '1 dag geleden';
   } else {
       return (parseInt(delta / 86400)).toString() + ' dagen geleden';
   }
}


function slideShow(){
	firstli = $(".imagesgallery li:first");
	nextli = firstli.next();
	
	firstli.addClass("curslide");
	nextli.addClass("nextslide");
	
	firstimg = firstli.find("img");
	nextimg = nextli.find("img");
	
	firstimgsrc = firstimg.attr("src");
	firstnwimg = setNewImg(blokwidth*19, firstimgsrc,"ngg");
	firstimg.attr("src",firstnwimg);
	
	nextimgsrc = nextimg.attr("src");
	nextnwimg = setNewImg(blokwidth*19, nextimgsrc,"ngg");
	nextimg.attr("src",nextnwimg);
	
	
	
//	$(".imagesgallery li:first img").each(function(){
//		curimg = $(this).attr("src");
//		nwimg = setNewImg(blokwidth*19, curimg,"ngg");
//		
//		$(this).attr("src",newimg);
//	})
}

function initiatePosterousGallery(){

//	$('.xngg-thumbnail-list a').click(function () { 
		//alert("x");
//		source = $(this).attr("rel");
//		
//		
//		$('.ngg-galleryoverview .pic img').attr("src",source) ;
//		
//		return false;	
//	});
	
	setTimeout(setCarouselThumbsWidth, 500);
	$("#foto-carousel .jMyCarousel li a").click(function(event){
		
		
		clickedimage = $(this).attr("rel");
		//alert(clickedimage);
		description = $(this).find(".description").html();
			$(".jMyCarousel li").removeClass("clicked").removeClass("firstclicked");
			$(this).parent().addClass("clicked");
			//$("a[rel$j='"+clickedimage+"']").parent().addClass("clicked");
			//$(".enlarged p.fotobijschrift").html(description);
		loadImg(clickedimage);
		event.preventDefault();
		return false;
	});
	
	$(".enlarged .imgB img").attr("src",$('.jMyCarousel li:first a').attr("rel"));
	//$(".enlarged p.fotobijschrift").html($('.jMyCarousel li:first a div.description').html())
	$('.jMyCarousel li:first').addClass("clicked");
	bringtofront();


}
function setCarouselThumbsWidth() {
	//alert($('#foto-carousel .jMyCarousel li').length);
	
	
	if ( $('#foto-carousel .jMyCarousel li').length > 7 ){
		//alert("x");
				$("#foto-carousel .jMyCarousel").addClass('carouseled').jMyCarousel({
			        visible: '498px',
			        circular: false,
			        btnPrev: $('#jm-prev-f'),
			        btnNext: $('#jm-next-f')
				});
			}

}
function bringtofront(){
	nextImg = $("#foto-carousel .jMyCarousel .clicked").next("li"); 
	if (nextImg.length == 0) {
		nextImg = $(".jMyCarousel ul li:first");
	};
	nextImgSrc = $(nextImg).find("a").attr("rel");
	
	$("p#fotobijschrift").html($('.jMyCarousel li.clicked a div.description').html())	;	
	//nextImgDescr = $(nextImg).find("div.description").html();	
	$('.next-img').unbind('click').bind("click", function(e){
		$(".jMyCarousel li").removeClass("clicked");
		$(nextImg).addClass("clicked");
		loadImg(nextImgSrc);
		//$(".enlarged p.fotobijschrift").html(nextImgDescr);
	});
	
	prevImg = $(".jMyCarousel .clicked").prev("li"); 
	if ( prevImg.length == 0 ) {
		prevImg = $(".jMyCarousel ul li:last");	
	};
	prevImgSrc = $(prevImg).find("a").attr("rel");
	prevImgDescr = $(prevImg).find("div.description").html();	
	$('.prev-img').unbind('click').bind("click", function(e){
		$(".jMyCarousel li").removeClass("clicked");
		$(prevImg).addClass("clicked");
		loadImg(prevImgSrc);
		//$(".enlarged p.fotobijschrift").html(prevImgDescr);
	});
	
	$(".enlarged .ontop").animate(
		{opacity:0}, 500, function(){
				
				$(this).css("opacity","1");
				
				$(".enlarged .atbottom").addClass("ontop").removeClass("atbottom");
				$(this).removeClass('ontop').addClass('atbottom');
       		}
    	);
	}



function loadImg(theImg){
	newimage = theImg;
	var laadPlaatje = new Image();
		laadPlaatje.onload = function() {
			$(".enlarged .atbottom img").attr("src", newimage);
			
			bringtofront();
		}
		laadPlaatje.src = newimage;
		if ( $(".carousel").height() < 371){
			
			$(".carousel").animate({ 
			height: "371px"
			}, 500 );
			
		}
	}
//function nextSlide(norp){
	//("#gallery li.curslide").fadeOut(speed, callback)
//	current = $("#gallery li.curslide");
//	if (norp == "next") {
//		aanstaande = current.next();
//	} else {
//		aanstaande = current.prev();
//	}
//	
//	
	//alert(current.next().length);
//	if ( aanstaande.length > 0 ){
//		aanstaande.addClass("nextslide").css("display","block");
//	
//	
//			
//			
//			
//			
//			$("#gallery li.curslide").fadeOut('slow', function() {
			//$(this).removeClass("curslide");
//			
//				
				//alert("xy");
//				$(this).removeClass("curslide");
				  // .next().removeClass("nextslide").addClass("curslide").next().addClass("nextslide");
//				
//				
//				
//				
//				$("#gallery li.nextslide").removeClass("nextslide").addClass("curslide");
//				
				//nextslidesrc = $("#gallery li.curslide img").next().attr("src");
				// nextnwimg = setNewImg(blokwidth*19, nextslidesrc,"ngg");
				// $("#gallery li.nextslide img").attr("src",nextnwimg);
//			
//			});
//	  
//	  
//	} else {
//		$("#gal-next").css("display","none");
		//alert("x");
		//$("#gallery li:first").addClass("nextslide").css("display","block");
//	}
//	
	//.removeClass("curslide").next().removeClass("nextslide").addClass("curslide").next().addClass("nextslide");
//}
function setNewImg(width, curimg, what){
	//alert(curimg);
	//curimg = $(this).attr("src");
		if (what == "thumb"){
			curimgstripped = curimg.replace("http://www.hetgelaat.nl/wp-content/uploads", "");
			
			arr_url = curimgstripped.split("-");
			
			curimgend = arr_url[arr_url.length - 1];
			//alert(arr_url[1]);
			curimgstripped = curimgstripped.replace("-"+curimgend, ".jpg");
			
			newimg = "http://www.hetgelaat.nl/wp-content/plugins/magic-fields/thirdparty/phpthumb/phpThumb.php?src=../../../../uploads"+curimgstripped+"&q=90&wl="+width+"";
			
			
		} else {
			
			
			curimgstripped = curimg.replace("http://www.hetgelaat.nl/wp-content/gallery/","");
			curimgstripped = curimgstripped.replace("thumbs/thumbs_","");
			
			newimg = "http://www.hetgelaat.nl/wp-content/plugins/magic-fields/thirdparty/phpthumb/phpThumb.php?src=../../../../gallery/"+curimgstripped+"&q=90&wl="+width+"";
		}
		
		//
		//alert("a")
		//alert(newimg);
		
		//alert(curimgstripped);
		
		return newimg; 
}
