/*
 COPYRIGHT 2009 ESRI

 TRADE SECRETS: ESRI PROPRIETARY AND CONFIDENTIAL
 Unpublished material - all rights reserved under the
 Copyright Laws of the United States and applicable international
 laws, treaties, and conventions.

 For additional information, contact:
 Environmental Systems Research Institute, Inc.
 Attn: Contracts and Legal Services Department
 380 New York Street
 Redlands, California, 92373
 USA

 email: contracts@esri.com
 */

window[esri._dojoScopeName||"dojo"]._xdResourceLoaded(function(_1,_2,_3){return {depends:[["provide","esri.arcgis.Portal"],["require","dojox.socket"],["require","dojox.socket.Reconnect"],["require","dojo.DeferredList"],["require","esri.IdentityManager"]],defineResource:function(_4,_5,_6){if(!_4._hasResource["esri.arcgis.Portal"]){_4._hasResource["esri.arcgis.Portal"]=true;_4.provide("esri.arcgis.Portal");_4.require("dojox.socket");_4.require("dojox.socket.Reconnect");_4.require("dojo.DeferredList");_4.require("esri.IdentityManager");(function(){var _7={"options":{"disableIdentityLookup":true},"requestParams":{"f":"json"}};_4.declare("esri.arcgis.Portal",null,{onLoad:function(){},constructor:function(_8){_4.mixin(this,{"url":_8});this.init(_8).then(_4.hitch(this,"onLoad",this));},init:function(_9,_a){_9=(_9||this.portalUrl);this.portalUrl=_9+((_9.indexOf("/sharing")||_9.indexOf("/sharing/rest"))!==-1?"":"/sharing/rest/");return this._getSelf(this.portalUrl).then(_4.hitch(this,function(_b){var _c=_b;var _d=_b.user;if(_d){_d.portal=this;_d=new esri.arcgis.PortalUser(_d);}_7.self=_4.mixin({},_c);_4.mixin(this,_7.self);_7.loggedInUser=_4.mixin({},_4.mixin(_d,{"credential":_a}));this.thumbnailUrl=esri.arcgis.PortalUtil.formatUrl(this.portalUrl+"accounts/self/resources/"+this.thumbnail);this.isOrganization=(this.access&&this.access.length)||false;this.created=new Date(this.created);this.modified=new Date(this.modified);this.defaultExtent=this.defaultExtent;return this;}));},signIn:function(){_7.options.disableIdentityLookup=false;return esri.id.getCredential(this.url).then(_4.hitch(this,"init",this.url)).then(function(){return _7.loggedInUser;},function(_e){_7.options.disableIdentityLookup=true;throw _e;});},signOut:function(){_7.loggedInUser.credential&&_7.loggedInUser.credential.destroy&&_7.loggedInUser.credential.destroy();_7.loggedInUser=null;_7.options.disableIdentityLookup=true;esri.arcgis.PortalUtil.clearFieldsFromObject(_7.self,this);_7.self=null;return this.init(this.url);},queryGroups:function(_f){var url=this.portalUrl+"community/groups";return this._queryPortal(url,esri.arcgis.PortalUtil.formatQueryParams({},_f),esri.arcgis.PortalGroup);},queryItems:function(_10){var url=this.portalUrl+"search";return this._queryPortal(url,esri.arcgis.PortalUtil.formatQueryParams({},_10),esri.arcgis.PortalItem);},queryUsers:function(_11){var url=this.portalUrl+"community/users";return this._queryPortal(url,esri.arcgis.PortalUtil.formatQueryParams({"sortField":"username"},_11),esri.arcgis.PortalUser);},_getSelf:function(url){var _12=url+"accounts/self?"+"culture="+_4.locale;return esri.arcgis.PortalUtil.request(_12);},_queryPortal:function(url,_13,_14){var _15=_4.mixin({"num":10,"start":0,"sortField":"title","sortOrder":"asc"},_13),_16=["start","query","num","nextStart"],dfd=esri.arcgis.PortalUtil.request(url,_15).then(_4.hitch(this,function(_17){_17.results=esri.arcgis.PortalUtil.resultsToTypedArray(_14,{"portal":this},_17.results);_17.queryParams=_4.mixin({},_15);_17.nextQueryParams=_4.mixin(_15,{"start":_17.nextStart});return esri.arcgis.PortalUtil.clearFieldsFromObject(_16,_17);}));dfd=_4.delegate(dfd);dfd.queryParams=_4.mixin({},_15);dfd.nextQueryParams=_4.when(dfd,function(_18){return _18.nextQueryParams;});return esri.arcgis.PortalResult(dfd);}});esri.arcgis.PortalResult=function(_19){if(!_19){return _19;}if(_19.then){_19=_4.delegate(_19);}if(!_19.total){_19.total=_4.when(_19,function(_1a){return esri._isDefined(_1a.total)?_1a.total:(_1a.length||0);});}function _1b(_1c){if(!_19[_1c]){_19[_1c]=function(){var _1d=arguments;return _4.when(_19,function(_1e){Array.prototype.unshift.call(_1d,(_1e.results||_1e));return esri.arcgis.PortalResult(_4[_1c].apply(_4,_1d));});};}};_1b("forEach");_1b("filter");_1b("map");_1b("some");_1b("every");return _19;};_4.declare("esri.arcgis.PortalFolder",null,{constructor:function(_1f){_4.mixin(this,_1f);this.url=(this.portal&&this.portal.portalUrl)+"content/users/"+this.username+"/"+this.id;this.created=new Date(this.created);},getItems:function(){return esri.arcgis.PortalResult(esri.arcgis.PortalUtil.requestToTypedArray(this.url,null,null,esri.arcgis.PortalItem,{"portal":this.portal}));}});_4.declare("esri.arcgis.PortalGroup",null,{constructor:function(_20){_4.mixin(this,_20);this.url=(this.portal&&this.portal.portalUrl)+"community/groups/"+this.id;this.thumbnailUrl=esri.arcgis.PortalUtil.formatUrl(this.url+"/info/"+this.thumbnail);this.modified=new Date(this.modified);this.created=new Date(this.created);},getMembers:function(){return esri.arcgis.PortalUtil.request(this.url+"/users");},queryItems:function(_21){_21=esri.arcgis.PortalUtil.formatQueryParams({},_21);_21.q="group:"+this.id+(_21.q?(" "+_21.q):"");return this.portal.queryItems(_21);}});_4.declare("esri.arcgis.PortalItem",null,{constructor:function(_22){_4.mixin(this,_22);this.itemUrl=(this.portal&&this.portal.portalUrl)+"content/items/"+this.id;this.userItemUrl=_7.loggedInUser?this.itemUrl.replace("content",("content/users/"+_7.loggedInUser.username)):"";this.itemDataUrl=esri.arcgis.PortalUtil.formatUrl(this.itemUrl+"/data");this.thumbnailUrl=esri.arcgis.PortalUtil.formatUrl(this.itemUrl+"/info/"+this.thumbnail);this.extent=this.extent;this.spatialReference=this.spatialReference;this.created=new Date(this.created);this.uploaded=new Date(this.uploaded);this.modified=new Date(this.modified);},addComment:function(_23){var _24=_4.isString(_23)?{"comment":_23}:_23;return esri.arcgis.PortalUtil.request(this.itemUrl+"/addComment",_24,{"usePost":true}).then(_4.hitch(this,function(_25){return new esri.arcgis.PortalComment(_4.mixin(_24,{"id":_25.commentId,"item":this}));}));},updateComment:function(_26){if(_26&&_26.url&&_26.comment){var _27={"comment":_26.comment};return esri.arcgis.PortalUtil.request(_26.url+"/update",_27,{"usePost":true}).then(function(_28){_26.id=_28.commentId;return _26;});}else{throw new Error();}},getComments:function(){return esri.arcgis.PortalResult(esri.arcgis.PortalUtil.requestToTypedArray(this.itemUrl+"/comments",null,null,esri.arcgis.PortalComment,{"item":this}));},deleteComment:function(_29){if(_29&&_29.url){return esri.arcgis.PortalUtil.request(_29.url+"/delete",null,{"usePost":true});}else{throw new Error();}},addRating:function(_2a){var _2b=_4.isObject(_2a)?_2a:{"rating":parseFloat(_2a)};return esri.arcgis.PortalUtil.request(this.itemUrl+"/addRating",_2b,{"usePost":true}).then(_4.hitch(this,function(_2c){return new esri.arcgis.PortalRating(_4.mixin(_2b,{"id":_2c.ratingId,"item":this}));}));},getRating:function(){return esri.arcgis.PortalUtil.request(this.itemUrl+"/rating").then(_4.hitch(this,function(_2d){return new esri.arcgis.PortalRating(_4.mixin(_2d,{"item":this}));}));},deleteRating:function(){return esri.arcgis.PortalUtil.request(this.itemUrl+"/deleteRating",null,{"usePost":true});}});_4.declare("esri.arcgis.PortalComment",null,{constructor:function(_2e){_4.mixin(this,_2e);this.url=this.item.itemUrl+"/comments/"+this.id;this.created=new Date(this.created);}});_4.declare("esri.arcgis.PortalRating",null,{constructor:function(_2f){_4.mixin(this,_2f);this.url=this.item.itemUrl+"/rating";this.created=new Date(this.created);}});_4.declare("esri.arcgis.PortalUser",null,{constructor:function(_30){_4.mixin(this,_30);this.url=(this.portal&&this.portal.portalUrl)+"community/users/"+this.username;this.thumbnailUrl=esri.arcgis.PortalUtil.formatUrl(this.url+"/info/"+this.thumbnail);this.extent=this.extent;this.modified=new Date(this.modified);this.created=new Date(this.created);},getGroups:function(){return esri.arcgis.PortalResult(esri.arcgis.PortalUtil.requestToTypedArray(this.url,null,null,esri.arcgis.PortalGroup,{"portal":this.portal}));},getNotifications:function(){return esri.arcgis.PortalResult(esri.arcgis.PortalUtil.request(this.url+"/notifications"));},getGroupInvitations:function(){return esri.arcgis.PortalResult(esri.arcgis.PortalUtil.request(this.url+"/invitations"));},getTags:function(){return esri.arcgis.PortalResult(esri.arcgis.PortalUtil.request(this.url+"/tags"));},getFolders:function(){var dfd=this.getContent().then(function(_31){return _31.folders;});return esri.arcgis.PortalResult(dfd);},getItems:function(_32){var dfd=this.getContent(_32).then(function(_33){return _33.items;});return esri.arcgis.PortalResult(dfd);},getContent:function(_34){var url=(this.url.replace("community","content"))+(_34?("/"+_34):"");return esri.arcgis.PortalUtil.request(url).then(_4.hitch(this,function(_35){_35.folders=esri.arcgis.PortalUtil.resultsToTypedArray(esri.arcgis.PortalFolder,{"portal":this.portal},_35.folders);_35.items=esri.arcgis.PortalUtil.resultsToTypedArray(esri.arcgis.PortalItem,{"portal":this.portal},_35.items);return _35;}));}});esri.arcgis.PortalUtil={useSSL:function(_36,url){return (_36.indexOf("https:")!==-1||(_7.self&&_7.self.allSSL))?url.replace("http:","https:"):url;},formatUrl:function(url){var _37=_7.loggedInUser&&_7.loggedInUser.credential&&_7.loggedInUser.credential.token;return url.indexOf("null")!==-1?null:(_37?url+(url.indexOf("?")!==-1?"&":"?")+("token="+_37):url);},resultsToTypedArray:function(_38,_39,_3a){_3a=_3a?(_3a.items||_3a.groups||_3a.comments||_3a.results||_3a):[];return _4.map(_3a,function(_3b){_3b=_4.mixin(_3b,_39);return _38?new _38(_3b):_3b;});},clearFieldsFromObject:function(_3c,obj){if(!_4.isArray(_3c)){for(var fld in _3c){delete obj[fld];}}else{for(var i=0,l=_3c.length;i<l;i++){delete obj[_3c[i]];}}return obj;},getSocket:function(url,_3d){_3d=_3d||{};var _3e=_6.socket({url:url,transport:_3d.transport||esri.arcgis.PortalUtil.request,interval:_3d.interval||60000});if(!_3d.reconnect===false){_3e=_6.socket.Reconnect(_3e);}return _3e;},requestToTypedArray:function(url,_3f,_40,_41,_42){return esri.arcgis.PortalUtil.request(url,_3f,_40).then(_4.partial(esri.arcgis.PortalUtil.resultsToTypedArray,_41,_42));},request:function(url,_43,_44){_43&&_43.portal&&delete _43.portal;var _45=_4.mixin(_4.mixin({},_43||{}),_7.requestParams);var _46=_4.mixin(_44||{},_7.options);return esri.request({url:esri.arcgis.PortalUtil.useSSL(window.location.protocol,(url.url||url)),content:_45,callbackParamName:"callback",timeout:(_46&&_46.timeout)||0},_46).promise;},formatQueryParams:function(_47,_48){return _4.mixin(_4.mixin({},_47),(_4.isString(_48)?{"q":_48}:(_48||{})));}};})();}}};});