(function(a,q){a.widget("mobile.lazyloader",a.mobile.widget,{_defaultOptions:{threshold:a(window).height(),retrieve:20,retrieved:20,bubbles:!1,offset:0},_defaultParameters:{retrieve:20,retrieved:20,offset:0},_defaultSettings:{pageId:"",templateType:"",templatePrecompiled:!1,templateId:"",template:"",mainId:"",progressDivId:"",moreUrl:"",clearUrl:"",JSONP:!1,JSONPCallback:""},_defaultSelectors:{main:"ul",single:"ul li",bottom:'[data-role="list-divider"]'},_handleScrollStartJustFired:!1,_handleScrollStopJustFired:!1, _mouseWheelEventJustFired:!1,_handleScrollStartTimeoutId:null,_handleScrollStopTimeoutId:null,_mouseWheelTimeoutId:null,_instances:{},_moreOutstandingPageId:null,_parameters:null,_settings:null,_selectors:null,timeoutOptions:{mousewheel:350,scrollstart:500,scrollstop:50,showprogress:200,scrolldown:400,immediately:0},_widgetName:"lazyloader",_widgetState:{busy:!1,done:!1},_create:function(){this._initialize(this._defaultOptions,this._defaultSettings,this._defaultParameters,this._defaultSelectors); this._bind()},_init:function(){},_initialize:function(b,d,c,e){if("undefined"!=typeof b&&""!=b&&(this._widgetState.busy=!1,this._widgetState.done=!1,this._settings=a.extend(!0,this._settings,this._defaultSettings),this._settings=a.extend(!0,this._settings,d),"undefined"!==typeof this._settings.mainId&&""!==this._settings.mainId&&(this._defaultSelectors.main="#"+this._settings.mainId,this._defaultSelectors.single="#"+this._settings.mainId+" li",this._defaultSelectors.bottom='[data-role="list-divider"]'), "undefined"!==typeof this._settings.pageId&&""!==this._settings.pageId&&(this._settings.totalHeight=a("#"+this._settings.pageId).height()),this._selectors=a.extend(!0,this._selectors,this._defaultSelectors),this._selectors=a.extend(!0,this._selectors,e),this._parameters=a.extend(!0,this._parameters,this._defaultParameters),this._parameters=a.extend(!0,this._parameters,c),this.options=a.extend(!0,this.options,this._defaultOptions),this.options=a.extend(!0,this.options,b),b=d.pageId,"undefined "!=typeof b&& ""!=b&&!this._instances[b])){if(("undefined"==typeof this._settings.template||""==this._settings.template)&&"undefined"!=typeof this._settings.templateId&&""!=this._settings.templateId)d=a("#"+this._settings.templateId).html(),c="",e=this._settings.templatePrecompiled,"undefined"!=typeof this._settings.templateType&&""!=this._settings.templateType&&(c=this._settings.templateType),this._settings.template="dust"===c&&""!==d&&!e?dust.compile(d,this._settings.templateId):d;this._instances[b]=[];this._instances[b].options= a.extend(!0,{},this.options);this._instances[b].settings=a.extend(!0,{},this._settings);this._instances[b].selectors=a.extend(!0,{},this._selectors)}},_bind:function(){a("body").bind("scrollstart",a.proxy(this._handleScrollStart,this));a("body").bind("scrollstop",a.proxy(this._handleScrollStop,this));/Firefox/i.test(navigator.userAgent)?a(window).bind("DOMMouseScroll",a.proxy(this._handleMouseWheelEvent,this)):"undefined"!=typeof this._selectors&&null!=this._selectors&&""!=this._selectors&&"undefined"!= typeof this._selectors.main&&(a(this._selectors.main).attachEvent?a(window).bind("onmousewheel",a.proxy(this._handleMouseWheelEvent,this)):a(window).bind("mousewheel",a.proxy(this._handleMouseWheelEvent,this)))},_unbind:function(){a("body").unbind("scrollstart",this._handleScrollStart);a("body").unbind("scrollstop",this._handleScrollStop);/Firefox/i.test(navigator.userAgent)?a(window).unbind("DOMMouseScroll",this._handleMouseWheelEvent):"undefined"!=typeof this._selectors&&null!=this._selectors&& ""!=this._selectors&&"undefined"!=typeof this._selectors.main&&(a(this._selectors.main).attachEvent?a(window).unbind("onmousewheel",this._handleMouseWheelEvent):a(window).unbind("mousewheel",this._handleMouseWheelEvent))},destroy:function(){this._unbind();this._defaultParameters=this._defaultSettings=this._defaultOptions=this._widgetState=this._mouseWheelTimeoutId=this._handleScrollStopTimeoutId=this._handleScrollStartTimeoutId=this._mouseWheelEventJustFired=this._handleScrollStopJustFired=this._handleScrollStartJustFired= this._instances=this._parameters=this._settings=this.timeoutOptions=this.options=null;a.Widget.prototype.destroy.apply(this)},_check:function(b){var b=this.options.threshold||b,d,c,e;c=document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop;d=this._instances[this._settings.pageId]?this._instances[this._settings.pageId].settings.totalHeight:this._settings.totalHeight;e=a(window).height();return d-b<=c+e},_load:function(b){typeof this._settings.pageId!=q&&""!=this._settings.pageId&& (a(".ui-page-active").attr("id")==this._settings.pageId?!this._widgetState.busy&&!this._widgetState.done?(this._moreOutstandingPageId=this._settings.pageId,$that=this,setTimeout(function(){$that._moreOutstandingPageId==$that._settings.pageId&&($that._check($that.options.threshold)||0===b)&&a("#"+$that._settings.progressDivId).show($that.timeoutOptions.showprogress,function(){moreUrl=$that._settings.moreUrl;var b="POST",c="json",e="",n=!1,o="",h=0;$that._instances[$that._settings.pageId]?($that._parameters.retrieve= $that._instances[$that._settings.pageId].options.retrieve,$that._parameters.retrieved=$that._instances[$that._settings.pageId].options.retrieved,$that._parameters.offset=$that._instances[$that._settings.pageId].options.offset,$that._instances[$that._settings.pageId].settings.JSONP&&(n=!0,o=$that._instances[$that._settings.pageId].settings.JSONPCallback)):($that._parameters.retrieve=$that.options.retrieve,$that._parameters.retrieved=$that.options.retrieved,$that._parameters.offset=$that.options.offset); if("undefined"!=typeof $that._settings.pageId&&""!=$that._settings.pageId){var p=a("#"+$that._settings.pageId).find('[type="hidden"]');for(i=0;i0){m=$that._selectors.main;b=$that._selectors.single; n=$that._selectors.bottom;l=$that._getBottomElement(m,n);if(typeof more.data[0].html!="undefined"&&more.data[0].html!=""){e=more.data[0].html;l?a(b).last().before(e):a(m).append(e)}else{if($that._instances[$that._settings.pageId]){if(typeof $that._instances[$that._settings.pageId].settings.templateId!="undefined"&&$that._instances[$that._settings.pageId].settings.templateId!=""){f=$that._instances[$that._settings.pageId].settings.templateId;if(typeof $that._instances[$that._settings.pageId].settings.templateType!= "undefined"&&$that._instances[$that._settings.pageId].settings.templateType!="")h=$that._instances[$that._settings.pageId].settings.templateType;if(typeof $that._instances[$that._settings.pageId].settings.template!="undefined"&&$that._instances[$that._settings.pageId].settings.template!="")j=$that._instances[$that._settings.pageId].settings.template}k=$that._instances[$that._settings.pageId].settings.templatePrecompiled}else{if(typeof $that._settings.templateId!="undefined"&&$that._settings.templateId!= ""){f=$that._settings.templateId;if(typeof $that._settings.templateType!="undefined"&&$that._settings.templateType!="")h=$that._settings.templateType;if(typeof $that._settings.template!="undefined"&&$that._settings.template!="")j=$that._settings.template}k=$that._settings.templatePrecompiled}if(h!==""&&f!==""&&j!=="")if(h==="json2html"){g=more.data[0].json;l&&l.remove();a(m).json2html(g,j);l&&a(b).last().append(l)}else{g=more.data[0];switch(h){case "handlebars":j=k?Handlebars.templates[f+".tmpl"]: Handlebars.compile(j);e=j(g);break;case "icanhaz":ich.addTemplate("listitem",j);e=ich.listitem(g,true);ich.clearAll();break;case "dust":if(k)dust.render(f,g,function(b,a){e=a});else{dust.loadSource(j);dust.render(f,g,function(b,a){e=a})}break;case "dot":j=doT.template(j);e=j(g)}l?a(b).last().before(e):a(m).append(e)}}a(m).listview("refresh");g=0;d=parseInt(d);if($that._instances[$that._settings.pageId]){var o=$that._instances[$that._settings.pageId].settings.totalHeight;if(typeof $that._instances[$that._settings.pageId].settings.singleItemHeight!== "undefined")g=$that._instances[$that._settings.pageId].settings.singleItemHeight;else{g=a(b).first().next().height();$that._instances[$that._settings.pageId].settings.singleItemHeight=g}$that._instances[$that._settings.pageId].settings.totalHeight=o+g*d}else{if(typeof $that._settings.singleItemHeight!=="undefined")g=$that._settings.singleItemHeight;else{g=a(b).first().next().height();$that._settings.singleItemHeight=g}$that._settings.totalHeight=$that._settings.totalHeight+$that._settings.singleItemHeight* d}$that._instances[$that._settings.pageId].options.retrieved=$that._instances[$that._settings.pageId].options.retrieved+d;if(d<$that.options.retrieve||$that.options.retrieve=="all"){$that._widgetState.done=true;$that._triggerEvent("alldone","_load")}}else{$that._widgetState.done=true;$that._triggerEvent("alldone","_load")}a("#"+$that._settings.progressDivId).hide(250,function(){$that._widgetState.busy=false});$that._triggerEvent("doneloading","_load")}catch(p){$that._triggerEvent("error","_load", p.message);a("#"+$that._settings.progressDivId).hide(250,function(){$that._widgetState.busy=false});return false}},error:function(b){$that._triggerEvent("error","_load",b);a("#"+$that._settings.progressDivId).hide(250,function(){$that._widgetState.busy=false})},complete:function(){}})})},b)):this._widgetState.done?$that._triggerEvent("alldone","_load"):this._widgetState.busy&&$that._triggerEvent("busy","_load"):a("#"+this._settings.progressDivId).hide(250,function(){"undefined"!=typeof this._widgetState&& (this._widgetState.busy=!1)}))},_getBottomElement:function(b,d){var c=a(b).last().find(d);switch(c.length){case 2:c=c.last();break;default:c=null}return"undefined"!=typeof c&&null!=c&&""!=c&&"null"!=c?c:!1},_handleMouseWheelEvent:function(){if(!this._mouseWheelEventJustFired&&!this._handleScrollStopJustFired&&!this._handleScrollStartJustFired){this._mouseWheelEventJustFired=!0;this._load(this.timeoutOptions.mousewheel);var b=this;this._mouseWheelTimeoutId=setTimeout(function(){b._mouseWheelEventJustFired= !1},1E3)}},_handleScrollStart:function(){if(!this._mouseWheelEventJustFired&&!this._handleScrollStopJustFired&&!this._handleScrollStartJustFired){this._handleScrollStartJustFired=!0;this._load(this.timeoutOptions.scrollstart);var b=this;this._handleScrollStartTimeoutId=setTimeout(function(){b._handleScrollStartJustFired=!1},1200)}},_handleScrollStop:function(){if(!this._mouseWheelEventJustFired&&!this._handleScrollStopJustFired&&!this._handleScrollStartJustFired){this._handleScrollStopJustFired=!0; this._load(this.timeoutOptions.scrollstop);var b=this;this._handleScrollStopTimeoutId=setTimeout(function(){b._handleScrollStopJustFired=!1},1200)}},loadMore:function(b){0===b?this._load(this.timeoutOptions.immediately):this._load(this.timeoutOptions.scrolldown)},_setOption:function(b,d){this._instances[this._settings.pageId]&&this._instances[this._settings.pageId].options[b]&&(this._instances[this._settings.pageId].options[b]=d);a.Widget.prototype._setOption.apply(this,arguments)},refresh:function(b, d){if("parameters"==b){if("undefined"!=typeof this.options)for(var c in this._parameters)"undefined"!=typeof this.options[c]&&(this._parameters[c]=this.options[c])}else"parameter"==b&&(c=d,"undefined"!=typeof this.options[c]&&(this._parameters[c]=this.options[c]));c=JSON.stringify(this._parameters);this._parameters=a.parseJSON(c)},reInitialize:function(b,a,c,e){this._initialize(b,a,c,e)},reset:function(b){var d=this;a.ajax({type:"POST",url:d._settings.clearUrl,async:!0,data:"section="+b,success:function(a){parseInt(a)&& (d.options.retrieved=d._defaultOptions.retrieved,d._widgetState.done=!1,"undefined"!=typeof d._instances[b]&&delete d._instances[b],d._triggerEvent("reset","reset","All session variables for the '"+b+"' page and the lazyloader instance variables have been cleared."))},error:function(b){d._triggerEvent("error","reset",b);a("#"+d._settings.progressDivId).hide(250,function(){d._widgetState.busy=!1})}})},resetAll:function(){var b=this;a.ajax({type:"POST",url:b._settings.clearUrl,async:!0,data:"",success:function(a){if(parseInt(a)){for(pageId in b._instances)delete b._instances[pageId]; b.options.retrieved=b._defaultOptions.retrieved;b._widgetState.done=!1;b._widgetState.busy=!1;b._triggerEvent("resetall","resetAll","All session variables for all pages currently being tracked by the lazyloader have been cleared.")}}})},_triggerEvent:function(a,d,c){c=c||"";switch(a){case "error":case "resetall":this._trigger(a,{type:"lazyloader"+a,"function":d,message:c,settings:this._settings,options:this.options,parameters:this._parameters});break;default:this._trigger(a,{type:"lazyloader"+a,"function":d, message:c,pageId:this._settings.pageId,mainId:this._settings.mainId,loaded:this.options.retrieved})}}});a(document).bind("pagecreate create",function(b){a.mobile.lazyloader.prototype.enhanceWithin(b.target)})})(jQuery);