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



$(document).ready(function(){
window.$my =
{
	// Initialize all the queries you want to use more than once
	metacontainer : $("#metacontainer"),
	spread : $('#spread'),
	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("/"); 
			
			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] == "bureauals") ){
				if ( $("iframe#als").hasClass("insight") ){
				} else {
					//alert("nu ben ik me aan het bemoeien");
				  gotoALS();
				}	
			} else {
			}
				
	});

	var scrollbarWidth = $(window).width() - document.body.offsetWidth;
	var fnScrollbarWidth = getScrollbarWidth();
	var viewportWidth = parseInt($(window).width() + scrollbarWidth);
	var viewportHeight = $(window).height();
	var windowWidth = parseInt(viewportWidth - scrollbarWidth - fnScrollbarWidth); //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)+"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)+"px;} \n";
			poststyle = poststyle + post_ID + " #spread .imgspread .li-divider { left:"+parseInt(fromWindowLeft+width_of_page)+"px;} \n";	
		});
	newStyle = "<style type='text/css'>"+poststyle+"</style>";
	
	$("head").append(newStyle);
	
	$('.post .mainspread').bind('click', function(event){
	
		showPostContent($(this).attr("id"));
	});
	$('.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");
	}
	$(".bureauals").bind("click",function(){
	
		if ( $(this).hasClass("naarsla")) {
			//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" );
				gotoALS();
			//}
		}
	
	});


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

	//$my.scroller1.scroll( $.throttle( 1000, actOnScroll ) );
		
	//$("#shownav").bind('mouseenter', showNavigation);
	//$("#grabnav").bind('mouseleave', hideNavigation);
	
	//initiateDrag();
	
	$(".bureausla").css("width",fnScrollbarWidth+"px").css("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");
		}
	});
 
 	 initiatePosterousGallery();
 
 }); // end document ready

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

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


function gotoALS(){
	scroller = $("#scroller2");
	if (scroller.hasClass("loaded")){
		iframecontent = $('iframe#als').contents().find("body");
		
		//alert(iframecontent.attr("rel"));
		$.address.title( iframecontent.attr("title") );
		$.address.value( iframecontent.attr("rel") );
	} else {
		
		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('');
		}
	}
	
	//$("#metacontainer").draggable( "destroy" );
	
	$(".bureauals.noscroll").addClass("tmpscroll")
	
	
	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){
		event.preventDefault();
		loadPosterousDetail($(this).attr("href"));
	});
	//alert("x");

}

	
	

function lightboxImages(some){
	$(some+" .lightbox").lightbox({
	    fitToScreen: true,
	    overlayOpacity: 0.4
	});
}

function moveScroller(where){
	if (where == "back"){	
		//$("#shownav").show();
		$(".bureausla").css("display","none");
		
		$("#metacontainer").animate({
			left:"0px"
		}, 1800, function() {
			//initiateDrag();
			$(".bureauals").css("cursor","w-resize");
			$("iframe#als").removeClass("insight");	
			$my.scroller1.css("opacity","");  	 
		});
		
		//$("#navline").animate({
		//	left:"0px"
		//}, 1800 );	
		
	}else{
		procent = $("#metacontainer").outerWidth()/100 * 4;
		//$("#shownav").hide();
		$(".bureausla").css("display","block");
		
		
		whereto = "-"+ parseInt($("#metacontainer").outerWidth()/2  - procent) +"px";
		$("#metacontainer").animate({
			left:whereto
		 }, 1800, function() {
			$("iframe#als").addClass("insight");	 
			//initiateDrag();
			$(".bureauals").css("cursor","e-resize");		  	 
			$my.scroller1.css("opacity","0.4");
		});
		//$("#navline").animate({
		//	left:whereto
		//}, 1800 );
		 
	}

}
function loadPosterousDetail(link){
	if ( $('.pakme').length ) {
		$('.pakme').html("");
	} else {
		$('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){
	
	clickedpost = $("#"+which);
	
	
	
	
	$(".clicked").removeClass("clicked").bind('click', function(event){
		showPostContent($(this).attr("id"));
	});
	
	
	
	
	
	clickedpost.addClass("clicked");
	clickedpost.unbind('click').bind('click',function(){
		$my.spread.remove();
		$(".clicked").removeClass("clicked");
		
		addressvalue = "bureausla"; 
		addresstitle = "bureau SLA";
		$.address.value(addressvalue); 
		$.address.title(addresstitle);
		
		$("body").attr("title",addresstitle).attr("rel",addressvalue);
		
		clickedpost.unbind('click').bind('click', function(event){
			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 ) {
	
		$("#spread").html("");
		$("#spread").css("height","1px");					
		$("#"+dezeID).append($("#spread"));	  
	} else {
		
		$('<div id="spread" />').appendTo("#"+dezeID);  
		$my.spread = $("#spread");
		
	}


	
	$my.spread.load(thisSlug, function() {
		
		
		 maakColumns(pagewidth-80, pageheight -40);
	
		setWidthofImages();
		
		if (issingle == "yes"){
			
			addressvalue = 'project/'+dezeSlug;
			addresstitle = 'Bureau SLA | '+ dezeSlug;
		
			$.address.value(addressvalue); 
			$.address.title(addresstitle);
			$("body").attr("title",addresstitle).attr("rel",addressvalue);
		
		} else {
		
			addressvalue = 'project/'+dezeSlug;
			addresstitle = 'Bureau SLA | '+ dezeSlug;
			
		  	$.address.value(addressvalue); 
		  	$.address.title(addresstitle);
		  	$("body").attr("title",addresstitle).attr("rel",addressvalue);
		  	
		  	
	  	}
	//_gaq.push(['_trackPageview', '/sign-up-complete.php']); 
	  _gaq.push(['_trackEvent', 'Projecten', 'Show', dezeSlug]);
	});
	
	



}
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 =  $my.spread.find(".spread");
	heightofspread = spreadchild.height();

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


function maakColumns(breedte,hoogte){
	deze = $my.spread.find('.textpages');
	
	
	
	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 {
	
		scrollToPost();
		$my.spread.find(".imgspread").css("margin-top","-1px");
	}
}

function buildNewsletter(content_width,content_height){ 
		
	if($('#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
		$('#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");
		$my.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 );
			
		}
	}

 
