/* Author: Brogramming.com */

$(function(){
	
	/* lazy load */
	 $("img.lazy:visible").lazyload({
		effect: "slideDown"
	 });


	/* adjust header on scroll */
	/*$('article').bind('inview', function (event, visible) {
	  if (visible == true) {
		// element is now visible in the viewport
		$(this).addClass("inview");
	  } else {
		// element has gone out of viewport
		$(this).removeClass("inview");
	  }
	});
	var targety = $(window).height() / 2;*/
	
	
	/* adjust header on scroll */
	function onScroll() {
		var $articlesInview = $("article:in-viewport");
		var articlesInviewSize = $articlesInview.size();
		if (articlesInviewSize == 1) {
			title = $articlesInview.find("header:not(.big) h2").text();
		} else if (articlesInviewSize > 1) {
			title = $articlesInview.eq(1).find("header:not(.big) h2").text()
		}
		if (title != "" && $('#title').text() != title) {
			$('#title').text(title);
			_gaq.push(['_trackPageview', location.pathname + "#" + escape(title)]);
		}
 		// test
		/*var pos = $("#info").offset();
		var top = pos.top - $(window).scrollTop();
		if (top < targeth) {
			$('#title').text("aa");
		} else {
			$('#title').text("Atelier Varga");
		}
		$('#title').text( top );*/
	};
	// Bind the throttled handler to the scroll event.
	var targeth = $(window).height() / 2;
	var title = $('#title').text();
	$(window).scroll( $.throttle( 250, onScroll ) );
	//$(window).scroll();
	
	
	/* img slide */
	$("article figure").each(function(){
		var $imgs = $(this).children("img");
		if ($imgs.size() > 1) {
			$(this).addClass("slider");
			$(this).click(function(event){
				$currentimg = $imgs.filter(':visible').hide();
				//css("display","block").height($currentimg.height()).
				if ($imgs.index($currentimg) < $imgs.size()-1) {
					//$currentimg.next().show().lazyload();
					$currentimg.next().css("display","block").height($currentimg.height()).lazyload({effect: "fadeIn"});
				} else {
					//$imgs.first().show();
					$imgs.first().css("display","block").height($currentimg.height()).lazyload({effect: "fadeIn"});
				}
			});
		}
	});
	
	/* external links */
	$("a[href^='http://']").each(function(e){
	    $(this).attr("target","_blank");
	});

});
