wmp.editor.menu = new Class({
	initialize: function(options)
	{
		this.children = [];
		this.options = options || {title:""};
		if (!this.options.position)
			this.options.position = "main";
		
		this.nodes = {
			container: 	new Element("div", {'class': 'wmpe-menu'}),
			title: 		new Element("div", {'class': 'wmpe-title'}),
			content:	new Element("div", {'class': 'wmpe-menu-content'})
		}
		if (this.options.className)
			this.nodes.container.addClass(this.options.className);
		
		this.nodes.container.appendChild(this.nodes.title);
		this.nodes.container.appendChild(this.nodes.content);
		
		this.nodes.title.innerHTML = this.options.title;
		this.setPosition(this.options.position);
		
		if (this.options.hidden)
			this.hide();
	},
	
	setPosition:  function(position, bottom)
	{
		var cont = this.options.container;
		if (this.options.top)
			this.nodes.container.inject(cont, 'top');
		else
			cont.appendChild(this.nodes.container);
	},
	
	remove: function()
	{
		while (this.children.length>0)
			this.children.pop().remove();
		this.nodes.container.empty();
		this.nodes.container.dispose();
		delete(this.nodes);
	},
	
	addItem: function(options)
	{
		options.menu = this;
		var newItem = new wmp.editor.menu.item(options);
		this.children.push(newItem);
		this.nodes.content.appendChild(newItem.nodes.container);
		return newItem;
	},
	
	addMessage: function(options)
	{
		options.menu = this;
		var newItem = new wmp.editor.menu.message(options);
		this.children.push(newItem);
		this.nodes.content.appendChild(newItem.nodes.container);
		return newItem;
	},
	
	addInput: function(options)
	{
		options.menu = this;
		var newItem = new wmp.editor.menu.input(options);
		this.children.push(newItem);
		this.nodes.content.appendChild(newItem.nodes.container);
		return newItem;
	},
	
	addContent: function(options)
	{
		options.menu = this;
		var newItem = new wmp.editor.menu.content(options);
		this.children.push(newItem);
		this.nodes.content.appendChild(newItem.nodes.container);
		return newItem;
	},
	addSeparator: function(options)
	{
		options.menu = this;
		var newItem = new wmp.editor.menu.separator(options);
		this.children.push(newItem);
		this.nodes.content.appendChild(newItem.nodes.container);
		return newItem;
	},
	show: function() {
		this.nodes.container.style.display = "block";
	},
	
	hide: function() {
		this.nodes.container.style.display = "none";
	},
	
	addShortcut: function(shortcut, item) {
		this.options.shortcuts["control+"+shortcut] = item;
		if (!this.options.shortcuts._init) {
			this.options.shortcuts._init = true;
			var doc = this.options.editor.doc;
			doc.addEvent("keydown", this.checkShortcut.bindWithEvent(this, this.options.shortcuts));
			doc.addEvent("keypress", this.checkShortcutPrevent.bindWithEvent(this, this.options.shortcuts));
		}		
	},
	removeShortcut: function(shortcut) {
		delete(this.options.shortcuts["control+"+shortcut]);
		//todo: remove event
	},
	checkShortcut: function(event, shortcuts) {
		var o = shortcuts["control+" + event.key];
		if (event.control && o) {
			if (o.options.enabled)
				o.options.func(event);
			event.preventDefault();
		}
	},
	checkShortcutPrevent: function(event, shortcuts) {
		if (event.control && shortcuts["control+" + event.key])
			event.preventDefault();
	}
});

wmp.editor.menu.message = new Class({
	initialize: function (options) {
		this.options = options;
		this.nodes = {
			container: new Element("div", {"class":"wmpe-status"})
		}
		this.setMessage(this.options.text);
	},
	
	setMessage: function (text) {
		this.nodes.container.innerHTML = text;
	}
});

wmp.editor.menu.content = new Class({
	initialize: function (options) {
		this.options = options;
		this.nodes = {
			container: new Element("div", {"class":"wmpe-help"})
		}
		this.nodes.container.innerHTML = this.options.text;
	},
	
	remove: function()
	{
		this.nodes.container.dispose();
	}
});

wmp.editor.menu.separator = new Class({
	initialize: function (options) {
		this.options = options;
		this.nodes = {
			container: new Element("div", {"class":"wmpe-separator"})
		}
	},
	
	remove: function()
	{
		this.nodes.container.dispose();
	}
});

wmp.editor.menu.input = new Class({
	initialize: function (options) {
		this.options = options;
		this.nodes = {
			container: new Element("div", {'class': 'wmpe-input'}),
			title: new Element('div', {'class': 'wmpe-input-title'}),
			input: $(this.options.input.node)
		}
		
		if (this.options.title) {
			this.nodes.title.set('text', this.options.title)
			this.nodes.container.appendChild(this.nodes.title);
		}
		if (!this.options.closed)
			this.options.state = "closed";
		this.toggle();
		this.nodes.title.addEvent("click", this.toggle.bind(this));
		this.nodes.container.appendChild(this.nodes.input);
	},
	
	toggle: function()
	{
		if (this.options.state == "closed") {
			this.nodes.title.set('text', this.options.title + " -");
			this.nodes.input.style.display = "";
			this.options.state = "open";
			try {
				if (this.options.input.node.focus)
					this.options.input.node.focus();
			}
			catch(e){}
		}
		else {
			this.nodes.title.set('text', this.options.title + " +");
			this.nodes.input.style.display = "none";
			this.options.state = "closed";
		}
	},
	
	remove: function()
	{
		if (this.nodes.input._input)
			this.nodes.input._input.stop();
		this.nodes.container.dispose();

	}
});

wmp.editor.menu.item = new Class({
	initialize: function (options) {
		this.options = options || {};
		this.children = [];
		this.options.enabled = true;
		this.options.active = true
		if (!this.options.func)
			this.options.func = this.toggleContent.bind(this);
		var title = this.options.title;
		
		this.nodes = {
			container:	new Element('div', {'class': 'wmpe-item'}),
			content:	new Element('div', {'class': 'wmpe-item-content','styles': {	'display': 'none'}}),
			title:		new Element('div', {'class': 'wmpe-item-title', 'unselectable': 'on'}),
			text:		new Element('div', {'class': 'wmpe-item-text'}),
			tip:		new Element('div', {'class': 'wmpe-tip'})
		}
		if (this.options.icon)
			this.nodes.title.style.backgroundImage = "url(" + wmp.config.path.image + this.options.icon + ")";
		
		this.nodes.container.appendChild(this.nodes.content);
		this.nodes.title.appendChild(this.nodes.tip);
		this.nodes.title.appendChild(this.nodes.text);
		this.nodes.container.appendChild(this.nodes.title);
		
		
		this.nodes.content.addEvent('mouseleave', this.collapseContent.bind(this));
		this.nodes.text.innerHTML = title;
		
		//tips
		if (this.options.shortcut)
			title = title + "  <span>(Strg+"+this.options.shortcut.toUpperCase()+")</span>";
		this.nodes.tip.innerHTML = title;
		
		
		//shortcut
		this.options.menu.addShortcut(this.options.shortcut, this);
		this.nodes.title.addEvent("mousedown", this.click.bind(this));
	},
	
	addItem: function(options) {
		options.menu = this.options.menu;
		options.parentItem = this;
		var newItem = new wmp.editor.menu.item(options);
		this.children.push(newItem);
		this.nodes.content.appendChild(newItem.nodes.container);
		return newItem;
	},
	
	click: function(event) {
		event.stop();
		if (this.options.parentItem)
			this.options.parentItem.collapseContent();
		if (this.options.enabled)
			this.options.func(event);
		this.options.menu.options.editor.focus();
	},
	
	remove: function(){
		while (this.children.length>0)
			this.children.pop().remove();
		this.nodes.container.dispose();
		this.nodes.title.removeEvents();
		if (this.options.shortcut)
			this.options.menu.removeShortcut(this.options.shortcut);
	},
	
	toggleContent: function() {
		if (this.nodes.content.style.display == "none")
			this.nodes.content.style.display = "";
		else
			this.nodes.content.style.display = "none";
	},
	
	collapseContent: function() {
		this.nodes.content.style.display = "none";
	},
	
	enable: function() {
		if (!this.options.enabled) {
			this.nodes.container.set('opacity',1);
			this.options.enabled = true;
		}
	},
	disable: function() {
		if (this.options.enabled) {
			this.nodes.container.set('opacity', .4);
			this.options.enabled = false;
		}
	},
	
	activate: function() {
		if (!this.options.active) {
			this.nodes.container.addClass("active");
			this.options.active = true;
		}
	},
	deactivate: function() {
		if (this.options.active) {
			this.nodes.container.removeClass("active");
			this.options.active = false;
		}
	}
});