/**Bracket Interface 
 * @author danyastuff
 * @description based on converterInterface css-styles
 */
 
var BracketInterface = function (sets) {
	this.sets    = sets // {body,inside,outside,onChange}
	this.bracket = this.sets.body.find('DIV.brackets')
	
	this.assignHandlers()
	var e = {data: { parent:this } }
	this.sets.onCreate(e)
	this.refresh()
}

BracketInterface.prototype = {
	refresh : function () {
		var lis = this.sets.inside.find('LI') ,
			self = this
		if (lis.length > 1) { 
			this.bracket.addClass('show').css('padding-right', '2em')
			lis.find('DIV.remove').removeClass('hidden')
		} else { 
			this.bracket.removeClass('show').css('padding-right', '0')
			lis.find('DIV.remove').addClass('hidden')
		}
		lis.filter(':last').find('DIV.add')
			.bind('click', function () { self.addLI() })
			.addClass('last')
			.get(0).setAttribute('title', s('BracketInterface add field'))
		
		var e = {data: { parent:this } }
		this.sets.onChange(e)
	},
	assignHandlers : function () {
		var pattern = this.sets.inside.find('LI'),
			self = this
		pattern.find('INPUT').bind(
			'keyup', { parent:this }, this.sets.onChange
		)
		pattern.find('SELECT').bind(
			'change', { parent:this }, this.sets.onChange
		)
		pattern.find('DIV.remove').bind(
			'click', { parent: pattern }, function(e) { 
				$(e.target.parentNode.parentNode).remove()
				self.refresh()
			}
		)
		this.pattern = pattern.clone(true)
		
	},
	addLI : function () {
		var ins = this.sets.inside,
			li  = this.pattern.clone(true),
			self = this
			
		ins.find('DIV.last')
			.unbind('click')
			.removeClass('last')
			.attr('title', '')
		ins.append(li)
		
		var e = {data: { parent:this } }
		this.sets.onAdd(e)
		
		this.refresh()
	}
}
