/**************************************************************************
 *                                                                        *
 *  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 */
var MIN_IMAGE_DIMENSION=320,MAX_PIXELS=1024e3,createSuccessResult=function(e){return'{"status": "success", "value": "'+e+'"}'},createErrorResult=function(e){return'{"status": "error", "message": "'+e+'"}'},createRecoverableErrorResult=function(e,t){return'{"status": "recoverableError", "message": "'+e+'", "subMessage": "'+t+'"}'},getDocumentWidth=function(){return parseInt(app.activeDocument.width,10)},getDocumentHeight=function(){return parseInt(app.activeDocument.height,10)},fitToMaxDimension=function(e){var t=stringIDToTypeID("3caa3434-cb67-11d1-bc43-0060b0a13dc4"),n=new ActionDescriptor,r=app.charIDToTypeID("#Pxl");n.putUnitDouble(app.charIDToTypeID("Wdth"),r,e),n.putUnitDouble(app.charIDToTypeID("Hght"),r,e),app.executeAction(t,n,DialogModes.NO)},doSaveActiveDocument=function(e,t){try{app.activeDocument.quickMaskMode=!1}catch(n){}try{app.activeDocument.flatten()}catch(n){}if(app.activeDocument.mode===DocumentMode.BITMAP)try{app.activeDocument.changeMode(ChangeMode.GRAYSCALE)}catch(n){}try{app.activeDocument.changeMode(ChangeMode.RGB)}catch(n){}try{app.activeDocument.bitsPerChannel=BitsPerChannelType.EIGHT}catch(n){}var r=getDocumentWidth()*getDocumentHeight();if(r>MAX_PIXELS){var i=Math.sqrt(MAX_PIXELS/r),s=Math.floor(getDocumentWidth()*i),o=Math.floor(getDocumentHeight()*i),u=s>o?s:o;fitToMaxDimension(u)}var a=new JPEGSaveOptions;return a.quality=12,app.activeDocument.saveAs(new File(e),a,!0),createSuccessResult(t)},validateActiveDocument=function(){var e=null;if(app.documents.length===0)e=createRecoverableErrorResult("Must have one open document","Please close this window, open a document, and try again.");else if(getDocumentWidth()<MIN_IMAGE_DIMENSION||getDocumentHeight()<MIN_IMAGE_DIMENSION)e=createRecoverableErrorResult("Image must be at least 320 x 320","Please close this window and try sharing a larger image.");else{var t=getDocumentWidth()*getDocumentHeight();if(t>MAX_PIXELS){var n=Math.sqrt(MAX_PIXELS/t),r=Math.floor(getDocumentWidth()*n),i=Math.floor(getDocumentHeight()*n);i<MIN_IMAGE_DIMENSION?e=createRecoverableErrorResult("Image is too narrow","Please close this window and try sharing an image that is taller."):r<MIN_IMAGE_DIMENSION&&(e=createRecoverableErrorResult("Image is too narrow","Please close this window and try sharing an image that is wider."))}}return e},saveActiveDocument=function(e){var t=app.preferences.rulerUnits,n=app.displayDialogs;app.preferences.rulerUnits=Units.PIXELS,app.displayDialogs=DialogModes.NO;var r=null,i=createErrorResult("Error saving document.");try{var s=validateActiveDocument();if(s)i=s;else{var o=app.activeDocument.name;r=app.activeDocument.duplicate(),i=doSaveActiveDocument(e,o)}}catch(u){}finally{return r&&r.close(SaveOptions.DONOTSAVECHANGES),app.preferences.rulerUnits=t,app.displayDialogs=n,i}},getUserId=function(){var e="";try{var t=app.charIDToTypeID("bhnc"),n=new ActionReference;n.putProperty(app.charIDToTypeID("Prpr"),t),n.putEnumerated(app.charIDToTypeID("capp"),app.charIDToTypeID("Ordn"),app.charIDToTypeID("Trgt"));var r=app.executeActionGet(n);r.hasKey(t)&&(e=r.getString(t))}catch(i){}return createSuccessResult(e)};