<NME> layer.cocoascript
<BEF> @import 'inventory/classes/state.cocoascript'

@import 'inventory/getters/normalize.cocoascript'
@import 'inventory/getters/get-place.cocoascript'

@import 'actions/generate-inventory.cocoascript'
@import 'actions/describe-inventory.cocoascript'

	// name of layer
	this.layerName = layerName;
	this.states = [];
	//this.image = nil
	
	// active conditions
	this.areOpacityActive = false

	this.filterRequired = false
	this.titleState = nil

	// active conditions
	this.areOpacityActive = false
	this.areCornersActive = false
	this.areRotationsActive = false

	this.areShadowsActive = false
	this.areInnersActive = false
	this.areBordersActive = false
	this.areBackgroundsActive = true // always export bg


	// exportable conditions
	this.areShadowsExportable = true
	this.areInnersExportable = true
	this.areBordersExportable = true
	this.areBackgroundsExportable = true

	// if equals -> css-filter, else -> image
	this.areAllShadowsEqual = true
	this.areAllBackgroundsExist = true

}


Layer.prototype.composeState = function(layer) {
	var state = new State(layer)
	this.updateConditions(state)
	this.states.push(state)
}


// add state and make it transparent



Layer.prototype.updateConditions = function(state) {
	// active
	this.areOpacityActive = this.areOpacityActive || state.opacityActive



Layer.prototype.updateConditions = function(state) {
	// active
	this.areOpacityActive = this.areOpacityActive || state.opacityActive
	this.areCornersActive = this.areCornersActive || state.cornerActive
	this.areRotationsActive = this.areRotationsActive || state.rotationActive

	this.areShadowsActive = this.areShadowsActive || state.shadowsActive
	this.areInnersActive = this.areInnersActive || state.innersActive
	this.areBordersActive = this.areBordersActive || state.bordersActive
	this.areBackgroundsActive = this.areBackgroundsActive || state.backgroundsActive



	// exportable
	this.areShadowsExportable = this.areShadowsExportable && state.shadowsExportable
	this.areAllBackgroundsExist = this.areAllBackgroundsExist && state.backgroundsActive
}




	return true
}







Layer.prototype.describeState = function(currentState, imageRequired) {
	if (imageRequired == nil) { imageRequired = false }

	var stateDescription = ""
	// stateDescription += GenerateInventory.generatePosition(currentState.composePosition())
	stateDescription += GenerateInventory.generateWidth(currentState.width)
	stateDescription += GenerateInventory.generateHeight(currentState.height)
	stateDescription += GenerateInventory.generateX(currentState.x)
	stateDescription += GenerateInventory.generateY(currentState.y)

	if (!imageRequired) { stateDescription += GenerateInventory.generateCorner(currentState.corner) }

	if (!imageRequired) { stateDescription += GenerateInventory.generateBackgrounds(currentState.composeBackgrounds()) }

	stateDescription += GenerateInventory.generateRotation(currentState.rotation)

	if (!imageRequired) { stateDescription += GenerateInventory.generateShadows(currentState.composeShadows()) }
	if (!imageRequired) { stateDescription += GenerateInventory.generateBorders(currentState.composeBorders()) }

	stateDescription += GenerateInventory.generateOpacity(currentState.opacity)

	return stateDescription
}


Layer.prototype.describeLayerTitle = function(currentState, imageRequired) {
	stateDescription += generateRotation(currentState.rotation)
	
	if (!imageRequired) { stateDescription += generateShadows(currentState.composeShadows()) }
	if (!imageRequired) { stateDescription += generateBorders(currentState.composeBorders()) }
	
	stateDescription += generateOpacity(currentState.opacity)
	stateDescription += GenerateInventory.generateX(currentState.x)
	stateDescription += GenerateInventory.generateY(currentState.y)

	if (!imageRequired) { stateDescription += GenerateInventory.generateCorner(currentState.corner) }




Layer.prototype.returnShadowOpacity = function(value) {
	if (!this.checkStates()) { return nil }
	for (var i = 0; i < this.states.length; i++) { this.states[i].shadowOpacity = value }

	stateDescription += GenerateInventory.generateOpacity(currentState.opacity)

	return stateDescription.replace("Layer,", "Layer")
}







Layer.prototype.returnShadowOpacity = function(value) {
	if (!this.checkStates()) { return nil }
	for (var i = 0; i < this.states.length; i++) { this.states[i].shadowOpacity = value }
}

Layer.prototype.returnShadowHex = function(value) {
	if (!this.checkStates()) { return nil }
	for (var i = 0; i < this.states.length; i++) { this.states[i].shadowHex = value }
}

Layer.prototype.returnBorderOpacity = function(value) {
	if (!this.checkStates()) { return nil }
	for (var i = 0; i < this.states.length; i++) { this.states[i].borderOpacity = value }
}

Layer.prototype.returnBorderHex = function(value) {
	if (!this.checkStates()) { return nil }



Layer.prototype.printLayer = function() {
	return this.generateFramerStates()
}

Layer.prototype.generateFramerStates = function() {
	if (!this.checkStates()) { return nil }
	
	var layerType = this.analyseType()
	var areAllGeneratable = this.areAllGeneratable()
	var imageRequired = ((layerType == 2 || layerType == 3) && areAllGeneratable) ? false : true
	
	var generatedStates = []
	var statesDescriptions = []
	var statesNames = []
	
	
	var prevState = nil
	var maxSpace = 0
	
	var titleState = new State()
	
	titleState.corner = this.analyseCorner()
	titleState.rotation = this.analyseRotation()
	
	var titleShadows = []
	titleShadows.push(this.analyseShadowX())
	titleShadows.push(this.analyseShadowY())
	return this.generateFramerStates()
}


Layer.prototype.generateFramerTitle = function(imageRequired) {
	var titleState = new State()

	titleState.corner = this.analyseCorner()
	titleState.rotation = this.analyseRotation()

	this.filterRequired = this.analyseFilterShadows(this.areShadowsExportable)

	var titleShadows = []
	titleShadows.push(this.analyseShadowX())
	titleShadows.push(this.analyseShadowY())
	titleShadows.push(this.analyseShadowBlur())
	titleShadows.push(this.analyseShadowSpread())
	var shadowColor = this.analyseShadowColor()
	if (shadowColor == nil) {
		titleShadows.push(nil)
		titleShadows.push(nil)
	}
	else {
		titleShadows.push(shadowColor[0])
		titleShadows.push(shadowColor[1])
	}
	titleState.shadows = [titleShadows]


	var titleBorders = []
	titleBorders.push(1)
	titleBorders.push(this.analyseBorderWidth())
	titleBorders.push(0)
	var shadowColor = this.analyseBorderColor()
	if (shadowColor == nil) {
	}
	
	titleState.opacity = this.analyseOpacity()
	log("OPACITY: " + titleState.opacity)
	if (imageRequired) { this.updatePositionImage() }
	
	var titlePosition = []
	titleState.borders = [titleBorders]


	var titleBackgrounds = []
	titleState.position = titlePosition
	
	titleState.updateState()


	var titleDescription = this.describeLayerTitle(titleState, imageRequired)
	
	
	
	for (var i = 0; i < this.states.length; i++) {
		var currentState = this.states[i]
		// currentState.updateState()
		
		if (currentState.isEqualToState(prevState)) {
			if (showLog) { log("Skipping duplicated state") }
	var layerType = this.analyseType()
	var areAllGeneratable = this.areAllGeneratable()

		var stateDescription = this.describeState(currentState, imageRequired)
		
		if (layerType == 2 || layerType == 3) {
			/*
			// operating with rects and ovals
			if (areAllGeneratable) {
				outputString += generateCorner(currentState.corner)
				outputString += generateRotation(currentState.rotation)
				outputString += generateBackgrounds(currentState.backgrounds)
				outputOpacityString = generateOpacity(currentState.opacity)
				outputString += generateShadows(currentState.shadows)
				outputString += generateBorders(currentState.borders)
				outputPositionString += generatePosition(currentState.position)			
			}
			else {
				var currentStatePlace = currentState.position
				var normal = this.normalizeLayer(currentState.layer, false)

				if (currentStatePlace != nil) {
					var currentSpace = currentStatePlace[0] + currentStatePlace[1]
					if (currentSpace > maxSpace) {
						maxSpace = currentSpace
						this.copyLayerToNormalizedPage(normal)
					}
				}
				
				outputPositionString += generatePosition(currentStatePlace)
			}
			*/
		}
		/*
		else if (layerType == 1) {
			
			// operating with groups
			var currentStatePlace = []
			var normal = nil
			
			if (this.areAllShadowsEqual) {

				if (this.areShadowsActive && outputFilterShadowsString == "" && currentState.shadowsActive) {
						outputFilterShadowsString = generateFilterShadows(currentState.shadows)
				}
				
				currentStatePlace = getPlaceImage(currentState.layer)
				normal = this.normalizeLayer(currentState.layer, true)
			}
			else {
				currentStatePlace = getPlaceImage(currentState.layer)
				normal = this.normalizeLayer(currentState.layer, false)
			}
			
			var currentSpace = currentStatePlace[0] + currentStatePlace[1]
			if (currentSpace > maxSpace) {
				maxSpace = currentSpace
				this.copyLayerToNormalizedPage(normal)
			}

			outputPositionString += generatePosition(currentStatePlace)
			if (outputImageNameString == "") { outputImageNameString = generateImagePath(this.layerName) }
			
		}
		else if (layerType == 5) {
			
			// operating with other shapes
			var currentStatePlace = []
			var normal = nil
			
			if (areAllGeneratable && this.areAllShadowsEqual) {

				if (this.areShadowsActive && outputFilterShadowsString == "" && currentState.shadowsActive) {
	if (StatisticsInventory.getStatus() == Status.simulateID() && userDefaults.myMinimalExport == 1) { this.states = this.minimazeStates() }

	for (var i = 0; i < this.states.length; i++) {
		var currentState = this.states[i]

		if (currentState.isEqualToState(prevState) && userDefaults.myMinimalExport == 1) {
			// if (showLog) { log("Skipping duplicated state: " + currentState.stateName) }
			continue
		}

		var stateDescription = this.describeState(currentState, imageRequired)

		// if layer description has all properties
		if (stateDescription != "") {
			statesDescriptions.push(stateDescription)
			statesNames.push(currentState.stateName)
			FramerInventory.addStateName(currentState.stateName)

			outputPositionString += generatePosition(currentStatePlace)
			if (outputImageNameString == "") { outputImageNameString = generateImagePath(this.layerName) }
			
		}*/
		else {
			log("ERROR. CANT PRINT STATE OF THIS TYPE.")
		}
		
		prevState = currentState
		
		// statesDescriptions.push(outputPositionString + outputString + outputOpacityString)
		// statesNames.push(currentState.stateName)
		
		statesDescriptions.push(stateDescription)
		statesNames.push(currentState.stateName)
	}
	
	
	// for (var i = 0; i < statesDescriptions.length; i++) {
// 		log(statesNames[i] + " " + statesDescriptions[i])
// 	}
//
// 	if (statesDescriptions.length > 0) {
// 		outputNameString += statesDescriptions[0] + outputImageNameString + outputFilterShadowsString
//
// 		for (var i = 1; i < statesDescriptions.length; i++) {
// 			generatedStates.push(generateState(statesNames[i], statesDescriptions[i]))
// 		}
// 	}

	for (var i = 0; i < statesDescriptions.length; i++) {
		log(statesNames[i] + " " + statesDescriptions[i])
	}
	
	if (statesDescriptions.length > 1) {
		for (var i = 0; i < statesDescriptions.length; i++) {
			generatedStates.push(generateState(statesNames[i], statesDescriptions[i]))
		}
	}
	
	return titleDescription + dln() + generateStateSection(this.layerName, generatedStates)
	if (statesAndSwitchString == "") { return titleDescription + dln() }
	return titleDescription + dln() + statesAndSwitchString
}






}

Layer.prototype.updatePositionImage = function() {
	for (var i = 0; i < this.states.length; i++) {
		this.states[i].position = getPlaceImage(this.states[i].layer)
		this.states[i].updateState()
	}
}






  [size setScale:scaleData]
  [size setName:""]
}













	return defaultType
}


Layer.prototype.analyseOpacity = function() {
	if (!this.checkStates()) { return nil }

	for (var i = 1; i < this.states.length; i++) {
		if (!LogicInventory.compareMatrices(defaultValue, this.states[i].shadows)) {
			return false
		}
	}

	if (defaultValue == nil) { return false}
	return true
}

Layer.prototype.analyseOpacity = function() {
	if (!this.checkStates()) { return nil }

	var defaultOpacity = this.states[0].opacity
	var isSameOpacity = true
	var opacityAbsolute = 1

	for (var i = 1; i < this.states.length; i++) {
		if (defaultOpacity != this.states[i].opacity) {
			isSameOpacity = false
			break
		}
	}

	if (isSameOpacity) {
		for (var i = 0; i < this.states.length; i++) {
			this.states[i].opacity = nil
			this.states[i].opacityActive = false
		}

		this.areOpacityActive = false
		return defaultOpacity
	}
	else {
		for (var i = 0; i < this.states.length; i++) {
			if (this.states[i].opacity == nil) {
				this.states[i].opacity = opacityAbsolute
				this.states[i].opacityActive = true
			}
		}
	}

	return nil
}


Layer.prototype.analyseCorner = function() {
	if (!this.checkStates()) { return nil }

	var defaultCorner = this.states[0].corner
	var isSameCorner = true
	var cornerAbsolute = 0

	for (var i = 1; i < this.states.length; i++) {
		if (defaultCorner != this.states[i].corner) {
			isSameCorner = false
			break
		}
	}

	if (isSameCorner) {
		for (var i = 0; i < this.states.length; i++) {
			this.states[i].corner = nil
			this.states[i].cornerActive = false
		}

		this.areCornersActive = false
		return defaultCorner
	}
	else {
		for (var i = 0; i < this.states.length; i++) {
			if (this.states[i].corner == nil) {
				this.states[i].corner = cornerAbsolute
				this.states[i].cornerActive = true
			}
		}
	}

	return nil
}


Layer.prototype.analyseRotation = function() {
	if (!this.checkStates()) { return nil }

	var defaultRotation = this.states[0].rotation
	var isSameRotation = true
	var rotationAbsolute = 0

	for (var i = 1; i < this.states.length; i++) {
		if (defaultRotation != this.states[i].rotation) {
			isSameRotation = false
			break
		}
	}

	if (isSameRotation) {
		for (var i = 0; i < this.states.length; i++) {
			this.states[i].rotation = nil
			this.states[i].rotationActive = false
		}

		this.areRotationsActive = false
		return defaultRotation
	}
	else {
		for (var i = 0; i < this.states.length; i++) {
			if (this.states[i].rotation == nil) {
				this.states[i].rotation = rotationAbsolute
				this.states[i].rotationActive = true
			}
		}
	}

	return nil
}




Layer.prototype.analyseBorderWidth = function() {
	if (!this.checkStates()) { return nil }

	var defaultValue = this.states[0].borderWidth
	var sameValue = true

	for (var i = 1; i < this.states.length; i++) {
		if (defaultValue != this.states[i].borderWidth) {
			sameValue = false
			break
		}
	}

	if (sameValue) {
		for (var i = 0; i < this.states.length; i++) { this.states[i].borderWidth = nil }
		if (defaultValue == 0) { return nil }
		return defaultValue
	}
	else {
		for (var i = 0; i < this.states.length; i++) {
			if (this.states[i].borderWidth == nil) {
				this.states[i].borderWidth = 0
			}
		}

		defaultValue = this.states[0].borderWidth
		sameValue = true

		for (var i = 1; i < this.states.length; i++) {
			if (defaultValue != this.states[i].borderWidth) {
				sameValue = false
				break
			}
		}

		if (sameValue) {
			for (var i = 0; i < this.states.length; i++) { this.states[i].borderWidth = nil }
			return defaultValue
		}
	}

	return nil
}

Layer.prototype.analyseBorderColor = function() {
	if (!this.checkStates()) { return nil }

	var defaultValueOpacity = this.states[0].borderOpacity
	var defaultValueHex = this.states[0].borderHex
	var sameValue = true

	for (var i = 1; i < this.states.length; i++) {
		if (defaultValueOpacity != this.states[i].borderOpacity || defaultValueHex != this.states[i].borderHex) {
			sameValue = false
			break
		}
	}

	if (sameValue) {
		for (var i = 0; i < this.states.length; i++) {
			this.states[i].borderOpacity = nil
			this.states[i].borderHex = nil
		}
		return [defaultValueOpacity, defaultValueHex]
	}
	else {
		////
		for (var i = 0; i < this.states.length; i++) {
			if (this.states[i].borderHex == nil) {
				var suggestedBorders = this.suggestBorders(i)
				if (suggestedBorders == nil) { log("ERROR: suggestBorders is nil") }
				this.states[i].borderHex = suggestedBorders[0][4]
				this.states[i].borderOpacity = suggestedBorders[0][3]
			}
		}

		defaultValueOpacity = this.states[0].borderOpacity
		defaultValueHex = this.states[0].borderHex
		sameValue = true

		for (var i = 1; i < this.states.length; i++) {
			if (defaultValueOpacity != this.states[i].borderOpacity || defaultValueHex != this.states[i].borderHex) {
				sameValue = false
				break
			}
		}

		if (sameValue) {
			for (var i = 0; i < this.states.length; i++) {
				this.states[i].borderOpacity = nil
				this.states[i].borderHex = nil
			}
			return [defaultValueOpacity, defaultValueHex]
		}
	}
	return nil
}








Layer.prototype.analyseBackgroundColor = function() {
	if (!this.checkStates()) { return nil }

	var defaultValueOpacity = this.states[0].backgroundOpacity
	var defaultValueHex = this.states[0].backgroundHex
	var sameValue = true

	for (var i = 1; i < this.states.length; i++) {
		if (defaultValueOpacity != this.states[i].backgroundOpacity || defaultValueHex != this.states[i].backgroundHex) {
			sameValue = false
			break
		}
	}

	if (sameValue) {
		for (var i = 0; i < this.states.length; i++) {
			this.states[i].backgroundOpacity = nil
			this.states[i].backgroundHex = nil
		}
		// if all layers have bg with zero opacity
		if (defaultValueHex == nil && (this.states[0].type == 2 || this.states[0].type == 3)) {
			for (var i = 0; i < this.states.length; i++) { this.states[i].opacity = 0 }
		}
		return [defaultValueOpacity, defaultValueHex]
	}
	else {
		for (var i = 0; i < this.states.length; i++) {
			if (this.states[i].backgroundHex == nil) {
				this.states[i].opacity = 0

				var suggestedBackgrounds = this.suggestBackgrounds(i)
				if (suggestedBackgrounds == nil) { log("ERROR: suggestedBackgrounds is nil") }
				this.states[i].backgroundOpacity = suggestedBackgrounds[0][1]
				this.states[i].backgroundHex = suggestedBackgrounds[0][2]
			}
		}

		defaultValueOpacity = this.states[0].backgroundOpacity
		defaultValueHex = this.states[0].backgroundHex
		sameValue = true

		for (var i = 1; i < this.states.length; i++) {
			if (defaultValueOpacity != this.states[i].backgroundOpacity || defaultValueHex != this.states[i].backgroundHex) {
				sameValue = false
				break
			}
		}

		if (sameValue) {
			for (var i = 0; i < this.states.length; i++) {
				this.states[i].backgroundOpacity = nil
				this.states[i].backgroundHex = nil
			}
			return [defaultValueOpacity, defaultValueHex]
		}
	}
	return nil
}




Layer.prototype.analysePosition = function() {
	this.analyseWidth()
	this.analyseHeight()
	this.analyseX()
	this.analyseY()
}

Layer.prototype.analyseWidth = function() {
	if (!this.checkStates()) { return nil }

	var defaultWidth = this.states[0].width
	var isSameWidth = true

	for (var i = 1; i < this.states.length; i++) {
		if (defaultWidth != this.states[i].width) {
			isSameWidth = false
			break
		}
	}

	if (isSameWidth) {
		for (var i = 0; i < this.states.length; i++) { this.states[i].width = nil }
		return defaultWidth
	}

	return nil
}

Layer.prototype.analyseHeight = function() {
	if (!this.checkStates()) { return nil }
		for (var i = 0; i < this.states.length; i++) {
			if (this.states[i].backgroundHex == nil) {
				this.states[i].opacity = 0
				log("KNUDGE")
				
				var suggestedBackgrounds = this.suggestBackgrounds(i)
				if (suggestedBackgrounds == nil) { log("ERROR: suggestedBackgrounds is nil") }
			isSameHeight = false
			break
		}
	}

	if (isSameHeight) {
		for (var i = 0; i < this.states.length; i++) { this.states[i].height = nil }
		return defaultHeight
	}

	return nil
}

Layer.prototype.analyseX = function() {
	if (!this.checkStates()) { return nil }

	var defaultX = this.states[0].x
	var isSameX = true

	for (var i = 1; i < this.states.length; i++) {
		if (defaultX != this.states[i].x) {
			isSameX = false
			break
		}
	}

	if (isSameX) {
		for (var i = 0; i < this.states.length; i++) { this.states[i].x = nil }
		return defaultX
	}

	return nil
}

Layer.prototype.analyseY = function() {
	if (!this.checkStates()) { return nil }

	var defaultY = this.states[0].y
	var isSameY = true

	for (var i = 1; i < this.states.length; i++) {
		if (defaultY != this.states[i].y) {
			isSameY = false
			break
		}
	}

	if (isSameY) {
		for (var i = 0; i < this.states.length; i++) { this.states[i].y = nil }
		return defaultY
	}

	return nil
}








Layer.prototype.analyseShadowX = function() {
	if (!this.checkStates()) { return nil }

	var defaultValue = this.states[0].shadowX
	var sameValue = true

	for (var i = 1; i < this.states.length; i++) {
		if (defaultValue != this.states[i].shadowX) {
			sameValue = false
			break
		}
	}

	if (sameValue) {
		for (var i = 0; i < this.states.length; i++) { this.states[i].shadowX = nil }
		if (defaultValue == 0) { return nil }
		return defaultValue
	}
	else {
		for (var i = 0; i < this.states.length; i++) {
			if (this.states[i].shadowX == nil) {
				this.states[i].shadowX = 0
			}
		}

		defaultValue = this.states[0].shadowX
		sameValue = true

		for (var i = 1; i < this.states.length; i++) {
			if (defaultValue != this.states[i].shadowX) {
				sameValue = false
				break
			}
		}

		if (sameValue) {
			for (var i = 0; i < this.states.length; i++) { this.states[i].shadowX = nil }
			return defaultValue
		}
	}

	return nil
}

Layer.prototype.analyseShadowY = function() {
	if (!this.checkStates()) { return nil }

	var defaultValue = this.states[0].shadowY
	var sameValue = true

	for (var i = 1; i < this.states.length; i++) {
		if (defaultValue != this.states[i].shadowY) {
			sameValue = false
			break
		}
	}

	if (sameValue) {
		for (var i = 0; i < this.states.length; i++) { this.states[i].shadowY = nil }
		if (defaultValue == 0) { return nil }
		return defaultValue
	}
	else {
		for (var i = 0; i < this.states.length; i++) {
			if (this.states[i].shadowY == nil) {
				this.states[i].shadowY = 0
			}
		}

		defaultValue = this.states[0].shadowY
		sameValue = true

		for (var i = 1; i < this.states.length; i++) {
			if (defaultValue != this.states[i].shadowY) {
				sameValue = false
				break
			}
		}

		if (sameValue) {
			for (var i = 0; i < this.states.length; i++) { this.states[i].shadowY = nil }
			return defaultValue
		}
	}

	return nil
}

Layer.prototype.analyseShadowBlur = function() {
	if (!this.checkStates()) { return nil }

	var defaultValue = this.states[0].shadowBlur
	var sameValue = true

	for (var i = 1; i < this.states.length; i++) {
		if (defaultValue != this.states[i].shadowBlur) {
			sameValue = false
			break
		}
	}

	if (sameValue) {
		for (var i = 0; i < this.states.length; i++) { this.states[i].shadowBlur = nil }
		if (defaultValue == 0) { return nil }
		return defaultValue
	}
	else {
		for (var i = 0; i < this.states.length; i++) {
			if (this.states[i].shadowBlur == nil) {
				this.states[i].shadowBlur = 0
			}
		}

		defaultValue = this.states[0].shadowBlur
		sameValue = true

		for (var i = 1; i < this.states.length; i++) {
			if (defaultValue != this.states[i].shadowBlur) {
				sameValue = false
				break
			}
		}

		if (sameValue) {
			for (var i = 0; i < this.states.length; i++) { this.states[i].shadowBlur = nil }
			return defaultValue
		}
	}

	return nil
}

Layer.prototype.analyseShadowSpread = function() {
	if (!this.checkStates()) { return nil }

	var defaultValue = this.states[0].shadowSpread
	var sameValue = true

	for (var i = 1; i < this.states.length; i++) {
		if (defaultValue != this.states[i].shadowSpread) {
			sameValue = false
			break
		}
	}

	if (sameValue) {
		for (var i = 0; i < this.states.length; i++) { this.states[i].shadowSpread = nil }
		if (defaultValue == 0) { return nil }
		return defaultValue
	}
	else {
		for (var i = 0; i < this.states.length; i++) {
			if (this.states[i].shadowSpread == nil) {
				this.states[i].shadowSpread = 0
			}
		}

		defaultValue = this.states[0].shadowSpread
		sameValue = true

		for (var i = 1; i < this.states.length; i++) {
			if (defaultValue != this.states[i].shadowSpread) {
				sameValue = false
				break
			}
		}

		if (sameValue) {
			for (var i = 0; i < this.states.length; i++) { this.states[i].shadowSpread = nil }
			return defaultValue
		}
	}

	return nil
}






Layer.prototype.analyseShadowColor = function() {
	if (!this.checkStates()) { return nil }

	var defaultValueOpacity = this.states[0].shadowOpacity
	var defaultValueHex = this.states[0].shadowHex
	var sameValue = true

	for (var i = 1; i < this.states.length; i++) {
		if (defaultValueOpacity != this.states[i].shadowOpacity || defaultValueHex != this.states[i].shadowHex) {
			sameValue = false
			break
		}
	}

	if (sameValue) {
		for (var i = 0; i < this.states.length; i++) {
			this.states[i].shadowOpacity = nil
			this.states[i].shadowHex = nil
		}
		return [defaultValueOpacity, defaultValueHex]
	}
	else {
		////
		for (var i = 0; i < this.states.length; i++) {
			if (this.states[i].shadowHex == nil) {
				var suggestedShadows = this.suggestShadows(i)
				if (suggestedShadows == nil) { log("ERROR: suggestShadows is nil") }
				this.states[i].shadowOpacity = suggestedShadows[0][4]
				this.states[i].shadowHex = suggestedShadows[0][5]
			}
		}

		defaultValueOpacity = this.states[0].shadowOpacity
		defaultValueHex = this.states[0].shadowHex
		sameValue = true

		for (var i = 1; i < this.states.length; i++) {
			if (defaultValueOpacity != this.states[i].shadowOpacity || defaultValueHex != this.states[i].shadowHex) {
				sameValue = false
				break
			}
		}

		if (sameValue) {
			for (var i = 0; i < this.states.length; i++) {
				this.states[i].shadowOpacity = nil
				this.states[i].shadowHex = nil
			}
			return [defaultValueOpacity, defaultValueHex]
		}

	}
	return nil
}









Layer.prototype.suggestShadows = function(currentStateNumber) {

	for (var i = currentStateNumber; i < this.states.length; i++) {
		if (this.states[i].shadowsActive && this.states[i].shadows != nil) {
			return this.states[i].shadows
		}
	}

	for (var i = currentStateNumber; i >= 0; i--) {
		if (this.states[i].shadowsActive && this.states[i].shadows != nil) {
			return this.states[i].shadows
		}
	}
	return nil

}

Layer.prototype.suggestBorders = function(currentStateNumber) {

	for (var i = currentStateNumber; i < this.states.length; i++) {
		if (this.states[i].bordersActive && this.states[i].borders != nil) {
			return this.states[i].borders
		}
	}
	for (var i = currentStateNumber; i >= 0; i--) {
		if (this.states[i].bordersActive && this.states[i].borders != nil) {
			return this.states[i].borders
		}
	}
	return nil

}

Layer.prototype.suggestBackgrounds = function(currentStateNumber) {

	for (var i = currentStateNumber; i < this.states.length; i++) {
		if (this.states[i].backgroundsActive && this.states[i].backgrounds != nil) {
			return this.states[i].backgrounds
		}
	}
	for (var i = currentStateNumber; i >= 0; i--) {
		if (this.states[i].backgroundsActive && this.states[i].backgrounds != nil) {
			return this.states[i].backgrounds
		}
	}
	return nil

}










Layer.prototype.minimazeStates = function() {
	if (!this.checkStates()) { return nil }

	var minimazedStates = []
	minimazedStates.push(this.states[0])

	var baseState = nil
	var currentState = nil

	for (var i = 1; i < this.states.length; i++) {
		baseState = this.states[i - 1]
		currentState = this.states[i]

		var cleanedState = baseState.getMinimalState(currentState)
		cleanedState.stateName = currentState.stateName
		minimazedStates.push(cleanedState)
	}

	return minimazedStates
}






var ln = function() { return "\n" }
var dln = function() { return ln() + ln() }
var tln = function() { return "\t" }

var br = function() {
	if (userDefaults.myDetailedNotation == 1) { return ln() + tln() }
	else { return ", " }
}
var brf = function() {
	if (userDefaults.myDetailedNotation == 1) { return tln() }
	else { return " " }
}



Layer.prototype.areAllGeneratable = function(state) {
	return (this.areShadowsExportable && this.areInnersExportable && this.areBordersExportable && this.areBackgroundsExportable) ? true : false
}

Layer.prototype.compareShadows = function(state) {
	if (this.states == nil || state == nil || this.states.length < 1) { return true }
	if (!compareMatrices(this.states[0].shadows, state.shadows)) { return false }
	return true
}







Layer.prototype.normalizeLayer = function(layer, cleanLayer) {
	if (cleanLayer == nil) { cleanLayer = false }
	
	var layer_copy = [layer duplicate]
	[layer_copy removeFromParent]
	
	if (cleanLayer) { cleanShadows(layer_copy) }
	
	return layer_copy
}


Layer.prototype.copyLayerToNormalizedPage = function(layer) {
	var size_data = {
	    "format": "png",
	    "scale": 2,
	    "suffix": ""
	}
	
	var currentPage = FramerInventory.getCurrentPage()
	var framerInventoryPage = FramerInventory.addPage(currentPage)
	FramerInventory.cleanLayer(layer, framerInventoryPage)
	[framerInventoryPage addLayers: [layer]]
	
	var size = [[layer exportOptions] addExportSize]
 	[size setFormat:size_data.format]
    [size setScale:size_data.scale]
    [size setName:size_data.suffix]
}




var ln = function() { return "\n" }
var dln = function() { return ln() + ln() }
var tln = function() { return "\t" }

<MSG> Support for filter shadow and exportOptions

- if the shadows are the same everywhere, we should use filterShadow
- properly created exportOptions

<DFF> @@ -9,7 +9,8 @@ function Layer (layerName) {
 	// name of layer
 	this.layerName = layerName;
 	this.states = [];
-	//this.image = nil
+	
+	this.filterRequired = false
 	
 	// active conditions
 	this.areOpacityActive = false
@@ -52,6 +53,9 @@ Layer.prototype.addFirstState = function(state) {
 
 
 
+
+
+
 Layer.prototype.updateConditions = function(state) {
 	// active
 	this.areOpacityActive = this.areOpacityActive || state.opacityActive
@@ -76,6 +80,16 @@ Layer.prototype.updateConditions = function(state) {
 	this.areAllBackgroundsExist = this.areAllBackgroundsExist && state.backgroundsActive
 }
 
+Layer.prototype.areAllGeneratable = function(state) {
+	return (this.areShadowsExportable && this.areInnersExportable && this.areBordersExportable && this.areBackgroundsExportable) ? true : false
+}
+
+Layer.prototype.compareShadows = function(state) {
+	if (this.states == nil || state == nil || this.states.length < 1) { return true }
+	if (!compareMatrices(this.states[0].shadows, state.shadows)) { return false }
+	return true
+}
+
 
 
 
@@ -117,6 +131,7 @@ Layer.prototype.describeLayerTitle = function(currentState, imageRequired) {
 	stateDescription += generateRotation(currentState.rotation)
 	
 	if (!imageRequired) { stateDescription += generateShadows(currentState.composeShadows()) }
+	if (imageRequired && this.filterRequired) { stateDescription += generateFilterShadows(currentState.composeShadows()) }
 	if (!imageRequired) { stateDescription += generateBorders(currentState.composeBorders()) }
 	
 	stateDescription += generateOpacity(currentState.opacity)
@@ -128,6 +143,8 @@ Layer.prototype.describeLayerTitle = function(currentState, imageRequired) {
 
 
 
+
+
 Layer.prototype.returnShadowOpacity = function(value) {
 	if (!this.checkStates()) { return nil }
 	for (var i = 0; i < this.states.length; i++) { this.states[i].shadowOpacity = value }
@@ -163,30 +180,21 @@ Layer.prototype.returnBackgroundHex = function(value) {
 
 
 
+
+
 Layer.prototype.printLayer = function() {
 	return this.generateFramerStates()
 }
 
-Layer.prototype.generateFramerStates = function() {
-	if (!this.checkStates()) { return nil }
-	
-	var layerType = this.analyseType()
-	var areAllGeneratable = this.areAllGeneratable()
-	var imageRequired = ((layerType == 2 || layerType == 3) && areAllGeneratable) ? false : true
-	
-	var generatedStates = []
-	var statesDescriptions = []
-	var statesNames = []
-	
-	
-	var prevState = nil
-	var maxSpace = 0
-	
+Layer.prototype.generateFramerTitle = function(imageRequired) {
 	var titleState = new State()
 	
 	titleState.corner = this.analyseCorner()
 	titleState.rotation = this.analyseRotation()
 	
+	this.filterRequired = this.analyseFilterShadows(this.areShadowsExportable)
+	log("FILTER: " + this.filterRequired)
+	
 	var titleShadows = []
 	titleShadows.push(this.analyseShadowX())
 	titleShadows.push(this.analyseShadowY())
@@ -228,7 +236,7 @@ Layer.prototype.generateFramerStates = function() {
 	}
 	
 	titleState.opacity = this.analyseOpacity()
-	log("OPACITY: " + titleState.opacity)
+
 	if (imageRequired) { this.updatePositionImage() }
 	
 	var titlePosition = []
@@ -239,15 +247,34 @@ Layer.prototype.generateFramerStates = function() {
 	titleState.position = titlePosition
 	
 	titleState.updateState()
+	
+	return titleState
+}
 
+Layer.prototype.generateFramerStates = function() {
+	if (!this.checkStates()) { return nil }
+	
+	var layerType = this.analyseType()
+	var areAllGeneratable = this.areAllGeneratable()
+	var imageRequired = ((layerType == 2 || layerType == 3) && areAllGeneratable) ? false : true
+	
+	var generatedStates = []
+	var statesDescriptions = []
+	var statesNames = []
+	
+	
+	var prevState = nil
+	var maxSpace = 0
+	
+	
 
+	var titleState = this.generateFramerTitle(imageRequired)
 	var titleDescription = this.describeLayerTitle(titleState, imageRequired)
 	
 	
 	
 	for (var i = 0; i < this.states.length; i++) {
 		var currentState = this.states[i]
-		// currentState.updateState()
 		
 		if (currentState.isEqualToState(prevState)) {
 			if (showLog) { log("Skipping duplicated state") }
@@ -256,71 +283,14 @@ Layer.prototype.generateFramerStates = function() {
 
 		var stateDescription = this.describeState(currentState, imageRequired)
 		
-		if (layerType == 2 || layerType == 3) {
-			/*
-			// operating with rects and ovals
-			if (areAllGeneratable) {
-				outputString += generateCorner(currentState.corner)
-				outputString += generateRotation(currentState.rotation)
-				outputString += generateBackgrounds(currentState.backgrounds)
-				outputOpacityString = generateOpacity(currentState.opacity)
-				outputString += generateShadows(currentState.shadows)
-				outputString += generateBorders(currentState.borders)
-				outputPositionString += generatePosition(currentState.position)			
-			}
-			else {
-				var currentStatePlace = currentState.position
-				var normal = this.normalizeLayer(currentState.layer, false)
-
-				if (currentStatePlace != nil) {
-					var currentSpace = currentStatePlace[0] + currentStatePlace[1]
-					if (currentSpace > maxSpace) {
-						maxSpace = currentSpace
-						this.copyLayerToNormalizedPage(normal)
-					}
-				}
-				
-				outputPositionString += generatePosition(currentStatePlace)
-			}
-			*/
-		}
+		prevState = currentState
+		
+		statesDescriptions.push(stateDescription)
+		statesNames.push(currentState.stateName)
+		
+		
+		
 		/*
-		else if (layerType == 1) {
-			
-			// operating with groups
-			var currentStatePlace = []
-			var normal = nil
-			
-			if (this.areAllShadowsEqual) {
-
-				if (this.areShadowsActive && outputFilterShadowsString == "" && currentState.shadowsActive) {
-						outputFilterShadowsString = generateFilterShadows(currentState.shadows)
-				}
-				
-				currentStatePlace = getPlaceImage(currentState.layer)
-				normal = this.normalizeLayer(currentState.layer, true)
-			}
-			else {
-				currentStatePlace = getPlaceImage(currentState.layer)
-				normal = this.normalizeLayer(currentState.layer, false)
-			}
-			
-			var currentSpace = currentStatePlace[0] + currentStatePlace[1]
-			if (currentSpace > maxSpace) {
-				maxSpace = currentSpace
-				this.copyLayerToNormalizedPage(normal)
-			}
-
-			outputPositionString += generatePosition(currentStatePlace)
-			if (outputImageNameString == "") { outputImageNameString = generateImagePath(this.layerName) }
-			
-		}
-		else if (layerType == 5) {
-			
-			// operating with other shapes
-			var currentStatePlace = []
-			var normal = nil
-			
 			if (areAllGeneratable && this.areAllShadowsEqual) {
 
 				if (this.areShadowsActive && outputFilterShadowsString == "" && currentState.shadowsActive) {
@@ -344,42 +314,15 @@ Layer.prototype.generateFramerStates = function() {
 
 			outputPositionString += generatePosition(currentStatePlace)
 			if (outputImageNameString == "") { outputImageNameString = generateImagePath(this.layerName) }
-			
-		}*/
-		else {
-			log("ERROR. CANT PRINT STATE OF THIS TYPE.")
-		}
-		
-		prevState = currentState
-		
-		// statesDescriptions.push(outputPositionString + outputString + outputOpacityString)
-		// statesNames.push(currentState.stateName)
-		
-		statesDescriptions.push(stateDescription)
-		statesNames.push(currentState.stateName)
+		*/		
 	}
-	
-	
-	// for (var i = 0; i < statesDescriptions.length; i++) {
-// 		log(statesNames[i] + " " + statesDescriptions[i])
-// 	}
-//
-// 	if (statesDescriptions.length > 0) {
-// 		outputNameString += statesDescriptions[0] + outputImageNameString + outputFilterShadowsString
-//
-// 		for (var i = 1; i < statesDescriptions.length; i++) {
-// 			generatedStates.push(generateState(statesNames[i], statesDescriptions[i]))
-// 		}
-// 	}
 
-	for (var i = 0; i < statesDescriptions.length; i++) {
-		log(statesNames[i] + " " + statesDescriptions[i])
-	}
+	// for (var i = 0; i < statesDescriptions.length; i++) {
+		// log(statesNames[i] + " " + statesDescriptions[i])
+	// }
 	
 	if (statesDescriptions.length > 1) {
-		for (var i = 0; i < statesDescriptions.length; i++) {
-			generatedStates.push(generateState(statesNames[i], statesDescriptions[i]))
-		}
+		for (var i = 0; i < statesDescriptions.length; i++) { generatedStates.push(generateState(statesNames[i], statesDescriptions[i])) }
 	}
 	
 	return titleDescription + dln() + generateStateSection(this.layerName, generatedStates)
@@ -395,14 +338,74 @@ Layer.prototype.checkStates = function() {
 }
 
 Layer.prototype.updatePositionImage = function() {
+	var maxSize = 0
+	var maxSizeNumber = -1
+	
 	for (var i = 0; i < this.states.length; i++) {
-		this.states[i].position = getPlaceImage(this.states[i].layer)
+		var layer = this.states[i].layer
+		var layerForPosition = [layer duplicate]
+		if (this.filterRequired) { Layer.cleanLayerShadows(layerForPosition) }
+		
+		this.states[i].position = getPlaceImage(layerForPosition)
 		this.states[i].updateState()
+		
+		[layerForPosition removeFromParent]
+		
+		
+		var currentSize = this.states[i].width + this.states[i].height
+		if (currentSize > maxSize) {
+			maxSize = currentSize
+			maxSizeNumber = i
+		}
+	}
+	
+	if (maxSizeNumber != -1) {
+		var layerForPosition = this.states[maxSizeNumber].layer
+		
+		layerForPosition = Layer.returnLayerCopy(layerForPosition)
+		if (this.filterRequired) { Layer.cleanLayerShadows(layerForPosition) }
+		Layer.pasteLayer(layerForPosition)
+		Layer.addExportOptions(layerForPosition)
 	}
 }
 
+Layer.returnLayerCopy = function(layer) {
+	var layer_copy = [layer duplicate]
+	[layer_copy removeFromParent]
+	return layer_copy
+}
+
+Layer.cleanLayerShadows = function(layer) {
+	var shadowsCollection = [[layer style] shadows]
+	for (var i = 0; i < shadowsCollection.count(); i++) {
+		[shadowsCollection removeStylePartAtIndex: i]
+	}
+}
 
+Layer.pasteLayer = function(layer) {
+	var currentPage = FramerInventory.getCurrentPage()
+	var framerInventoryPage = FramerInventory.addPage(currentPage)
+	FramerInventory.cleanLayer(layer, framerInventoryPage)
+	[framerInventoryPage addLayers: [layer]]
+}
 
+Layer.addExportOptions = function(layer) {
+	var size_data = {
+	    "format": "png",
+	    "scale": 1,
+	    "suffix": ""
+	}
+	
+	var exportSizes = [[layer exportOptions] sizes]
+	while([exportSizes count] > 0) {
+	    [[exportSizes firstObject] remove]
+	}
+
+	var size = [[layer exportOptions] addExportSize]
+ 	[size setFormat:size_data.format]
+    [size setScale:size_data.scale]
+    [size setName:size_data.suffix]
+}
 
 
 
@@ -425,6 +428,21 @@ Layer.prototype.analyseType = function() {
 	return defaultType
 }
 
+Layer.prototype.analyseFilterShadows = function(areExportable) {
+	if (!this.checkStates()) { return nil }
+	if (!areExportable) { return false }
+	
+	var defaultValue = this.states[0].shadows
+	
+	for (var i = 1; i < this.states.length; i++) {
+		if (!compareMatrices(defaultValue, this.states[i].shadows)) {
+			return false
+		}
+	}
+	
+	if (defaultValue == nil) { return false}
+	return true
+}
 
 Layer.prototype.analyseOpacity = function() {
 	if (!this.checkStates()) { return nil }
@@ -749,7 +767,6 @@ Layer.prototype.analyseBackgroundHex = function() {
 		for (var i = 0; i < this.states.length; i++) {
 			if (this.states[i].backgroundHex == nil) {
 				this.states[i].opacity = 0
-				log("KNUDGE")
 				
 				var suggestedBackgrounds = this.suggestBackgrounds(i)
 				if (suggestedBackgrounds == nil) { log("ERROR: suggestedBackgrounds is nil") }
@@ -1219,15 +1236,7 @@ Layer.prototype.updateStates = function(states) {
 
 
 
-Layer.prototype.areAllGeneratable = function(state) {
-	return (this.areShadowsExportable && this.areInnersExportable && this.areBordersExportable && this.areBackgroundsExportable) ? true : false
-}
 
-Layer.prototype.compareShadows = function(state) {
-	if (this.states == nil || state == nil || this.states.length < 1) { return true }
-	if (!compareMatrices(this.states[0].shadows, state.shadows)) { return false }
-	return true
-}
 
 
 
@@ -1333,40 +1342,6 @@ Layer.prototype.getClassicalState = function() {
 
 
 
-
-Layer.prototype.normalizeLayer = function(layer, cleanLayer) {
-	if (cleanLayer == nil) { cleanLayer = false }
-	
-	var layer_copy = [layer duplicate]
-	[layer_copy removeFromParent]
-	
-	if (cleanLayer) { cleanShadows(layer_copy) }
-	
-	return layer_copy
-}
-
-
-Layer.prototype.copyLayerToNormalizedPage = function(layer) {
-	var size_data = {
-	    "format": "png",
-	    "scale": 2,
-	    "suffix": ""
-	}
-	
-	var currentPage = FramerInventory.getCurrentPage()
-	var framerInventoryPage = FramerInventory.addPage(currentPage)
-	FramerInventory.cleanLayer(layer, framerInventoryPage)
-	[framerInventoryPage addLayers: [layer]]
-	
-	var size = [[layer exportOptions] addExportSize]
- 	[size setFormat:size_data.format]
-    [size setScale:size_data.scale]
-    [size setName:size_data.suffix]
-}
-
-
-
-
 var ln = function() { return "\n" }
 var dln = function() { return ln() + ln() }
 var tln = function() { return "\t" }
\ No newline at end of file

Support for filter shadow and exportOptions