/*
	BFTonalityTuning.jsx
	Javascript implementation of DOP Tonality Tuning Toolkit
	Version 1.0.2
	2007-08-06
	(c) 2007 Bernard Frangoulis
*/

// ================================================================================

var action = 0;
var invert = 0;

// ================================================================================

function CreateUI()
{
	var dlg = new Window('dialog', 'DOP Tonality Tuning Toolkit', [20,60,701,756]);
	dlg.panl_tuning = dlg.add('panel', [21,14,661,644], '');
	dlg.panl_tuning.stat1 = dlg.panl_tuning.add('statictext', [18,8,145,28], 'Tonality Tuning:');
	dlg.panl_tuning.stat2 = dlg.panl_tuning.add('statictext', [336,8,463,28], 'Selection only:');
	dlg.panl_tuning.stat_DkHl = dlg.panl_tuning.add('statictext', [18,48,218,68], 'Darken Highlights:');
	dlg.panl_tuning.stat_BrSh = dlg.panl_tuning.add('statictext', [18,134,218,154], 'Brighten Shadows:');
	dlg.panl_tuning.stat_MidW = dlg.panl_tuning.add('statictext', [18,220,218,240], 'Midtones (wide):');
	dlg.panl_tuning.stat_MidM = dlg.panl_tuning.add('statictext', [18,356,218,376], 'Midtones (medium):');
	dlg.panl_tuning.stat_MidN = dlg.panl_tuning.add('statictext', [18,492,218,512], 'Midtones (narrow):');
	dlg.panl_tuning.rdio_DkHlNo = dlg.panl_tuning.add('radiobutton', [49,73,315,93], 'Darken Highlights normal');
	dlg.panl_tuning.rdio_DkHlNa = dlg.panl_tuning.add('radiobutton', [49,98,315,118], 'Darken Highlights narrow');
	dlg.panl_tuning.rdio_BrShNo = dlg.panl_tuning.add('radiobutton', [49,159,315,179], 'Brighten Shadows normal');
	dlg.panl_tuning.rdio_BrShNa = dlg.panl_tuning.add('radiobutton', [49,184,315,204], 'Brighten Shadows narrow');
	dlg.panl_tuning.rdio_MidWCo = dlg.panl_tuning.add('radiobutton', [49,245,315,265], 'Midtones Contrast');
	dlg.panl_tuning.rdio_MidWCoLu = dlg.panl_tuning.add('radiobutton', [49,270,315,290], 'Midtones Contrast Luminosity');
	dlg.panl_tuning.rdio_MidWBr = dlg.panl_tuning.add('radiobutton', [49,295,315,315], 'Midtones Brighter');
	dlg.panl_tuning.rdio_MidWDk = dlg.panl_tuning.add('radiobutton', [49,320,315,340], 'Midtones Darker');
	dlg.panl_tuning.rdio_MidMCo = dlg.panl_tuning.add('radiobutton', [49,381,315,401], 'Midtones Contrast');
	dlg.panl_tuning.rdio_MidMCoLu = dlg.panl_tuning.add('radiobutton', [49,406,315,426], 'Midtones Contrast Luminosity');
	dlg.panl_tuning.rdio_MidMBr = dlg.panl_tuning.add('radiobutton', [49,431,315,451], 'Midtones Brighter');
	dlg.panl_tuning.rdio_MidMDk = dlg.panl_tuning.add('radiobutton', [49,456,315,476], 'Midtones Darker');
	dlg.panl_tuning.rdio_MidNCo = dlg.panl_tuning.add('radiobutton', [49,517,315,537], 'Midtones Contrast');
	dlg.panl_tuning.rdio_MidNCoLu = dlg.panl_tuning.add('radiobutton', [49,542,315,562], 'Midtones Contrast Luminosity');
	dlg.panl_tuning.rdio_MidNBr = dlg.panl_tuning.add('radiobutton', [49,567,315,587], 'Midtones Brighter');
	dlg.panl_tuning.rdio_MidNDk = dlg.panl_tuning.add('radiobutton', [49,592,315,612], 'Midtones Darker');
	dlg.panl_tuning.rdio_SelHiNo = dlg.panl_tuning.add('radiobutton', [336,73,602,93], 'Highlights Selection normal');
	dlg.panl_tuning.rdio_SelHiNa = dlg.panl_tuning.add('radiobutton', [336,98,602,118], 'Highlights Selection narrow');
	dlg.panl_tuning.rdio_SelShNo = dlg.panl_tuning.add('radiobutton', [336,159,602,179], 'Shadows Selection normal');
	dlg.panl_tuning.rdio_SelShNa = dlg.panl_tuning.add('radiobutton', [336,184,602,204], 'Shadows Selection narrow');
	dlg.panl_tuning.rdio_SelMidW = dlg.panl_tuning.add('radiobutton', [336,245,602,265], 'Midtones Selection wide');
	dlg.panl_tuning.rdio_SelMidM = dlg.panl_tuning.add('radiobutton', [336,381,602,401], 'Midtones Selection medium');
	dlg.panl_tuning.rdio_SelMidN = dlg.panl_tuning.add('radiobutton', [336,517,602,537], 'Midtones Selection narrow');
	dlg.panl_tuning.panl_sep1 = dlg.panl_tuning.add('panel', [18,33,308,34]);
	dlg.panl_tuning.panl_sep2 = dlg.panl_tuning.add('panel', [336,33,626,34]);
	dlg.panl_tuning.chck_Invert = dlg.panl_tuning.add('checkbox', [474,8,628,28], 'Invert selection');
	dlg.push_About = dlg.add('button', [21,656,111,676], 'About');
	dlg.push_Cancel = dlg.add('button', [469,656,559,676], 'Cancel', {name:'cancel'});
	dlg.push_OK = dlg.add('button', [571,656,661,676], 'OK', {name:'ok'});
	return dlg;
}

// ================================================================================

function CreateUIAbout()
{
	var dlg = new Window('dialog', 'About the toolkit', [20,60,431,215]);
	dlg.stat_header1 = dlg.add('statictext', [20,30,391,48], 'DOP Tonality Tuning Toolkit V1.03');
	dlg.stat_header2 = dlg.add('statictext', [20,50,391,68], '(c) 2005 Uwe SteinmŸller / www.outbackphoto.com');
	dlg.stat_header3 = dlg.add('statictext', [20,70,391,88], 'Javascript implementation (c) 2007 Bernard Frangoulis');
	dlg.push_close = dlg.add('button', [160,115,250,135], 'Close');
	return dlg;
}

// ================================================================================

function RunUI(ui)
{
	// Initialize the UI
	InitializeUI(ui);
	
	// Run the builder dialog
	if(CheckAssumptions() == true) 
		ui.show();
	else {
		ui.close(0);
	}
}

// ================================================================================

function RunUIAbout(ui)
{
	InitializeUIAbout(ui);
	ui.show();
}

// ================================================================================

function InitializeUI(ui)
{
	var moduleName = 'InitializeUI';

	try {	
		if (app.documents.length == 0) {
			return;
		}
		
		// Attach event callback functions to controls		
		ui.panl_tuning.rdio_DkHlNo.onClick = function () {
			EnableInvertCheckbox(ui);
			action = 1;
		};
		ui.panl_tuning.rdio_DkHlNa.onClick = function () {
			EnableInvertCheckbox(ui);
			action = 2;
		};
		ui.panl_tuning.rdio_BrShNo.onClick = function () {
			EnableInvertCheckbox(ui);
			action = 3;
		};
		ui.panl_tuning.rdio_BrShNa.onClick = function () {
			EnableInvertCheckbox(ui);
			action = 4;
		};
		ui.panl_tuning.rdio_MidWCo.onClick = function () {
			EnableInvertCheckbox(ui);
			action = 5;
		};
		ui.panl_tuning.rdio_MidWCoLu.onClick = function () {
			EnableInvertCheckbox(ui);
			action = 6;
		};
		ui.panl_tuning.rdio_MidWBr.onClick = function () {
			EnableInvertCheckbox(ui);
			action = 7;
		};
		ui.panl_tuning.rdio_MidWDk.onClick = function () {
			EnableInvertCheckbox(ui);
			action = 8;
		};
		ui.panl_tuning.rdio_MidMCo.onClick = function () {
			EnableInvertCheckbox(ui);
			action = 9;
		};
		ui.panl_tuning.rdio_MidMCoLu.onClick = function () {
			EnableInvertCheckbox(ui);
			action = 10;
		};
		ui.panl_tuning.rdio_MidMBr.onClick = function () {
			EnableInvertCheckbox(ui);
			action = 11;
		};
		ui.panl_tuning.rdio_MidMDk.onClick = function () {
			EnableInvertCheckbox(ui);
			action = 12;
		};
		ui.panl_tuning.rdio_MidNCo.onClick = function () {
			EnableInvertCheckbox(ui);
			action = 13;
		};
		ui.panl_tuning.rdio_MidNCoLu.onClick = function () {
			EnableInvertCheckbox(ui);
			action = 14;
		};
		ui.panl_tuning.rdio_MidNBr.onClick = function () {
			EnableInvertCheckbox(ui);
			action = 15;
		};
		ui.panl_tuning.rdio_MidNDk.onClick = function () {
			EnableInvertCheckbox(ui);
			action = 16;
		};
		
		ui.panl_tuning.rdio_SelHiNo.onClick = function () {
			EnableInvertCheckbox(ui);
			action = 17;
		};
		ui.panl_tuning.rdio_SelHiNa.onClick = function () {
			EnableInvertCheckbox(ui);
			action = 18;
		};
		ui.panl_tuning.rdio_SelShNo.onClick = function () {
			EnableInvertCheckbox(ui);
			action = 19;
		};
		ui.panl_tuning.rdio_SelShNa.onClick = function () {
			EnableInvertCheckbox(ui);
			action = 20;
		};
		ui.panl_tuning.rdio_SelMidW.onClick = function () {
			EnableInvertCheckbox(ui);
			action = 21;
		};
		ui.panl_tuning.rdio_SelMidM.onClick = function () {
			EnableInvertCheckbox(ui);
			action = 22;
		};
		ui.panl_tuning.rdio_SelMidN.onClick = function () {
			EnableInvertCheckbox(ui);
			action = 23;
		};		
		ui.panl_tuning.chck_Invert.onClick = function () {
			invert = (ui.panl_tuning.chck_Invert.value)?1:0;
		};
		
		ui.push_About.onClick = function() {
			var ui3 = CreateUIAbout();
			RunUIAbout(ui3);
		}
		
		ui.push_Cancel.onClick = function() {
			action = 0;
			ui.close(0);
		}
		
		ui.push_OK.onClick = function() {
			ui.close(1);
		}
		
		ui.onShow = function() {
			ui.panl_tuning.rdio_DkHlNo.value = true;
			ui.panl_tuning.rdio_DkHlNo.notify();
		}
	}
	
	catch(someError)
	{
		alert( "An unexpected JavaScript error occurred in " +  moduleName + ". Message = " + someError.description);
		ui.close(0);
	}
}

// ================================================================================

function InitializeUIAbout(ui)
{
	var moduleName = 'InitializeUIAbout';

	try {
		ui.push_close.onClick = function() {
			ui.close(1);
		}
	}
	
	catch(someError) {
		alert( "An unexpected JavaScript error occurred in " +  moduleName + ". Message = " + someError.description);
		ui.close(0);
	}
}

// ================================================================================

function EnableInvertCheckbox(ui)
{
	ui.panl_tuning.chck_Invert.enabled = (ui.panl_tuning.rdio_SelHiNo.value || ui.panl_tuning.rdio_SelHiNa.value || ui.panl_tuning.rdio_SelShNo.value
									   || ui.panl_tuning.rdio_SelShNa.value || ui.panl_tuning.rdio_SelMidW.value || ui.panl_tuning.rdio_SelMidM.value
									   || ui.panl_tuning.rdio_SelMidN.value);
	if(ui.panl_tuning.chck_Invert.enabled==false) ui.panl_tuning.chck_Invert.value = 0;
}

// ================================================================================

function CallAppropriateAction()
{
	switch(action) {
		case 1:
			doAction("Highlight normal","DOP_tonality_tuning_V1_03");
			break;
		case 2:
			doAction("Highlight narrow","DOP_tonality_tuning_V1_03");
			break;
		case 3:
			doAction("Brighten Shadows normal","DOP_tonality_tuning_V1_03");
			break;
		case 4:
			doAction("Brighten Shadows narrow","DOP_tonality_tuning_V1_03");
			break;
		case 5:
			doAction("Midtone Contrast","DOP_tonality_tuning_V1_03");
			break;
		case 6:
			doAction("Midtone Contrast Luminosity","DOP_tonality_tuning_V1_03");
			break;
		case 7:
			doAction("Midtone Brighter","DOP_tonality_tuning_V1_03");
			break;
		case 8:
			doAction("Midtone Darker","DOP_tonality_tuning_V1_03");
			break;
		case 9:
			doAction("Midtone Contrast medium","DOP_tonality_tuning_V1_03");
			break;
		case 10:
			doAction("Midtone Contrast medium  Luminosity ","DOP_tonality_tuning_V1_03");
			break;
		case 11:
			doAction("Midtone Brighter medium","DOP_tonality_tuning_V1_03");
			break;
		case 12:
			doAction("Midtone Darker medium","DOP_tonality_tuning_V1_03");
			break;
		case 13:
			doAction("Midtone Contrast narrow","DOP_tonality_tuning_V1_03");
			break;
		case 14:
			doAction("Midtone Contrast narrow  Luminosity","DOP_tonality_tuning_V1_03");
			break;
		case 15:
			doAction("Midtone Brighter narrow","DOP_tonality_tuning_V1_03");
			break;
		case 16:
			doAction("Midtone Darker narrow","DOP_tonality_tuning_V1_03");
			break;
			
		case 17:
			doAction("Highlight_Selection _normal","DOP_tonality_tuning_V1_03");
			if(invert==1) DoInvertSelection();
			break;
		case 18:
			doAction("Highlight_Selection _narrow","DOP_tonality_tuning_V1_03");
			if(invert) DoInvertSelection();
			break;
		case 19:
			doAction("Shadow_Selection _normal","DOP_tonality_tuning_V1_03");
			if(invert) DoInvertSelection();
			break;
		case 20:
			doAction("Shadow_Selection _narrow","DOP_tonality_tuning_V1_03");
			if(invert) DoInvertSelection();
			break;
		case 21:
			doAction("Midtones_Selection_wide","DOP_tonality_tuning_V1_03");
			if(invert) DoInvertSelection();
			break;
		case 22:
			doAction("Midtones_Selection_medium","DOP_tonality_tuning_V1_03");
			if(invert) DoInvertSelection();
			break;
		case 23:
			doAction("Midtones_Selection_narrow","DOP_tonality_tuning_V1_03");
			if(invert) DoInvertSelection();
			break;
	}
}

// ================================================================================

function DoInvertSelection()
{
	var docRef = app.activeDocument;
	docRef.selection.invert();
}

// ================================================================================

function CheckAssumptions(paramArray)
{
	var moduleName = 'CheckAssumptions';

	try {	
		// Check that there is a loaded document
		if (app.documents.length == 0) {
			alert ('There is no active document selected.');
			return false;
		}
	
		return true;
	}
	catch(someError)
	{
		alert( "An unexpected JavaScript error occurred in " +  moduleName + ". Message = " + someError.description);
		ui.close(0);
	}
}

// ================================================================================

// Remember unit settings; and set to values expected by this script
var originalRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;

// Create a reference to the UI
var ui = CreateUI();

// Display the UI and wait for user interaction
RunUI(ui);

// Call the action
CallAppropriateAction();

// Restore unit setting
app.preferences.rulerUnits = originalRulerUnits;
