﻿var myIds = {
		options: {
			myDocument : null,
			retour : {error :  0, retour : new Array()},
			path_syspad : '',
			path_project : '',
			path_xml : '',
			path_font: '',
			path_export : '',
			path_gabarit : '',
			path_package : '',
			path_package_filename : '',
			path_jsx_supp: '',
			protocole_color : null,
			gabarit_name : '',
			gabarit_add_suffixe : '',
			build_indd : 0,
			build_pdf : 0,
			folio : 992, // Convention : Signifie que le folio n'a pas été envoyé
			nb_page : 1,
			nb_product : 1,
			id_file : '',
			is_fiche_hotel : '',
			is_fiche_excursion : '',
			is_sejete18 : '',
			is_white: '',
			is_double: '',
			_ARGS_NAMES : new Array(),
			_ARGS_VALUES : new Array()
		},
		_construct: function () {
			alert('BEGIN DEFAULT GENERATION Projet ' + this.options.path_project + ' Gab ' + this.options.gabarit_name+this.options.gabarit_add_suffixe);
			alert("getParams()");			
			this.getParams();
			alert("setOptions()");			
			this.setOptions();
			alert("loadJSX()");			
			this.loadJSX();
			alert("openDocument()");			
            if (this.openDocument() == false) {
				return false;
			}
			alert("unlockLayers()");			
			this.unlockLayers();
			alert("removeElement()");			
			this.removeElement();
			alert("setFolio()");			
			this.setFolio();
			alert("setColor()");			
			this.setColor();
			alert("removeBlock()");			
			this.removeBlock();
			alert("importXml()");			
			if (this.importXml() == false) {
			alert("closeDocument()");			
				this.closeDocument();
				return false;
			}
        

			alert("removeRetour()");			
            this.removeRetour();
			alert("syspadReplacement()");			
			this.syspadReplacement();
			alert("loadProjectJSX()");			
			this.loadProjectJSX();
			alert("cadragePhotos()");			
			this.cadragePhotos();
			alert("debord()");			
			this.debord();
        
			alert("INDD PARAM = " + this.options.build_indd);
			if (this.options.build_indd == 0) {
				alert("exportPDF()");			
				this.exportPDF();
			}
			if (this.options.build_indd == 2) {
				alert("exportINDD()");			
				this.exportINDD();
			}
			if (this.options.build_indd == 1) {
				alert("exportHD()");			
				this.exportHD();
			}
			if (this.options.build_indd == 11 || this.options.build_indd == 12) {
				alert("exportPackage()");			
				this.packageINDD();
			}
			if (this.closeDocument() == false) {
				return false;
			}
			alert('END DEFAULT GENERATION');
		},
		removeBlock : function() {
			
			try {
				if (this.options.is_sejete18 == 1) {
					var myRuleSet = new Array (new EmptyBloc());
					var elements = this.options.myDocument.xmlElements;
					XMLRules.__processRuleSet(elements.item(0), myRuleSet);
				}
			}
			catch (e) {
				alert('Erreur removeBlock ('+e+')');
				return false;
			}
			
			function EmptyBloc() {
				this.name = "EmptyBloc";
				this.xpath = '//*';
				this.apply = function(myElement, myRuleProcessor){
					try {
						var name_balise = myElement.markupTag.name;
						if (name_balise == 'BLOC_NOUVEAU') {
							var obj = myElement.xmlContent;
							obj.contents = '';
						}
						return false;
					}
					catch (e) {
						alert('ERROR EmptyBloc '+e);
					}
				}
			}
		},
        removeRetour : function() {
            var myRuleSet = new Array (new RemoveRetour());
            var elements = this.options.myDocument.xmlElements;
            XMLRules.__processRuleSet(elements.item(0), myRuleSet);



            function RemoveRetour() {
                this.name = "RemoveRetour";
                this.xpath = '//*[@remove_retour="true"]';

                this.apply = function(myElement, myRuleProcessor) {
                    try {
                        var name_balise = myElement.markupTag.name;
                        var obj = myElement.xmlContent;
                        obj.contents="";
                        obj.recompose();
                    }
                    catch (e) {
                        alert('ERREUR RemoveRetour : '+e + ' line : ' + e.line);
                    }
                }
            }  
        },
		loadProjectJSX : function () {
			try {		
				if(this.options.path_jsx_supp){
					alert(' chemin jsx project : ' + this.options.path_jsx_supp);
					var myFileJsx = new File(this.options.path_jsx_supp);
		
					if (myFileJsx.exists == true) {
						$.evalFile(myFileJsx);
					}
					else {
						alert('Impossible d\'inclure le fichier  ('+myFileJsx+')');
					}
				}	
			}
			catch (e) {
				alert('ERROR loadProjectJSX ' +e);
				alert('Synthaxe JS [loadProjectJSX] ('+e+')');
				return false;
			}
			return true;
		},
		loadJSX : function () {
			try {
				var myFileJsx = new File('/Users/administrateur/SpoolerIndesign/jsx/json2.js');

				if (myFileJsx.exists == true) {
					$.evalFile(myFileJsx);
				}
				else {
					alert('Impossible d\'inclure le fichier  ('+myFileJsx+')');
				}
				var myFileJsx = new File('/Users/administrateur/SpoolerIndesign/jsx/xml_rules.jsx');

				if (myFileJsx.exists == true) {
					$.evalFile(myFileJsx);
				}
				else {
					alert('Impossible d\'inclure le fichier  ('+myFileJsx+')');
				}
			}
			catch (e) {
				alert('ERROR loadJSX' +e);
				alert('Synthaxe JS [loadJSX] ('+e+')');
				return false;
			}
			return true;
		},
		setError: function (val, txt) {
			this.options.retour.error = val;
			this.options.retour.retour.push(txt);
		},
		openDocument: function () {
			try {
				if (this.options.nb_page == 1 && this.options.gabarit_add_suffixe == 1) {
					if (this.options.folio % 2) {
						this.options.gabarit_name += '_D';
					}
					else {
						this.options.gabarit_name += '_G';
					}
				}
			}
			catch (e) {
				alert('Synthaxe JS [openDocument][folio] ('+e+')');
				alert('DOC NOT OK' + e);
				return false;
			}
			try {
				var fileNameGabarit = this.options.gabarit_name + '.indt';
				alert('test : ' + this.options.path_gabarit+fileNameGabarit);
				var myFile = File(this.options.path_gabarit+fileNameGabarit);
				if (myFile.exists == true) {
					if (this.options.path_font) {
						var fontPath = this.options.path_font + 'tmpGab.indt';
						alert('On fait le path : ' + fontPath);
						var tmpFile = app.open(myFile);
						alert('On ouvre le fichier : ' + myFile);
						tmpFile.save(File(fontPath));
						alert('Creation : ' + fontPath);
						tmpFile.close();
						this.options.myDocument = app.open(fontPath);
					} else {
						this.options.myDocument = app.open(myFile);
					}
				}
				else {
					alert('Impossible d\'ouvrir le fichier ('+this.options.path_gabarit+fileNameGabarit+')');
					return false;
				}
			}
			catch (e) {
				alert('Synthaxe JS [openDocument] ('+e+')');
				return false;
			}
			return true;
		},
		unlockLayers : function() {
			try {
				for (var i = 0; i < this.options.myDocument.layers.length; i++) {
					var obj_layer = this.options.myDocument.layers[i];
					obj_layer.locked = false;
					obj_layer.visible = true;
				} 
			}
			catch (e) {
				alert('Synthaxe JS [unlockLayers] ('+e+')');
				return false;
			}
			return true;
		},
		removeElement : function() {
			try {
				var rootElement = this.options.myDocument.xmlElements.item(0);
				var myBlocTableau = rootElement.xmlElements.itemByName('BLOC_TABLEAU');
				if (myBlocTableau.isValid == true) {
					var xmlElementTest = myBlocTableau.xmlElements.itemByName('TABLEAU').xmlContent;
					xmlElementTest.remove();
				}
			}
			catch (e) {
				alert('Synthaxe JS [removeElement] ('+e+')');
				return false;
			}
			return true;
		},
		debord : function() {
			var myDocument = this.options.myDocument;
			var style_object = myDocument.objectStyles.itemByName('DEBORD');
			var myPageItems = myDocument.allPageItems;
			
			
			if (style_object.isValid == false) {
				return false;
			}
			
			for (var i = 0; i <= myPageItems.length; i++) {
				if (myPageItems[i] == '[object TextFrame]') {
					if (myPageItems[i].overflows == true) {
						try {
							myPageItems[i].applyObjectStyle(style_object);
						}
						catch (e) {
							alert('Synthaxe JS [debord] ('+e+')');
							return false;
						}
					}
					else {
						if (myPageItems[i].parentStory.isValid == true) {
							var obj_story = myPageItems[i].parentStory;
							if (obj_story.associatedXMLElement == '[object XMLElement]') {
								var myElement = obj_story.associatedXMLElement;
								var obj_attribute = myElement.xmlAttributes.itemByName('bloc_debord');
								var do_debord = obj_attribute.isValid ? obj_attribute.value : 'false';
								if (do_debord == 'true' && obj_attribute.isValid == true) {
									var nb_containers = obj_story.textContainers.length;
									if (obj_story.textContainers[(nb_containers-1)].lines.length >= 1) {
										obj_story.textContainers[(nb_containers-1)].applyObjectStyle(style_object);
										obj_attribute.value = 'false';
									}
								}
							}
						}
					}
				}
			}

		},
		cadragePhotos : function() {
			var gabarit_name = this.options.gabarit_name;
			try {
				var myRuleSet = new Array (new PhotoCadrageAuto(), new PhotoCadrage());
				var elements = this.options.myDocument.xmlElements;
				XMLRules.__processRuleSet(elements.item(0), myRuleSet);
			}
			catch (e) {
				alert('Erreur Cadrage Photos ('+e+')');
				return false;
			}
			
			
			function PhotoCadrageAuto() {
				this.name = "PhotoCadrageAuto";
				this.xpath = '//*[@do_cadrage="0"]';
				
				this.apply = function(myElement, myRuleProcessor) {
					try {
						var name_balise = myElement.markupTag.name;
						var obj = myElement.xmlContent;
						
						
						if (name_balise == 'CARTE' && (obj == '[object PDF]' || obj == '[object EPS]' || obj == '[object Image]')) {
							obj.absoluteHorizontalScale = 100;
							obj.absoluteVerticalScale = 100;
							obj.verticalScale = 100;
							obj.horizontalScale = 100;
							
							var obj_attribute = myElement.xmlAttributes.itemByName('fit');
							var do_fit = obj_attribute.isValid ? ((obj_attribute.value == 'false') ? false : true) : true
							if (do_fit == true) {
								obj.fit(FitOptions.CENTER_CONTENT);
							}
						}
						else if ((obj == '[object Image]' || obj == '[object PDF]' || obj == '[object EPS]') && name_balise != 'CARTE') {
							obj.fit(FitOptions.FILL_PROPORTIONALLY);
						}
						return false;
					}
					catch (e) {
						alert('ERREUR PhotoCadrageAuto '+e);
					}
				}
			}
			function PhotoCadrage() {
				this.name = "PhotoCadrage";
				this.xpath = '//*[@do_cadrage="1"]';

				this.apply = function(myElement, myRuleProcessor) {
					try {
						var name_balise = myElement.markupTag.name;
						var obj = myElement.xmlContent;
						if (obj == '[object Image]' || obj == '[object PDF]' || obj == '[object EPS]') {
							var obj_attribute = myElement.xmlAttributes.itemByName('do_cadrage');
							var do_cadrage = obj_attribute.isValid  ?obj_attribute.value : '';
							if (do_cadrage == 1) {
								var obj_attribute = myElement.xmlAttributes.itemByName('w');
								var w = obj_attribute.isValid  ? obj_attribute.value : '';
								var obj_attribute = myElement.xmlAttributes.itemByName('h');
								var h = obj_attribute.isValid  ? obj_attribute.value : '';
								var obj_attribute = myElement.xmlAttributes.itemByName('posx');
								var posx = obj_attribute.isValid  ? obj_attribute.value : '';
								var obj_attribute = myElement.xmlAttributes.itemByName('posy');
								var posy = obj_attribute.isValid  ? obj_attribute.value : '';
								
								var obj_attribute = myElement.xmlAttributes.itemByName('rotation');
								var rotation = obj_attribute.isValid  ? obj_attribute.value : '';
								var obj_mirror = myElement.xmlAttributes.itemByName('mirror');
								var mirror = obj_mirror.isValid  ? obj_mirror.value : '';
								
								
								obj.fit(FitOptions.CONTENT_TO_FRAME);
								
								var sav_rotation = obj.parent.absoluteRotationAngle;

								if (sav_rotation != 0) {
									obj.parent.absoluteRotationAngle = 0;
								}
								obj.resize(BoundingBoxLimits.GEOMETRIC_PATH_BOUNDS, AnchorPoint.TOP_LEFT_ANCHOR, ResizeMethods.REPLACING_CURRENT_DIMENSIONS_WITH, new Array(UnitValue(w,"px").as('pt'), UnitValue(h,"px").as('pt')));
								
								
								obj.move('', new Array (posx+" px", posy+" px"));
								

								
								if (mirror == 1) {
									myRotateMatrix = app.transformationMatrices.add({counterclockwiseRotationAngle:Number(rotation)});
									obj.transform(CoordinateSpaces.pasteboardCoordinates, AnchorPoint.centerAnchor, myRotateMatrix);
									
									var fixed_rotation = Number(rotation)*-1;
									obj.flipItem(Flip.HORIZONTAL, AnchorPoint.CENTER_ANCHOR);
								}
								else if (rotation != '0') {
									myRotateMatrix = app.transformationMatrices.add({counterclockwiseRotationAngle:-Number(rotation)});
									obj.transform(CoordinateSpaces.pasteboardCoordinates, AnchorPoint.centerAnchor, myRotateMatrix);
								}
								
								
								if (sav_rotation != 0) {
									obj.parent.absoluteRotationAngle = sav_rotation;
								}
								
							}
						
						}
						return false;
					}
					catch (e) {
						alert('ERREUR PhotoCadrage '+e);
					}
				}
			}
			
			
		},
		exportINDD : function() {
			try {
				var obj_layer = this.options.myDocument.layers.itemByName('BLOC_GRIS');
				if (obj_layer.isValid == true) {
					obj_layer.remove();
				}
			}
			catch (e) {
				alert('ERROR REMOVE LAYER (indd) '+e);
			}
			try {
				var to = new File('/Users/administrateur/SpoolerIndesign/out/'+this.options.id_file);
				var flag = Folder(to).create();
				if (flag === false) {
					alert('Le dossier package du projet n\'existe pas. ' +this.options.path_export);
					return false;
				}
				
				this.options.myDocument.save(new File('/Users/administrateur/SpoolerIndesign/out/'+this.options.id_file+'/'+this.options.path_package_filename+'.indd'));
			}
			catch (e) {
				return false;
			}
			return true;
		},
		exportHD: function() {
			var myPresets = app.pdfExportPresets.everyItem().name;

			var myPDFExportPreset = app.pdfExportPresets.item("PDF HD 1.3");
			
			alert(myPDFExportPreset.name);
			var highQualProperties = app.pdfExportPresets.item(0).properties; // [High Quality Print]
			app.pdfExportPreferences.properties = highQualProperties;  
			app.pdfExportPreferences.useDocumentBleedWithPDF = true;  
			app.pdfExportPreferences.cropMarks = true; 
				
			
			alert('JobOptions => ' + myPDFExportPreset.name);
			alert('JobOptions isValid => ' + myPDFExportPreset.isValid);
			alert(myPDFExportPreset.name);

			var obj_folder = new Folder(this.options.path_syspad);
			if (obj_folder.exists == false) {
				alert('Le dossier preview du projet n\'existe pas.');
				return false;
			}

			var to = new File(this.options.path_syspad+this.options.id_file);
			var flag = Folder(to).create();
			if (flag === false) {
				alert('Le dossier package du projet n\'existe pas. ' +this.options.path_export);
				return false;
			}

			var path_export_pdf = this.options.path_syspad+this.options.id_file+'/'+this.options.id_file+'.pdf';
			var path_export_jpg = this.options.path_syspad+this.options.id_file+'/'+this.options.id_file+'.jpg';




			this.options.myDocument.exportFile(ExportFormat.pdfType, File(path_export_pdf, false, myPDFExportPreset));

			app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.medium;
			app.jpegExportPreferences.exportingSpread = false; //Permet de split les jpeg en cas de multi page
			app.jpegExportPreferences.exportResolution = 72;
			this.options.myDocument.exportFile(ExportFormat.jpg, File(path_export_jpg,myPDFExportPreset));	
		},
		packageINDD : function() {
			try {
				var obj_layer = this.options.myDocument.layers.itemByName('BLOC_GRIS');
				if (obj_layer.isValid == true) {
					obj_layer.remove();
				}
			}
			catch (e) {
				alert('ERROR REMOVE LAYER (package) '+e);
			}
			
			
			try {
				var obj_folder = new Folder(this.options.path_package);
				if (obj_folder.exists == false) {
					alert('Le dossier export du projet n\'existe pas. ' +this.options.path_package);
					return false;
				}
				var obj_new_file = new File(this.options.path_package+this.options.path_package_filename+'.indd');
					
				var myDocumentTmp = this.options.myDocument.save(obj_new_file);
					
				
				var xml_elt = myDocumentTmp.xmlElements.item(0).xmlElements;
				var stop = xml_elt.count();
				for (var i = 0; i < stop; i++) {
					xml_elt.firstItem().untag()
				}

				var to = new File(this.options.path_package+'/'+this.options.path_package_filename);
				var flag = Folder(to).create();
				if (flag === false) {
					alert('Le dossier package du projet n\'existe pas. ' +this.options.path_export);
					return false;
				}
				
				var obj_folder = new Folder(this.options.path_package+'/'+this.options.path_package_filename+'/');
				if (obj_folder.exists == false) {
					alert('Le dossier export du projet n\'existe pas. ' +this.options.path_export);
					return false;
				}
				
				var myInddfile = File(this.options.path_package+'/'+this.options.path_package_filename+'/'); 
				
				var copyingFonts = true;
				var copyingLinkedGraphics = true;
				var copyingProfiles = true;
				var updatingGraphics = true;
				var includingHiddenLayers = true;
				var ignorePreflightErrors = true;
				var creatingReport = true;
				var versionComments = "comment";
				var forceSave = true;
				
				myDocumentTmp.packageForPrint(
						myInddfile, false, true, false, true, false, true, false
				);
				myDocumentTmp.close();
				
				obj_new_file.remove();
				var myInddfile = File(this.options.path_package+'/'+this.options.path_package_filename+'/'+this.options.path_package_filename+'.indd'); 
				var to = new File(this.options.path_package+'/'+this.options.path_package_filename+'/'+this.options.path_package_filename+'.idml');
				if (myInddfile.exists == true) {
					myDocumentTmp = app.open(myInddfile);
					myDocumentTmp.exportFile(ExportFormat.INDESIGN_MARKUP, to);
					myDocumentTmp.close();
				}
			}
			catch (e) {
				alert('Erreur lors du package : ' +e);
				return false;
			}
			return true;
		},
		exportPDF : function() {
			try {
				var obj_folder = new Folder(this.options.path_syspad);
				if (obj_folder.exists == false) {
					alert('Le dossier preview du projet n\'existe pas.');
					return false;
				}

				var path_export_pdf = this.options.path_syspad+this.options.id_file+'/'+this.options.id_file+'.pdf';
				var path_export_jpg = this.options.path_syspad+this.options.id_file+'/'+this.options.id_file+'.jpg';
				var path_export_thumb = this.options.path_syspad+this.options.id_file+'/'+this.options.id_file+'_thumb.jpg';
				
				var to = new File(this.options.path_syspad+this.options.id_file);
				var flag = Folder(to).create();
				if (flag === false) {
					alert('Le dossier package du projet n\'existe pas. ' +this.options.path_export);
					return false;
				}
				
                var profile = '[Taille de fichier minimale]';
				var obj_profile = app.pdfExportPresets.item(profile);
                
				app.pdfExportPreferences.useDocumentBleedWithPDF = false;  
				app.pdfExportPreferences.cropMarks = false; 
				app.pdfExportPreferences.exportReaderSpreads = true;
				app.pdfExportPreferences.colorBars = false;
				app.pdfExportPreferences.pageInformationMarks = false;
				app.pdfExportPreferences.colorBitmapCompression = BitmapCompression.jpeg;
				app.pdfExportPreferences.colorBitmapQuality = CompressionQuality.maximum;
				this.options.myDocument.exportFile(ExportFormat.pdfType, File(path_export_pdf,obj_profile));
				
				app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.medium;
				app.jpegExportPreferences.exportingSpread = false; //Permet de split les jpeg en cas de multi page
				app.jpegExportPreferences.exportResolution = 72;
				this.options.myDocument.exportFile(ExportFormat.jpg, File(path_export_jpg,obj_profile));
			
			}
			catch (e) {
				alert('Erreur lors de l\'extraction du pdf : ' +e);
				alert('BAH CA MARCHE PAS : ' + e);
				return false;

            }
			return true;
		},
		closeDocument : function () {
			try {
				this.options.myDocument.close();
			}
			catch (e) {
				return false; 
			}
			return true;
		},
		importXml: function () {
			try {
				this.options.myDocument.xmlImportPreferences.removeUnmatchedExisting = true;
				this.options.myDocument.xmlElements.item(0).importXML(File(this.options.path_xml+'generate.xml'));
			}
			catch (e) {
				alert('Impossible d\'importer les donn&eacute;es dans le gabarit ('+e+')');
				return false;
			}
			return true;
		},
		syspadReplacement: function () {
			var myObj = this;
			try {
				var myDocument = this.options.myDocument;
				var myRuleSet = new Array (new SyspadReplacement());
				var elements = this.options.myDocument.xmlElements;
				XMLRules.__processRuleSet(elements.item(0), myRuleSet);
				var myRuleSet = new Array (new Move(), new Resize(), new Scale(), new FrameToContent(), new Tracking(), new ChangeNbColumn());
				var elements = this.options.myDocument.xmlElements;
				XMLRules.__processRuleSet(elements.item(0), myRuleSet);
				var myRuleSet = new Array (new FrameToContent());
				var elements = this.options.myDocument.xmlElements;
				XMLRules.__processRuleSet(elements.item(0), myRuleSet);
                var myRuleSet = new Array (new ApplyObjectStyle());
				var elements = this.options.myDocument.xmlElements;
				XMLRules.__processRuleSet(elements.item(0), myRuleSet);
			}
			catch (e) {
				alert('Erreur Syspad Replacement ('+e+')');
				return false;
			}
			
			function SyspadReplacement(){
				try {
				    this.name = "SyspadReplacement";
				    this.xpath = "//SYSPAD_REPLACEMENT";

				    this.apply = function(myElement, myRuleProcessor) {
				    	var specialChar = myElement.xmlContent.contents;
				    	myElement.xmlContent.contents = "";
				    	myElement.xmlContent.contents = eval(specialChar);
				        return false;
				    }
				}
				catch (e) {
					myObj.setError(2, 'Erreur Syspad Replacement ('+e+')');
				}
			}
			function Tracking(){
				this.name = "Tracking";
				this.xpath = '//*[@tracking="true"]';
				
				this.apply = function(myElement, myRuleProcessor) {
					try {
						var obj = myElement.xmlContent;
						if (obj.parent == '[object XmlStory]') {
							return false;
						}
						if (obj == '[object Story]') {
							var obj_attribute = myElement.xmlAttributes.itemByName('max');
							var max = obj_attribute.isValid  ? obj_attribute.value : 0;
							
							var obj_attribute = myElement.xmlAttributes.itemByName('etroitisation');
							var etroitisation = obj_attribute.isValid  ? ((obj_attribute.value == 'true') ? true : false) : false;
							
							max = Number(max);
							
							var i = 0;
							while (obj.overflows == true) {
								i++;
								obj.tracking -= 1;
								if (obj.tracking <= max) {
									break;
								}
								if (i >= 200) {
									break;
								} 
							}
							
							if (obj.overflows == true && etroitisation == true) {
								var obj_attribute = myElement.xmlAttributes.itemByName('max_etroitisation');
								var max_etroitisation = obj_attribute.isValid  ? obj_attribute.value : 100;
								var i = 0;
								while (obj.overflows == true) {
									i++;
									obj.horizontalScale -= 1;
									if (obj.horizontalScale <= max_etroitisation) {
										break;
									}
									if (i >= 200) {
										break;
									} 
								}
							}
						}
					}
					catch (e) {
						myObj.setError(2, 'Erreur Tracking ('+e+')');
					}
					return true;
				}
			}
			function Scale(){
					this.name = "Scale";
					this.xpath = '//*[@scale="true"]';
					
					this.apply = function(myElement, myRuleProcessor) {
						try {
							var obj = myElement.xmlContent;
							if (obj.parent == '[object XmlStory]') {
								return false;
							}
							var obj_attribute = myElement.xmlAttributes.itemByName('horizontal');
							var horizontal = obj_attribute.isValid  ? obj_attribute.value : '';
							var obj_attribute = myElement.xmlAttributes.itemByName('vertical');
							var vertical = obj_attribute.isValid  ? obj_attribute.value : '';
							
							try {
								obj.verticalScale = Number(vertical);
							}
							catch (e) {
								myObj.setError(2, 'Erreur Scale  verticalScale ('+e+')');
							}
							try {
								obj.horizontalScale = Number(horizontal);
							}
							catch (e) {
								myObj.setError(2, 'Erreur Scale  horizontalScale ('+e+')');
							}
							obj.parent.frameFittingOptions.fittingOnEmptyFrame = EmptyFrameFittingOptions.NONE;
							obj.parent.frameFittingOptions.bottomCrop = 0;
							obj.parent.frameFittingOptions.leftCrop = 0;
							obj.parent.frameFittingOptions.rightCrop = 0;
							obj.parent.frameFittingOptions.topCrop = 0;
						}
						catch (e) {
							myObj.setError(2, 'Erreur Scale ('+e+')');
						}
					return true;
				}
			}
			function FrameToContent () {
				this.name = "FrameToContent";
				this.xpath = '//*[@ajust="true"]';
				this.apply = function(myElement, myRuleProcessor) {
					try {
						var obj = myElement.xmlContent;
						var obj_attribute = myElement.xmlAttributes.itemByName('parent');
						var value = obj_attribute.isValid  ? obj_attribute.value : '0';
						if (obj.parent == '[object XmlStory]') {
							return false;
						}
						if (obj == "[object Story]") {
							if (obj.textContainers.length >= 1) {
								obj.textContainers[0].fit(FitOptions.FRAME_TO_CONTENT);
								obj.textContainers[0].fit(FitOptions.FRAME_TO_CONTENT);
							}
						}
						else {
							obj.fit(FitOptions.FRAME_TO_CONTENT);
						}
						return false;
					}
					catch (e) {
						myObj.setError(2, 'Erreur FrameToContent ('+e+')');
					}
				}
			}
			function Move(){
				this.name = "Move";
				this.xpath = '//*[@move="true"]';
				
				this.apply = function(myElement, myRuleProcessor) {
					try {
						var obj = myElement.xmlContent;
						if (obj.parent == '[object XmlStory]') {
							return false;
						}
						
						
						
						
						var obj_attribute = myElement.xmlAttributes.itemByName('value_x');
						var value_x = obj_attribute.isValid  ? obj_attribute.value+' mm' : '0 mm';
						
						var obj_attribute = myElement.xmlAttributes.itemByName('value_y');
						var value_y = obj_attribute.isValid  ? obj_attribute.value+' mm' : '0 mm';
						if (obj == '[object Story]') {
							var myAnchorPoint = AnchorPoint.BOTTOM_LEFT_ANCHOR;
							obj.textContainers[0].move('', new Array (value_x, value_y));
						}
						return false;
					}
					catch (e) {
						myObj.setError(2, 'Erreur Resize ('+e+')');
					}
				}
			}
			function Resize(){
					this.name = "Resize";
					this.xpath = '//*[@resize="true"]';
					
					this.apply = function(myElement, myRuleProcessor) {
						try {
							var obj = myElement.xmlContent;
							if (obj.parent == '[object XmlStory]') {
								return false;
							}
							
							
							var obj_attribute = myElement.xmlAttributes.itemByName('resize_x');
							var value_x = obj_attribute.isValid  ? obj_attribute.value : '0';
							
							var obj_attribute = myElement.xmlAttributes.itemByName('resize_y');
							var value_y = obj_attribute.isValid  ? obj_attribute.value : '0';
							
							var obj_attribute = myElement.xmlAttributes.itemByName('sens');
							var sens = obj_attribute.isValid  ? obj_attribute.value : '';
							if (obj == '[object Story]') {
								if (obj.textContainers.length >= 1) {
									if (sens == 'up') {
										var myAnchorPoint = AnchorPoint.BOTTOM_LEFT_ANCHOR;
									}
									else if (sens == 'down') {
										var myAnchorPoint = AnchorPoint.TOP_LEFT_ANCHOR;
									}
									else if (sens == 'right') {
										var myAnchorPoint = AnchorPoint.TOP_RIGHT_ANCHOR;
									}
									else {
										var myAnchorPoint = AnchorPoint.TOP_LEFT_ANCHOR;
									}
									alert('DO RESIZE'+obj);
									obj.textContainers[0].resize(BoundingBoxLimits.GEOMETRIC_PATH_BOUNDS, myAnchorPoint, ResizeMethods.ADDING_CURRENT_DIMENSIONS_TO, new Array(UnitValue(value_x,"mm").as('pt'), UnitValue(value_y,"mm").as('pt')));
								}
							}
							return false;
						}
						catch (e) {
							myObj.setError(2, 'Erreur Resize ('+e+')');
						}
					}
			}
			function ChangeNbColumn(){
				this.name = "ChangeNbColumn";
				this.xpath = '//*[@change_nb_column="true"]';
				
				this.apply = function(myElement, myRuleProcessor) {
					try {
						var obj = myElement.xmlContent;
						if (obj.parent == '[object XmlStory]') {
							return false;
						}
						
						if (obj != '[object Story]') {
							return false;
						}
						var obj_attribute = myElement.xmlAttributes.itemByName('nb_column');
						var nb_column = obj_attribute.isValid  ? obj_attribute.value : false;
						
						if (nb_column !== false) {
							obj.textContainers[0].textFramePreferences.textColumnCount = 1;
						}
						
					
						return false;
					}
					catch (e) {
						myObj.setError(2, 'Erreur Resize ('+e+')');
					}
				}
			}
			function ApplyObjectStyle() {
				this.name = "ApplyObjectStyle";
				this.xpath = '//*[@object_style="true"]';
				
				this.apply = function(myElement, myRuleProcessor) {
					try {
						var obj = myElement.xmlContent;
						if (obj.parent == '[object XmlStory]') {
							return false;
						}
						var obj_attribute = myElement.xmlAttributes.itemByName('name');
						var name = obj_attribute.isValid  ? obj_attribute.value : '';
                        
						var obj_attribute = myElement.xmlAttributes.itemByName('parent');
						var parent = obj_attribute.isValid  ? ((obj_attribute.value == "false") ? false: true)  : true;
						
						var style_object = myDocument.objectStyles.itemByName(name);
						if (style_object.isValid == true) {
							if (parent == true) {
								obj.parent.applyObjectStyle(style_object);
							}
							else {
								if (obj == '[object Story]') {
									var nb_containers = obj.textContainers.length;
									if (nb_containers >= 1) {
										obj.textContainers[0].applyObjectStyle(style_object);
									}
								}
							}
						}
						return false;
					}
					catch (e) {
						myObj.setError(2, 'Erreur ApplyObjectStyle ('+e+')');
					}
				}
			}
			return true;
		},
		setOptions: function () {
			if (this.options._ARGS_VALUES['path_syspad'] != undefined) {
				this.options.path_syspad = this.options._ARGS_VALUES['path_syspad'];
			}
			if (this.options._ARGS_VALUES['path_xml'] != undefined) {
				this.options.path_xml = this.options._ARGS_VALUES['path_xml'];
			}
			if (this.options._ARGS_VALUES['path_font'] != undefined) {
				this.options.path_font = this.options._ARGS_VALUES['path_font'];
			}
			if (this.options._ARGS_VALUES['path_export'] != undefined) {
				this.options.path_export = this.options._ARGS_VALUES['path_export'];
			}
			if (this.options._ARGS_VALUES['path_project'] != undefined) {
				this.options.path_project = this.options._ARGS_VALUES['path_project'];
			}
			if (this.options._ARGS_VALUES['path_gabarit'] != undefined) {
				this.options.path_gabarit = this.options._ARGS_VALUES['path_gabarit'];
			}
			if (this.options._ARGS_VALUES['path_package'] != undefined) {
				this.options.path_package = this.options._ARGS_VALUES['path_package'];
			}
			if (this.options._ARGS_VALUES['path_package_filename'] != undefined) {
				this.options.path_package_filename = this.options._ARGS_VALUES['path_package_filename'];
			}
			if (this.options._ARGS_VALUES['path_jsx_supp'] != undefined) {
				this.options.path_jsx_supp = this.options._ARGS_VALUES['path_jsx_supp'];
			}
			if (this.options._ARGS_VALUES['gabarit_name'] != undefined) {
				this.options.gabarit_name = this.options._ARGS_VALUES['gabarit_name'];
			}
			if (this.options._ARGS_VALUES['gabarit_add_suffixe'] != undefined) {
				this.options.gabarit_add_suffixe = this.options._ARGS_VALUES['gabarit_add_suffixe'];
			}
			if (this.options._ARGS_VALUES['build_indd'] != undefined) {
				this.options.build_indd = this.options._ARGS_VALUES['build_indd'];
			}
			if (this.options._ARGS_VALUES['build_pdf'] != undefined) {
				this.options.build_pdf = this.options._ARGS_VALUES['build_pdf'];
			}
			if (this.options._ARGS_VALUES['folio'] != undefined) {
				this.options.folio = this.options._ARGS_VALUES['folio'];
			}
			if (this.options._ARGS_VALUES['nb_page'] != undefined) {
				this.options.nb_page = this.options._ARGS_VALUES['nb_page'];
			}
			if (this.options._ARGS_VALUES['nb_product'] != undefined) {
				this.options.nb_product = this.options._ARGS_VALUES['nb_product'];
			}
			if (this.options._ARGS_VALUES['id_file'] != undefined) {
				this.options.id_file = this.options._ARGS_VALUES['id_file'];
			}
			if (this.options._ARGS_VALUES['PROTOCOLE_COULEUR'] != undefined) {
				this.options.protocole_color = this.options._ARGS_VALUES['PROTOCOLE_COULEUR'];
			}
			if (this.options._ARGS_VALUES['is_fiche_hotel'] != undefined) {
				this.options.is_fiche_hotel = this.options._ARGS_VALUES['is_fiche_hotel'];
			}
			if (this.options._ARGS_VALUES['is_fiche_excursion'] != undefined) {
				this.options.is_fiche_excursion = this.options._ARGS_VALUES['is_fiche_excursion'];
			}
			if (this.options._ARGS_VALUES['is_sejete18'] != undefined) {
				this.options.is_sejete18 = this.options._ARGS_VALUES['is_sejete18'];
			}
			if (this.options._ARGS_VALUES['is_white'] != undefined) {
				this.options.is_white = this.options._ARGS_VALUES['is_white'];
			}
			if (this.options._ARGS_VALUES['is_double'] != undefined) {
				this.options.is_double = this.options._ARGS_VALUES['is_double'];
			}
		},
		getParams: function () {
			var string_args = app.scriptArgs.isDefined("string_args") ? app.scriptArgs.getValue("string_args") : '';
			alert(string_args);

			if (string_args != '') {
				this.options._ARGS_NAMES = string_args.split(',');
			}
			for (var i = 0; i < this.options._ARGS_NAMES.length; i++) {
				alert('==> '+this.options._ARGS_NAMES[i]);
				if (app.scriptArgs.isDefined(this.options._ARGS_NAMES[i])) {
					this.options._ARGS_VALUES[this.options._ARGS_NAMES[i]] = app.scriptArgs.getValue(this.options._ARGS_NAMES[i]);
				}
			}
		},
		setColor: function() {
			try {
				if (this.options.protocole_color != null) {
					var list_color = new Array();
					list_color = this.options.protocole_color.split(';');
					
					for (var i = 0; i < list_color.length; i++) {
						var obj_color = this.options.myDocument.colors.itemByName(list_color[i]);
						if (obj_color.isValid == true) {
							var COLOR_C = 0;
							var COLOR_M = 0;
							var COLOR_Y = 0;
							var COLOR_K = 0;
							if (this.options._ARGS_VALUES[list_color[i]+'_C'] != undefined) {
								COLOR_C = this.options._ARGS_VALUES[list_color[i]+'_C'];
							}
							if (this.options._ARGS_VALUES[list_color[i]+'_M'] != undefined) {
								COLOR_M = this.options._ARGS_VALUES[list_color[i]+'_M'];
							}
							if (this.options._ARGS_VALUES[list_color[i]+'_Y'] != undefined) {
								COLOR_Y = this.options._ARGS_VALUES[list_color[i]+'_Y'];
							}
							if (this.options._ARGS_VALUES[list_color[i]+'_K'] != undefined) {
								COLOR_K = this.options._ARGS_VALUES[list_color[i]+'_K'];
							}
							var ArrayColor = new Array(Number(COLOR_C), Number(COLOR_M), Number(COLOR_Y), Number(COLOR_K));
							obj_color.colorValue = ArrayColor;
						}
						else {
							alert('Couleur ('+list_color[i]+')non pr&eacute;sente dans le gabarit');
						}
					}
				}
			}
			catch (e) {
				alert('Synthaxe JS [setColor] ('+e+')');
				return false;
			}
			return true;
		},
		setFolio: function() {
			try {
				if (this.options.folio >= 1 && this.options.folio <= 9999) {
					this.options.myDocument.pages[0].appliedSection.pageNumberStart = Number(this.options.folio);
				}
				else if (this.options.folio == -1);
				else {
					alert('Folio non applicable ('+this.options.folio+')');
					return false;
				}
			}
			catch (e) {
				alert('Synthaxe JS [setFolio] ('+e+')');
			    return false;
			}
			return true;
		},
		getRetour : function() {
			return this.options.retour;
		}
};

myIds._construct();

var retour = JSON.stringify(myIds.getRetour());


retour;
