/*
 * LazyLoader - A jquery extension to render images and html content as the user scrolls down the page.
 *
 * Copyright (c) 2011 Michael Collins
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Project home:
 *  http://ivorycity.com/blog/
 *
 */

( function($) {

var settings = {
	threshold: 0
};
	
var scrolling_timer = false;
var lazy_scroll_f = function()
{
	//Prevent scroll stacking
//		if (!scrolling)
//		{
//			scrolling = true;
		
		clearTimeout(scrolling_timer);
		//don't unlock for 250ms
		scrolling_timer = setTimeout( function()
		{
			var d = $(window).data('lazyloaders');
			if (!d || !d.length)
			{
				$(window).unbind('scroll', lazy_scroll_f);
				return;
			}
			var new_d = [];
//				alert(d.length);
			$( d ).each( function(i)
			{
				var s;
				var r = false
				if ( this.e && $.inviewport( this.e, s = $.extend({}, settings, this.s)) )
				{
					if (typeof s.show == 'function') r = s.show.call(this.e);
					else r = $(this.e).showComment();
				}
				if (r === false) new_d.push(this);
			});
			$(window).data('lazyloaders', new_d);
		
//				scrolling = false;
		}, 250);
				
//		}
};

jQuery.fn.extend(
{	
	lazyLoad: function(s)
	{
		var d = $(window).data('lazyloaders');
		if (!d) d = [];
		
		this.each( function() { d.push({e: this, s: s}); } );
		
		$(window).data('lazyloaders', d );
		
		// rebind and trigger
		$(window).unbind('scroll', lazy_scroll_f).bind('scroll', lazy_scroll_f).trigger('scroll');
	},
	
	getComment: function()
	{
		return this.each(function()
		{
			var child = this.firstChild;
			if (child && child.nodeType === 8) // 8 is a comment node
			{
				return child.nodeValue;
			}
		});
	},
	
	showComment: function()
	{
		return this.each(function()
		{
			var child = this.firstChild;
			if (child && child.nodeType === 8) // 8 is a comment node
			{
				this.innerHTML = ''; // Fixes IE sometimes breaking with replaceWith() call
				var $node = $(child.nodeValue);//.hide();
				$(this).replaceWith($node);
//				$node.fadeIn();	// messes up scripts
			}
		});
	}

});

//$(function() {
//	$(window).scroll(lazy_scroll_f).trigger('scroll');
//});

//
///*
// * Viewport - jQuery selectors for finding elements in viewport
// *
// * Copyright (c) 2008-2009 Mika Tuupola
// *
// * Licensed under the MIT license:
// *   http://www.opensource.org/licenses/mit-license.php
// *
// * Project home:
// *  http://www.appelsiini.net/projects/viewport
// *
// */
//    $.belowthefold = function(element, settings) {
//        var fold = $(window).height() + $(window).scrollTop();
//        return fold <= $(element).offset().top - settings.threshold;
//    };
//
//    $.abovethetop = function(element, settings) {
//        var top = $(window).scrollTop();
//        return top >= $(element).offset().top + $(element).height() - settings.threshold;
//    };
//
//    $.rightofscreen = function(element, settings) {
//        var fold = $(window).width() + $(window).scrollLeft();
//        return fold <= $(element).offset().left - settings.threshold;
//    };
//
//    $.leftofscreen = function(element, settings) {
//        var left = $(window).scrollLeft();
//        return left >= $(element).offset().left + $(element).width() - settings.threshold;
//    };
//
//    $.inviewport = function(element, settings) {
//        return !$.rightofscreen(element, settings) && !$.leftofscreen(element, settings) && !$.belowthefold(element, settings) && !$.abovethetop(element, settings);
//    };
//
//    $.extend($.expr[':'], {
//        "below-the-fold": function(a, i, m) {
//            return $.belowthefold(a, {threshold : 0});
//        },
//        "above-the-top": function(a, i, m) {
//            return $.abovethetop(a, {threshold : 0});
//        },
//        "left-of-screen": function(a, i, m) {
//            return $.leftofscreen(a, {threshold : 0});
//        },
//        "right-of-screen": function(a, i, m) {
//            return $.rightofscreen(a, {threshold : 0});
//        },
//        "in-viewport": function(a, i, m) {
//            return $.inviewport(a, {threshold : 0});
//        }
//    });

})(jQuery);
