/**************************************************************************
 *                                                                        *
 *  Copyright 2013 Adobe Systems Incorporated                             *
 *  All Rights Reserved.                                                  *
 *                                                                        *
 * NOTICE:  All information contained herein is, and remains the property *
 * of Adobe Systems Incorporated and its suppliers, if any.               *
 * The intellectual and technical concepts contained herein are           *
 * proprietary to Adobe Systems Incorporated and its suppliers and may be *
 * covered by U.S. and Foreign Patents, patents in process, and are       *
 * protected by trade secret or copyright law.                            *
 * Dissemination of this information or reproduction of this material     *
 * strictly forbidden unless prior written permission is obtained from    *
 * Adobe Systems Incorporated.                                            *
 *                                                                        *
 **************************************************************************/

/* ShareOnBehance 1.0.0.120 */
function CSXSWindowType(){}function Version(e,t,n,r){this.major=e,this.minor=t,this.micro=n,this.special=r}function VersionBound(e,t){this.version=e,this.inclusive=t}function VersionRange(e,t){this.lowerBound=e,this.upperBound=t}function Runtime(e,t){this.name=e,this.versionRange=t}function Extension(e,t,n,r,i,s,o,u,a,f,l,c,h,p,d,v){this.id=e,this.name=t,this.mainPath=n,this.basePath=r,this.windowType=i,this.width=s,this.height=o,this.minWidth=u,this.minHeight=a,this.maxWidth=f,this.maxHeight=l,this.defaultExtensionDataXml=c,this.specialExtensionDataXml=h,this.requiredRuntimeList=p,this.isAutoVisible=d,this.isPluginExtension=v}function CSEvent(e,t,n,r){this.type=e,this.scope=t,this.appId=n,this.extensionId=r}function SystemPath(){}function ColorType(){}function RGBColor(e,t,n,r){this.red=e,this.green=t,this.blue=n,this.alpha=r}function Direction(e,t){this.x=e,this.y=t}function GradientStop(e,t){this.offset=e,this.rgbColor=t}function GradientColor(e,t,n,r){this.type=e,this.direction=t,this.numStops=n,this.arrGradientStop=r}function UIColor(e,t,n){this.type=e,this.antialiasLevel=t,this.color=n}function AppSkinInfo(e,t,n,r,i,s,o){this.baseFontFamily=e,this.baseFontSize=t,this.appBarBackgroundColor=n,this.panelBackgroundColor=r,this.appBarBackgroundColorSRGB=i,this.panelBackgroundColorSRGB=s,this.systemHighlightColor=o}function HostEnvironment(e,t,n,r,i,s,o){this.appName=e,this.appVersion=t,this.appLocale=n,this.appUILocale=r,this.appId=i,this.isAppOffline=s,this.appSkinInfo=o}function CSInterface(){}function AAMStatus(e,t,n){this.status=e,this.callerId=t,this.payload=n}function IMSInterface(){}define("lib/CEPGlobals",[],function(){return window.cep}),define("window",[],function(){return window}),define("config",["window"],function(e){var t="c2hhcmUtb24tYmVoYW5jZQ==",n="NmExODlhOGItY2JjOC00ODgyLWFhZGUtMDBkNzUxODYyYTFk",r="aHR0cHM6Ly9vb2JlLmFkb2JlLmNvbS8=";return{aameeServiceName:"ShareOnBehance",behanceDomain:"https://www.behance.net",behanceIFrameUrl:"https://www.behance.net/connect/adobe/to",behanceHelpUrl:"https://www.behance.net/faq",behancePingUrl:"https://www.behance.net/xd.php",imsCi:e.atob(t),imsCs:e.atob(n),imsRu:e.atob(r),logging:!1,timeout:3e4,version:"1.0.0",whiteListedDomains:["adobe.com","behance.net","prosite.com"]}}),define("log",["config"],function(e){var t=(new Date).getTime();return{error:function(n){e.logging&&console.log("ERROR ("+((new Date).getTime()-t)+"): "+n)},info:function(n){e.logging&&console.log("INFO ("+((new Date).getTime()-t)+"): "+n)},stringify:function(t){if(!e.logging)return"";try{return JSON.stringify(t)}catch(n){return t+""}}}}),CSXSWindowType._PANEL="Panel",CSXSWindowType._MODELESS="Modeless",CSXSWindowType._MODAL_DIALOG="ModalDialog",EvalScript_ErrMessage="EvalScript error.",Version.MAX_NUM=999999999,CSEvent.prototype.data="",SystemPath.USER_DATA="userData",SystemPath.COMMON_FILES="commonFiles",SystemPath.MY_DOCUMENTS="myDocuments",SystemPath.APPLICATION="application",SystemPath.EXTENSION="extension",SystemPath.HOST_APPLICATION="hostApplication",ColorType.RGB="rgb",ColorType.GRADIENT="gradient",ColorType.NONE="none",CSInterface.THEME_COLOR_CHANGED_EVENT="com.adobe.csxs.events.ThemeColorChanged",CSInterface.prototype.hostEnvironment=JSON.parse(window.__adobe_cep__.getHostEnvironment()),CSInterface.prototype.closeExtension=function(){window.__adobe_cep__.closeExtension()},CSInterface.prototype.getSystemPath=function(e){var t=decodeURI(window.__adobe_cep__.getSystemPath(e)),n=CSInterface.prototype.getOSInformation();return n.indexOf("Windows")>=0?t=t.replace("file:///",""):n.indexOf("Mac")>=0&&(t=t.replace("file://","")),t},CSInterface.prototype.evalScript=function(e,t){if(t==null||t==undefined)t=function(e){};window.__adobe_cep__.evalScript(e,t)},CSInterface.prototype.getApplicationID=function(){var e=this.hostEnvironment.appId;return e},CSInterface.prototype.getHostCapabilities=function(){var e=JSON.parse(window.__adobe_cep__.getHostCapabilities());return e},CSInterface.prototype.dispatchEvent=function(e){typeof e.data=="object"&&(e.data=JSON.stringify(e.data)),window.__adobe_cep__.dispatchEvent(e)},CSInterface.prototype.addEventListener=function(e,t,n){window.__adobe_cep__.addEventListener(e,t,n)},CSInterface.prototype.removeEventListener=function(e,t,n){window.__adobe_cep__.removeEventListener(e,t,n)},CSInterface.prototype.requestOpenExtension=function(e,t){window.__adobe_cep__.requestOpenExtension(e,t)},CSInterface.prototype.getExtensions=function(e){var t=JSON.stringify(e),n=window.__adobe_cep__.getExtensions(t),r=JSON.parse(n);return r},CSInterface.prototype.getNetworkPreferences=function(){var e=window.__adobe_cep__.getNetworkPreferences(),t=JSON.parse(e);return t},CSInterface.prototype.initResourceBundle=function(){var e=JSON.parse(window.__adobe_cep__.initResourceBundle()),t=document.querySelectorAll("[data-locale]");for(var n=0;n<t.length;n++){var r=t[n],i=r.getAttribute("data-locale");if(i)for(var s in e)if(s.indexOf(i)==0){var o=e[s];if(s.indexOf(".")==-1)r.innerHTML=o;else{var u=s.substring(s.indexOf(".")+1);r[u]=o}}}return e},CSInterface.prototype.dumpInstallationInfo=function(){return window.__adobe_cep__.dumpInstallationInfo()},CSInterface.prototype.getOSInformation=function(){var e=navigator.userAgent;if(navigator.platform=="Win32"||navigator.platform=="Windows"){var t="Windows platform";e.indexOf("Windows NT 5.0")>-1?t="Windows 2000":e.indexOf("Windows NT 5.1")>-1?t="Windows XP":e.indexOf("Windows NT 5.2")>-1?t="Windows Server 2003":e.indexOf("Windows NT 6.0")>-1?t="Windows Vista":e.indexOf("Windows NT 6.1")>-1?t="Windows 7":e.indexOf("Windows NT 6.2")>-1&&(t="Windows 8");var n="32-bit";return e.indexOf("WOW64")>-1&&(n="64-bit"),t+" "+n}if(navigator.platform=="MacIntel"||navigator.platform=="Macintosh"){var r=new String;r=e;var i=r.indexOf(")")-r.indexOf("Mac OS X"),s=r.substr(r.indexOf("Mac OS X"),i),o=s.replace("_",".");return o=o.replace("_","."),o}return"Unknown Operation System"},define("lib/CSInterface-4.0.0",function(){}),define("lib/CSInterface",["lib/CSInterface-4.0.0"],function(e){var t=new CSInterface;return t.SystemPath=SystemPath,t.EvalScript_ErrMessage=EvalScript_ErrMessage,t}),define("utils",["lib/CEPGlobals","lib/CSInterface","config","log","window"],function(e,t,n,r,i){var s=i.navigator.platform.toLowerCase().indexOf("win")>-1;r.info("Platform is: "+(s?"Windows":"Mac"));var o=t.getSystemPath(t.SystemPath.EXTENSION);r.info("Extension directory is: "+o);var u="/";s&&(u=t.getSystemPath(t.SystemPath.COMMON_FILES).substring(0,3)),r.info("Root directory is: "+u);var a="com.adobe.behance.shareonbehance.html.extension",f="/tmp/"+a;s&&(f=t.getSystemPath(t.SystemPath.USER_DATA)+"/../Local/Temp/"+a),e.fs.makedir(f);var l=function(e){r.info("Pinging "+n.behancePingUrl+" to check internet connection");var t=new i.XMLHttpRequest;t.open("GET",n.behancePingUrl,!0),t.onerror=function(){e.recoverableError("Unable to connect to internet","Please check your internet connection and try again.")},t.send()},c=function(e){r.info("Getting network preferences");var n=t.getNetworkPreferences();r.info("Found network preferences: "+r.stringify(n));if(n&&n.isAppOnline===!1){e.recoverableError("Unable to connect to internet","Please check plug-in network permissions and try again.");return}l(e)},h=function(e){var t=i.document.createElement("a");t.href=e;var n=t.hostname.split(".");return n=n.slice(n.length-2,n.length),{protocol:t.protocol,domain:n.join(".")}},p=function(t){if(!t){r.error("URL is null");return}var i=h(t);if(i.protocol!=="http:"&&i.protocol!=="https:"){r.error("Invalid protocol: "+i.protocol);return}var o=!1;for(var a=0;a<n.whiteListedDomains.length;a++){var f=n.whiteListedDomains[a];if(f===i.domain){o=!0;break}}if(!o){r.error("Invalid domain: "+i.domain);return}var l="/usr/bin/open";s&&(l=u+"Windows/explorer.exe"),r.info("Opening browser to url: "+t),e.process.createProcess(l,t)};return{checkInternetConnection:c,extensionDir:o,isWindows:s,openInDefaultBrowser:p,rootDir:u,tempDir:f}}),define("aamee",["lib/CEPGlobals","config","log","utils","window"],function(e,t,n,r,i){return{isExtensionEnabled:function(){n.info("Checking to see if AAMEE has the extension enabled or disabled");var s="/Library/Application Support/Adobe/CDI/override.xml";r.isWindows&&(s=r.rootDir+"ProgramData/Adobe/CDI/override.xml");var o=e.fs.readFile(s);if(!o||!o.data||o.data.length===0||o.err!==0)return n.info("Unable to read override.xml, assuming extension should be enabled"),!0;var u=null;try{var a=new i.DOMParser;u=a.parseFromString(o.data,"text/xml")}catch(f){return n.error("Error parsing override file, assuming extension is enabled"),!0}var l=u.documentElement.getAttribute("suppress");if(l==="true")return n.info("Global suppression is turned on, disable"),!1;var c=u.getElementsByTagName("Service");for(var h=0;h<c.length;h++){var p=c[h];if(p.getAttribute("name")===t.aameeServiceName&&p.getAttribute("suppress")==="true")return n.info("Found service specific supression, disable"),!1}return n.info("Unable to find an entry that suppresses this extension, assuming enabled"),!0}}}),IMSInterface.prototype.addEventListener=function(e,t){window.__adobe_cep__.addEventListener(e,t)},IMSInterface.prototype.removeEventListener=function(e,t){window.__adobe_cep__.removeEventListener(e,t)},IMSInterface.prototype.imsConnect=function(){var e=window.__adobe_cep__.imsConnect();return e},IMSInterface.prototype.imsDisconnect=function(e){window.__adobe_cep__.imsDisconnect(e)},IMSInterface.prototype.imsFetchAccounts=function(e,t){var n=window.__adobe_cep__.imsFetchAccounts(e,t);return n},IMSInterface.prototype.imsFetchAccessToken=function(e,t,n,r,i,s){window.__adobe_cep__.imsFetchAccessToken(e,t,n,r,i,s)},IMSInterface.prototype.imsRevokeDeviceToken=function(e,t,n,r,i){window.__adobe_cep__.imsFetchAccessToken(e,t,n,r,i,"REVOKE")},IMSInterface.prototype.imsSetProxyCredentials=function(e,t){window.__adobe_cep__.imsSetProxyCredentials(e,t)},IMSInterface.prototype.showAAM=function(e,t,n,r,i,s){window.__adobe_cep__.showAAM(e,t,n,r,i,s)},define("lib/IMSInterface-4.0.0",function(){}),define("lib/IMSInterface",["lib/IMSInterface-4.0.0"],function(e){return new IMSInterface}),define("host",["lib/CSInterface","log"],function(e,t){var n="Generic scripting error.";return{evalScript:function(r,i){t.info("Evaling script: "+r),e.evalScript(r,function(r){t.info("Received result from eval script: "+r);if(!r||r.length===0||r===e.EvalScript_ErrMessage){i.error(n);return}var s=JSON.parse(r);switch(s.status){case"success":i.success(s.value);break;case"error":i.error(s.message);break;case"recoverableError":i.recoverableError(s.message,s.subMessage);break;default:i.error(n)}})}}}),define("ui",["lib/CSInterface","lib/spin","config","log","utils","window"],function(e,t,n,r,i,s){var o=!1,u=!1,a=!1,f=s.document.getElementsByTagName("body")[0],l=s.document.getElementsByClassName("close-button")[0],c=s.document.getElementsByClassName("error")[0],h=s.document.getElementsByClassName("error-message")[0],p=s.document.getElementsByClassName("error-reference")[0],d=s.document.getElementsByClassName("error-reference-string")[0],v=s.document.getElementsByClassName("error-sub-message")[0],m=s.document.getElementsByClassName("help-link")[0],g=s.document.getElementsByClassName("behance-iframe")[0],y=s.document.getElementsByClassName("loading")[0],b=s.document.getElementsByClassName("loading-message")[0],w=function(){return g.contentWindow},E=function(t){r.info("Initializing iframe");var i=e.hostEnvironment,s=n.behanceIFrameUrl;s+="?destination=wipframe",s+="&extVersion="+n.version,s+="&appId="+i.appName,s+="&appVersion="+i.appVersion,s+="&appLocale="+i.appUILocale,s+="&accessToken="+t,r.info("Pointing iframe to "+s),g.src=s,o=!0},S=function(){c.style.display="none",p.style.display="none",g.style.display="none",y.style.display="none"},x=function(){r.info("Attempting to display iframe"),u?r.info("Already displaying error"):(a=!0,S(),g.style.display="block",r.info("Successfully displayed iframe"))},T=function(e,t){r.error("Attempting to display recoverable error: "+e),u?r.info("Already displaying error"):(u=!0,S(),h.innerHTML=e,t&&(v.innerHTML=t),c.style.display="block",r.info("Successfully displayed recoverable error"))},N=function(e){r.info("About to display error"),setTimeout(function(){r.error("Attempting to display error: "+e),u?r.info("Already displaying error"):(u=!0,S(),d.innerHTML=e,c.style.display="block",p.style.display="inline",r.info("Successfully displayed error"))},100)},C=function(t){t.keyCode===27&&(r.info("Caught ESC key, attempting to close extension"),e.closeExtension())},k=function(){r.info("Help link clicked, opening browser to help url"),i.openInDefaultBrowser(n.behanceHelpUrl)},L=function(){r.info("Close button clicked, closing extension"),e.closeExtension()},A=function(){var e={lines:30,length:0,width:2,radius:9,corners:0,color:"#1769ff",speed:2,trail:100,hwaccel:!0,className:"spinner",zIndex:2e9,top:"19px",left:"20px"};return(new t(e)).spin(b)},O=function(){r.info("Initializing UI"),A(),s.document.addEventListener("keydown",C),m.addEventListener("click",k),l.addEventListener("click",L),r.info("Done initializing UI")};return O(),{getIFrameWindow:w,hasInitializedIFrame:function(){return o},hasShownIFrame:function(){return a},initIFrame:E,showIFrame:x,showRecoverableError:T,showError:N}}),define("response",["log","ui"],function(e,t){return{create:function(n){return n||(n={}),n.success||(n.success=function(){e.info("Generic success callback triggered")}),n.recoverableError||(n.recoverableError=function(e,n){t.showRecoverableError(e,n)}),n.error||(n.error=function(e){t.showError(e)}),n}}}),define("ims",["lib/IMSInterface","config","host","log","response"],function(e,t,n,r,i){var s="com.adobe.csxs.events.internal.ims.FetchAccessToken",o="openid,AdobeID,creative_cloud,sao.behance",u=t.imsCi,a=t.imsCs,f=t.imsRu;return{getAccessToken:function(t){r.info("Connecting to IMS");var f=e.imsConnect(u);r.info("Connected to IMS, with ims reference: "+f);var l=function(){try{e.removeEventListener(s,c),e.imsDisconnect(f),r.info("Successfully cleaned up IMS")}catch(t){r.error("Error cleaning up IMS: "+r.stringify(t))}},c=function(e){r.info("Handling fetch access token callback: "+r.stringify(e)),e&&e.data&&e.data.access_token?(l(),t.success(e.data.access_token)):(l(),t.error("Error fetching access token."))};e.addEventListener(s,c),r.info("Getting current user id"),n.evalScript("getUserId()",i.create({success:function(n){r.info("Received callback from evalScript: "+n),n===null||n.length===0?(l(),t.recoverableError("Must sign in before connecting to Behance","Please close this window, sign into Photoshop, and try again.")):(r.info("Found a user, fetch access token"),e.imsFetchAccessToken(f,u,a,n,"",o))}}))}}}),define("message",["lib/CSInterface","config","log","ui","utils","window"],function(e,t,n,r,i,s){var o=!1,u=[],a=function(e){n.info("Attempting to send message of type: "+e.type);if(o){n.info("Behance iframe is ready, sending message");var i=r.getIFrameWindow();i.postMessage(JSON.stringify(e),t.behanceDomain)}else n.info("Behance iframe is not ready, queueing message"),u.push(e)},f=function(){n.info("Flushing queued messages");for(var e=0;e<u.length;e++)a(u[e]);u=[]};return s.onmessage=function(s){if(t.behanceDomain!==s.origin){n.info("Unable to respond to message from domain: "+s.origin);return}if(s.source!==r.getIFrameWindow()){n.info("Unable to respond to message from different iframe window");return}var u=JSON.parse(s.data);if(u){n.info("Received message: "+n.stringify(u)+", from origin: "+s.origin);switch(u.type){case"close":e.closeExtension();break;case"openBrowser":i.openInDefaultBrowser(u.url);break;case"ready":r.showIFrame();break;case"readyAcceptImage":o=!0,f()}}},{sendWip:function(e){a({type:"wip",image:e})}}}),define("wip",["lib/CEPGlobals","host","log","response","utils"],function(e,t,n,r,i){var s={},o=function(e){var t=e.split(".");return t.length>1&&t[t.length-1].length>0&&t.pop(),t.join(".")},u=function(t){var r=null;try{n.info("Reading file: "+t),r=e.fs.readFile(t,e.encoding.Base64).data.replace(/\s/g,""),n.info("Successfully read file")}catch(i){n.error("Error reading file")}return r},a=function(t){try{n.info("Deleting file: "+t);var r=e.fs.deleteFile(t);!r||r.err!==0?n.error("Error deleting file"):n.info("Successfully deleted file")}catch(i){n.error("Error deleting file: "+n.stringify(i))}};return s.isLoaded=function(){return s.value&&s.value.length>0},s.load=function(e){n.info("Loading wip module");var f=i.tempDir+"/wip.jpg";n.info("Saving temporary image to "+f),t.evalScript("saveActiveDocument('"+f+"')",r.create({success:function(t){n.info("Received callback from evalScript: "+t);if(t===null){e.error("Error saving document.");return}n.info("Successfully saved active document"),s.mime="image/jpeg",s.encoding="base64",s.title=o(t),s.value=u(f),a(f),s.value&&s.value.length>0?(n.info("Successfully read WIP, sending to iframe"),e.success()):e.error("Error reading document.")}}))},s}),require(["aamee","config","ims","log","message","response","ui","utils","wip"],function(e,t,n,r,i,s,o,u,a){var f=function(){r.info("Starting timeout countdown"),setTimeout(function(){r.info("Checking to see if iframe has been shown: "+o.hasShownIFrame()),o.hasShownIFrame()||o.showError("Error connecting to Behance.")},t.timeout)},l=function(){a.load(s.create({success:function(){i.sendWip(a),o.hasInitializedIFrame()&&f()}}))},c=function(){r.info("Starting Behance extension");if(!e.isExtensionEnabled()){o.showError("Extension is disabled.");return}u.checkInternetConnection(s.create()),n.getAccessToken(s.create({success:function(e){o.initIFrame(e),a.isLoaded()&&f()}})),setTimeout(l,100)};c()}),define("main",function(){});