/**************************************************************************
 *                                                                        *
 *  Copyright 2014 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 3.0.0.17 */
var MIN_DIMENSION_WIDTH=640,MIN_DIMENSION_HEIGHT=320,MAX_DIMENSION_WIDTH=1500,MAX_DIMENSION_HEIGHT=1500,MAX_FILE_SIZE=3242880,MAX_PIXELS=MAX_DIMENSION_WIDTH*MAX_DIMENSION_HEIGHT,MAX_ASPECT_RATIO=10,localize=function(e){return e},escapeForJson=function(e){var t=e;return t=t.replace(/\\/g,"\\\\"),t=t.replace(/"/g,'\\"'),t},createSuccessResult=function(e){return'{"status": "success", "value": "'+escapeForJson(e)+'"}'},createErrorResult=function(e){return'{"status": "error", "message": "'+escapeForJson(e)+'"}'},createRecoverableErrorResult=function(e,t){return'{"status": "recoverableError", "message": "'+escapeForJson(e)+'", "subMessage": "'+escapeForJson(t)+'"}'},createCancelResult=function(){return'{"status": "cancel"}'},getDocumentDimensions=function(){return{width:parseInt(app.activeDocument.width,10),height:parseInt(app.activeDocument.height,10)}},getDocumentTooWideError=function(){return createRecoverableErrorResult(localize("Recoverable_error_document_too_narrow"),localize("Recoverable_error_document_too_narrow_share_taller"))},getDocumentTooTallError=function(){return createRecoverableErrorResult(localize("Recoverable_error_document_too_narrow"),localize("Recoverable_error_document_too_narrow_share_wider"))},validateDocument=function(){var e=null;if(app.documents.length===0)e=createRecoverableErrorResult(localize("Recoverable_error_no_documents_open"),localize("Recoverable_error_no_documents_open_therefore_open_one"));else{var t=getDocumentDimensions(),n=t.width,r=t.height;r*MAX_ASPECT_RATIO<n?e=getDocumentTooWideError():n*MAX_ASPECT_RATIO<r&&(e=getDocumentTooTallError())}return e};var DEFAULT_RESOLUTION=96,MAX_RESOLUTION=2400,MIN_RESOLUTION=1,getDocumentTooWideError=function(){return createRecoverableErrorResult(localize("Recoverable_error_spread_too_narrow"),localize("Recoverable_error_spread_too_narrow_share_taller"))},getDocumentTooTallError=function(){return createRecoverableErrorResult(localize("Recoverable_error_spread_too_narrow"),localize("Recoverable_error_spread_too_narrow_share_wider"))},getDocumentDimensions=function(){var e=app.activeDocument.layoutWindows.item(0),t=e.activeSpread,n=0,r=0,i=t.pages;for(var s=0;s<i.length;s++){var o=i.item(s).bounds,u=o[2]-o[0];u>r&&(r=u),n+=o[3]-o[1]}return{width:n,height:r}},getPageString=function(e){var t=null,n=app.scriptPreferences.version,r=app.generalPreferences.pageNumbering;try{app.scriptPreferences.version=3,app.generalPreferences.pageNumbering=PageNumberingOptions.absolute,t="+"+e.pages.item(0).name}catch(i){}finally{app.scriptPreferences.version=n,app.generalPreferences.pageNumbering=r}return t},getResolution=function(){var e=getDocumentDimensions(),t=e.width,n=e.height,r=DEFAULT_RESOLUTION,i=DEFAULT_RESOLUTION;t*r<MIN_DIMENSION_WIDTH&&(r=Math.ceil(MIN_DIMENSION_WIDTH/t)),n*i<MIN_DIMENSION_HEIGHT&&(i=Math.ceil(MIN_DIMENSION_HEIGHT/n));var s=r<i?i:r,o=n*t*s*s;if(o>MAX_PIXELS&&s===DEFAULT_RESOLUTION){var u=Math.sqrt(MAX_PIXELS/o),a=Math.floor(s*u-1);t*a<MIN_DIMENSION_WIDTH||n*a<MIN_DIMENSION_HEIGHT?s=Math.ceil(s*u):s=a}return s},isMaster=function(e){var t=!1,n=app.activeDocument.masterSpreads;for(var r=0;r<n.length;r++){var i=n.item(r);if(i.id===e.id){t=!0;break}}return t},validateIdDocument=function(e){var t=null;return app.activeDocument.layoutWindows.length===0?t=t||createRecoverableErrorResult(localize("Recoverable_error_no_documents_open"),localize("Recoverable_error_no_documents_open_therefore_open_one")):isMaster(app.activeDocument.layoutWindows.item(0).activeSpread)?t=t||createRecoverableErrorResult(localize("Recoverable_error_sharing_master"),localize("Recoverable_error_sharing_master_share_regular")):e>MAX_RESOLUTION?t=t||createRecoverableErrorResult(localize("Recoverable_error_spread_too_small"),localize("Recoverable_error_spread_too_small_share_larger")):e<MIN_RESOLUTION&&(t=t||createRecoverableErrorResult(localize("Recoverable_error_spread_too_large"),localize("Recoverable_error_spread_too_large_share_smaller"))),t},saveActiveDocument=function(e){var t=app.scriptPreferences.userInteractionLevel,n=app.scriptPreferences.measurementUnit;app.scriptPreferences.userInteractionLevel=UserInteractionLevels.neverInteract,app.scriptPreferences.measurementUnit=MeasurementUnits.inches;var r=validateDocument();if(r)return r;var i=getResolution();r=validateIdDocument(i);if(r)return r;var s=app.jpegExportPreferences,o=s.antiAlias,u=s.jpegColorSpace,a=s.embedColorProfile,f=s.jpegRenderingStyle,l=s.exportingSpread,c=s.exportResolution,h=s.jpegExportRange,p=s.pageString,d=s.jpegQuality,v=s.simulateOverprint,m=s.useDocumentBleeds,g=createErrorResult(localize("Error_saving_spread"));try{var y=app.activeDocument.layoutWindows.item(0),b=y.activeSpread;app.jpegExportPreferences.properties={antiAlias:!0,embedColorProfile:!1,exportingSpread:!0,exportResolution:i,jpegColorSpace:JpegColorSpaceEnum.rgb,jpegExportRange:ExportRangeOrAllPages.exportRange,jpegQuality:JPEGOptionsQuality.maximum,jpegRenderingStyle:JPEGOptionsFormat.baselineEncoding,pageString:getPageString(b),simulateOverprint:y.overprintPreview,useDocumentBleeds:!1},app.activeDocument.exportFile(ExportFormat.jpg,new File(e)),g=createSuccessResult(app.activeDocument.name)}catch(w){}finally{app.scriptPreferences.userInteractionLevel=t,app.scriptPreferences.measurementUnit=n,app.jpegExportPreferences.properties={antiAlias:o,embedColorProfile:a,exportingSpread:l,exportResolution:c,jpegColorSpace:u,jpegExportRange:h,jpegQuality:d,jpegRenderingStyle:f,pageString:p,simulateOverprint:v,useDocumentBleeds:m}}return g};