var scrolled = "no"; // means that scrollTop  < 178
//var naviscloned = "no";
var issingle = "no";

var hg_body = $('body');
var hg_spread;
$(document).ready(function(){
window.$my =
{
	// Initialize all the queries you want to use more than once
	metacontainer : $("#metacontainer"),
	scroller1 : $('#scroller1')
};
	$.address.init(function(event) {
		}).change(function(event) {
			
		}).internalChange(function(event) { //als er geklikt wordt
			//functin to .load of .ajax calls	
			
		}).externalChange(function(event) { // met de backbutton
			//functin to .load of .ajax calls	
			thisEvent = event.value;
			eventarray = thisEvent.split("/"); 
			//alert(thisEvent);
			if ( (thisEvent === "/") ){
				//alert("binnen op SLA");
				if ( $("iframe#als").hasClass("insight") ){
					$("iframe#als").removeClass("insight");
					$.address.value('');
					$.address.title( '' );
					gotoSLA();
				} else {
					//close all opened books
					$(".clicked").unbind('click').bind('click', function(event){
						showPostContent($(this).attr("id"));
					});	
					$(".clicked").removeClass("clicked");	
					//$("#spread").remove();							
				}
			} else if (eventarray[1] == "project"){
				
				if ( $("iframe#als").hasClass("insight") ){
					 gotoSLA();
				}
				if ($("#"+eventarray[2]).length){
					showPostContent(eventarray[2]);
				} else {
					location.href =  "/project/"+eventarray[2];
				}			
			} else if (eventarray[1] == "bureau-als"){
				//alert("x");
				if ( $("iframe#als").hasClass("insight") ){
					 gotoSLA();
				} else {
					//location.href =  "/project/"+eventarray[2];
					gotoALS();
				}			
			} 
				
	});

	var scrollbarWidth = $(window).width() - document.body.offsetWidth;
	var fnScrollbarWidth = getScrollbarWidth();
	var viewportWidth = parseInt($(window).width() + scrollbarWidth, 10);
	var viewportHeight = $(window).height();
	var windowWidth = parseInt(viewportWidth - scrollbarWidth - fnScrollbarWidth, 10); //subtract both, to account for all browsers


	$my.metacontainer.css({
		"height" : viewportHeight+"px",
		"width" : viewportWidth*2+"px",
		"background-position" : $my.scroller1.width()+"px top"	
	});
	
	
	
	
	
	var poststyle;	
	
	poststyle = ".imgspread { width:"+windowWidth+"px;} \n";
	//poststyle = poststyle + "#navline { width:"+windowWidth+"px;} \n"
	
		$(".post:not(.nospread)").each(function(){
			post_ID = "#"+$(this).attr("id");
			width_of_page = $(this).find(".fotopage").width();
			width_of_spread = width_of_page * 2;
			fromWindowLeft = $(this).offset().left;
			goRightWidth = windowWidth - (fromWindowLeft+width_of_spread + 2) ;
			goRightLeft = fromWindowLeft+width_of_spread+1;
			goLeftWidth = Math.ceil( (windowWidth - width_of_spread)/2 );
	
			poststyle = poststyle + post_ID + " #spread .imgspread { margin-left:-"+parseInt(fromWindowLeft + 1, 10)+"px;} \n";
			poststyle = poststyle + post_ID + " #spread .imgspread .goRight { left:"+goRightLeft+"px;width:"+goRightWidth+"px;} \n";		
			poststyle = poststyle + post_ID + " #spread .imgspread .goLeft { width:"+fromWindowLeft+"px;} \n";
			poststyle = poststyle + post_ID + " #spread .imgspread .images { margin-left:"+parseInt(fromWindowLeft + 1, 10)+"px;} \n";
			poststyle = poststyle + post_ID + " #spread .imgspread .li-divider { left:"+parseInt(fromWindowLeft+width_of_page,10)+"px;} \n";	
		});
	newStyle = "<style type='text/css'>"+poststyle+"</style>";
	
	$("head").append(newStyle);
	
	$('.post .mainspread').unbind('click').bind('click', function(event){
		if (!$(event.target).is('.label a')){
				
		clickedid = $(this).attr("id");
		showPostContent(clickedid);
		}
	});
	$('.post .mainspread p.readmore a').bind('click', function(event){
		event.preventDefault();
	});
	if ( $("#cats").find(".current-cat").length ){
		$("#pages").find(".page-item-900").addClass("current_page_item");
	}
	$("#moveMetacontainer").bind("click",function(){
	
		if ( $(this).hasClass("to_sla")) {
			
			//moveScroller('to_sla');
			//if ( ui.position.left > $("#metacontainer").position().left ){
				//$("body").removeClass("dragginginprogress");
				//$("#metacontainer").draggable( "destroy" );
				//alert($("body").attr("rel"));
				gotoSLA();
			//}
		} else {
			//if (ui.position.left < -20 ){
				//$("#metacontainer").draggable( "destroy" );
			//moveScroller('to_als');
			
				gotoALS();
			//}
		}
	
	});


	if (issingle == "yes"){ 
		hg_mainspread = $('.post .mainspread');
		hg_mainspread.trigger('click');
		hg_mainspread.addClass('issingle');
		
	}

	//$my.scroller1.scroll( $.throttle( 1000, actOnScroll ) );
		
	//$("#shownav").bind('mouseenter', showNavigation);
	//$("#grabnav").bind('mouseleave', hideNavigation);
	
	//initiateDrag();
	
	
	
	$(".bureausla").css({ "width": fnScrollbarWidth+"px", "margin-left": "-"+fnScrollbarWidth+"px" });

	setALSscroll();
	lightboxImages("body");	
	setLabels('mainspread');

//	$("#single-posterous a.moreposterous").live('click', function(event){
//		event.preventDefault();
//		$('.pakme').hide();
//		gotoALS();
//	});
//
//	$("#single-posterous ul.p-nav a.filter").live('click', function(event){
//		event.preventDefault();
//	
//		$('.pakme').hide();
//		gotoALS();
//
//		$("#als-header li a").removeClass("clicked");
//	
//		dowhat = $(this).attr("rel");
//		slug = $(this).attr("slug");
//		$("#als-header li li."+slug+" a").addClass("clicked");
//		$.address.value('bureauals/'+dowhat+'/'+slug);
//	});

	$(".label.clone a:not(.label3), .mainspread .label a:not(.label3)").live('mouseover mouseout', function(event) {
		if (event.type == 'mouseover') {
			//alert($(this).text());
			
			$(this).addClass("showtip");
		} else {
			$(this).removeClass("showtip");
		}
	});
    if($.browser.SafariMobile){
		$(".publicatie").click(function(){
			if ($(this).hasClass('pub-hover')){
				$(this).removeClass("pub-hover");
			} else {
				$(".pub-hover").removeClass("pub-hover");
				$(this).addClass("pub-hover");
			}
		});
	} else {
		$(".publicatie").hover(
			function(){
				$(this).addClass("pub-hover");
			},
			function(){
				$(this).removeClass("pub-hover");
			}
		);
	}

	$('a[href$="pdf"]').attr("target","_blank");

}); // end document ready

function setALSscroll(){
	if ( $my.scroller1[0].scrollHeight > $my.scroller1.height() ){
		$("#moveMetacontainer").removeClass("noscroll");
		
	} else {
		$("#moveMetacontainer").addClass("noscroll");
	}
}
function gotoSLA(){
	
	//$("#metacontainer").draggable( "destroy" );
	
	if (hg_body.attr("rel") == "") { // the ALS homepage is still 'unused'
		$.address.title( hg_body.attr("title") );
		$.address.value( 'sla' );
	} else {
		$.address.title( hg_body.attr("title") );
		$.address.value( hg_body.attr("rel") );
	}
	
	//$(".bureauals.tmpscroll").removeClass("tmpscroll")

	moveScroller('to_sla');
	$(".bureauals").removeClass("naarsla");
}


function gotoALS(){
		//scroller = $("#scroller2");
	
		
		//scroller.addClass("loaded");
		//window.open('/all_posterous/','mywindow','width=400,height=200')
		//$("#scroller2 iframe").attr("src", "/all_posterous/");
		
		
//		thisEvent = $.address.value();
		//alert(thisEvent);
//		eventarray = thisEvent.split("/"); 
//		
		//if (eventarray[1] == "bureauals"){ // means we entered by pasting a URL from bureauALS
//		$.address.title($.address.title());
//		$.address.value($.address.value());
		
		//} else {
		//$.address.title(''); //empty values so the iFrame will fill them
		//$.address.value('');
		//}
		
	
	
	
		moveScroller();
	//$(".bureauals").addClass("naarsla");
}


//function initiateDrag(){
//	$("#metacontainer").draggable({
//		axis: "x",
//		handle: ".bureauals",
//		start: function(e, ui) {
			//$("body").addClass("dragginginprogress");
//			
//		},
//		drag: function(e, ui) {
			// document.onselectstart = function () { return false; }; 
//			if ( $(".bureauals").hasClass("naarsla")) {
//				if ( ui.position.left > $("#metacontainer").position().left ){
					//$("body").removeClass("dragginginprogress");
//					$("#metacontainer").draggable( "destroy" );
//					gotoSLA();
//				}
//			} else {
//				if (ui.position.left < -20 ){
//					$("#metacontainer").draggable( "destroy" );
//					gotoALS();
//				}
//			}
//		},
//		stop: function(e, ui) {
//		}
//	});
//}


//function bindShowPosterousDetail(){
//	$("#posterous-list a").bind("click", function(event){
//		event.preventDefault();
//		loadPosterousDetail($(this).attr("href"));
//	});
//}

//function actOnScroll(){
//	scrollB = $(this).scrollTop();
//	if ( scrollB > 178 ){
//		if ( scrolled == "no" ){
//			scrolled = "yes";
//			$("#navline").show("slow");
//		} 
//	}
//	if ( scrollB < 178 ){
//		if ( scrolled == "yes" ){
//			scrolled = "no";
//			$("#navline").hide("fast")
//		} 
//	}
//}


//function filterPosterous(specs){
//	$("#als-header li").removeClass("onpage");
//	$("#posterouscontent").load(specs, function() {
//		$(this).addClass("loaded");
//		bindShowPosterousDetail();
//	});
//}
function showPosterousLabelTitle() {
//	$(".label a").unbind('click').bind('click', function(event){
//		
//		
//		moveScroller('to_als')
//		loadPosterousURL($(this).attr("href"));
//		event.preventDefault();
		//loadPosterousDetail($(this).attr("href"));
//	});
	//alert("x");

	$(".label a").unbind('click').bind('click', function(event){
			
			var pakme = $(".pakme"),
				shade = $('.shade'),
				sluiters = $(".pakme .close, .shade"),
				thisiframe = pakme.find("iframe"),
				spinner = $('.spinner');
			
			
			
			sluiters.bind('click', function(){
				pakme.hide();
				shade.hide();
				
			});
			pakme.find('.footer a').unbind('click').bind('click',function(){
				pakme.fadeOut('slow');
				shade.fadeOut('slow');
				moveScroller('to_als');
				
				return false;
			});
			link = $(this).attr("href")+"#articles";
			linktekst = $(this).find('span').text();
	
			thisiframe.attr("src",link);
	
			shade.fadeIn('slow', function(){
				spinner.html("loading raw data...").show();
				thisiframe.load(function(){
					spinner.hide();
					pakme.fadeIn('fast');
				});	
			});
			event.preventDefault();
		});



}

	
	

function lightboxImages(some){
	if($.browser.SafariMobile){
		$(some+" .lightbox").colorbox({innerWidth:"800", innerHeight:"768", slideshow:true});
		
	}
	$(some+" .lightbox").colorbox({innerWidth:"1024", innerHeight:"768", slideshow:true});
}

//$("a[rel='example1']").colorbox({innerWidth:"80%", innerHeight:"80%"});

function moveScroller(where){

	

	if (where == "to_sla"){	
		//$("#shownav").show();
		$("#moveMetacontainer").removeClass("to_sla");
		$("#moveMetacontainer").css("padding-right","0px");
		$my.scroller1.css("opacity","");  
		$("#metacontainer").animate({
			left:"0px"
		}, 1800, function() {
			//initiateDrag();
			$("#moveMetacontainer").css("cursor","w-resize");
			$("iframe#als").removeClass("insight");	 
		});
		
		
		
	}else{
		if ($("#scroller2").hasClass("urlloaded")){
			//setAddresonPosterous();
			loadPosterousURL();
		}else {
			
			loadPosterousURL("http://bureausla.posterous.com/");
		}
		procent = $("#metacontainer").outerWidth()/100 * 4;
		//$("#shownav").hide();
		$("#moveMetacontainer").addClass("to_sla");
		extrawidth = getScrollbarWidth();
		$("#moveMetacontainer").css("padding-right",extrawidth+"px");
		//$("#moveMetacontainer").addClass("naarsla");
		//$(".bureausla").css("display","block").addClass("naarsla");
		
		
		whereto = "-"+ parseInt($("#metacontainer").outerWidth()/2  - procent) +"px";
		$("#metacontainer").animate({
			left:whereto
		 }, 1800, function() {
			$("iframe#als").addClass("insight");	 
			//initiateDrag();
			$("#moveMetacontainer").css("cursor","e-resize");		  	 
			$my.scroller1.css("opacity","0.4");
		});
		//$("#navline").animate({
		//	left:whereto
		//}, 1800 );
		 
	}

}
function loadPosterousURL(url){
	if (url == ""){
	
	} else {
		loadedURL = $("#scroller2 iframe").attr("src");
		
		if (loadedURL == url){
		
		} else {
		$("#scroller2 iframe").attr("src",url);	
		}
	}
	setAddresonPosterous();
	$("#scroller2").addClass("urlloaded");
	//moveScroller('to_als');
	
}
function setAddresonPosterous(){
	$.address.value('bureau-als');
	$.address.title( 'bureau ALS | bureau SLA\'s raw data' );
}
function loadPosterousDetail(link){
	if ( $('.pakme').length ) {
		$('.pakme').html("");
	} else {
		hg_body.append("<div class='pakme insla'></div>");
	}
	
	$('.pakme').load(link, function() {
		//initiatePosterousGallery();
		$('.pakme').show();
		$(".close-single").click(function(){
			$('.pakme').hide();	
		});
		_gaq.push(['_trackEvent', 'Posterouspost-detail', 'ShowfromSLA', link]);
	});	
}

//function hideNavigation() {
//	grabnav = $("#grabnav");
//	grabnav.removeClass("hasnav");
//	grabnav.animate({
//			height:"1px",
//			marginTop:"0px"
//		}, 800, function() {	
//			$("#metacontainer").css("opacity","");
//			grabnav.css("display","none");
//	});
//}

function getScrollbarWidth() {
    var div = $('<div style="width:50px;height:50px;overflow:hidden;position:absolute;top:-200px;left:-200px;"><div style="height:100px;"></div>');
   $('body').append(div);
    var w1 = $('div', div).innerWidth();
    div.css('overflow-y', 'scroll');
    var w2 = $('div', div).innerWidth();
    $(div).remove();
    return (w1 - w2);
}
//function showNavigation() {
	//alert("X");
//	$("#metacontainer").css("opacity","0.4");
//	if (naviscloned == "no"){
//		$("#containhead").clone().appendTo("#grabnav");
//		naviscloned = "yes";
//	} 
//	$("#grabnav").addClass("hasnav");
//	$("#grabnav").animate({
//	   height:"176px",
//	   marginTop:"-89px"
	  // opacity: "1"
//	 }, 500, function() {
	  	//$("#grabnav #containhead .bureauals").show();	
		//alert("klaar"); 	  	 
//	 	   
//	 });
//	$("#grabnav #containhead").animate({
//	   
//	   marginTop:"0px"
	  // opacity: "1"
//	 }, 500, function() {
//	  });
//}

function showPostContent(which){
	console.time("aaaaaaaaaaaaaaaaaaaaaa");
	clickedpost = $("#"+which);
	
	
	
	
	$(".clicked").removeClass("clicked").unbind('click').bind('click', function(event){
		showPostContent($(this).attr("id"));
	});
	
	
	
	
	
	clickedpost.addClass("clicked");
	
	clickedpost.unbind('click').bind('click',function(event){
		if (!$(event.target).is('.label a')){
			hg_spread.remove();
			$(".clicked").removeClass("clicked");
			
			addressvalue = "bureausla"; 
			addresstitle = "bureau SLA";
			$.address.value(addressvalue); 
			$.address.title(addresstitle);
			
			//hg_body.attr("title",addresstitle).attr("rel",addressvalue);
			
			hg_body.attr({ "rel": addressvalue, "title": addresstitle });
			
			
			
			
			clickedpost.unbind('click').bind('click', function(event){
				if (!$(event.target).is('.label a')){
				showPostContent($(this).attr("id"));
				}
			});
		}
	});
	
	
	
	
	thisURL = clickedpost.find("h2 a").attr("href");
	
	//$(this).addClass("clicked");
	dezeID = clickedpost.parents(".post").attr("id");
	dezeSlug = clickedpost.attr("id");
	thisSlug = "http://www.bureausla.nl/ajax-handler/?slug="+dezeSlug;
	
	clickedpostfotopage = clickedpost.find(".fotopage");
	
	pagewidth = clickedpostfotopage.width();
	pageheight = clickedpostfotopage.height();
	//spread = $("#spread");
	//$(this).addClass("clicked");
	if ( $('#spread').length ) {
	
		hg_spread.html("");
		hg_spread.css("height","1px");					
		$("#"+dezeID).append($("#spread"));	  
	} else {
		
		$('<div id="spread" />').appendTo("#"+dezeID);  
		hg_spread = $("#spread");
		
	}



$("#success").load("/not-here.php", function(response, status, xhr) {

});





	
	hg_spread.load(thisSlug, function() {
		
		
		
		
	
		
		maakColumns(pagewidth-80, pageheight -40);
		scrollToPost();
		setWidthofImages();
		hg_spread.find('a[href$="pdf"]').attr("target","_blank");
		nicetitle = $('.clicked .left h2, .issingle .left h2').text();
		if (issingle == "yes"){
			
			addressvalue = 'project/'+dezeSlug;
			addresstitle = nicetitle+' | bureau SLA';
		
			$.address.value(addressvalue); 
			$.address.title(addresstitle);
			hg_body.attr("title",addresstitle).attr("rel",addressvalue);
		
		} else {
		
			addressvalue = 'project/'+dezeSlug;
			addresstitle = nicetitle+' | bureau SLA';
			
		  	$.address.value(addressvalue); 
		  	$.address.title(addresstitle);
		  	hg_body.attr("title",addresstitle).attr("rel",addressvalue);
		  	
		  	
	  	}
	//_gaq.push(['_trackPageview', '/sign-up-complete.php']); 
	  _gaq.push(['_trackEvent', 'Projecten', 'Show', dezeSlug]);
	  console.timeEnd("aaaaaaaaaaaaaaaaaaaaaa");
	});
	
	



}
function setWidthofImages(){
	
	theseLi = $("#spread .images li");
	theseImages = $("#spread .images");
	thisSpread = $("#spread .imgspread");
	
	//imgLiHeight = theseLi.outerHeight(); //php
	
	aantalImg = theseLi.length;
	//amountOfLi = Math.ceil(aantalImg / 2 );
	
	pageWidth = theseLi.outerWidth();
	
		
	theseImages.css("width", (aantalImg*pageWidth)+1+"px");
	
	// heights = "height:"+parseInt(imgLiHeight + 1)+"px"; //php
	
	//$("#spread .imgspread").prepend('<div class="goLeft" style="'+heights+'"></div><div class="goRight"  style="'+heights+'"></div><div class="li-divider" style="'+heights+'"></div>');
	thisSpread.prepend('<div class="goLeft" alt="click for more images"></div><div class="goRight" alt="click for more images"></div><div class="li-divider"></div>');
	
	if (aantalImg == 2){
		$("#spread .goRight").hide();
	}

	$("#spread .imgspread .goLeft, #spread .imgspread .goRight").bind('click', function() {
		
		currentMargin = Math.ceil(theseImages.offset().left - 1);
		
			
		if ( $(this).hasClass("goLeft") ){
			step = parseFloat( parseInt(thisSpread.attr("rel"))+ 1);
			thisSpread.attr("rel",step);
	
			theseImages.stop(true, true).animate({
				//"marginLeft" : (currentMargin + (pageWidth - 1))+"px"
				"marginLeft" : "+="+(pageWidth)+"px"
			}, 600);
		}
		if ( $(this).hasClass("goRight") ){
			step = parseFloat( parseInt(thisSpread.attr("rel"))  - 1);
			thisSpread.attr("rel",step);
	
			theseImages.stop(true, true).animate({
				//"marginLeft" : (currentMargin - (pageWidth + 1))+"px"
				"marginLeft" : "-="+(pageWidth )+"px"
			}, 600);
		}
		
	
		if ( thisSpread.attr("rel") == "0" ) {
			$("#spread .goLeft").hide();
		} else {
			$("#spread .goLeft").show();
		}

		//alert(2 - aantalImg);
		if ( thisSpread.attr("rel") == (2 - aantalImg)  ) {
			$("#spread .goRight").hide();
		} else {
			$("#spread .goRight").show();
		}
			
	
	});
	
	lightboxImages("#spread");
	
	

}

function setLabels(which){
	
	if ( which == "mainspread"){
		
		welkepakken = $("#labels-main div.label");
		showPosterousLabelTitle();
	} else {
		welkepakken = $("#labels div.label");
		
	}
	
	
	
	welkepakken.each(function(){
		
		if ( which == "mainspread"){
			welkeplakken = $(this).parents(".post").attr("id");
		} else {
			welkeplakken = "spread";
		}
		
		
	//$("#labels-main div.label").each(function(){
	//$("#labels div.label").each(function(){
		attachto = $(this).find("a").attr("rel");
		
		//alert($(this).parents(".post").attr("id"));
		
		
		arrCoord = attachto.split("-");
		
		if ( (arrCoord[0]) && (arrCoord[1])){
			attachtovlak = arrCoord[0];
			attachtowhich = arrCoord[1];
			
			//console.log(attachtowhich);
			
			if (attachtovlak == "img"){
				attachtovlak = "images li";	
			}
			
			if (attachtovlak == "laatste"){
				attachtovlak = "lastspread .page";	
			}
			if (attachtovlak == "main"){
				attachtovlak = "mainspread .page";	
			}
			
			
			//if (attachtowhich){
				thisspread = $("#"+welkeplakken+" ."+attachtovlak+":nth-child("+attachtowhich+")");
			
			if (attachtovlak == "column"){
				attachtovlak = "textpages .column";	
				attachtowhich = attachtowhich-1;
				thisspread = $("#"+welkeplakken+" ."+attachtovlak+":eq("+attachtowhich+")");
			}	
			//}
			
			$(this)
				.clone()
				.appendTo(thisspread)
				.addClass("clone")
				.show("slow", function(){
					
					//$(".label.clone a.label3, .label.clone a.label1").draggable({ containment: '#scroller1', scroll: false });
				
			
					
			});
		}
		showPosterousLabelTitle();
		
	});
}
function scrollToPost(){

	spreadchild =  hg_spread.find(".spread");
	heightofspread = spreadchild.height();

	hg_spread.animate({
	   height:heightofspread+"px"
	 }, 1500, function() {	
		setALSscroll();
		heightofspreadx = spreadchild.height(); 
		hg_spread.css({ "border-bottom": "none", "height": heightofspreadx+"px" });
		setTimeout(setLabels, 200);
	   	if($.browser.SafariMobile){
		   	setTimeout(function () {
	   			myScroll.refresh();
	   		}, 0);
	   	}
	 });
	
	
		//fromtop = dezeParent.position().top; 
		//alert(fromtop);
		dezeParent =  hg_spread.parents(".post");
		fromtop = dezeParent.position().top; 
		
		
		if($.browser.SafariMobile){
		   //myScroll.scrollTo(0, -(fromtop+120), 200)
		 } else {
		$my.scroller1.animate({
		    "scrollTop":fromtop+120
		  }, 1000);	
		}
	
}


function maakColumns(breedte,hoogte){
	deze = hg_spread.find('.textpages');
	
	//scrollToPost();
	
	if (deze.length){
		// the height of the content, discluding the header/footer  
		var content_height = hoogte;  
		var content_width = breedte;  
		// the beginning page number to show in the footer   
		buildNewsletter(content_width,content_height);  
	} else {
	
		// xxxx scrollToPost();
		hg_spread.find(".imgspread").css("margin-top","-1px");
	}
}

function buildNewsletter(content_width,content_height){ 
	hg_newsletterContent = 	$('#newsletterContent');
	if(hg_newsletterContent.contents().length > 0){
		// when we need to add a new page, use a jq object for a template
		// or use a long HTML string, whatever your preference
		$page = $("#page_template").clone().addClass("npage").css("display", "block");
		
		// fun stuff, like adding page numbers to the footer
		//$page.find(".footer span").append(page);
		$("#spread .textpages").append($page);
		//page++;
		
		// here is the columnizer magic
		hg_newsletterContent.columnize({
			columns: 2,
			target: ".npage:last",
			overflow: {
				height: content_height,
				width: content_width,
				id: "#newsletterContent",
				doneFunc: function(){
					//console.log("done with page");
					buildNewsletter(content_width,content_height);
				}
			}
		});
	} else {
		//scrollToPost();
		$("#spread .npage:last").css("border-bottom","none");
		hg_spread.find(".imgspread").css("margin-top","-1px");
		$("#spread .npage .column").css("height",$("#spread .npage").height()-40+"px");
	}


	
	
	
} 	

		
//function maakColumnsx(breedte,hoogte){
//	
//	
//	deze = $my.spread.find('.wide');
//	
//	
//	
//	if (deze.length){
//		
//	
//		deze.columnize({
//		
//		
//			width : breedte,
//			lastNeverTallest: true,
//			height : hoogte - 40,
//			buildOnce : true,
//			doneFunc : function(){
//					dezecolumns = deze.find('.column');
//					tellen = dezecolumns.size() - 1;
//					dezecolumnsEven = deze.find('.column:even');
//					dezecolumnsLast = deze.find('.column.last');
//					dezecolumnsNotLast = deze.find('.column:not(.last)');
//					dezecolumnsLastTwo = deze.find(".column:not(.last):not(.column:nth-child("+tellen+"))")
//					deze.css("width", (breedte + 100) * 2  +"px"); // MUST be set after .columnize()
					//deze.css("background-color", "RED"); // MUST be set after .columnize()
//					dezecolumns.css("height",hoogte +"px");// MUST be set after .columnize(				
//					dezecolumnsEven.addClass("even").css("border-right","solid 1px #000");
//					
//					if ( dezecolumnsLast.hasClass("even") ) {
//						dezecolumnsNotLast.css("border-bottom","solid 1px #000");	
//					} else {
//						dezecolumnsLastTwo.css("border-bottom","solid 1px #000");
//					}
//					
//					dezecolumnsEven.each(function(){
//						if ( $(this).is(':empty') ) {
//							$(this).remove();
//							dezecolumns.css("border-bottom","none");		
//						}
//					});
//					if ( ($('#spread .wide .column').length % 2) != 0 ) {
						// can't use 'dezecolumns', because some of them might have been removed (empty())
						//$(deze).find("br").before("<div style='height:"+hoogte+"px;width:"+breedte+"px;float:left;' class='column' />");
//					}	
//					
//					scrollToPost();
//				}
//			});
//		} else {//  deze.length
//		
//			scrollToPost();
//			$my.spread.find(".imgspread").css("margin-top","-1px");
//		}// end deze.length
	
//	}
//function initiatePosterousGallery(){
//
//	$('.ngg-thumbnail-list a').click(function () { 
//		source = $(this).attr("rel");
//		$('.ngg-galleryoverview .pic img').attr("src",source) ;
//		return false;	
//	});
//	
//	setTimeout(setCarouselThumbsWidth, 500);
//	$("#foto-carousel .jMyCarousel li a").click(function(){
//		clickedimage = $(this).attr("rel");
//		description = $(this).find(".description").html();
//			$(".jMyCarousel li").removeClass("clicked").removeClass("firstclicked");
//			$("a[rel$='"+clickedimage+"']").parent().addClass("clicked");
//			$(".enlarged p.fotobijschrift").html(description);
//		loadImg(clickedimage);
//		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() {
//	if ( $('#foto-carousel .jMyCarousel li').length > 7 ){
//		$("#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");	
//	
//	$('.next-img').unbind('click').bind("click", function(e){
//		$(".jMyCarousel li").removeClass("clicked");
//		$(nextImg).addClass("clicked");
//		loadImg(nextImgSrc);
//	});
//	
//	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 .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 );
//			
//		}
//	}
//
// 

