$(function(){
	/* Fancybox */
	$(".galerie a").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'				:	600, 
		'speedOut'			:	200	
	});	
	/* DOM Populate */
	var videos = ["epJnraH2YFM","LGSlymWjAzk","3v1sm4Do5QU","m5wD-Ya9lzU"],
			photos = {
				paths : ["quest-ce","3pp","marinewertz"],
				counts : [12,5,3]
			},
			rand = Math.round(Math.random()*10%(videos.length-1));
	// Video load	
	$("#videos article iframe").attr("src","https://www.youtube-nocookie.com/embed/"+videos[rand]+"?border=0&modestbranding=0&rel=0&wmode=transparent");	
	// Photo load
	for(var i=0; i < photos.paths.length; i++){
		for(var j=2; j < photos.counts[i]; j++){
			var uri = photos.paths[i]+"/"+photos.paths[i],
					path = (j<10)?(uri+"_0"+j):(uri+"_"+j);
			$("#photos article").append('<figure class="hide"><a href="http://www.marinewertz.com/illustrations/'+path+'.jpg" rel="gal"><img src="http://www.marinewertz.com/illustrations/'+path+'.jpg" alt="'+path+'" width="100%" height="auto" /></a></figure>');			
		}
	}
	var hrefWidth = $("#photos article a:first").innerWidth();
	$("#photos article a").each(function(){$(this).width(hrefWidth);});
	if($(document).innerWidth()<640){
		$("#videos iframe").each(function(){$(this).height(hrefWidth*9/16);});
	}
	
	/* Carrousel */
	$('.carrousel').each(function(){
		var $this = $(this),
				nbElem = $this.children().size(),
				width = $this.outerWidth(),
				totalWidth = width*nbElem,
				timer = setInterval(function(){
					nextSlide();
				}, 10000);
		function nextSlide(){
			$this.children().eq(0).fadeOut("slow",function(){
				var $elem = $(this);
				$elem.next().fadeIn("slow", function(){
					$elem.detach();
					$elem.appendTo($this);
				});				
			});		
		}		
		$this.css({'width':totalWidth});
		$this.children().each(function(){
			var $elem = $(this);
			if($elem.is("figure")){
				if($(document).innerWidth()>639){
					$elem.children("a").fancybox({
						'transitionIn'	:	'elastic',
						'transitionOut'	:	'elastic',
						'speedIn'				:	600, 
						'speedOut'			:	200,
						'onStart'				: function(){						
							clearInterval(timer);
						},
						'onClosed'			:	function(){
							timer = setInterval(function(){
								nextSlide();
							}, 7000);					
						}
					});
				}else{
					$elem.children("a").on("click", function(e){e.preventDefault();});
				}
			}else if($elem.is("iframe")){
				$elem.on("click", function(){
					console.log("click iframe");
					clearInterval(timer);
				});
			}
		});
	});	
});
$(window).load(function(){
	/* Masonry */
	$('.galerie').masonry({
		itemSelector : '.galerie_photo'
	});
});
