Element.addMethods({
	img_src : function(element) {alert($(element).getAttribute("_src"))},

	lazyload: function(element, options)
	{
		function $restore()
		{
			// this function restores the original image source; called when above the fold
			//if ( true === $(element).hasAttribute('_src') ) {
				$(element).writeAttribute({ src: $(element).readAttribute('_src') }).writeAttribute({ '_src' :  0 });
			//}
		}
		function $scroll()
		{
			// this function returns the amount the page is scrolled vertically
			var scroll_y = self.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
			return parseInt(scroll_y);
		}
		function $height()
		{
			// this function returns the height of the viewport
			var window_height = window.innerHeight || document.documentElement.clientHeight;
			return parseInt(window_height);
		}

		var element     = $(element);
		var options     = Object.extend({
			threshold   : 0,//临界高度
			event       : 'scroll',
			frequency   : 0.1
		}, options || {});

		var offset      = $(element).cumulativeOffset()[1];
		var activate_on = (offset - options.threshold) - $height();

		if ( 'scroll' === options.event )
		{
			new PeriodicalExecuter(function($executor)
			{
				if ( activate_on <= $scroll() )
				{
					$restore(); $executor.stop();
				}
			}, options.frequency);
		}
		else
		{
			$(element).observe(options.event, function(event)
			{
				$restore(); $(element).stopObserving();
			});
		}

		return $(element);
	}
});

document.observe('dom:loaded', function() {
	//$$('img[_src]').invoke('lazyload');
});
