﻿function myCallback() {
	$("#loading").hide();
	$("#images").show();
}
function backToTop(link) {
	if (jQuery(window).scrollTop() > 0) {
		link.fadeIn(200);
	} else {
		link.fadeOut(200);
	}
}
$(document).ready(function() {
	/*$('.tooltip').tipsy({ gravity: $.fn.tipsy.autoNS, fade: true });
	
	$("div.inner-page").css({
		opacity: 1,
		visibility: "visible"
	})
	$("#initial-loading").hide();*/

	// Fancy a fancybox?
	$("a.fancy").fancybox({
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic',
		'easingIn'      : 'easeOutBack',
		'easingOut'     : 'easeInBack',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	true
	});
	
	if(!$.browser.msie || ($.browser.msie && $.browser.version>7)) {
		$("a#logo img").hover(
			function() {
				$(this).stop().animate({"opacity": "1"}, "fast");
			},
			function() {
				$(this).stop().animate({"opacity": "0"}, "fast");
			}
		);
	
		// Back to top link
		var backToTopLink = jQuery('a#backtotop');
		backToTopLink.hide();
		
		jQuery(window).scroll(function() {
			backToTop(backToTopLink);
		});
		backToTopLink.click(function() {
			jQuery('html, body').stop().animate({scrollTop:0}, 500);
			return false;
		});
		
		// Isotope initialisation
		var $container = $('#content');
		
		$("#content article").find("img").imagesLoaded(function() {
			$("#initial-loading").show();
			$container.isotope({
				itemSelector : 'article'
			});
			$container.css({
				opacity: 0,
				visibility: "visible"
			}).animate({
				opacity: 1
			});
			$("#initial-loading").fadeOut();
		})
		
		// Infinite Scroll
		$container.infinitescroll({
			navSelector  : 'a#showmore:last', 
			nextSelector : 'a#showmore:last',
			itemSelector : 'article',
			loadingImg   : "http://ogynche.com/img/icons/loading.gif", 
			loadingText  : "Loading new posts...",
			donetext     : "You've reached the end of the universe!" ,
			animate		 : true,
		},
		function(newElements) {
			$container.imagesLoaded(function(){
				$container.isotope('appended', $(newElements));
				$('a#showmore').removeClass('active');
				$('a#showmore').addClass('normal');
				$("a.fancy").fancybox({
					'transitionIn'	: 'elastic',
					'transitionOut'	: 'elastic',
					'easingIn'      : 'easeOutBack',
					'easingOut'     : 'easeInBack',
					'speedIn'		:	600, 
					'speedOut'		:	200, 
					'overlayShow'	:	true
				});
			});
		});
		
		$(window).unbind('.infscr');
		
		$('a#showmore').click(function(){
			$(document).trigger('retrieve');
			$('a#showmore').removeClass('normal');
			$('a#showmore').addClass('active');
			return false;
		});
		
		$(document).ajaxError(function(e,xhr,opt){
			if (xhr.status == 404) $('a#showmore').remove();
		});
		
		$("#content").find('img').batchImageLoad({
			loadingCompleteCallback: myCallback,
			imageLoadedCallback: imageLoaded
		});
	}
});
