$.fn.chpEditToolbar = function(options) {
	this.each(function(i, el) {
		new $.chp.EditToolbar(el, options);
	});
	return this;
};

if (!$.chp) $.chp = {};

$.chp.EditToolbar = function() { return this.initialize.apply(this, arguments); };
$.chp.EditToolbar.active = null;

$.chp.EditToolbar.prototype = {
	initialize: function(ownerEl, options) {
		var name, label;
		var linkEl;
		
		this.ownerEl = ownerEl;
		this.enabled = true;
		this.visible = false;
		
		this.options = $.extend({
			buttons: {},
			className: '',
			click: function(){},
			offset: [0, 0]
		}, options);
		
		this.el = document.body.appendChild(document.createElement('div'));
		
		$(this.el).
			hide().
			addClass(this.options.className).
			css('position', 'absolute');
		
		$(this.ownerEl).
			hover(
				$.bind(this, this.hoverOver),
				$.bind(this, this.hoverOut)
			);
		
		for (name in this.options.buttons) {
			label = this.options.buttons[name];
			
			linkEl = this.el.appendChild(document.createElement('a'));
			linkEl.href = '#';
			linkEl.appendChild(document.createTextNode(label));
			
			(function(name) {
				$(linkEl).click($.bind(this, function() {
					this.click(name);
				}));
			}).call(this, name);
			
			$(linkEl).
				hover(
					$.bind(this, this.hoverOver),
					$.bind(this, this.hoverOut)
				);
		}
		
		$(this.el).find('a:first').addClass('first');
		$(this.el).find('a:last').addClass('last');
	},
	
	click: function(name) {
		this.options.click(name);
	},
	
	setEnabled: function(enabled) {
		this.enabled = !!enabled;
	},
	
	transitionIn: function() {
		var offset = $(this.ownerEl).offset();
		var width = $(this.ownerEl).outerWidth();
		var height = $(this.ownerEl).outerHeight();
		$(this.el).css({
			left: offset.left + width - $(this.el).outerWidth() + this.options.offset[0],
			top: offset.top + height + this.options.offset[1]
		}).hide().slideDown(100);	
	},
	
	beginShow: function(duration) {
		clearTimeout(this.hideTimeout);
		this.hideTimeout = null;
		if (!this.visible && !this.showTimeout) {
			this.showTimeout = setTimeout($.bind(this, this.show), duration || 100);
		}
	},
	
	beginHide: function(duration) {
		clearTimeout(this.showTimeout);
		this.showTimeout = null;
		if (this.visible && !this.hideTimeout) {
			this.hideTimeout = setTimeout($.bind(this, this.hide), duration || 300);
		}
	},
	
	show: function() {
		if ($.chp.EditToolbar.active) {
			$.chp.EditToolbar.active.hide();
		}
		$.chp.EditToolbar.active = this;
		this.visible = true;
		clearTimeout(this.showTimeout);
		this.showTimeout = null;
		this.transitionIn();
	},
	
	hide: function() {
		if ($.chp.EditToolbar.active == this) {
			$.chp.EditToolbar.active = null;
		}
		this.visible = false;
		clearTimeout(this.hideTimeout);
		this.hideTimeout = null;
		$(this.el).hide();
	},
	
	hoverOver: function(e) {
		if (this.enabled) {
			$(this.ownerEl).addClass('hover');
			this.beginShow();
		}
	},
	
	hoverOut: function() {
		if (this.enabled) {
			$(this.ownerEl).removeClass('hover');
			this.beginHide();
		}
	}
};
