/*!@license
 * Infragistics.Web.ClientUI Grid LoadOnDemand 13.2.20132.355
 *
 * Copyright (c) 2011-2013 Infragistics Inc.
 *
 * http://www.infragistics.com/
 *
 * Depends on:
 * Depends on:
 *	jquery-1.4.4.js
 *	jquery.ui.core.js
 *	jquery.ui.widget.js
 *	infragistics.ui.grid.framework.js
 *	infragistics.ui.editors.js
 *	infragistics.ui.shared.js
 *	infragistics.dataSource.js
 *	infragistics.util.js
 */
if(typeof jQuery!=="function"){throw new Error("jQuery is undefined")}(function($){$.widget("ui.igGridLoadOnDemand",{options:{type:null,chunkSize:25,recordCountKey:null,chunkSizeUrlKey:null,chunkIndexUrlKey:null,defaultChunkIndex:0,currentChunkIndex:0,loadTrigger:"auto",loadMoreDataButtonText:$.ig.GridLoadOnDemand.locale.loadMoreDataButtonText},events:{rowsRequesting:"rowsRequesting",rowsRequested:"rowsRequested"},_loadingIndicator:null,_injectGrid:function(gridInstance,isRebind){this.grid=gridInstance;this._checkNotSupportedScenarios();this.options.currentChunkIndex=0;if(this.options.type===null){this.options.type=this.grid._inferOpType()}this.grid.dataSource.settings.paging.type=this.options.type||"remote";this._defaultChunkSize=parseInt(this.options.chunkSize,10)*(this.options.defaultChunkIndex||1);this.grid.dataSource.settings.paging.pageSize=this._defaultChunkSize;if(this.options.chunkSizeUrlKey!==null&&this.options.chunkIndexUrlKey){this.grid.dataSource.settings.paging.pageSizeUrlKey=this.options.chunkSizeUrlKey;this.grid.dataSource.settings.paging.chunkIndexUrlKey=this.options.chunkIndexUrlKey}if(this.options.recordCountKey!==null){this.grid.dataSource.settings.responseTotalRecCountKey=this.options.recordCountKey}this.grid.dataSource.settings.paging.enabled=true;this.grid.dataSource.settings.paging.appendPage=true;if(this.options.loadTrigger==="auto"){this._verticalScrollHandler=$.proxy(this._probeForNextPage,this)}this._appendRecordsHandler=$.proxy(this._appendRecords,this);this.grid.element.bind("iggridrowsrendered",this._rowsRenderingHandler)},_dataRendered:function(){var scrollId="#"+this.grid.scrollContainer()[0].id,buttonId,container;this.grid.scrollContainer().css("background-color","white");this.options.currentChunkIndex=0;this.grid.dataSource.settings.paging.pageSize=parseInt(this.options.chunkSize,10);this._originalDataSourceCallback=this.grid.dataSource.settings.callback;this._initLoadingIndicator();if(this.options.loadTrigger==="auto"){$(scrollId).unbind("scroll",this._verticalScrollHandler);$(scrollId).bind("scroll",this._verticalScrollHandler);this._probeForNextPage()}this._requestPending=false;if(this.options.loadTrigger==="button"){if(!this._buttonRow){buttonId=this.grid.element.id+"_loadMoreButton";container=this.grid.options.height?this.grid.scrollContainer():this.grid.container();this._buttonRow=container.append("<div class='ui-iggrid-loadmorebutton'><input type='button' id='"+buttonId+"'></input></div>");$("#"+buttonId).igButton({labelText:this.options.loadMoreDataButtonText,click:$.proxy(this._nextChunk,this),width:"100%"})}}if(this._loadingIndicator){this._hideLoading()}},_checkNotSupportedScenarios:function(){if(this.options.loadTrigger==="auto"&&!this.grid.options.height){throw new Error($.ig.GridLoadOnDemand.locale.loadOnDemandRequiresHeight)}if(this.grid.options.virtualization){throw new Error($.ig.GridLoadOnDemand.locale.virtualizationNotSupported)}var i,featureName,features=this.grid.options.features,featuresLength=features.length;if(featuresLength===1){return}for(i=0;i<featuresLength;i++){featureName=features[i].name;if(!featureName){continue}featureName=featureName.toLowerCase();switch(featureName){case"groupby":throw new Error($.ig.GridLoadOnDemand.locale.groupByNotSupported);case"paging":throw new Error($.ig.GridLoadOnDemand.locale.pagingNotSupported);case"cellmerging":throw new Error($.ig.GridLoadOnDemand.locale.cellMergingNotSupported)}}},_setOption:function(key,value){$.Widget.prototype._setOption.apply(this,arguments);if(key==="defaultChunkIndex"){throw new Error($.ig.Grid.locale.optionChangeNotSupported+" "+key)}if(key==="currentChunkIndex"||key==="chunkSize"){this.grid.dataSource.settings.paging.pageSize=this.options.currentChunkIndex*this.options.chunkSize;this.grid.dataSource.dataBind()}},_initLoadingIndicator:function(){this._loadingIndicator=this.grid.container().igLoading().data("igLoading").indicator()},_nextChunk:function(){var noCancel=true;if(this.options.currentChunkIndex>=this.grid.dataSource.pageCount()-1){return}noCancel=this._trigger(this.events.rowsRequesting,null,{owner:this,chunkIndex:this.options.currentChunkIndex+1,chunkSize:this.grid.dataSource.settings.paging.chunkSize});if(noCancel){this._showLoading();this.grid.element.trigger("iggriduisoftdirty",{owner:this});this.grid.dataSource.settings.paging.appendPage=true;this._originalDataSourceCallback=this.grid.dataSource.settings.callback;this.grid.dataSource.settings.callback=this._appendRecordsHandler;this._requestPending=true;this.grid.dataSource.nextPage();this.options.currentChunkIndex=this.options.currentChunkIndex+1}},_showLoading:function(){this._loadingIndicator.show()},_hideLoading:function(){this._loadingIndicator.hide()},_getButtonRow:function(){this._buttonRow=this._buttonRow||this.grid.scrollContainer().find("div.ui-iggrid-loadmorebutton");return this._buttonRow},destroy:function(){if(this._loadingIndicator){delete this._loadingIndicator}this.grid.element.unbind("iggridrowsrendered",this._rowsRenderingHandler);$.Widget.prototype.destroy.call(this);return this},_appendRecords:function(success,errmsg){var i,currentPage,noCancelError;if(success===true){currentPage=this.grid.dataSource.recordsForPage(this.options.currentChunkIndex);for(i=0;i<currentPage.length;i++){this.grid.renderNewRow(currentPage[i])}}this._requestPending=false;this.grid.dataSource.settings.paging.appendPage=false;this._hideLoading();this._trigger(this.events.rowsRequested,null,{owner:this,chunkIndex:this.options.currentChunkIndex+1,chunkSize:this.grid.dataSource.settings.paging.chunkSize,rows:this.grid.dataSource.dataView()});this.grid.dataSource.settings.callback=this._originalDataSourceCallback;if(success===false){noCancelError=this._trigger(this.grid.events.requestError,null,{owner:this,message:errmsg});if(noCancelError){throw new Error(errmsg)}}},_refreshData:function(){this._showLoading();this.grid.dataSource.settings.paging.appendPage=false;this.grid.dataSource.settings.callback=this._originalDataSourceCallback;this._requestPending=true;this.grid.dataSource.dataBind()},_probeForNextPage:function(evt,ui){var delta=(this.grid.scrollContainer().scrollTop()+this.grid.scrollContainer().height())/$(this.grid.element).height();if(delta>=1&&!this._requestPending){this._nextChunk()}},nextChunk:function(){this._nextChunk()}});$.extend($.ui.igGridLoadOnDemand,{version:"13.2.20132.355"})})(jQuery);