//********************************************************************************
// © VIERO - http://viero.wordpress.com
// Place a signature on your photograph - Plaats uw handtekening op uw foto.
// Version: 1.2
// This script is provided as freeware with no warranty. Free to use.
// Only thing I ask is to leave this header in the script  !
// Thanks ! Viero
//********************************************************************************


//Check if a document is open - Nazien of er een document open staat

if ( documents.length > 0 )
{
	var originalRulerUnits = preferences.rulerUnits;
	preferences.rulerUnits = Units.PIXELS;
	preferences.typeUnits = TypeUnits.POINTS;
	try
	{
		var docRef = activeDocument;

		// Init

			//Text size - tekst size    		
   			initDocWidth = docRef.width.value;
	    	initDocHeight = docRef.height.value;
			initDocResolution = docRef.resolution;
			//catch portrait or landscape - opvangen portrait of landscape 
			initShortSide = initDocHeight;
			if (initDocHeight > initDocWidth) {
				initShortSide = initDocWidth;
			}
				
			initTextSize = (initShortSide / 100);      
			initTextSize *= (300 / initDocResolution); 	

			//Get year - Huidig jaartal Ophalen 
			var now = new Date();
			initYear = now.getFullYear();


		
			
		// Create a text layer at the front - Text laag maken in bovenste positie
		
		var myLayerRef = docRef.artLayers.add();
		myLayerRef.kind = LayerKind.TEXT;
		myLayerRef.name = "Signature";
		var myTextRef = myLayerRef.textItem;
				


		
		

		// Insert here your signature - plaats hier uw handtekening  		<-------------------------------
		var TextSignature = "© " + initYear  + "  V I E R O   P H O T O G R A P H Y";
		
			
		// Set font size in Points - tekst size plaatsten in Points 		<-------------------------------
		//myTextRef.size = 70;
		myTextRef.size = initTextSize;
		
		
		//Set font - lettertype bepalen 					<-------------------------------
		myTextRef.font = "Arial";
		
		//Set text colour in RGB values - kleur letters bepalen in RGB 		<-------------------------------
		var newColor = new SolidColor();
		newColor.rgb.red = 0;
		newColor.rgb.green = 0;
		newColor.rgb.blue = 0;
		myTextRef.color = newColor;
	
		// Set the initial position of the text -  left first, then from top.		
		myTextRef.position = new Array ((initDocWidth * 0.02),(initDocHeight * 0.96));
		
		// Set the Blend Mode of the Text Layer. 				<-------------------------------
		// CAPITALS, NORMAL or DIFFERENCE.
		myLayerRef.blendMode = BlendMode.NORMAL;
		
		// select opacity in percentage 					<-------------------------------
		myLayerRef.opacity = 35;





// Write signature to photograph - plaats de handtekening			
		
	myTextRef.contents = TextSignature
	//myTextRef.contents = TextSignature + " - Width:" + initDocWidth  + " - Height:" + initDocHeight + " - Resolution:" + initDocResolution; 


//center layer Signature
	
centerLayer();


		
	}
	catch( e )
	{
		// An error occurred. - Fout opgetreden
		preferences.rulerUnits = originalRulerUnits;
		throw e;
	}

	// Everything went Ok - Alles ok
	preferences.rulerUnits = originalRulerUnits;
}
else
{
	alert( "You must have a document open to add your signature! - Er moet een document open staan om uw handtekening toe te voegen !" );
}













function centerLayer() {
	if (checkInitialConditions()) {
		
		var docRef = app.activeDocument;
		var layerRef = docRef.activeLayer;

		// set document resolution to 72ppi (required due to a script bug in Photoshop)
		var docRes = activeDocument.resolution;
		docRef.resizeImage(undefined, undefined, 72, ResampleMethod.NONE);

		var allLocked = activeDocument.activeLayer.allLocked; 
		var posLocked = activeDocument.activeLayer.positionLocked; 

		activeDocument.activeLayer.allLocked = false; 
		activeDocument.activeLayer.positionLocked = false; 

		var docWidth = docRef.width;
		var docHeight = docRef.height;

		var layerWidth = layerRef.bounds[2] - layerRef.bounds[0];
		var layerHeight = layerRef.bounds[3] - layerRef.bounds[1];

		var deltaX = (docWidth - layerWidth)/2 - layerRef.bounds[0];
		var deltaY = (docHeight - layerHeight)/2 - layerRef.bounds[1];

		layerRef.translate(deltaX); // centers the active layer

		// restore document's original resolution (required due to a bug)
		docRef.resizeImage(undefined, undefined, docRes, ResampleMethod.NONE);

		layerRef.allLocked = allLocked;
		layerRef.positionLocked = posLocked;
	}
}

function checkInitialConditions() {
	// check if Background layer is selected
	if (activeDocument.activeLayer.isBackgroundLayer) {
		alert('The Background layer cannot be centered.\nPlease select another layer and try again.');
		return false; // quit
	}
	// check to see if the current layer contains any artwork
	else if (activeDocument.activeLayer.bounds[0] == activeDocument.activeLayer.bounds[2]) {
		alert('The current layer contains no artwork.\nPlease select another layer and try again.');
		return false; // quit
	}
	// check to see if a group is currently selected
	else if (activeDocument.activeLayer.typename == "LayerSet" && !confirm('Are you sure you want to center the entire group?')) {
		return false; // quit
	}
	// if all above conditions are met, proceed with main function
	else {
		return true; // continue
	}
}
