=begin
Copyright 2004,2008 Rick Wilson 

THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.

Name :          Windowizer 4.0
Description :   makes storefront windows from selected faces
Author :        Rick Wilson
Usage :         select faces, set preferences,
                and POOF! - windows!
Date :          2008-10-24
Type :		tool
History:
	4.003 (2009/05/12)	- fixed units bug for "Max Wall Thickness"
	4.002 (2008/10/24)	- assorted fixes
	4.001 (2008/06/23)	- added full 3D frames
								- added support for creating wall openings
								- added complex polygon support
								- added grouped results
								- added style library
								- improved speed in large models
	3.0b6 (2005/06/22)	- fixed "edit" bug
	3.0b5 (2005/05/12)	- added support for multiple erase
						 		- added drop-down material selection
	3.0b4 (2005/01/25)	- added "edit" function
	3.0b3 (2005/01/25)	- added "inherit" and "erase" functions
	3.0b2 (2005/01/24)	- added nonQuadrilateral simple windows
								- added attribute handling
	3.0b1	(2005/01/20)	- added proportional rows/columns
	2.3   (2004/12/15)	- fixed bounded face verification bug
	2.2   (2004/09/15)	- fixed group/component bug
	2.1   (2004/08/17)	- fixed floating point bug in dialog box
								- fixed context menu validation bug
								- fixed selection validation bug
	2.000 (2004/08/12)	- added features: corner selections, non-bounded face support, multiple face support
	1.1e  (2004/07/26)	- metric support
	1.1d  (2004/07/23)	- new selection filters; added flush corner & floor windows
	1.1c  (2004/07/23)	- revised the revision to correct 0 col/row glitch
	1.1   (2004/07/23)	- revised to correct vector bugs resulting in strange windows outside of frames
	1.0   (2004/07/16)	- first version


To Do List:

1. Handle zero-width mullions (for butt-glazing) (4.1?)
2. Create Style Manager to export/import/delete styles (4.1?)
3. Fix UI where imported style frame &/or glass material does not exist (?) (4.1?)
4. Add EDIT feature (4.1?)
5. Add Inherit feature (4.1?)
6. Add Delete feature (4.1?)
7. Investigate making components (4.2?)
8. Enhanced row/column description format (5.0?)

=end




require 'sketchup.rb'
require 'arraysum.rb'
require 'array_to.rb'
require 'getMaterials.rb'
require 'offset.rb'
require 'windowizer4stylemanager.rb'
#require 'face_curves.rb'




class Windowizer4

#	@@idset = %w(vpanes hpanes framewidth frameheight framethickness frameinset framematerial mullwidth mullheight mullthickness glassinset glassthickness glassmaterial)
	@@idset = %w(vpanes hpanes framewidth framethickness frameinset framematerial mullwidth mullheight glassinset glassthickness glassmaterial maxwallthickness makegroup frontonly)

	@@dlg = UI::WebDialog.new("Windowizer4",false,"Windowizer4Dialog",330,620,20,20,true)

	@@vpanes = 2
	@@hpanes = 2

	@@framewidth = 2.inch
#	@@frameheight = 2.inch
	@@framethickness = 4.inch
	@@frameinset = 4.inch
	@@framematerial = "FrameColor"

	@@mullwidth = 2.inch
	@@mullheight = 2.inch
#	@@mullthickness = 4.inch

	@@glassinset = 2.inch
	@@glassthickness = 1.inch
	@@glassmaterial = "GlassColor"

	@@maxwallthickness = 16.inch
	@@makegroup = "Yes"

	@@frontonly = "No"

begin
	@@styles = {} unless @@styles
rescue
	@@styles = {}
end

	attr_accessor :mullwidth, :mullheight, :glassthickness, :framethickness, :framematerial, :glassmaterial, :values



#	protected	



	def initialize

		@model = Sketchup.active_model
		@sel = @model.selection

		@dict = @model.attribute_dictionaries

		if  @dict == nil || (not @dict["Windowizer4styles"])
			Sketchup.active_model.set_attribute("Windowizer4styles","item0","")
			@dict=Sketchup.active_model.attribute_dictionaries["Windowizer4styles"]
			@dict.delete_key("item0")
			check_volatile_styles()
		else
			@dict=Sketchup.active_model.attribute_dictionaries["Windowizer4styles"]
			get_existing_styles()
		end

		@vpanes = @@vpanes 


		@hpanes = @@hpanes

		@framewidth = @@framewidth
#		@frameheight = @@frameheight 
		@framethickness = @@framethickness 
		@frameinset = @@frameinset 
		@framematerial = @@framematerial 

		@mullwidth = @@mullwidth 
		@mullheight = @@mullheight 
#		@mullthickness = @@mullthickness 

		@glassinset = @@glassinset 
		@glassthickness = @@glassthickness 
		@glassmaterial = @@glassmaterial 

		@maxwallthicknes = @@maxwallthickness
		@makegroup = @@makegroup
		@frontonly = @@frontonly

		@dlg = @@dlg

		@dlg.add_action_callback("checkstyle") {|d,p| stylecheck(p) }
		@dlg.add_action_callback("getstyle")	{|d,p| populate_dialog(p)}
		@dlg.add_action_callback("addstyle")	{|d,p| addstyle(p) }
		@dlg.add_action_callback("apply")		{|d,p| apply()}
		@dlg.add_action_callback("done")			{|d,p| @dlg.close() }
		@dlg.add_action_callback("help")			{|d,p| help() }
		@dlg.add_action_callback("populate")	{|d,p| populate_dialog() }
		@dlg.add_action_callback("setframecolor") {|d,p| set_color("frame",p)}
		@dlg.add_action_callback("setglasscolor") {|d,p| set_color("glass",p)}

		optionsmanager()

	end



	def check_volatile_styles()
		@@styles.each_pair do |style,data|
			@dict[style] = data unless @dict[style]
		end
	end



	def get_existing_styles()
		@dict.each_pair do |style,data|
			@@styles[style] = data
		end
	end



	def addstyle(params)
		@dlg.execute_script("document.getElementById(\"styleprompt\").innerHTML = '';")
		stylename,index = params.split("&")
		values = []
		if stylename == ""
			puts "no name provided" 
			return nil
		end
		@@idset.each do |id|
			values << @dlg.get_element_value(id)
		end
	#	@dlg.execute_script()
		add_style(stylename,values) unless stylename == ""
	end



	def getstyle(name)
		populate_dialog(name)
	end



	def apply()
#		UI.messagebox("apply")
		@values = {}
		@@idset.each {|id| @values[id] = @dlg.get_element_value(id)}

		@vpanes = @values["vpanes"]
		@hpanes = @values["hpanes"]

		@framewidth = @values["framewidth"].to_l
#		@frameheight = @values["frameheight"].to_l
		@framethickness = @values["framethickness"].to_l
		@frameinset = @values["frameinset"].to_l
		@framematerial = @values["framematerial"]

		@mullwidth = @values["mullwidth"].to_l
		@mullheight = @values["mullheight"].to_l
#		@mullthickness = @values["mullthickness"].to_l

		@glassinset = @values["glassinset"].to_l
		@glassthickness = @values["glassthickness"].to_l
		@glassmaterial = @values["glassmaterial"]

begin
		@maxwallthickness = @values["maxwallthickness"].to_l
rescue
		@maxwallthickness = @values["maxwallthickness"].to_f.to_l
end
		@@maxwallthickness = @maxwallthickness
		@makegroup = @values["makegroup"]
		@frontonly = @values["frontonly"]

		@makegroup = "Yes" unless @makegroup=="No"
		@frontonly = "No" unless @frontonly=="Yes"

		if @sel.length>0

			if Sketchup.version[0,1].to_i >= 7
				@model.start_operation("Windowizer4",true)
			else
				@model.start_operation "Windowizer4"
			end

			@sel.each do |entity|
				if entity.typename=="Face"
					create_window(entity)
				end
			end

			@model.commit_operation

		end

	end



	def help()
		UI.messagebox("help")
	end



	def set_color(type,material)
		if type=="frame"
			@framematerial = material
		elsif type == "glass"
			@glassmaterial = material
		end
	end



	def optionsmanager()
		default_html = File.dirname(__FILE__) + "/windowizer4-en-US.html"
		dlghtml = "/windowizer5-"+Sketchup.get_locale+".html"
		html = File.dirname(__FILE__) + dlghtml
		@dlg = @@dlg unless @dlg
		(Dir[html]==[]) ? (@dlg.set_file(default_html)) : (@dlg.set_file(html))
		@dlg.show
		sleep(0.3)
		populate_dialog()

	end



	def populate_dialog(stylename=nil)

		matlist = getMaterials().split("|")

		matlist.each_with_index do |material,i|
			js = "document.getElementById(\"framematerial\")[#{i+1}] = new Option('#{material}','#{material}'"
			if @framematerial == material				#	SELECTED MATERIAL
				js += ",true,true"
			end
			js +=");"
			@dlg.execute_script(js)

			js = "document.getElementById(\"glassmaterial\")[#{i+1}] = new Option('#{material}','#{material}'"
			if @glassmaterial == material				#	SELECTED MATERIAL
				js += ",true,true"
			end
			js += ");"
			@dlg.execute_script(js)

		end

	### SET 'MAKEGROUP' AND 'FRONTONLY' OPTION ITEMS
	#
	#

		@makegroup = "Yes" unless @makegroup=="No"
		@frontonly = "No" unless @frontonly=="Yes"
	
		js = "document.getElementById(\"makegroup\")[0] = new Option('Yes','Yes'"
		if @makegroup=="Yes"
			js += ",true,true"
		end
		js += ");"
		@dlg.execute_script(js)

		js = "document.getElementById(\"makegroup\")[1] = new Option('No','No'"
		if @makegroup=="No"
			js += ",true,true"
		end
		js += ");"
		@dlg.execute_script(js)

		js = "document.getElementById(\"frontonly\")[0] = new Option('Yes','Yes'"
		if @frontonly=="Yes"
			js += ",true,true"
		end
		js += ");"
		@dlg.execute_script(js)

		js = "document.getElementById(\"frontonly\")[1] = new Option('No','No'"
		if @frontonly=="No"
			js += ",true,true"
		end
		js += ");"
		@dlg.execute_script(js)
	#
	#
	### END 'MAKEGROUP' AND 'FRONTONLY' OPTION ITEMS

		if stylename && stylename != ""
			valueset = styledata(stylename)
			js = "document.getElementById(\"getstyle\")[0] = new Option('','',false,true);"
			@dlg.execute_script(js)
		else
			valueset = [@@vpanes,@@hpanes,@@framewidth,@@framethickness,@@frameinset,@@framematerial,@@mullwidth,@@mullheight,@@glassinset,@@glassthickness,@@glassmaterial,@@maxwallthickness,@@makegroup,@@frontonly]
		end



		@@idset.each_with_index do |id,i|
			#puts "#{id} = #{valueset[i]}"
			if id=="maxwallthickness"
				value = valueset[i]
			#	puts "value was #{value}"

#				if value.to_s.index('"')			# THIS WORKS OKAY, BUT CREATES >12" PARAMETERS (IE 16").
#					value = valueset[i].to_f		# THE SECTION BELOW CHANGES THIS - IT ACCEPTS INCH OR
#					value = value.to_s + '"'		# FOOT-INCH INPUT

				if value.to_s.index("'")
					value = value.to_s
					value["'"] = '\\\''
				else
					value = value.to_s
				end
				#puts "value changed to #{value}"
			else
				value = valueset[i].to_s
			end
		#	js = "document.getElementById(\"#{id}\").value='#{valueset[i].to_s}'"
			js = "document.getElementById(\"#{id}\").value='#{value}'"
			@dlg.execute_script(js)
		end

		

		styles.each_with_index do |name,i|
			js = "document.getElementById(\"getstyle\")[#{i+1}] = new Option('#{name}');"
			@dlg.execute_script(js)
		end

	end



	def facemanager
		# GET OPTIONS
	end



	def create_window(face)

		(@model == Sketchup.active_model) ? (nil) : (@model = Sketchup.active_model)

		# CHECK FOR MULTIPLE LOOPS

		if face.loops.length > 1
			return if UI.messagebox("Face has inner faces which will be ignored", MB_OKCANCEL)==2
		end

		# COLLECT THE POINTS OF THE FACE, OMITTING COLLINEAR POINTS

		@entities = @model.active_entities

		@points = []
		@verts = []
		curves = []

		@axes = face.normal.axes

		face.outer_loop.vertices.each{|v| @verts << v}
		@verts.each_index do |i|
			ver1 = @verts[i-2].position
			ver2 = @verts[i-1].position
			ver3 = @verts[i].position
			@points << ver2 unless (ver1-ver2).parallel?(ver2-ver3)
		end

		# FIND THE OPPOSITE FACE OF THE WALL

		xx = 0
		yy = 0
		zz = 0
		v1 = face.normal.reverse

		@points.each do |pt|
			xx += pt.x
			yy += pt.y
			zz += pt.z
		end

		xx /= @points.length
		yy /= @points.length
		zz /= @points.length

		vpt = Geom::Point3d.new([xx,yy,zz])

		rpt,rv1 = @model.raytest([vpt,v1])
#		puts "rpt: #{rpt}\trv1: #{rv1}"

		# CHECK FACE FOR A BOUNDING FACE ON AT LEAST TWO EDGES

		bounding_faces = []
		face.edges.each do |edge|
			tempfaces = edge.faces-[face]
			tempfaces.each do |tempface|
				bounding_faces << tempface if (tempface.normal.parallel?(face.normal) && tempface!=face)
			end
		end
		bounded = false
		bounded = true if bounding_faces.length >= 2
			

		if bounded
			if @frontonly == "No"
				if rpt && rv1
					dist = vpt.distance(rpt)
					if dist < @maxwallthickness
						face.pushpull(-dist)
					else
						face.pushpull(-(@framethickness+@frameinset))
						rpt,rv1 = @model.raytest([vpt,v1])
						if rpt && rv1
							rv1.each do |rventity|
								if (vpt.distance(rpt) <= (@framethickness+@frameinset) && rventity.typename=="Face")
									rventity.erase!
									break
								end
							end
						end
					end
				else
#					puts "no intersected geometry\nrpt: #{rpt}\trv1: #{rv1}"
					face.pushpull(-(@framethickness+@frameinset))
					rpt,rv1 = @model.raytest([vpt,v1])
					if rpt && rv1
						rv1.each do |rventity|
							if (vpt.distance(rpt) <= @frameinset && rventity.typename=="Face")
								rventity.erase!
								break
							end
						end
					end
				end
			else
#				puts "===== FRONT ONLY ====="
				face.pushpull(-@frameinset)
				rpt,rv1 = @model.raytest([vpt,v1])
#				puts "rpt: #{rpt}\trv1: #{rv1}"
				if rpt && rv1
					rv1.each do |rventity|
						if (vpt.distance(rpt) <= @frameinset && rventity.typename=="Face")
							rventity.erase!
							break
						end
					end
				end
#				puts "rpt: #{rpt}\trv1: #{rv1}"
#				puts "active_model = #{Sketchup.active_model}"
#				puts "active_entities = #{Sketchup.active_model.active_entities}"
#				puts "stored variable @entities = #{@entities}"
			end
		else
			face.erase!
		end

#		puts "========================="
#		puts "active_model = #{Sketchup.active_model}"
#		puts "active_entities = #{Sketchup.active_model.active_entities}"
#		puts "stored variable @entities = #{@entities}"
		@frame = @entities.add_group
#		puts "@frame = #{@frame}"
#		puts "TRYING TO TRAP ERROR:  @frame = #{@frame}"

		# CREATE FRAME FACE

		@frameinset = @frameinset.to_l.to_f
		#puts @frameinset < 0

		if @frameinset > 0
			#puts ">0"
			v1.length = @frameinset
			#puts v1.length
			@points.collect{|p| p.transform!(v1)} if bounded
		elsif @frameinset < 0
			#puts "<0"
			v1.reverse! if @frameinset < 0
			v1.length = (@frameinset.abs)
			#puts v1.length
			@points.collect{|p| p.transform!(v1)} if bounded
			v1.reverse! if @frameinset < 0
		else
			#puts "=0"
			v2 = v1.reverse
			v2.length = 4
		end
		frameface = @frame.entities.add_face(@points)

		# CREATE FRAME THICKNESS

		frameface.pushpull(-@framethickness) if (@frontonly == "No")# && bounded)

		# CREATE SUBFRAME FOR PANE TESTING

		towcs = (fromwcs = Geom::Transformation.axes(vpt,@axes[0],@axes[1],@axes[2])).inverse

		@subframe = @frame.entities.add_group
		if @framewidth > 0
			pts2 = @points.offset(-@framewidth)

			# CHECK THAT pts2 POINTS ARE WITHIN @points

			unless check_points(pts2,@points,towcs)
				pts2 = @points.offset(@framewidth)
			end

		else
			pts2 = @points
		end
		subframeface = @subframe.entities.add_face(pts2)


		flatpts = pts2.collect{|p| p.transform(towcs)}
		minx = nil
		maxx = nil
		miny = nil
		maxy = nil

		flatpts.each do |pt|
			minx ? (minx = pt.x if pt.x < minx) : (minx = pt.x)
			miny ? (miny = pt.y if pt.y < miny) : (miny = pt.y)
			maxx ? (maxx = pt.x if pt.x > maxx) : (maxx = pt.x)
			maxy ? (maxy = pt.y if pt.y > maxy) : (maxy = pt.y)
		end

		x = minx
		y = miny

		dx = maxx - minx
		dy = maxy - miny

		@intersectgroup = @frame.entities.add_group
		@igents = @intersectgroup.entities
		@igpoints = []

	#	bounds = [Geom::Point3d.new(minx,miny,0),Geom::Point3d.new(maxx,miny,0),Geom::Point3d.new(maxx,maxy,0),Geom::Point3d.new(minx,maxy,0)]
	#	@igents.add_face(bounds)

		vp = nil
		hp = nil

		if @vpanes.class == String	&& @vpanes.include?(",")							# PROPORTIONAL SPACING ROWS
			vp = @vpanes.split(",").collect{|i| i.to_f.abs}
		else
			vp = @vpanes.to_i.abs
		end
#		puts vp

		if @hpanes.class == String	&& @hpanes.include?(",")						# PROPORTIONAL SPACING COLUMNS
			hp = (@hpanes.split(",")).collect{|i| i.to_f.abs}
		else
			hp = @hpanes.to_i.abs
		end
#		puts hp

		@ybottom = miny

	#	CALCULATE THE ROWS AND COLUMNS

		if vp.class == Array
			#vp = vp.collect{|row| row=row.to_i.abs}
			rows = vp.length
			vunit = ((dy - ((rows -1) * @mullheight)) / vp.sum.to_f)
			vp.each do |row|
				@ytop = @ybottom + (vunit * row)
				create_row(hp,x,dx,fromwcs)
				@ybottom = @ytop + @mullheight
			end
		else
			rows = vp.to_i
			vunit = ((dy - ((rows - 1) * @mullheight)) / rows)
			0.upto(rows-1) do |row|
				@ytop = @ybottom + vunit
			#	puts "@ybottom = #{@ybottom}\n@ytop = #{@ytop}"
				create_row(hp,x,dx,fromwcs)
				@ybottom = @ytop + @mullheight
			end
		end

	#	CREATE BOXES FOR INTERSECTING

		@igfaces = []
		@igpoints.each do |facepts|
			if @frameinset == 0
				facepts = facepts.collect{|pt| pt.transform(v2)}
			end
			@igfaces << @igents.add_face(facepts.uniq)
		end

		@igfaces.each{|face| face.pushpull(-(@maxwallthickness * 2)); face.pushpull(@maxwallthickness)}
		

		junktransformation = Geom::Transformation.new

	#	entities.intersect_with(recurse, transformation1, target entities, transformation2, hidden, entities2)
		@igents.intersect_with(nil, junktransformation, @subframe.entities, junktransformation, nil, @subframe)
		@intersectgroup.erase!

	#	ISOLATE PANE FACES

		@delete_edges = []

		@subframe.entities.each do |ent|
			if ent.typename == "Edge"
				ent.find_faces
				check_edge(ent,towcs)
			end
		end

		@delete_edges.each do |edge|
			edge.erase! if edge.valid?
		end

		@newpanes = []

		if @glassinset != 0
			v1.length = @glassinset
			faces = []
			@subframe.entities.each{|e| faces << e if e.typename=="Face"}
			faces.each{|pane| @newpanes << pane.vertices.collect{|v| v.position.transform(v1)} }
		end
			
		@subframe.explode

		frameface.loops.each do |loop|
			if loop == frameface.outer_loop
				loop.edges.each do |edge|
					edge.faces.each do |face|
						next if face == frameface
						if @frontonly == "No"
							face.pushpull(-@framethickness) if face.normal.parallel?(frameface.normal)
						else
							if @glassinset > 0
								face.pushpull(-@glassinset) if face.normal.parallel?(frameface.normal)
							end
						end
					end
				end
			else
				loop.edges[0].faces.each do |face|
					next if face == frameface
					if @frontonly == "No"
						face.pushpull(-@framethickness)
					else
						face.pushpull(-@glassinset)
					end
				end
			end
		end

		if @frontonly == "Yes"
			@frame.entities.each do |ent|
				if ent.typename=="Face"
					ent.erase! if (ent.normal.parallel?(frameface.normal) && ent != frameface)
				end
			end
		end

		@panes = @frame.entities.add_group
		v1.length = @glassthickness if @glassthickness != 0

		@newpanes.each do |pane|
			face = @panes.entities.add_face(pane)
			(face = @panes.entities.add_face(pane.collect{|p| p.transform(v1)})).reverse! unless (@glassthickness == 0 || @frontonly == "Yes")
		end

		@panes.material = @model.materials[@glassmaterial] if @glassmaterial
		@frame.material = @model.materials[@framematerial] if @framematerial
		@panes.explode

		@frame.explode if @makegroup=="No"
		
	end


	def create_row(hp,x,dx,fromwcs)
		if hp.class == Array
			cols = hp.length
			unit = ((dx - ((cols - 1) * @mullwidth)) / hp.sum)
			hp.each_with_index do |col,i|
				width = col.to_i.abs * unit
				panepoints = [Geom::Point3d.new(x,@ybottom,0),Geom::Point3d.new(x+width,@ybottom,0),Geom::Point3d.new(x+width,@ytop,0),Geom::Point3d.new(x,@ytop,0)]
				@igpoints << panepoints.collect{|pt| pt.transform(fromwcs)}
				x += (width + @mullwidth)
			end
		else
			hp = hp.to_i.abs
			width = (dx - ((hp - 1) * @mullwidth)) / hp
			0.upto(hp - 1) do |i|
				panepoints = [Geom::Point3d.new(x,@ybottom,0),Geom::Point3d.new(x+width,@ybottom,0),Geom::Point3d.new(x+width,@ytop,0),Geom::Point3d.new(x,@ytop,0)]
				@igpoints << panepoints.collect{|pt| pt.transform(fromwcs)}
				x += width + @mullwidth
			end
		end
	end



	def check_edge(edge,towcs)
		pt1 = edge.start.position
		pt2 = edge.end.position
		midpoint = (Geom::Point3d.new([(pt1.x+pt2.x)/2,(pt1.y+pt2.y)/2,(pt1.z+pt2.z)/2])).transform!(towcs)

		@igpoints.each do |igpoint|
			polygon = igpoint.collect{|pt| pt.transform(towcs)}
			return if Geom.point_in_polygon_2D(midpoint,polygon,true)
		end

		@delete_edges << edge
	end



	def check_points(points,poly,towcs)
		points.each do |point|
			pt1 = point.transform(towcs)
			polygon = poly.collect{|pt| pt.transform(towcs)}
			return nil unless Geom.point_in_polygon_2D(pt1,polygon,false)
		end

		return true
	end



	def edit_window
		# ADD CODE
	end



	def erase_window
		# ADD CODE
	end



	def inherit_settings
		# ADD CODE
	end



	def apply_style
		# ADD CODE
	end



	def stylecheck(val=nil)
		namecheck = false
		return nil if val == nil
	#	puts "checking #{val}"
		if styles.length>0
		#	puts "checking #{val} against styles"
			styles.each do |style|
			#	puts style.index(val)
				if	style.index(val)==0	# COMPARE TO EXISTING NAME
					if style==val			# EXACT MATCH TO EXISTING NAME
						@dlg.execute_script("document.getElementById(\"styleprompt\").innerHTML = '#{val}';")
						@dlg.execute_script("document.getElementById(\"styleprompt\").style.color = '#a00';")
						break
					else						# SIMILAR TO EXISTING NAME
						@dlg.execute_script("document.getElementById(\"styleprompt\").innerHTML = '#{val}';")
						@dlg.execute_script("document.getElementById(\"styleprompt\").style.color = '#bb0';")
						namecheck = true
					end
				else							# UNIQUE NAME
					@dlg.execute_script("document.getElementById(\"styleprompt\").innerHTML = '#{val}';")
					@dlg.execute_script("document.getElementById(\"styleprompt\").style.color = '#0a0';") unless namecheck
				end
			end
		else									# NO OTHER STYLES IN MODEL
			@dlg.execute_script("document.getElementById('styleprompt').style.color = '#0a0'")
			@dlg.execute_script("document.getElementById(\"styleprompt\").innerHTML = '#{val}';")
		end
	end



#	public



#	def Windowizer4.mainmenutest
#		@sel.each{|ent| return true if ent.typename=="Face"}
#	end



#	def Windowizer4.w4menutest
#		@sel.each{|ent| return true if ent.typename=="Face" && ent.get_attributes("Windowizer")}
#	end



	def Windowizer4.show_dialog
		optionsmanager()
	end

		
#####
#
#	axes = face.normal.axes
#	
#####


end #class Windowizer4


unless file_loaded?(__FILE__)
	file_loaded(__FILE__)
	UI.menu("Plugins").add_item("Windowizer4") { (Windowizer4.new) }

	UI.add_context_menu_handler do |menu|
		menu.add_separator
		submenu=menu.add_item("Windowizer4"){ Windowizer4.new }
	end
	
end