// Soapi JavaScript Library RC2 Version 0.9 Revision 2010.06.29.001
// http://soapi.info/
// 
// Copyright 2010, Sky Sanders
// Dual licensed under the MIT or GPL Version 2 licenses.
// http://soapi.info/code/js/license.aspx
// 
// Date: 7/20/2010 5:16:30 AM
// Version: 1.0 1.0 2010.07.09.004

var Soapi=Soapi||{};(function(){var r="/users/{id}/associated",m="stackauth.com",v="accepted",u="revision",x="question",k="votes",j="creation",n="activity",d=1e3,h="object",b="undefined",e=true,a=null;Soapi.Routes={};Soapi.Responses={};Soapi.Domain={};Soapi.RequestQueue={stop:function(){if(i){window.clearInterval(l);l=a}},start:function(){if(!l)l=window.setInterval(processQueue,i)},clear:function(){Soapi.RequestQueue.stop();var b=g;g=[];_onQueueComplete();Soapi.RequestQueue.start();for(var c=0;c<b.length;c++)b[c]=a;b=a},setInterval:function(a){Soapi.RequestQueue.stop();i=a;Soapi.RequestQueue.start()},getInterval:function(){return i},getLength:function(){return g.length}};var y=170,i,l,g=[],q=(new Date).getTime();function processQueue(){if(g.length){var b=g.pop();q=(new Date).getTime();b();b=a}}function enqueueRequest(a){if(!g&&q+i<(new Date).getTime()){q=(new Date).getTime();a()}else g.push(a)}Soapi.RequestQueue.setInterval(y);Soapi.RequestCache={clear:function(){f={}},setDuration:function(b,a){if(a<6e4)throw new({message:"Cache duration cannot be less than 60000 (1 minute)",code:0});p[b]=a},__namespace:e,__typeName:"Soapi.RequestCache"};var f={},p={};function cacheResponse(c,d,e,a){if(Soapi._disableCache)return;typeof f[c]!=b&&window.clearTimeout(f[c].timeout);if(typeof a==b)if(d in p)a=p[d];else a=6e4;f[c]={data:e};var g=window.setTimeout(function(){delete f[c]},a);f[c].timeout=a}function getCachedResponse(a){if(!Soapi._disableCache&&typeof f[a]!=b)return f[a].data}Soapi.Events={onError:function(a,b){},onSuccess:function(b,a){},onComplete:function(c,b,a){},onQueueActive:function(){},onQueueComplete:function(){}};function _onError(b,c,a){if(isFunction(Soapi.Events.onError))Soapi.Events.onError(b,c);isFunction(a)&&a(b,c)}function _onSuccess(c,b,a){if(isFunction(Soapi.Events.onSuccess))Soapi.Events.onSuccess(c,b);isFunction(a)&&a(c,b)}function _onComplete(c,b,a){if(isFunction(Soapi.Events.onComplete))Soapi.Events.onComplete(c,b,a);g.length==0&&_onQueueComplete()}function _onQueueActive(){isFunction(Soapi.Events.onQueueActive)&&Soapi.Events.onQueueActive()}function _onQueueComplete(){isFunction(Soapi.Events.onQueueComplete)&&Soapi.Events.onQueueComplete()}Soapi._internal={jsonpHandler:function(d,e,c){var a=document.createElement("script");a.type="text/javascript";a.src=d;a.id=c;var b=document.getElementsByTagName("head")[0];b.insertBefore(a,b.firstChild)},applyIds:function(a){var d=this,b=[],c;if(arguments.length>1||!isArray(a))throw{message:"Expected single array.",code:0};if(arguments.length!=0)if(a.length>0&&isArray(a[0])){b=a[0];if(a.length>1){if(a.length>2)throw{message:"Expected 2 parameters: Array, Map",code:0};if(!isArray(a[1]))c=a[1];else throw{message:"Expected object map in element 1",code:0};}}else if(a.length==1&&typeof a[0]==h)c=a[0];else{b=a;if(typeof b[b.length-1]==h)c=b.pop()}c&&Soapi._internal.applyParameters.call(d,c,false,d._target,d._apiKey);if(b.length>0)d.id=b},applyParameters:function(g,l,k,j){var c=this;c._target=c._target||k;c._apiKey=c._apiKey||j;if(g){for(var f in g)if(g.hasOwnProperty(f)){var a=g[f];if(!l&&(typeof a==b||!a||a==""||a==0))continue;if(isArray(a))for(var i=0;i<a.length;i++)Soapi._internal.applyParameters.call(a[i],a[i],e,k,j);else if(a instanceof Date)c[f]=a;else if(typeof a==h)Soapi._internal.applyParameters.call(a,a,e,k,j);else switch(f){case"fromdate":case"todate":case"bounty_closes_date":case"closed_date":case"creation_date":case"last_access_date":case"last_activity_date":case"last_edit_date":case"locked_date":case"on_date":case"protected_date":case"timed_penalty_date":if(a&&typeof a=="number"&&!isNaN(new Date(a*d)))c[f]=new Date(a*d);else if(a&&typeof a=="string"&&!isNaN(new Date(a)))c[f]=new Date(a);else c[f]=new Date(0);break;default:c[f]=a;break}}return c}},_cbid:0};function deleteJsonp(c){var b=document.getElementById(c);if(b){b.parentNode.removeChild(b);b=a}Soapi._internal[c]=a;delete Soapi._internal[c]}function jsonp(h,j,g,f,l,k,c,i){var b="_callback"+Soapi._internal._cbid++,d=h.replace("jsonp=?","jsonp=Soapi._internal."+b);Soapi._internal[b]=function(f){deleteJsonp(b);Soapi._internal.applyParameters.call(f,f,e,l,k);if(f.error){f.error.url=d;_onError(f.error,c,g);_onComplete(a,f.error,c)}else{for(var m in f)if(isArray(f[m])){f.items=f[m];f[m]=a;delete f[m];break}cacheResponse(h,i,f);_onSuccess(f,c,j);_onComplete(f,a,c)}f=a};Soapi._internal.jsonpHandler(d,Soapi._internal[b],b);f=f||3e3;window.setTimeout(function(){if(isFunction(Soapi._internal[b])){Soapi._internal[b]=function(a){deleteJsonp(b)};var e={code:408,message:"Request Timeout",url:d};_onError(e,c,g);_onComplete(a,e,c);e=a;window.setTimeout(function(){isFunction(Soapi._internal[b])&&deleteJsonp(b)},6e4)}},f)}Soapi.RouteFactory=function(c,b){var a=this;if(a.constructor!=Soapi.RouteFactory)return new Soapi.RouteFactory(c,b);a._target=c||"";a._apiKey=b||"";a._apiVersion=Soapi.RouteFactory.apiVersion;a._apiRevision=Soapi.RouteFactory.apiRevision;a.applyRouteParams=function(a){a._target=this._target;a._apiKey=this._apiKey;a._apiVersion=this._apiVersion}};Soapi.RouteFactory.version="1.0 1.0 2010.07.09.004";Soapi.RouteFactory.apiVersion="1.0";Soapi.RouteFactory.apiRevision="2010.07.09.004";Soapi.Routes.Route=function(){};Soapi.Routes.Route.prototype.applyParameters=function(a){return Soapi._internal.applyParameters.apply(this,arguments)};Soapi.Routes.Route.prototype.getPagedResponse=function(f,g,h,i,e){var d,c=this;c.page=c.page||1;c.pagesize=c.pagesize||100;function addItems(j){if(typeof j.total==b){var k={message:"not a paged response",code:0,url:c._lastUrl};Soapi._internal.applyParameters.call(k,k);_onError(k,e,g);_onComplete(a,k,e)}else if(typeof d==b){if(isFunction(f))d=j}else{d.page=j.page;if(isFunction(f))d.items=d.items.concat(j.items)}if(j.items.length==0||isFunction(h)&&h(j,e)){_onSuccess(d,e,f);_onComplete(d,a,e)}else{c.page++;c.getResponse(addItems,g,i,e)}}c.getResponse(addItems,g,i,e);return c};Soapi.Routes.Route.prototype.validateParameters=function(){};Soapi.Routes.Route.prototype.getResponse=function(k,o,i,h){var c=this;i=i|0;var n="http://";c._target=(c._target.indexOf(n)==-1?n:"")+c._target;var d=c._target+"/"+c._apiVersion+c._routeFormat,g=e;if(c._apiKey){g=false;d+="?key="+encodeURIComponent(c._apiKey)}for(var f in c)if(f.substr(0,1)!="_"&&c.hasOwnProperty(f)){var m=jsEncode(c[f]),l="{"+f+"}";if(d.indexOf(l)>0)d=d.replace(l,m);else{d+=(g?"?":"&")+f+"="+m;g=false}}c._lastUrl=d;d=d+(g?"?":"&")+"jsonp=?";this.validateParameters();_onQueueActive();var j=getCachedResponse(d);if(typeof j!=b)window.setTimeout(function(){_onSuccess(j,h,k);_onComplete(j,a,h)},10);else enqueueRequest(function(){jsonp(d,k,o,i,c._target,c._apiKey,h,c._routeFormat)});return c};Soapi.Utilities={VectorizedIdList:function(c){var b=this;b.maxBatchLength=c;var a=[],d=b;b.indexOf=function(c){for(var b=0;b<a.length;b++)if(a[b]==c)return b;return-1};b.addRange=function(b){a=a.concat(b)};b.add=function(b){this.indexOf(b)==-1&&a.push(b)};b.remove=function(c){var b=this.indexOf(c);b>-1&&a.splice(b,1)};b.getBatchesVectorized=function(c){for(var a=this.getBatches(c),b=0;b<a.length;b++)a[b]=a[b].join(";");return a};b.getBatches=function(h){for(var f=[],b=[],e="",c=0;c<a.length;c++){var g=a[c].toString()+";";if((e+g).length>=d.maxBatchLength){f.push(b);b=[];e="";b.push(a[c])}else{e+=g;b.push(a[c])}}b.length>0&&f.push(b);if(h)a=[];return f}},formatNumber:function(a){a=a+"";if(a<d)return a;else if(a<1e4)return a.charAt(0)+","+a.substring(1);else return(a/d).toFixed(a%d!=0)+"k"},getTimeAgo:function(c){c=new Date(c);var b=Math.floor((new Date-c)/d),a=Math.floor(b/31536000);if(a>1)return a+" years";a=Math.floor(b/2592000);if(a>1)return a+" months";a=Math.floor(b/86400);if(a>1)return a+" days";a=Math.floor(b/3600);if(a>1)return a+" hours";a=Math.floor(b/60);if(a>1)return a+" minutes";return Math.floor(b)+" seconds"}};function isFunction(a){return a&&typeof a=="function"}function isArray(a){return a&&typeof a!=b&&a.constructor&&a.constructor.toString().indexOf("Array")!=-1}function jsEncode(a){if(a instanceof Date)return Math.floor(a/d);else if(isArray(a)){for(var c=[],b=0;b<a.length;b++)c.push(encodeURIComponent(a[b]));return c.join(";")}else return encodeURIComponent(a)}function createEnum(a,c){Soapi.Domain[a]=function(){};Soapi.Domain[a].prototype=c;for(var b in Soapi.Domain[a].prototype)if(Soapi.Domain[a].prototype.hasOwnProperty(b))Soapi.Domain[a][b]=Soapi.Domain[a].prototype[b]}function createType(b,a){Soapi[a][b]=new Function("if (arguments[0] && (arguments[0] instanceof Soapi."+a+"."+b+")) {return arguments[0];};Soapi._internal.applyParameters.call(this, arguments[0],true);");Soapi[a][b]._typeName="Soapi."+a+"."+b}function createRouteMap(b,c){var a="RouteMap";Soapi.Routes[b+a]=new Function('Soapi._internal.applyIds.apply(this, arguments);this._routeFormat = "'+c+'";');Soapi.Routes[b+a].prototype=new Soapi.Routes.Route;Soapi.Routes[b+a].routeFormat=c;Soapi.RouteFactory.prototype[b]=function(){var c=new Soapi.Routes[b+a](Array.prototype.slice.call(arguments));this.applyRouteParams(c);return c};Soapi.Routes[b+a]._typeName="Soapi.Routes."+b+a}for(var w=["AnswersResponse","BadgesResponse","CommentsResponse","ErrorsResponse","PostTimelineResponse","QuestionsResponse","RepChangesResponse","RevisionsResponse","StatsResponse","TagsResponse","UserTimelinesResponse","UsersResponse","SitesResponse","UsersAssociatedResponse"],t=["Styling","User","Comment","Answer","Badge","BadgeCounts","Site","Migrated","Question","PostTimeline","Revision","ApiVersion","Statistics","Tag","RepChange","UserTimeline"],o=[{tn:"SortOrder",tp:{desc:"desc",asc:"asc"}},{tn:"PostSort",tp:{activity:n,views:"views",creation:j,votes:k}},{tn:"CommentSort",tp:{creation:j,votes:k}},{tn:"QuestionSort",tp:{activity:n,votes:k,creation:j,featured:"featured",hot:"hot",week:"week",month:"month"}},{tn:"TagSort",tp:{popular:"popular",activity:n,name:"name"}},{tn:"UserSort",tp:{reputation:"reputation",creation:j,name:"name"}},{tn:"FavoritesSort",tp:{activity:n,views:"views",creation:j,added:"added",votes:k}},{tn:"SiteState",tp:{normal:"normal",closed_beta:"closed_beta",open_beta:"open_beta",linked_meta:"linked_meta"}},{tn:"UserType",tp:{anonymous:"anonymous",unregistered:"unregistered",registered:"registered",moderator:"moderator"}},{tn:"PostType",tp:{question:x,answer:"answer"}},{tn:"PostTimelineType",tp:{question:x,answer:"answer",comment:"comment",revision:u,votes:k,state:"state",accepted:v,unaccepted:"unaccepted"}},{tn:"RevisionType",tp:{single_user:"single_user",vote_based:"vote_based"}},{tn:"UserTimelineType",tp:{comment:"comment",askoranswered:"askoranswered",badge:"badge",revision:u,accepted:v}}],s=[{tn:"AnswersById",rf:"/answers/{id}"},{tn:"AnswersByIdComments",rf:"/answers/{id}/comments"},{tn:"Badges",rf:"/badges"},{tn:"BadgesById",rf:"/badges/{id}"},{tn:"BadgesName",rf:"/badges/name"},{tn:"BadgesTags",rf:"/badges/tags"},{tn:"CommentsById",rf:"/comments/{id}"},{tn:"ErrorsById",rf:"/errors/{id}"},{tn:"PostsByIdComments",rf:"/posts/{id}/comments"},{tn:"Questions",rf:"/questions"},{tn:"QuestionsById",rf:"/questions/{id}"},{tn:"QuestionsByIdAnswers",rf:"/questions/{id}/answers"},{tn:"QuestionsByIdComments",rf:"/questions/{id}/comments"},{tn:"QuestionsByIdTimeline",rf:"/questions/{id}/timeline"},{tn:"QuestionsUnanswered",rf:"/questions/unanswered"},{tn:"RevisionsById",rf:"/revisions/{id}"},{tn:"RevisionsByIdByRevisionGuid",rf:"/revisions/{id}/{revisionguid}"},{tn:"Search",rf:"/search"},{tn:"Stats",rf:"/stats"},{tn:"Tags",rf:"/tags"},{tn:"Users",rf:"/users"},{tn:"UsersById",rf:"/users/{id}"},{tn:"UsersByIdAnswers",rf:"/users/{id}/answers"},{tn:"UsersByIdBadges",rf:"/users/{id}/badges"},{tn:"UsersByIdComments",rf:"/users/{id}/comments"},{tn:"UsersByIdCommentsByToId",rf:"/users/{id}/comments/{toid}"},{tn:"UsersByIdFavorites",rf:"/users/{id}/favorites"},{tn:"UsersByIdMentioned",rf:"/users/{id}/mentioned"},{tn:"UsersByIdQuestions",rf:"/users/{id}/questions"},{tn:"UsersByIdReputation",rf:"/users/{id}/reputation"},{tn:"UsersByIdTags",rf:"/users/{id}/tags"},{tn:"UsersByIdTimeline",rf:"/users/{id}/timeline"},{tn:"UsersModerators",rf:"/users/moderators"}],c=0;c<o.length;c++)createEnum(o[c].tn,o[c].tp,o[c].tf);for(c=0;c<t.length;c++)createType(t[c],"Domain");for(c=0;c<w.length;c++)createType(w[c],"Responses");for(c=0;c<s.length;c++)createRouteMap(s[c].tn,s[c].rf);Soapi.Routes.SitesRouteMap=function(){if(arguments[0]&&arguments[0]instanceof Soapi.Routes.SitesRouteMap)return arguments[0];this._routeFormat="/sites"};Soapi.Routes.SitesRouteMap._typeName="Soapi.Routes.SitesRouteMap";Soapi.Routes.SitesRouteMap.prototype=new Soapi.Routes.Route;Soapi.Routes.SitesRouteMap.routeFormat="/sites";Soapi.RouteFactory.prototype.Sites=function(){var a=new Soapi.Routes.SitesRouteMap;this.applyRouteParams(a);a._target=m;return a};Soapi.Routes.UsersByIdAssociatedRouteMap=function(a){if(arguments[0]&&arguments[0]instanceof Soapi.Routes.UsersByIdAssociatedRouteMap)return arguments[0];this.id=a;this._routeFormat=r};Soapi.Routes.UsersByIdAssociatedRouteMap._typeName="Soapi.Routes.UsersByIdAssociatedRouteMap";Soapi.Routes.UsersByIdAssociatedRouteMap.prototype=new Soapi.Routes.Route;Soapi.Routes.UsersByIdAssociatedRouteMap.routeFormat=r;Soapi.RouteFactory.prototype.UsersByIdAssociated=function(b){var a=new Soapi.Routes.UsersByIdAssociatedRouteMap(b);this.applyRouteParams(a);a._target=m;return a};Soapi.Routes.SearchRouteMap.prototype.validateParameters=function(){var a=this;if(!a.intitle&&!a.tagged&&!a.nottagged)throw{message:"One of intitle, tagged, or nottagged must be set.",code:0,url:a._lastUrl};}})()
