/* =========================================================================

InDesign ExtendScript

NAME: CalendarWizardCS_change_params
VERSION: 1.0

AUTHOR: Филянин Сергей
DATE  : 30.08.06

DESCRIPTION: 
Скрипт для подстройки параметров календарных сеток.

E-MAIL: dtp@mail.ru

============================================================================ */

with (app)
{
// Подключаем params_ru.ini
	var myIniFile = new File (activeScript.path + "/params_ru.ini");
	if (!myIniFile.exists) {
		alert ("Cant open params_ini file!");
		exit();
	} else {
		eval ("//@include \'params_ru.ini\';");
	}

	var myDoc=documents[0];
	// загрузка красок
	var myColors=new Array();
	for (i=0; i<myDoc.swatches.length; i++)
	{
		myColors[i]=myDoc.swatches[i].name;
	}
	// диалог
	var myDlgBegin = dialogs.add({name: "CalendarWizard_change_param"});
	with (myDlgBegin.dialogColumns.add().dialogRows.add())
	{
		//Создаем панель изменения параметров ячеек
		with (borderPanels.add().dialogColumns.add())
		{
			dialogRows.add().staticTexts.add({staticLabel:dialog_ChangeCellParams});
				with (dialogRows.add())
				{
					with (dialogColumns.add())
					{
						var myLabe31 = staticTexts.add({staticLabel:dialog_CellType});
						var myCellHeightCheck = checkboxControls.add({staticLabel:dialog_CellHeight, checkedState: false});
						var myCellWidthCheck = checkboxControls.add({staticLabel:dialog_CellWidth, checkedState: false});
						var myCellOffsetTopCheck = checkboxControls.add({staticLabel:dialog_CellMarginTop, checkedState: false});
						var myCellOffsetBottomCheck = checkboxControls.add({staticLabel:dialog_CellMarginBottom, checkedState: false});
						var myCellOffsetLeftCheck = checkboxControls.add({staticLabel:dialog_CellMarginLeft, checkedState: false});
						var myCellOffsetRightCheck = checkboxControls.add({staticLabel:dialog_CellMarginRight, checkedState: false});
						var myDropVerticalAligmentCheck = checkboxControls.add({staticLabel:dialog_CellVerticalAligment, checkedState: false});
						var myDropStrokeColorCheck = checkboxControls.add({staticLabel:dialog_ColorStroke, checkedState: false});
						var myDropFillColorCheck = checkboxControls.add({staticLabel:dialog_ColorFill, checkedState: false});
						var myCellStrokeCheck = checkboxControls.add({staticLabel:dialog_StrokeWidth, checkedState: false});
					}
					with (dialogColumns.add())
					{
						var myDropCellType = dropdowns.add({staticLabel:dialog_CellType, stringList: myCellType, selectedIndex: 0, minWidth: 200});
						var myCellHeight = realEditboxes.add({editValue:0});
						var myCellWidth = realEditboxes.add({editValue:0});
						var myCellOffsetTop = realEditboxes.add({editValue:0});
						var myCellOffsetBottom = realEditboxes.add({editValue:0});
						var myCellOffsetLeft = realEditboxes.add({editValue:0});
						var myCellOffsetRight = realEditboxes.add({editValue:0});
						var myDropVerticalAligment = dropdowns.add({stringList: verticalAligment, selectedIndex: 0});
						var myDropStrokeColor = dropdowns.add({stringList: myColors, selectedIndex: 0});
						var myDropFillColor = dropdowns.add({stringList: myColors, selectedIndex: 0});
						var myCellStroke = realEditboxes.add({editValue:0});
					}
				}
		}
		//Создаем панель изменения параметров текстовых блоков
		with (borderPanels.add().dialogColumns.add())
		{
			dialogRows.add().staticTexts.add({staticLabel:dialog_FrameOptions});
				with (dialogRows.add())
				{
					with (dialogColumns.add())
					{
						var myFrameWidthCheck = checkboxControls.add({staticLabel:dialog_FrameWidth, checkedState: false});
						var myFrameHeightCheck = checkboxControls.add({staticLabel:dialog_FrameHeight, checkedState: false});
					}
					with (dialogColumns.add())
					{
						var myFrameWidth = realEditboxes.add({editValue:0});
						var myFrameHeight = realEditboxes.add({editValue:0});
					}
				}
		}
	}
	resultBegin = myDlgBegin.show();
	////
	if (resultBegin)
	{
			for (i=0; i<myDoc.textFrames.length; i++)
			{
				try
				{
				myTable=myDoc.textFrames[i].tables[0];
				myTextFrame=myDoc.textFrames[i];
				if (myFrameHeightCheck.checkedState) myTextFrame.geometricBounds=[	myTextFrame.geometricBounds[0],
																				 	myTextFrame.geometricBounds[1],
																				 	myTextFrame.geometricBounds[0]+myFrameHeight.editValue,
																				 	myTextFrame.geometricBounds[3]];	//Высота блока
				if (myFrameWidthCheck.checkedState)  myTextFrame.geometricBounds=[	myTextFrame.geometricBounds[0],
																					myTextFrame.geometricBounds[1],
																					myTextFrame.geometricBounds[2],
																					myTextFrame.geometricBounds[1]+myFrameWidth.editValue];	//Ширина ячейки
				for (j=0; j<myTable.cells.length; j++)
				{
					var myCell=myTable.cells[j];
					var changeCellState=false;
	
					switch (myDropCellType.selectedIndex)
					{
						case 0:
							if (myCell.paragraphs[0].appliedParagraphStyle.name=="months") changeCellState=true;						
							break;
						case 1:
							if (myCell.paragraphs[0].appliedParagraphStyle.name=="weekdays") changeCellState=true;
							if (myCell.paragraphs[0].appliedParagraphStyle.name=="weeksaturdays") changeCellState=true;
							if (myCell.paragraphs[0].appliedParagraphStyle.name=="weeksundays") changeCellState=true;
							break;
						case 2:
							if (myCell.paragraphs[0].appliedParagraphStyle.name=="days") changeCellState=true;
							break;
						case 3:
							if (myCell.paragraphs[0].appliedParagraphStyle.name=="saturdays") changeCellState=true;
							break;
						case 4:
							if (myCell.paragraphs[0].appliedParagraphStyle.name=="sundays") changeCellState=true;
							break;
						case 5:
							if (myCell.paragraphs[0].appliedParagraphStyle.name=="holydays") changeCellState=true;
							break;
						case 6:
							if (myCell.paragraphs[0].appliedParagraphStyle.name=="days") changeCellState=true;
							if (myCell.paragraphs[0].appliedParagraphStyle.name=="saturdays") changeCellState=true;
							if (myCell.paragraphs[0].appliedParagraphStyle.name=="sundays") changeCellState=true;
							if (myCell.paragraphs[0].appliedParagraphStyle.name=="holydays") changeCellState=true;
							break;
						case 7:
							changeCellState=true;
							break;
					}
					if (changeCellState)
					{
						if (myCellHeightCheck.checkedState) myCell.height=myCellHeight.editValue;												//Высота ячейки
						if (myCellWidthCheck.checkedState) myCell.width=myCellWidth.editValue;													//Ширина ячейки
						
						if (myCellOffsetTopCheck.checkedState) myCell.topInset=myCellOffsetTop.editValue;										//Отступ сверху
						if (myCellOffsetBottomCheck.checkedState) myCell.bottomInset=myCellOffsetBottom.editValue;								//Отступ снизу
						if (myCellOffsetLeftCheck.checkedState) myCell.leftInset=myCellOffsetLeft.editValue;									//Отступ слева
						if (myCellOffsetRightCheck.checkedState) myCell.rightInset=myCellOffsetRight.editValue;									//Отступ справа
	
						if (myDropVerticalAligmentCheck.checkedState)																			//Вертикальное выравнивание
						{
							switch (myDropVerticalAligment.selectedIndex)
							{
								case 0:
									myCell.verticalJustification=VerticalJustification.topAlign;																
									break;
								case 1:
									myCell.verticalJustification=VerticalJustification.centerAlign;																
									break;
								case 2:
									myCell.verticalJustification=VerticalJustification.bottomAlign;																
									break;
							}
						}
	
						if (myDropStrokeColorCheck.checkedState)																				//Цвет обводки
						{
							myCell.bottomEdgeStrokeColor=myDoc.swatches.item(myColors[myDropStrokeColor.selectedIndex]);	
							myCell.topEdgeStrokeColor=myDoc.swatches.item(myColors[myDropStrokeColor.selectedIndex]);	
							myCell.leftEdgeStrokeColor=myDoc.swatches.item(myColors[myDropStrokeColor.selectedIndex]);	
							myCell.rightEdgeStrokeColor=myDoc.swatches.item(myColors[myDropStrokeColor.selectedIndex]);	
						}
						if (myDropFillColorCheck.checkedState) myCell.fillColor=myDoc.swatches.item(myColors[myDropFillColor.selectedIndex]);	//Заливка
	
						if (myCellStrokeCheck)																									//Толщина линий обводки
						{
							myCell.bottomEdgeStrokeWeight=myCellStroke.editValue;	
							myCell.topEdgeStrokeWeight=myCellStroke.editValue;	
							myCell.leftEdgeStrokeWeight=myCellStroke.editValue;	
							myCell.rightEdgeStrokeWeight=myCellStroke.editValue;	
						}
					}
				} 
			} catch (e) {};
		} 
	}
}