/**
 * This script is run everytime an album is created with JAlbum.
 * It passes user defined filter<n> variables to dynamically created image filters
 * and passes the html-attribute-style value of the variables to each setter methods of
 * each filter. For example, defining filter1 as
 * class="GrayscaleFilter" thumbnails
 * will will result in a GrayscaleFilter being applied to each thumbnail:
 * The filters are loaded from the plugins folder of JAlbum
 *
 * This script relies on filters following the JavaBean specification and that a class
 * attribute specifying the filter class to load is specified.
 *
 * Modified June 6, 2003 by Versil Summers
 *	This script now accepts a "prescale" variable for filter insertion
 *
 * @author David Ekholm
 */
import se.datadosen.tags.*;
import se.datadosen.util.*;
import se.datadosen.jalbum.*;

engine.removeAllFilters();

Map vars = engine.getUserVariables();
for (int i=1; vars.containsKey("filter"+i); i++) {
	AttributeTokenizer tokens = new AttributeTokenizer((String)vars.get("filter"+i));
	Properties props = new Properties();
	while (tokens.hasMoreTokens()) {
		String name = tokens.nextName();
		String value = tokens.nextValue();
		props.setProperty(name, value == null ? "true" : value);
	}
	JAFilter filter = (JAFilter)getClass(props.getProperty("class")).newInstance();
	BeanBinder.setProperties(filter, props);
	//Figure out where to install filter
	String closeups = props.getProperty("closeups");
	String thumbnails = props.getProperty("thumbnails");
	String prescale = props.getProperty("prescale");

	if (closeups == null && thumbnails == null)	// Default both
		if ("true".equals(prescale)) {
			engine.addFilter(filter, 1);	// PreScaling
		} else {
			engine.addFilter(filter, 2);	// PostScaling
		}
	else if ("true".equals(closeups) && "true".equals(thumbnails))
		if ("true".equals(prescale)) {
			engine.addFilter(filter, 1);	// PreScaling
		} else {
			engine.addFilter(filter, 2);	// PostScaling
		}
	else if ("true".equals(closeups))
		engine.addFilter(filter, 3);		
	else if ("true".equals(thumbnails))
		if ("true".equals(prescale)) {
			engine.addFilter(filter, 4);	// PreScaling
		} else {
			engine.addFilter(filter, 5);	// PostScaling
		}
}