
var images_preloaded = 0;
var images_to_preload = 0;
var root_url = "/index.php#";
var basis_url = root_url + "/";
var stuffhash = window.location.hash;
var closedButton = false;
var clickedThumb = false;
var openingItem = false;
var object_tmp = "";

$(document).ready(function() {	
	snorFix();
	
	$(".thumb .image").click(function()
	{
	    var item_url = $(this).parent().find(".item_url").text();
	    window.location = basis_url + item_url;
	    clickedThumb = true;
	}); 
	

	//close
	$(".thumb .close").click(function()
	{
		clickedThumb = false;
		closedButton = true;
		verstopSnor(this);
	$('.board').masonry({ columnWidth: 250, itemSelector: '.thumb, .big, .extra, .extra-still,.thumb1', animate: true });
	}); 
	
	
	//thumb hover
	$(".thumb").hover(function() {	
	
					
			$(this).fadeTo("fast", 1.0); 	// Rollover at 100%
	} , function() { 
		$(this).fadeTo("fast", 0.8); 	// Rollout at 80%
	
	});	
	$(".thumb1").hover(function() {	
	
					
			$(this).fadeTo("fast", 1.0); 	// Rollover at 100%
	} , function() { 
		$(this).fadeTo("fast", 0.8); 	// Rollout at 80%
	
	});	
		



	openSnor();	
    
   setInterval(function() {
      if ((window.location.hash != stuffhash) || (clickedThumb == true && closedButton == true) && openingItem == false) {
      	openingItem = true;
         openSnor();
      	stuffhash = window.location.hash;
      	
      	if (clickedThumb == true && closedButton == true) {
      		closedButton = false;
      		clickedThumb = false;	
      	}
      }
   }, 100);			
	
  
});

$(window).resize(function() {	
	snorFix();
});

function snorFix() {
	var TotalWidth = $(window).width();
	var cols = Math.floor(TotalWidth / 250);
	var boardWidth = cols * 250;
	var margin = (TotalWidth - boardWidth) / 2;
	$(".board").css({ width: boardWidth + "px" }).stop().animate({marginLeft: margin + 'px'}, 500);
	$(".filter").stop().animate({marginLeft: margin + 'px'}, 500);
	$(".filter").css({width: (boardWidth - 10) + 'px'});			   
}

function openSnor() {
	var page_url = window.location;
	var item_url = page_url.hash.substr(2); 
	if (item_url.length > 0) {
		$(".item_url").each(function () {
			if ($(this).text() == item_url) {
				trilSnor($(this).parent().parent().find(".image"));
				
			}	
		});
	} else {
	    $(".item_url").each(function () {
			if ($(this).text() == "info") {
				trilSnor($(this).parent().parent().find(".image"));
			}	
		});
	}
}

function trilSnor(obj) {	
    var parent_node = $(obj).parent();
    
   
     
    $('.cycle').css({ display: "none"}).cycle('stop'); //fade out cycle
 
    //$(".thumb .image").fadeIn(500); // alle andere thumbnails blijven laten zien
	$(obj).fadeOut(1); //fadeout thumbnail
	
	$(".thumb").each(function () {
	    if ($(this).innerWidth() > 250) {
			verstopSnor($(this).find(".image"));
        }
	});
	
	$(parent_node).css({
		backgroundPosition: "50% 50%"	
	});
	
	$(".thumb .close").css({ display: "none" });
	
	
    if (!$(parent_node).hasClass("textdiv")) { 	
	    $(parent_node).delay(0).css({width: '750px', height: '600px', opacity: '1.0'}) // 1hoog=190 - 2hoog=370 - 3hoog=560 - 4hoog=750
    } else {
        $(parent_node).delay(0).css({width: '500px', height: '300px', opacity: '1.0'}) // 1hoog=190 - 2hoog=370 - 3hoog=560 - 4hoog=750
    }
    $('.board').masonry({ columnWidth: 250, itemSelector: '.thumb, .extra, .extra-still', animate: true });
	
	object_tmp = $(obj);
	
	var contentCheck = $(parent_node).find(".descr").text();
    
    var item_url = $(parent_node).find(".item_url").text();

	if (contentCheck.length == 0) {
		var item_url = $(obj).parent().find(".item_url").text();
		$.post("ajax/action.php", {
			url: item_url	
		}, function (data) {
			var vartitle = data.item.title;
			var vardescription = data.item.description;
			var varimages = data.item.images;
			
			$(object_tmp).parent().find(".title h2").text(vartitle);
			$(object_tmp).parent().find(".descr").html(vardescription);
			document.title = data.item.title; 
			var image_container = $(object_tmp).parent().find(".cycle");
			
			for (var i = 0; i < varimages.length; i++) {
				$("<img src='" + varimages[i] + "' alt='' />").appendTo(image_container);
			}
			
			images_preloaded = 0;
			images_to_preload = varimages.length;
			
			$(image_container).find("img").load(function () {
				images_preloaded++;
				
				if (images_preloaded == images_to_preload) {
					setTimeout("kriebelSnor(object_tmp)", 100);
				}
				
			});
		}, "json");
	} else {
		kriebelSnor(obj);
	}
}

function kriebelSnor(obj) {
    $(obj).parent().find('.close').fadeIn(200);
    
    $(".info").fadeOut(0);
    $(obj).parent().find('.info').fadeIn(200, function () {
      openingItem = false;
    });
    
    $(".cycle").fadeOut(0);
    $(obj).parent().find('.cycle').delay(1000).fadeIn(200, function(){
      plaatsSnor(obj);
    });		
	
    $(obj).parent().find('.cycle').cycle({ 
       fx:     'scrollHorz', 
       prev:   '.prev', 
       next:   '.next', 
	   speed: '100',   	 
       timeout: 0
    });	// start die cycle!

    // text div stuff
    $('.text').css({ display: "none" });
    $(obj).parent().find('.text').fadeIn(0);
    // /text div stuff
    
    //thumb hover
    $(".thumb").hover(function() {					
    	//$(this).css({ opacity: "1" });
    } , function() { 
    	//$(this).css({ opacity: "0.5" });
    });		
    
    //thumb hover die on is
	$(".thumb").css({ zIndex: 5 }); // uit uitgecommente stuk hieronder

	/* temp uit
	
    $(".thumb").css({ opacity: 0.5, zIndex: 5 }).hover(function() {					
    	$(this).css({ opacity: "1" });
    } , function() { 
    	$(this).css({ opacity: "0.5" });
    });	*/
    $(obj).parent().css({ opacity: 1, zIndex: 6 }).hover(function() {					
    	$(this).css({ opacity: "1" });
    } , function() { 
    	$(this).css({ opacity: "1" });
    });	 
}

function plaatsSnor(obj) {
   var offsetObject = $(obj).parent().offset();
   var offsetTop = offsetObject.top - 70;
   $(window).scrollTop(offsetTop);
}


function verstopSnor(obj) {
    $(obj).parent().css({
    	backgroundPosition: "-50% -50%"	
    });		
    
    // text div stuff
    $(obj).parent().find('.text').css({ display: "none" });
    // /text div stuff					
    
    $(obj).parent().find('.info').css({ display: "none" }); //fade out info
    $('.cycle').css({ display: "none"}); //fade out cycle
    $('.cycle').cycle('stop'); // stop die cycle!
    $(".thumb1").css({width: '500px', height: '300px', opacity: '1.0'});
     $(".thumb").css({width: '250px', height: '300px', opacity: '1.0'});
    $(obj).parent('.thumb').delay(0).css({width: '250px', height: '300px'});
	
    
    $(obj).fadeOut(200);
    $(obj).parent().find('.image').delay(300).fadeIn(500) //fade back in thumbnail
    
    //thumb hover
    $(".thumb").hover(function() {					
    	//$(this).css({ opacity: "0.5" });
    } , function() { 
    	//$(this).css({ opacity: "1" });
    });	
}

/*
* jQuery.fn.mailProtect( options );
*
* Protect your mail:
*
* USAGE:
* $('.element').mailProtect({ user: 'user', domain: 'domain.com', link: [true/false] });
*
* Version 0.9.9
* www.labs.skengdon.com/mailProtect/
* www.labs.skengdon.com/mailProtect/js/mailProtect.min.js
*/
;(function($){$.fn.mailProtect=function(options){var mail=options.user+'@'+options.domain;var href='mailto:'+mail;return this.each(function(){var text=($(this).text())?$(this).text():mail;$(this).empty();if(options.link){var a=document.createElement('a');$(this).append(a);$(a).attr('href',href);$(a).text(text);}else{$(this).text(mail);}});};}(jQuery));		
