Meetup.PhotoAlbumPager={};(function(){var p={};p.index=-1;p.offset=0;p.limit=20;p.cache=[];p.cachedId={};p.cacheLastIndex=0;p.method_name="getChapterPhotoAlbums";p.chapter_id=0;p.member_id=0;p.current_album_id=undefined;p.album_count=0;p.hasCachedAll=false;p.queue=null;p.hasFetchedInitially=false;var genQuery=function(){var lim;if(p.current_album_id){lim=p.limit+1}else{lim=p.limit}return Meetup.Data.path_api+"?"+queryString({method:p.method_name,arg_chapterId:p.chapter_id,arg_memberId:p.member_id,arg_offset:p.offset,arg_limit:lim})};var getNextOffset=function(isBackward){isBackward=isBackward||false;var result;if(isBackward){result=p.offset-p.limit}else{result=p.offset+p.limit}if(isNaN(result)){throw new Error("target must be number")}if(result<0){result=0}else{if(result>p.album_count){result=p.album_count}}return result};var current=function(){if(p.hasFetchedInitially===false){LOG.error("Current should be call at least once fetched.");return[]}return p.cache.slice(p.offset,getNextOffset())};var receiver=function(xhr){if(p.hasFetchedInitially===false){p.hasFetchedInitially=true}try{var arr=eval(xhr.responseText)}catch(ex){LOG.error("Something is wrong");return false}var album,paid,cache_index;for(var i=0,len=arr.length;i<len;++i){album=arr[i];paid=album.photoAlbumId;if(p.current_album_id&&paid==p.current_album_id){continue}else{if(p.cachedId[paid]){continue}}cache_index=p.cacheLastIndex;p.cache[cache_index]=album;p.cachedId[paid]=cache_index;++p.cacheLastIndex}if(checkCached(0,true)){p.hasCachedAll=true}var cb=p.queue;p.queue=null;cb(current())};var checkCached=function(start,end){var result=false;if(p.hasFetchedInitially===true){if(start===true){start=0}else{if(start!==0&&typeof(start)!=="number"){start=p.offset}}if(end===true){if(p.current_album_id){end=p.album_count-1}else{end=p.album_count}}else{if(end!==0&&typeof(end)!=="number"){end=getNextOffset()}}if(start>end){throw new Error(["wrong arguments given. Start: ",start,", End: ",end].join(""))}var arr=p.cache.slice(start,end);if(arr.length===1){if(arr[0]){result=true}}else{var joint=arr.join(",");var reversed=joint.split("").reverse().join("");if((/,{2,}/.test(joint)===false)&&(reversed.indexOf(",")!==0)){result=true}}}return result};var next=function(callback){if(p.queue!==null){return false}if(typeof callback!=="function"){throw new Error("You need to pass call back.")}if(p.hasFetchedInitially===false){p.index=0;p.offset=0}else{if(hasNext()){p.index=p.index+1;p.offset=getNextOffset()}}if(checkCached()){callback(current())}else{p.queue=callback;var d=doXHR(genQuery());d.addCallback(receiver)}};var previous=function(callback){if(p.queue!==null){return false}if(typeof callback!=="function"){throw new Error("You need to pass call back.")}if(p.hasFetchedInitially===false){throw new Error("You are not supposed to call this before calling at least once.")}if(hasPrev()){p.index=p.index-1;p.offset=getNextOffset(true)}if(checkCached()){callback(current())}else{p.queue=callback;var d=doXHR(genQuery());d.addCallback(receiver)}};var getIndex=function(){return p.index};var getSize=function(){return p.limit};var hasNext=function(){var result=false;var count;if(p.current_album_id){count=p.album_count-1}else{count=p.album_count}if((count>p.limit)&&(p.offset<(count-p.limit))){result=true}return result};var hasPrev=function(){var result=false;if(p.index>0){result=true}return result};var getById=function(id){var c_index=p.cachedId[id];if(c_index===undefined){throw new Error("You have to pass the correct id.")}return p.cache[c_index]};Meetup.PhotoAlbumPager.init=function(c_id,m_id,count,size,ca_id){p.chapter_id=c_id;p.member_id=m_id;p.album_count=count;if(size&&size>0){p.limit=size}if(ca_id){p.current_album_id=ca_id;p.cache.length=p.album_count-1}else{p.cache.length=p.album_count}return{next:next,previous:previous,getIndex:getIndex,getSize:getSize,hasNext:hasNext,hasPrev:hasPrev,getById:getById}}})();
