/*
Copyright 2012, KISSY UI Library v1.30rc
MIT Licensed
build time: Jul 11 21:21
*/
KISSY.add("datalazyload",function(e,g,i,u,n){function p(a,b){var b=b||m,c=a.getAttribute(b);c&&a.src!=c&&(a.src=c,a.removeAttribute(b))}function q(a,b){for(var c=[],d=0;d<a.length;d++)e.inArray(a[d],b)||c.push(a[d]);return c}function r(a,b){a.style.display=l;a.className="";var c=g.create("<div>");a.parentNode.insertBefore(c,a);g.html(c,a.value,b)}function v(a){return g.hasClass(a,s)}function j(a,b){if(!(this instanceof j))return new j(a,b);b===n&&(b=a,a=[o]);e.isArray(a)||(a=[g.get(a)||o]);b=b||{};
b.containers=a;j.superclass.constructor.call(this,b);this._callbacks={els:[],fns:[]};this._init()}function t(a,b){var c,d,f,h;c=Math.max(a.top,b.top);d=Math.min(a.bottom,b.bottom);f=Math.max(a.left,b.left);h=Math.min(a.right,b.right);return d>=c&&h>=f}var k=e.Env.host,o=k.document,m="data-ks-lazyload",s="ks-datalazyload",l="none";j.ATTRS={mod:{value:"manual"},diff:{value:"default"},placeholder:{value:l},execScript:{value:!0},containers:{valueFn:function(){return[o]}},autoDestroy:{value:!0}};e.extend(j,
u,{_init:function(){this._filterItems();this._initLoadEvent()},_filterItems:function(){var a=this.get("containers"),b,c,d,f=[],h=[];b=0;for(c=a.length;b<c;++b)d=q(g.query("img",a[b]),f),f=f.concat(e.filter(d,this._filterImg,this)),d=q(g.query("textarea",a[b]),h),h=h.concat(e.filter(d,v,this));this._images=f;this._areaes=h},_filterImg:function(a){var b=a.getAttribute(m),c=this.get("placeholder");if("manual"===this.get("mod")){if(b)return c!==l&&(a.src=c),!0}else if(!b&&!this._checkElemInViewport(a))return g.attr(a,
m,a.src),c!==l?a.src=c:a.removeAttribute("src"),!0},_initLoadEvent:function(){var a=this,b=a.get("autoDestroy"),c=function(){a._loadItems();b&&0===a._getItemsLength()&&a.destroy()},d=e.buffer(c,100,this);i.on(k,"scroll",d);i.on(k,"touchmove",d);i.on(k,"resize",d);e.each(a.get("containers"),function(a){9!=a.nodeType&&(i.on(a,"scroll",d),i.on(a,"touchmove",d))});a._loadFn=d;a._getItemsLength()&&e.ready(c)},_loadItems:function(){this._loadImgs();this._loadAreas();this._fireCallbacks()},_loadImgs:function(){this._images=
e.filter(this._images,this._loadImg,this)},_loadImg:function(a){if(this._checkElemInViewport(a))p(a);else return!0},_loadAreas:function(){this._areaes=e.filter(this._areaes,this._loadArea,this)},_loadArea:function(a){if(this._checkElemInViewport(a))r(a,this.get("execScript"));else return!0},_fireCallbacks:function(){var a=this._callbacks,b=a.els,c=a.fns,d,f,h,e=[],g=[];for(d=0;(f=b[d])&&(h=c[d++]);)this._checkElemInViewport(f)?h.call(f):(e.push(f),g.push(h));a.els=e;a.fns=g},addCallback:function(a,
b){var c=this._callbacks;if((a=g.get(a))&&e.isFunction(b))c.els.push(a),c.fns.push(b);this._fireCallbacks()},removeCallback:function(a,b){var c=this._callbacks,d=[],f=[],h=c.fns,a=g.get(a);e.each(c.els,function(c,e){c==a&&(b===n||b==h[e])||(d.push(c),f.push(h[e]))});c.fns=f;c.els=d},addElements:function(a){e.isArray(a)||(a=[a]);var b=this._images||[],c=this._areaes||[];e.each(a,function(a){var f=a.nodeName.toLowerCase();"img"==f?e.inArray(a,b)||b.push(a):"textarea"==f&&(e.inArray(a,c)||c.push(a))});
this._images=b;this._areaes=c},removeElements:function(a){e.isArray(a)||(a=[a]);var b=[],c=[];e.each(this._images,function(c){e.inArray(c,a)||b.push(c)});e.each(this._areaes,function(b){e.inArray(b,a)||c.push(b)});this._images=b;this._areaes=c},_getBoundingRect:function(a){var b,c,d;a!==n&&!e.isWindow(a)&&9!=a.nodeType?(b=g.outerHeight(a),c=g.outerWidth(a),d=g.offset(a),a=d.left,d=d.top):(b=g.viewportHeight(),c=g.viewportWidth(),a=g.scrollLeft(),d=g.scrollTop());var f=this.get("diff"),h=0,i="default"===
f?c:f,j=0,k="default"===f?b:f;c=a+c;b=d+b;e.isObject(f)&&(h=f.left||0,i=f.right||0,j=f.top||0,k=f.bottom||0);return{left:a-h,top:d-j,right:c+i,bottom:b+k}},_getItemsLength:function(){return this._images.length+this._areaes.length+this._callbacks.els.length},_checkElemInViewport:function(a){var a=g.css(a,"display")===l?a.parentNode:a,b=g.offset(a),c=!0,d;a:{var f=a;d=this.get("containers");for(var e=0;e<d.length;e++)if(9!=d[e].nodeType&&d[e].contains(f)){d=d[e];break a}d=0}f=this._getBoundingRect();
e=b.left;b=b.top;a={left:e,top:b,right:e+g.outerWidth(a),bottom:b+g.outerHeight(a)};d&&(c=this._getBoundingRect(d),c=t(c,a));a=t(f,a);return c&&a},destroy:function(){var a=this._loadFn;i.remove(k,"scroll",a);i.remove(k,"touchmove",a);i.remove(k,"resize",a);e.each(this.get("containers"),function(b){9!=b.nodeType&&(i.remove(b,"scroll",a),i.remove(b,"touchmove",a))});this._callbacks.els=[];this._callbacks.fns=[];this._images=[];this._areaes=[];this.fire("destroy")}});j.loadCustomLazyData=function(a,
b,c){var d;"img-src"===b&&(b="img");e.isArray(a)||(a=[g.get(a)]);e.each(a,function(a){switch(b){case "img":d="IMG"===a.nodeName?[a]:g.query("img",a);e.each(d,function(a){p(a,c||m+"-custom")});break;default:g.query("textarea",a).each(function(a){g.hasClass(a,c||s+"-custom")&&r(a,!0)})}})};return e.DataLazyload=j},{requires:["dom","event","base"]});
