behavior HyperSelect
	init

		set :og_select to me
		hide me

		set :is_building_select to false

		set :list_item_class to @data-hs-list-item-class or 'list-group-item'
		set :list_item_label_class to @data-hs-list-item-label-class or 'list-group-item-label'
		set :list_item_remove_btn_class to @data-hs-list-item-remove-class or ''
		set :list_class to @data-hs-list-class or 'list-group'
		set :add_action to @data-hs-add-action or 'disable'
		set :placeholder to @placeholder

		increment $list_select_idx
		set :container_cls to @data-container-class or 'list-select-wrapper'
		set :id_prefix to :og_select's @name +"_hs_"+$list_select_idx
		set :id_prefix to :id_prefix.replace('[]', '')

		set :og_select's @rel to :id_prefix

		-- make a <select/> called :remove_select

		make a <select/> called :drop_select
		set :drop_select's @class to :og_select's @class
		set :drop_select's @rel to :id_prefix
		set :drop_select's @id to :id_prefix+"_clone"
		set :drop_select's @data-hyper-select-dropdown to ''

		append :drop_select to :og_select's parentElement

		make a <ul/> called :list
		set :list's @class to :list_class
		append :list to :og_select's parentElement

		call build_select()
	end

	on change[target matches <[data-hyper-select-dropdown]/>] from body
		if event's target's @rel does not match :id_prefix exit end
		set selected to the first <option:checked/> in the event's target
		set id to selected's @rel
		if not id exit end
		set og_opt to the first <option#${id}/> in :og_select
		call add_list_item(og_opt)
	end

	on click[target matches <[data-action=remove]/>] from body
		if :og_select's parentElement does not contain the event's target exit end
		set item to the closest <li[rel]/> to the event's target
		set id to item's @rel
		set og_opt to the first <option#${id}/> in :og_select
		log og_opt
		set og_opt's checked to false
		remove @selected from og_opt
		remove item

		if :add_action is 'disable' then
			enable_select_item(id, :drop_select)
		else if :add_action is 'hide' then
			show_select_item(id, :drop_select)
		end
	end


	def build_select()
		hide :og_select
		set :is_building_select to true
		make a <option/> called :blank_opt
		set its textContent to :placeholder
		append it to :drop_select

		set og_opts to <option/> in :og_select

		for og_opt in og_opts
			if og_opt's value is '' continue end
			set opt_id to :id_prefix + "_item_" + og_opt's index
			set og_opt's id to opt_id
			set og_opt's @rel to opt_id
			set is_selected to og_opt's selected
			set is_disabled to og_opt's disabled
			if is_selected and not is_disabled
				call add_select_option(og_opt)
				call add_list_item(og_opt)
			else if not is_selected and is_disabled
				call add_select_option(og_opt)
			else
				call add_select_option(og_opt)
			end
		end
		set :is_building_select to false
	end

	def add_list_item(og_opt)

		-- text of item from select's option
		make a <span/> called item_label
		set item_label's @class to :list_item_label_class
		put og_opt's textContent into item_label

		-- remove btn
		make a <button/> called remove_btn
		add @data-action='remove' to remove_btn
		add @type='button' to remove_btn
		add @role='button' to remove_btn
		add @title='Remove' to remove_btn
		-- set remove_btn's innerHTML to '&#x2715;'
		set remove_btn's @class to :list_item_remove_btn_class

		-- list item itself
		make a <li/> called item
		set item's @class to :list_item_class
		set item's @rel to og_opt's @id

		append item_label to item
		append remove_btn to item
		append item to :list

		if not :is_building_select then
			set og_opt's @selected to true
		end

		if :add_action is 'disable' then
			disable_select_item(og_opt's @rel, :drop_select)
		else if :add_action is 'hide' then
			hide_select_item(og_opt's @rel, :drop_select)
		end
	end

	def add_select_option(og_opt)
		make a <option/> called new_opt
		set new_opt's value to og_opt's value
		put og_opt's textContent into new_opt's textContent
		set new_opt's @rel to og_opt's @id
		append new_opt to :drop_select
	end

	def disable_select_item(opt_id, which_select)
		set opt to the first <[rel=${opt_id}]/> in which_select
		set opt's disabled to true
	end

	def enable_select_item(opt_id, which_select)
		set opt to the first <[rel=${opt_id}]/> in which_select
		remove @disabled from opt
	end

	def hide_select_item(opt_id, which_select)
		set opt to the first <[rel=${opt_id}]/> in which_select
		add @hidden='' to opt
	end

	def show_select_item(opt_id, which_select)
		set opt to the first <[rel=${opt_id}]/> in which_select
		remove @hidden from opt
	end

end