/*
 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
 */
//>>built
define("esri/arcgis/Portal",["dojo/_base/declare","dojo/_base/lang","dojo/_base/kernel","dojo/_base/Deferred","dojo/_base/array","dojo/_base/sniff","dojo/Evented","dojo/DeferredList","esri/kernel","esri/lang","esri/request","esri/IdentityManager"],function(_1,_2,_3,_4,_5,_6,_7,_8,_9,_a,_b){var _c={"options":{"disableIdentityLookup":true},"requestParams":{"f":"json"}},_d=function(_e){if(!_e){return _e;}if(_e.then){_e=_2.delegate(_e);}if(!_e.total){_e.total=_4.when(_e,function(_f){return _a.isDefined(_f.total)?_f.total:(_f.length||0);});}function _10(_11){if(!_e[_11]){_e[_11]=function(){var _12=arguments;return _4.when(_e,function(_13){Array.prototype.unshift.call(_12,(_13.results||_13));return _d(_5[_11].apply(_5,_12));});};}};_10("forEach");_10("filter");_10("map");_10("some");_10("every");return _e;},_14={useSSL:function(_15,url){return (_15.indexOf("https:")!==-1||(_c.self&&_c.self.allSSL))?url.replace("http:","https:"):url;},formatUrl:function(url){var _16=_c.loggedInUser&&_c.loggedInUser.credential&&_c.loggedInUser.credential.token;return url.indexOf("null")!==-1?null:_14.useSSL(window.location.protocol,(_16?url+(url.indexOf("?")!==-1?"&":"?")+("token="+_16):url));},resultsToTypedArray:function(_17,_18,_19){_19=_19?(_19.listings||_19.notifications||_19.userInvitations||_19.tags||_19.items||_19.groups||_19.comments||_19.results||_19):[];return _5.map(_19,function(_1a){_1a=_2.mixin(_1a,_18||{});return _17?new _17(_1a):_1a;});},clearFieldsFromObject:function(_1b,obj){var fld,i,l=_1b.length;if(!_2.isArray(_1b)){for(fld in _1b){delete obj[fld];}}else{for(i=0;i<l;i++){delete obj[_1b[i]];}}return obj;},requestToTypedArray:function(url,_1c,_1d,_1e,_1f){return _d(_14.request(url,_1c,_1d).then(_2.partial(_14.resultsToTypedArray,_1e,_1f)));},request:function(url,_20,_21){_20&&_20.portal&&delete _20.portal;var _22=_2.mixin(_2.mixin({},_20||{}),_c.requestParams);var _23=_2.mixin(_21||{},_c.options);return _b({url:_14.useSSL(window.location.protocol,(url.url||url)),content:_22,callbackParamName:"callback",timeout:(_23&&_23.timeout)||0},_23);},formatQueryParams:function(_24,_25,_26){var qp=_2.mixin(_2.mixin({},_24),(_2.isString(_25)?{"q":_25}:(_25||{})));qp.q=!_26&&_c.extraQuery?"("+qp.q+")"+_c.extraQuery:qp.q;return qp;}},_27=_1([],{declaredClass:"esri.arcgis.PortalComment",constructor:function(_28){_2.mixin(this,_28);this.url=this.item.itemUrl+"/comments/"+this.id;this.created=this.created?new Date(this.created):null;}}),_29=_1([],{declaredClass:"esri.arcgis.PortalRating",constructor:function(_2a){_2.mixin(this,_2a);this.url=this.item.itemUrl+"/rating";this.created=this.created?new Date(this.created):null;}}),_2b=_1([],{declaredClass:"esri.arcgis.PortalItem",constructor:function(_2c){_2.mixin(this,_2c);this.folderId=this.ownerFolder||this.folderId;this.itemUrl=(this.portal&&this.portal.portalUrl)+"content/items/"+this.id;this.userItemUrl=this.itemUrl.replace("content",("content/users/"+this.owner)+(this.folderId?"/"+this.folderId:""));this.itemDataUrl=_14.formatUrl(this.itemUrl+"/data");this.thumbnailUrl=_14.formatUrl(this.itemUrl+"/info/"+this.thumbnail);this.created=this.created?new Date(this.created):null;this.uploaded=this.uploaded?new Date(this.uploaded):null;this.modified=this.modified?new Date(this.modified):null;},addComment:function(_2d){var _2e=_2.isString(_2d)?{"comment":_2d}:_2d;return _14.request(this.itemUrl+"/addComment",_2e,{"usePost":true}).then(_2.hitch(this,function(_2f){return _27(_2.mixin(_2e,{"id":_2f.commentId,"item":this}));}));},updateComment:function(_30){if(_30&&_30.url&&_30.comment){var _31={"comment":_30.comment};return _14.request(_30.url+"/update",_31,{"usePost":true}).then(function(_32){_30.id=_32.commentId;return _30;});}else{throw new Error();}},getComments:function(){return _14.requestToTypedArray(this.itemUrl+"/comments",null,null,_27,{"item":this});},deleteComment:function(_33){if(_33&&_33.url){return _14.request(_33.url+"/delete",null,{"usePost":true});}else{throw new Error();}},addRating:function(_34){var _35=_2.isObject(_34)?_34:{"rating":parseFloat(_34)};return _14.request(this.itemUrl+"/addRating",_35,{"usePost":true}).then(_2.hitch(this,function(_36){return new _29(_2.mixin(_35,{"id":_36.ratingId,"item":this}));}));},getRating:function(){return _14.request(this.itemUrl+"/rating").then(_2.hitch(this,function(_37){return new _29(_2.mixin(_37,{"item":this}));}));},deleteRating:function(){return _14.request(this.itemUrl+"/deleteRating",null,{"usePost":true});}}),_38=_1([],{declaredClass:"esri.arcgis.PortalListing",constructor:function(_39){_2.mixin(this,_39);this.id=this.itemId;this.url=(this.portal&&this.portal.portalUrl)+"content/"+(this.userItemUrl?"items/":"listings/")+this.itemId;this.created=this.created?new Date(this.created):null;this.banner=_14.formatUrl(this.url+"/info/"+this.banner);this.thumbnail=_14.formatUrl(this.url+"/info/"+this.thumbnail);this.largeThumbnail=_14.formatUrl(this.url+"/info/"+this.largeThumbnail);this.avgRating=this.avgRating||0;this.numRatings=this.numRatings||0;this.numComments=this.numComments||0;this.commentsUrl=(this.portal&&this.portal.portalUrl)+"/content/items/"+this.itemId+"/comments";this.listingProperties=this.listingProperties||{priceDesc:"",creditsPerTransaction:0,licenseType:"free",trialSupported:false,trialDuration:0,listingAccess:"private"};for(var _3a in this.listingProperties){if(this[_3a]){this.listingProperties[_3a]=this[_3a];}}this.properties=this.properties||{"systemRequirements":"","termsAndConditions":"","version":"1.0"};this.screenshots=_5.map(this.screenshots,_2.hitch(this,function(_3b){return _14.formatUrl(this.url+"/info/"+_3b);}));this.vendor.vendorPhone=this.vendor.vendorPhone;this.vendor.vendorEmail=this.vendor.vendorEmail;this.vendor.vendorUrl=this.vendor.vendorUrl;this.vendor.description=this.vendor.description;this.vendor.thumbnail=this.userItemUrl?_14.formatUrl(this.portal.portalUrl+"/accounts/self/resources/"+this.vendor.thumbnail):_14.formatUrl(this.url+"/info/"+this.vendor.thumbnail);},getComments:function(){return _14.requestToTypedArray(this.commentsUrl,null,null,_27,{"item":this});},getVendor:function(){return this.vendor;}}),_3c=_1([],{declaredClass:"esri.arcgis.PortalProvision",constructor:function(_3d){_2.mixin(this,_3d);this.created=this.created?new Date(this.created):null;this.startDate=this.startDate?new Date(this.startDate):null;this.endDate=this.endDate&&this.endDate!==-1?new Date(this.endDate):null;this.listing=_3d.listing?new _38(_2.mixin(_3d.listing,{"portal":this.portal})):null;}}),_3e=_1([],{declaredClass:"esri.arcgis.PortalGroup",constructor:function(_3f){_2.mixin(this,_3f);this.url=(this.portal&&this.portal.portalUrl)+"community/groups/"+this.id;this.thumbnailUrl=_14.formatUrl(this.url+"/info/"+this.thumbnail);this.modified=this.modified?new Date(this.modified):null;this.created=this.created?new Date(this.created):null;},getMembers:function(){return _14.request(this.url+"/users");},queryItems:function(_40){_40=_14.formatQueryParams({},_40);_40.q="group:"+this.id+(_40.q?(" "+_40.q):"");return this.portal.queryItems(_40);}}),_41=_1([],{declaredClass:"esri.arcgis.PortalFolder",constructor:function(_42){_2.mixin(this,_42);this.url=(this.portal&&this.portal.portalUrl)+"content/users/"+this.username+"/"+this.id;this.created=this.created?new Date(this.created):null;},getItems:function(){return _14.requestToTypedArray(this.url,null,null,_2b,{"portal":this.portal,"folderId":this.id});}}),_43=_1([],{declaredClass:"esri.arcgis.PortalUser",constructor:function(_44){_2.mixin(this,_44);this.url=(this.portal&&this.portal.portalUrl)+"community/users/"+this.username;this.userContentUrl=(this.portal&&this.portal.portalUrl)+"content/users/"+this.username;this.thumbnailUrl=_14.formatUrl(this.url+"/info/"+this.thumbnail);this.modified=this.modified?new Date(this.modified):null;this.created=this.created?new Date(this.created):null;},getGroups:function(){return _d(_14.request(this.url).then(_2.hitch(this,function(_45){return _14.resultsToTypedArray(_3e,{"portal":this.portal},_45.groups);})));},getNotifications:function(){return _14.requestToTypedArray(this.url+"/notifications",null,null,null,{"portal":this.portal});},getGroupInvitations:function(){return _14.requestToTypedArray(this.url+"/invitations",null,null,null,{"portal":this.portal});},getTags:function(){return _14.requestToTypedArray(this.url+"/tags",null,null,null,{"portal":this.portal});},getFolders:function(){return _d(this.getContent().then(function(_46){return _46.folders;}));},getItems:function(_47){return _d(this.getContent(_47).then(function(_48){return _48.items;}));},getItem:function(_49){var url=this.portal.portalUrl+"content/items/"+_49;return _14.request(url).then(_2.hitch(this,function(_4a){return new _2b(_2.mixin(_4a,{"portal":this.portal}));}));},getContent:function(_4b){var url=(this.url.replace("community","content"))+(_4b?("/"+_4b):"");return _14.request(url).then(_2.hitch(this,function(_4c){_4c.folders=_14.resultsToTypedArray(_41,{"portal":this.portal},_4c.folders);_4c.items=_14.resultsToTypedArray(_2b,{"portal":this.portal,"folderId":_4b},_4c.items);return _4c;}));}}),_4d=_1([_7],{declaredClass:"esri.arcgis.Portal",onLoad:function(){},constructor:function(url){_2.mixin(this,{"url":url});this.init(url).then(_2.hitch(this,function(){this.emit("ready",this);this.onLoad(this);}));},init:function(url,_4e){url=(url||this.portalUrl).replace(/\/+$/,"");var idx=url.indexOf("/sharing");this.portalUrl=(idx!==-1?(url+"/"):(url+"/sharing/rest/"));return this._getSelf(this.portalUrl).then(_2.hitch(this,function(_4f){_c.self=_2.mixin({},_4f);var _50=_4f.user;if(_50){_50.portal=this;_50=new _43(_50);_c.loggedInUser=_2.mixin({},_2.mixin(_50,{"credential":_4e}));}if(_c.self.id&&_c.self.canSearchPublic===false){_c.extraQuery=" AND orgid:"+_c.self.id;}_2.mixin(this,_c.self);this.thumbnailUrl=_14.formatUrl(this.portalUrl+"accounts/self/resources/"+this.thumbnail);this.isOrganization=(this.access&&this.access.length)||false;this.created=this.created?new Date(this.created):null;this.modified=this.modified?new Date(this.modified):null;return this;}));},signIn:function(){if(_c&&_c.loggedInUser){var dfd=new _4();dfd.resolve(_c.loggedInUser);return dfd;}else{_c.options.disableIdentityLookup=false;return _9.id.getCredential(this.portalUrl).then(_2.hitch(this,"init",this.url)).then(function(){return _c.loggedInUser;},function(_51){_c.options.disableIdentityLookup=true;throw _51;});}},signOut:function(){_c.loggedInUser.credential&&_c.loggedInUser.credential.destroy();_c.loggedInUser=null;_c.options.disableIdentityLookup=true;_14.clearFieldsFromObject(_c.self,this);_c.self=null;return this.init(this.url);},getPortalUser:function(){return _c.loggedInUser;},addResource:function(_52,_53){var _54={"key":_52,"text":_53};return _14.request((this.portalUrl+"portals/self/addResource"),_54,{"usePost":true});},update:function(_55){return _14.request((this.portalUrl+"portals/self/update"),_55,{"usePost":true});},queryGroups:function(_56){return this._queryPortal(this.portalUrl+"community/groups",_14.formatQueryParams({},_56),_3e);},queryItems:function(_57){return this._queryPortal(this.portalUrl+"search",_14.formatQueryParams({},_57),_2b);},queryListings:function(_58){var qp=_14.formatQueryParams({},_58,true),qs="";if(qp.q&&qp.q.toLowerCase().indexOf("mylistings:true")>-1){qp.q=qp.q.toLowerCase().replace("mylistings:true","");qs="?mylistings=true";}return this._queryPortal(this.portalUrl+"content/listings"+qs,qp,_38);},getPurchases:function(){return this.getCustomers().then(_2.hitch(this,function(_59){return _59.purchases;}));},getInterests:function(){return this.getCustomers().then(_2.hitch(this,function(_5a){return _5a.interests;}));},getTrials:function(){return this.getCustomers().then(_2.hitch(this,function(_5b){return _5b.trials;}));},getCustomers:function(){var url=((this.portalUrl)+"portals/self/customers");return _14.request(url);},getMyPurchases:function(){return this.getPurchases().then(function(_5c){return _5c.purchases;});},getMyInterests:function(){return this.getPurchases().then(function(_5d){return _5d.interests;});},getPurchases:function(){var url=((this.portalUrl)+"portals/self/purchases");return _14.request(url).then(_2.hitch(this,function(_5e){_5e.interests=_5.map(_5e.interests,function(_5f){return _2.mixin(_5f.provision,{"listing":_5f.listing});});_5e.purchases=_5.map(_5e.purchases,function(_60){return _2.mixin(_60.provision,{"listing":_60.listing});});_5e.trials=_5.map(_5e.trials,function(_61){return _2.mixin(_61.provision,{"listing":_61.listing});});_5e.interests=_14.resultsToTypedArray(_3c,{"portal":this},_5e.interests);_5e.trials=_14.resultsToTypedArray(_3c,{"portal":this},_5e.trials);_5e.purchases=_14.resultsToTypedArray(_3c,{"portal":this},_5e.purchases);return _5e;}));},queryUsers:function(_62){return this._queryPortal(this.portalUrl+"community/users",_14.formatQueryParams({"sortField":"username"},_62),_43);},_getSelf:function(url){var _63=url+"accounts/self?"+"culture="+_3.locale;return _14.request(_63);},_queryPortal:function(url,_64,_65){var _66=_2.mixin({"num":10,"start":0,"sortField":"title","sortOrder":"asc"},_64),_67=["start","query","num","nextStart"],dfd=_14.request(url,_66).then(_2.hitch(this,function(_68){_68.results=_14.resultsToTypedArray(_65,{"portal":this},_68);_68.queryParams=_2.mixin({},_66);_68.nextQueryParams=_2.mixin(_66,{"start":_68.nextStart});return _14.clearFieldsFromObject(_67,_68);}));dfd=_2.delegate(dfd);dfd.queryParams=_2.mixin({},_66);dfd.nextQueryParams=_4.when(dfd,function(_69){return _69.nextQueryParams;});return _d(dfd);}}),_6a={Portal:_4d,PortalFolder:_41,PortalGroup:_3e,PortalItem:_2b,PortalComment:_27,PortalRating:_29,PortalUtil:_14,PortalResult:_d,PortalListing:_38};if(_6("extend-esri")){_2.mixin(_2.getObject("arcgis",true,_9),_6a);}return _6a;});