/*
 * Paper.js
 *
 * This file is part of Paper.js, a JavaScript Vector Graphics Library,
 * based on Scriptographer.org and designed to be largely API compatible.
 * http://paperjs.org/
 * http://scriptographer.org/
 *
 * Distributed under the MIT license. See LICENSE file for details.
 *
 * Copyright (c) 2011, Juerg Lehni & Jonathan Puckey
 * http://lehni.org/ & http://jonathanpuckey.com/
 *
 * All rights reserved.
 */

var PathStyle = this.PathStyle = Base.extend(new function() {
	var keys = ['windingRule', 'resolution', 'strokeColor', 'strokeWidth',
			'strokeCap', 'strokeJoin', 'dashOffset','dashArray', 'miterLimit',
			'strokeOverprint', 'fillColor', 'fillOverprint'],
		strokeFlags = {
			strokeWidth: true,
			strokeCap: true,
			strokeJoin: true,
			miterLimit: true
		};

	var fields = {
		beans: true,

		initialize: function(style) {
			if (style) {
				for (var i = 0, l = keys.length; i < l; i++) {
					var key = keys[i],
						value = style[key];
					if (value !== undefined)
						this[key] = value;
				}
			}
		},

		clone: function() {
			return new PathStyle(this);
		},

		statics: {
			create: function(item, other) {
				var style = new PathStyle(PathStyle.dont);
				style._item = item;
				style.initialize(other);
				return style;
			}
		}
	};

	Item.inject(Base.each(keys, function(key) {
		var isColor = !!key.match(/Color$/),
			part = Base.capitalize(key),
			set = 'set' + part,
			get = 'get' + part;

		fields[set] = function(value) {
			var children = this._item && this._item._children;
			value = isColor ? Color.read(arguments) : value;
			if (children) {
				for (var i = 0, l = children.length; i < l; i++)
					children[i]._style[set](value);
			} else {
				var old = this['_' + key];
				if (old != value && !(old && old.equals && old.equals(value))) {
					this['_' + key] = value;
					if (this._item) {
						this._item._changed(ChangeFlags.STYLE
							| (strokeFlags[key] ? ChangeFlags.STROKE : 0));
					}
				}
			}
			return this;
		};

		fields[get] = function() {
			var children = this._item && this._item._children,
				style;
			// If this item has children, walk through all of them and see if
			// they all have the same style.
			if (children) {
				for (var i = 0, l = children.length; i < l; i++) {
					var childStyle = children[i]._style[get]();
					if (!style) {
						style = childStyle;
					} else if (style != childStyle) {
						// If there is another item with a different style,
						// the style is not defined:
						// PORT: Change this in Sg (currently returns null)
						return undefined;
					}
				}
				return style;
			} else {
				return this['_' + key];
			}
		};

		// 'this' = the Base.each() side-car = the object that is injected into
		// Item above:
		this[set] = function(value) {
			this._style[set](value);
			return this;
		};

		this[get] = function() {
			return this._style[get]();
		};
	}, { beans: true }));

	return fields;
});

@@ -859,24 +859,133 @@
 f (style) %7B%0A
+%09%09%09%09// Note: This relies on bean setters that get implicetly%0A%09%09%09%09// called when setting values on this%5Bkey%5D.%0A
 %09%09%09%09for (var
@@ -1299,16 +1299,181 @@
 .dont);%0A
+%09%09%09%09// We need _item to be set before calling initialize(), since%0A%09%09%09%09// it is setting bean properties that propagate changes through%0A%09%09%09%09// all of item's children.%0A
 %09%09%09%09styl
@@ -2988,16 +2988,49 @@
 that is 
+returned from%0A%09%09// Base.each and 
 injected
@@ -3034,21 +3034,16 @@
 ted into
-%0A%09%09//
  Item ab

Improve comments.