(function($){
	$.ImgDynamic = {
		settings: {
			selector: '.img-dynamic',
			urlAttribute: 'img-dynamic-url'
		},
		elements: [],
		bottom: [],
		top: [],
		total: 0
	}
	$.extend($.ImgDynamic, {
		show: function(element){		
			var self = this;
			var url = element.attr(self.settings.urlAttribute);			
			element.css({background: "url(" + url + ") no-repeat center"});		
		},
		watch: function(){			
			var self	= this;
			var length	= self.top.length;			

			if(length <= 0){
				return;
			}		

			var s = $(window).scrollTop();
			var h = $(window).height();
			var bt = s + h + 100;
			var gc = [];			

			for(var i=0; i < length; i++){				
				if(self.top[i] < bt && self.bottom[i] > s){					
					self.show(self.elements[i]);
					gc.push(i);
				}
			}	
		},
		init: function(selector){			
			var self		= this;
			var selector	= selector || this.settings.selector;
			$(selector).each(function(index, value){				
				self.elements.push($(this));
				var offset = $(this).offset();
				self.bottom.push(offset.top + $(this).innerHeight());
				self.top.push(offset.top);
			});	

			self.watch();
			$(window)
			.scroll(function(){self.watch()})
			.resize(function(){self.watch()});
		}
	});	
})(jQuery);

