/*
Copyright 2010, KISSY UI Library v1.1.0
MIT Licensed
build: 896 Jul 22 10:00
*/
KISSY.add("datalazyload",function(c,q){function h(a,b){if(!(this instanceof h))return new h(a,b);if(b===q){b=a;a=[m]}c.isArray(a)||(a=[c.get(a)||m]);this.containers=a;this.config=c.merge(r,b);this._init()}var f=c.DOM,k=c.Event,l=window,m=document,n={AUTO:"auto",MANUAL:"manual"},o="scroll",p="resize",r={mod:n.MANUAL,diff:"default",placeholder:"none"};c.augment(h,{_init:function(){this.threshold=this._getThreshold();this._filterItems();this._getItemsLength()&&this._initLoadEvent()},_filterItems:function(){var a=
this.containers,b,d,e,g=[],i=[];b=0;for(d=a.length;b<d;++b){e=c.query("img",a[b]);g=g.concat(c.filter(e,this._filterImg,this));e=c.query("textarea",a[b]);i=i.concat(c.filter(e,this._filterArea,this))}this.images=g;this.areas=i},_filterImg:function(a){var b=a.getAttribute("data-lazyload-src"),d=this.threshold,e=this.config.placeholder;if(this.config.mod===n.MANUAL){if(b){if(e!=="none")a.src=e;return true}}else if(f.offset(a).top>d&&!b){f.attr(a,"data-lazyload-src",a.src);if(e!=="none")a.src=e;else a.removeAttribute("src");
return true}},_filterArea:function(a){return f.hasClass(a,"ks-datalazyload")},_initLoadEvent:function(){function a(){d||(d=setTimeout(function(){b();d=null},100))}function b(){e._loadItems();if(e._getItemsLength()===0){k.remove(l,o,a);k.remove(l,p,a)}}var d,e=this;k.on(l,o,a);k.on(l,p,function(){e.threshold=e._getThreshold();a()});e._getItemsLength()&&c.ready(function(){b()})},_loadItems:function(){this._loadImgs();this._loadAreas()},_loadImgs:function(){this.images=c.filter(this.images,this._loadImg,
this)},_loadImg:function(a){var b=this.threshold+f.scrollTop();if(f.offset(a).top<=b)this._loadImgSrc(a);else return true},_loadImgSrc:function(a,b){b=b||"data-lazyload-src";var d=a.getAttribute(b);if(d&&a.src!=d){a.src=d;a.removeAttribute(b)}},_loadAreas:function(){this.areas=c.filter(this.areas,this._loadArea,this)},_loadArea:function(a){var b=f.offset(a).top;if(!b&&f.css(a,"none")=="none")b=f.offset(a.parentNode).top;if(b<=this.threshold+f.scrollTop())this._loadAreaData(a.parentNode,a);else return true},
_loadAreaData:function(a,b){b.style.display="none";b.className="";var d=f.create("<div>");a.insertBefore(d,b);f.html(d,b.value,true)},_getThreshold:function(){var a=this.config.diff,b=f.viewportHeight();return a==="default"?2*b:b+a},_getItemsLength:function(){return this.images.length+this.areas.length},loadCustomLazyData:function(a,b,d){var e=this,g,i;c.isArray(a)||(a=[c.get(a)]);c.each(a,function(j){switch(b){case "textarea-data":if((g=c.get("textarea",j))&&f.hasClass(g,d||"ks-datalazyload-custom"))e._loadAreaData(j,
g);break;default:i=j.nodeName==="IMG"?[j]:c.query("img",j);c.each(i,function(s){e._loadImgSrc(s,d||"data-lazyload-src-custom")})}})}});c.mix(h,h.prototype,true,["loadCustomLazyData","_loadImgSrc","_loadAreaData"]);c.DataLazyload=h});

