/*
<javascriptresource>
<name>XSHOCK_TrimSelection</name>
<about>XSHOCK Photoshop-Script | By Matthias Schuetz | http://blog.xshock.de | http://www.xshock.de</about>
<category>XSHOCK</category>
</javascriptresource>
*/

if (app.documents.length > 0) {
var startRulerUnits = app.preferences.rulerUnits;
var startDisplayDialogs = app.displayDialogs;
app.preferences.rulerUnits = Units.PIXELS;
app.displayDialogs = DialogModes.NO;

function optionsWindow() {
	try {
		var xA = bounds[0];
		var yA = bounds[1];
		var xB = bounds[2];
		var yB = bounds[3];

		var selectionSizeX = (xB - xA);
		var selectionSizeY = (yB - yA);
		
		var $anchorPosition = 5;
		var $relActive = 0;

		selectionSizeX = String(selectionSizeX);
		selectionSizeX = selectionSizeX.replace('px','');
		selectionSizeY = String(selectionSizeY);
		selectionSizeY = selectionSizeY.replace('px','');
		selectionSizeX = parseInt(selectionSizeX);
		selectionSizeY = parseInt(selectionSizeY);

		var dlg = new Window('dialog', 'Trim selection');
		dlg.bounds = {x:0, y:0, width:330, height:250};
		dlg.actualSizePanel = dlg.add ('panel', [15,10,200,75], 'Actual selection-size');
		dlg.actualSizeX = dlg.actualSizePanel.add('statictext', [25,14,221,30], "Width:  " + selectionSizeX + " Pixel");
		dlg.actualSizeY = dlg.actualSizePanel.add('statictext', [22,32,221,50], "Height:  " + selectionSizeY + " Pixel");
		
		dlg.inputPanel = dlg.add ('panel', [15,80,200,235], 'New selection-size');
		dlg.inputTextX = dlg.inputPanel.add('edittext', [65,14,115,35], selectionSizeX, {multiline:false});
		dlg.inputTextY = dlg.inputPanel.add('edittext', [65,38,115,59], selectionSizeY, {multiline:false});
		dlg.inputTextX.active = true;
		dlg.inputActualSizeX = dlg.inputPanel.add('statictext', [25,17,62,35], "Width:");
		dlg.pixelActualSizeX = dlg.inputPanel.add('statictext', [119,16,150,35], "Pixel");
		dlg.inputActualSizeY = dlg.inputPanel.add('statictext', [22,41,62,55], "Height:");
		dlg.pixelActualSizeY = dlg.inputPanel.add('statictext', [119,39,150,55], "Pixel");
		dlg.inputActualSizeY = dlg.inputPanel.add('statictext', [16,69,62,95], "Position");
		
		dlg.anchorTopLeft = dlg.inputPanel.add('radiobutton', [65,70,80,87], ''); 
		dlg.anchorTopCenter = dlg.inputPanel.add('radiobutton', [88,70,105,87], ''); 
		dlg.anchorTopRight = dlg.inputPanel.add('radiobutton', [112,70,136,87], ''); 
		
		dlg.anchorMiddleLeft = dlg.inputPanel.add('radiobutton', [65,90,80,112], ''); 
		dlg.anchorMiddleCenter = dlg.inputPanel.add('radiobutton', [88,90,105,112], ''); 
		dlg.anchorMiddleRight = dlg.inputPanel.add('radiobutton', [112,90,136,112], ''); 
		
		dlg.anchorBottomLeft = dlg.inputPanel.add('radiobutton', [65,110,80,137], ''); 
		dlg.anchorBottomCenter = dlg.inputPanel.add('radiobutton', [88,110,105,137], ''); 
		dlg.anchorBottomRight = dlg.inputPanel.add('radiobutton', [112,110,136,137], ''); 
		
		dlg.anchorMiddleCenter.value = true; 
		
		dlg.relativeSizeBox = dlg.add('checkbox', [216,101,330,121], ' Relative', {name:'relativesize'});
		dlg.okBtn = dlg.add('button', [215,15,315,35], 'OK', {name:'ok'});
		dlg.canelBtn = dlg.add('button', [215,40,315,60], 'Cancel', {name:'cancel'});
		
		dlg.anchorTopLeft.onClick = function() { $anchorPosition = 1; }
		dlg.anchorTopCenter.onClick = function() { $anchorPosition = 2; }
		dlg.anchorTopRight.onClick = function() { $anchorPosition = 3; }
		dlg.anchorMiddleLeft.onClick = function() { $anchorPosition = 4; }
		dlg.anchorMiddleCenter.onClick = function() { $anchorPosition = 5; }
		dlg.anchorMiddleRight.onClick = function() { $anchorPosition = 6; }
		dlg.anchorBottomLeft.onClick = function() { $anchorPosition = 7; }
		dlg.anchorBottomCenter.onClick = function() { $anchorPosition = 8; }
		dlg.anchorBottomRight.onClick = function() { $anchorPosition = 9; }
		
		dlg.okBtn.onClick = function() {
			xA = parseInt(xA);
			xB = parseInt(xB);
			yA = parseInt(yA);
			yB = parseInt(yB);
			
			var inputSetX = parseInt(dlg.inputTextX.text);
			var inputSetY = parseInt(dlg.inputTextY.text);

			switch ($anchorPosition) {
				case 1:
				if ($relActive == 1) {
					inputSetX += (xB - xA);
					inputSetY += (yB - yA);
				}
				xB = xA + inputSetX;
				yB = yA + inputSetY;
				break;
				
				case 2:
				if ($relActive == 1) {
					inputSetX += (xB - xA);
					inputSetY += (yB - yA);
				}
				yB = yA + inputSetY;
				var xAddition = ((inputSetX - (xB - xA)) / 2);
				xAddition = parseInt(xAddition);
				if (((inputSetX - (xB - xA)) % 2) == 1) {
					var xAdditionDiv = ((inputSetX - (xB - xA)) % 2);
					xAddition += 0.5;
				} else if (((inputSetX - (xB - xA)) % 2) < 0) {
					var xAdditionDiv = ((inputSetX - (xB - xA)) % 2);
					xAddition -= 0.5;
				}
				xA = xA - xAddition;
				xB = xB + xAddition;
				break;
				
				case 3:
				if ($relActive == 1) {
					inputSetX += (xB - xA);
					inputSetY += (yB - yA);
				}
				xA = xB - inputSetX;
				yB = yA + inputSetY;	
				break;
				
				case 4:
				if ($relActive == 1) {
					inputSetX += (xB - xA);
					inputSetY += (yB - yA);
				}
				xB = xA + inputSetX;
				var yAddition = ((inputSetY - (yB - yA)) / 2);
				yAddition = parseInt(yAddition);
				if (((inputSetY - (yB - yA)) % 2) == 1) {
					var yAdditionDiv = ((inputSetY - (yB - yA)) % 2);
					yAddition += 0.5;
				} else if (((inputSetY - (yB - yA)) % 2) < 0) {
					var yAdditionDiv = ((inputSetY - (yB - yA)) % 2);
					yAddition -= 0.5;
				}
				yA = yA - yAddition;
				yB = yB + yAddition;
				break;
				
				case 5:
				if ($relActive == 1) {
					inputSetX += (xB - xA);
					inputSetY += (yB - yA);
				}
				var xAddition = ((inputSetX - (xB - xA)) / 2);
				xAddition = parseInt(xAddition);
				if (((inputSetX - (xB - xA)) % 2) == 1) {
					var xAdditionDiv = ((inputSetX - (xB - xA)) % 2);
					xAddition += 0.5;
				} else if (((inputSetX - (xB - xA)) % 2) < 0) {
					var xAdditionDiv = ((inputSetX - (xB - xA)) % 2);
					xAddition -= 0.5;
				}
				xA = xA - xAddition;
				xB = xB + xAddition;
				var yAddition = ((inputSetY - (yB - yA)) / 2);
				yAddition = parseInt(yAddition);
				if (((inputSetY - (yB - yA)) % 2) == 1) {
					var yAdditionDiv = ((inputSetY - (yB - yA)) % 2);
					yAddition += 0.5;
				} else if (((inputSetY - (yB - yA)) % 2) < 0) {
					var yAdditionDiv = ((inputSetY - (yB - yA)) % 2);
					yAddition -= 0.5;
				}
				yA = yA - yAddition;
				yB = yB + yAddition;
				break;
				
				case 6:
				if ($relActive == 1) {
					inputSetX += (xB - xA);
					inputSetY += (yB - yA);
				}
				xA = xB - inputSetX;
				var yAddition = ((inputSetY - (yB - yA)) / 2);
				yAddition = parseInt(yAddition);
				if (((inputSetY - (yB - yA)) % 2) == 1) {
					var yAdditionDiv = ((inputSetY - (yB - yA)) % 2);
					yAddition += 0.5;
				} else if (((inputSetY - (yB - yA)) % 2) < 0) {
					var yAdditionDiv = ((inputSetY - (yB - yA)) % 2);
					yAddition -= 0.5;
				}
				yA = yA - yAddition;
				yB = yB + yAddition;
				break;
				
				case 7:
				if ($relActive == 1) {
					inputSetX += (xB - xA);
					inputSetY += (yB - yA);
				}
				xB = xA + inputSetX;
				yA = yB - inputSetY;
				break;
				
				case 8:
				if ($relActive == 1) {
					inputSetX += (xB - xA);
					inputSetY += (yB - yA);
				}
				yA = yB - inputSetY;
				var xAddition 	= ((inputSetX - (xB -xA)) / 2);
				xAddition = parseInt(xAddition);
				if (((inputSetX - (xB - xA)) % 2) == 1) {
					var xAdditionDiv = ((inputSetX - (xB - xA)) % 2);
					xAddition += 0.5;
				} else if (((inputSetX - (xB - xA)) % 2) < 0) {
					var xAdditionDiv = ((inputSetX - (xB - xA)) % 2);
					xAddition -= 0.5;
				}
				xA = xA - xAddition;
				xB = xB + xAddition;
				break;
				
				case 9:
				if ($relActive == 1) {
					inputSetX += (xB - xA);
					inputSetY += (yB - yA);
				}
				xA = xB - inputSetX;
				yA = yB - inputSetY;
				break;
			}
		
			activeDocument.selection.select([[xA,yA],[xB,yA],[xB,yB],[xA,yB]], SelectionType.REPLACE, 0, false);
			dlg.close();
		}

		dlg.canelBtn.onClick = function() {
			dlg.close();
		}
		
		dlg.relativeSizeBox.onClick = function() {
			if ($relActive == 0) {
				var originSelectionSizeX = parseInt(dlg.inputTextX.text);
				var originSelectionSizeY = parseInt(dlg.inputTextY.text);
				dlg.inputTextX.text = originSelectionSizeX - selectionSizeX;
				dlg.inputTextY.text = originSelectionSizeY - selectionSizeY;
				$relActive = 1;
			} else {
				var originSelectionSizeX = parseInt(dlg.inputTextX.text);
				var originSelectionSizeY = parseInt(dlg.inputTextY.text);
				dlg.inputTextX.text = originSelectionSizeX + selectionSizeX;
				dlg.inputTextY.text = originSelectionSizeY + selectionSizeY;
				$relActive = 0;
			}
		}
		
		dlg.center();
		dlg.show();
	}
	catch (e) { }
}

try {
if (activeDocument.activeLayer.typename == "LayerSet") {
	var activeLayerSetName = activeDocument.activeLayer.name;
	var layerSetCollapsed = 0;

	var method = charIDToTypeID("Mk  ");
	var actDesc = new ActionDescriptor();
	var content = charIDToTypeID("null");
	var actRef = new ActionReference();
	var type = charIDToTypeID("Lyr ");
	actRef.putClass(type);
	actDesc.putReference(content, actRef);
	executeAction(method, actDesc, DialogModes.NO);

	activeDocument.activeLayer.name = " ";
	activeDocument.selection.fill(app.foregroundColor);
	
	try {
		if (activeDocument.activeLayer.parent.name == activeLayerSetName) {
			layerSetCollapsed = 1;
		}
	}
	catch (e) { }

	var bounds = new Array();
	bounds = activeDocument.activeLayer.bounds;

	try {
		if (layerSetCollapsed == 1) {
			activeDocument.activeLayer = activeDocument.activeLayer.parent;
			activeDocument.activeLayer.layers[0].remove();
		} else {
			activeDocument.activeLayer.remove();
		}
	}
	catch (e) { }
	
	optionsWindow();
} else {
	var method = charIDToTypeID("Mk  ");
	var actDesc = new ActionDescriptor();
	var content = charIDToTypeID("null");
	var actRef = new ActionReference();
	var type = charIDToTypeID("Lyr ");
	actRef.putClass(type);
	actDesc.putReference(content, actRef);
	executeAction(method, actDesc, DialogModes.NO);

	activeDocument.activeLayer.name = " ";
	activeDocument.selection.fill(app.foregroundColor);

	var bounds = new Array();
	bounds = activeDocument.activeLayer.bounds;

	activeDocument.activeLayer.remove();
	
	optionsWindow();
}
}
catch (e) { }

app.preferences.rulerUnits = startRulerUnits;
app.displayDialogs = startDisplayDialogs;
}