﻿$(function(){
	var start = true;
	$(window).bind('scroll resize', function(){
		if (!start) return;
		if (!$('body').imageDelayLoad().data('imagelength')) start = false;
	}).scroll();
});

$.fn.imageDelayLoad = function(bool, func){
	var scrollTop = $(window).scrollTop(), sh = scrollTop+$(window).height();
	if (isFunction(bool)) {
		func = bool;
		bool = undefined;
	}
	this.data('imagelength', this.find('img[vsrc]').each(function(i){
		var $this = $(this).css({zoom:1}), top = $this.offset().top, src;
		if ((top+$this.height() > scrollTop && top < sh) || bool === false) {
			src = $this.attr('vsrc');
			$this.removeAttr('vsrc').css({opacity:0}).bind('load', function(){ 
				$this.stop().animate({opacity:1}); 
				isFunction(func) && func.call($this);
			}).attr({src:src});
		}
	}).length);
	return this;
}


