
	var dropdownMenuItemOptions =
	{
		select: undefined,
		url: undefined,
		name: undefined,
		caption: undefined,
		disabled: false,
		submenu: undefined,
		separator: false,
		loader: undefined,
		className: 'dropdownMenuItem',
		element: undefined
	}

	function dropdownMenuItem(menu, options)
	{
		if(options == undefined) options = dropdownMenuItemOptions;
		for(var option in dropdownMenuItemOptions)
		if(options[option] == undefined) options[option] = dropdownMenuItemOptions[option];

		this.menu = menu;

		this.disabledFunction = function(event) { event.stopPropagation(); return false; }

		if(options.element)
		{
			var element = options.element;
			this.name = $(element).attr('key');
		}
		else
		{
			var element = document.createElement('li');
			$(menu.element).append(element);
		}
		
		$(element).addClass(options.className);
		this.element = element;

		dropdownMenuItem.parentClass.constructor.call(this, element);
		
		if(options.separator) $(element).addClass('separator');

		if(!$(element).find("a").length) $(element).append('<a />');
		
		if(options.url) $(element).find("a").attr('href', options.url);

		if(options.name) this.name = options.name;
		if(options.caption) $(element).find("a").text(options.caption);

		if(options.disabled) this.disable();

		if(options.submenu) this.addSubmenu(options.submenu);
		
		var itemObject = this;
		
		if(options.loader) this.loader = options.loader;
		if(menu.isOpen()) this.load();
		
		$(this.element).mouseenter(function() {itemObject.select()});
		$(this.element).mouseleave(function() {itemObject.deselect()});		
	}

	extend(dropdownMenuItem, control);

	dropdownMenuItem.prototype.element;
	dropdownMenuItem.prototype.menu;
	dropdownMenuItem.prototype.name;
	dropdownMenuItem.prototype.submenu;

	dropdownMenuItem.prototype.getType = function()
	{
		return 'dropdownMenuItem';
	}

	dropdownMenuItem.prototype.isSelected = function()
	{
		return $(this.element).is('.selected');
	}

	dropdownMenuItem.prototype.show = function()
	{
//		dropdownMenuItem.parentClass.show.apply(this);
		this.load();
	}

	dropdownMenuItem.prototype.hide = function() { }

	dropdownMenuItem.prototype.select = function()
	{
		if(!this.isSelected())
		{
			var itemObject = this;
		
			var items = this.menu.listItems();
			for(var i = 0; i< items.length; i++)
				if(items[i] != this) items[i].deselect();
		
			$(this.element).addClass('selected');
			if(this.submenu) setTimeout(function() { if(itemObject.isSelected()) itemObject.openSubmenu();}, 150);
		}		
	}
	
	dropdownMenuItem.prototype.deselect = function()
	{
		if(this.isSelected())
		{
			var itemObject = this;
			if(this.submenu) 
			{
				$(this.submenu.element).mouseenter(function() { $(itemObject.element).addClass('selected'); } )
				setTimeout(function() { if(!itemObject.isSelected()) itemObject.closeSubmenu();}, 150);		
			}
			$(this.element).removeClass('selected');
		}
	}

	dropdownMenuItem.prototype.load = function()
	{
		if(this.loader && !$(this.element).is('.loading'))
		{
			$(this.element).addClass('loading');
			var itemObject = this;
			this.loader.call(itemObject);
		}
	}

	dropdownMenuItem.prototype.remove = function()
	{
		$(this.element).remove();
	}

	dropdownMenuItem.prototype.openSubmenu = function()
	{
		if(this.submenu)
		{
			var itemOffset = $(this.element).offset();
			if(this.menu.isVertical())
			{
				var offset = {
					left: itemOffset.left + $(this.element).width(),
					top: itemOffset.top
				}
			}
			else
			{
				var offset = {
					left: itemOffset.left,
					top: itemOffset.top + $(this.element).height()
				}
			}
			this.submenu.open(offset.left, offset.top);
		}
	}

	dropdownMenuItem.prototype.closeSubmenu = function()
	{
		if(this.submenu) this.submenu.close();
	}

	dropdownMenuItem.prototype.addSubmenu = function(submenu)
	{
		if(submenu == undefined) submenu = new dropdownMenu();
		this.submenu = submenu;
		submenu.parentItem = this;
		$(this.element).addClass('hasSubmenu');
		return submenu;
	}

	dropdownMenuItem.prototype.removeSubmenu = function()
	{
		if(this.submenu)
		{
			this.closeSubmenu();
			this.submenu = undefined;
		}
		$(this.element).removeClass('hasSubmenu');
	}

	dropdownMenuItem.prototype.isDisabled = function()
	{
		return $(this.element).is('.disabled');
	}
	
	dropdownMenuItem.prototype.disable = function()
	{
		$(this.element)
			.addClass('disabled')
			.find("a").click(this.disabledFunction);
	}

	dropdownMenuItem.prototype.enable = function()
	{
		$(this.element)
			.removeClass('disabled')
			.find("a").unbind('click', this.disabledFunction);
	}

	dropdownMenuItem.prototype.bind = function(type, fn)
	{
		$(this.element).find("a").bind(type, {dropdownMenuItem: this}, fn);
	}

	dropdownMenuItem.prototype.unbind = function(type, fn)
	{
		$(this.element).find("a").unbind(type, fn);
	}
	
	var dropdownMenuOptions =
	{
		element: undefined,
		className: 'dropdownMenu',
		itemOptions: dropdownMenuItemOptions,
		align: undefined,
		parentItem: undefined
	}
	
	function dropdownMenu(options)
	{
		if(options == undefined) options = dropdownMenuOptions;
		for(var option in dropdownMenuOptions)
		if(options[option] == undefined) options[option] = dropdownMenuOptions[option];

		if(options.element)
		{
			var element = options.element;
			$(element).children('li').dropdownMenuItem(this, options.itemOptions);
		}
		else
		{
			var element = document.createElement('ul');
			$("body").append(element);
			$(element).hide();
		}

		this.element = element;

		this.parentItem = options.parentItem;

		$(element).addClass(options.className);

		if(options.align) $(element).addClass(options.align)

		dropdownMenu.parentClass.constructor.call(this, element);

		var menuObject = this;
		this.autoCloseFunction = function()
		{
			menuObject.close();
		}
	}

	extend(dropdownMenu, control);

	dropdownMenu.prototype.element;
	dropdownMenu.prototype.parentItem;

	dropdownMenu.prototype.getType = function()
	{
		return 'dropdownMenu';
	}

	dropdownMenu.prototype.isVertical = function()
	{
		return $(this.element).is('.vertical');
	}

	dropdownMenu.prototype.isHorizontal = function()
	{
		return $(this.element).is('.horizontal');
	}

	dropdownMenu.prototype.addItem = function(options)
	{
		return new dropdownMenuItem(this, options);
	}

	dropdownMenu.prototype.addSeparator = function()
	{
		return new dropdownMenuItem(this, {separator: true});
	}

	/**
	*	@todo fix
	*/
	dropdownMenu.prototype.getDefaultItem = function()
	{
		var items = this.listItems();
		for(var i = 0; i < items.length; i++)
			if(items[i].isDefault) return items[i];
	}

	dropdownMenu.prototype.listItems = function()
	{
		var items = new Array();
		itemElements = $(this.element).find('li').get();
		for(var i = 0; i < itemElements.length; i++)
			if(itemElements[i].control) items.push(itemElements[i].control);
		return items;
	}

	dropdownMenu.prototype.eachItem = function(fn)
	{
		var items = this.listItems();
		for(var i = 0; i < items.length; i++)
			fn.call(items[i]);
	}
	
	dropdownMenu.prototype.open = function(left, top)
	{		
		if(top != undefined) $(this.element).css('top', top + 'px');
		if(left != undefined) $(this.element).css('left', left + 'px');
		if(this.isHidden())
		{
			this.show();
			$(document).click(this.autoCloseFunction)
		}
	}
	
	dropdownMenu.prototype.isOpen = function()
	{ 
		return !this.isHidden();
	}

	dropdownMenu.prototype.close = function()
	{
		if(this.isOpen())
		{
			if(this.parentItem) this.parentItem.deselect();
			this.eachItem(function() {this.deselect()});
			this.hide();
			$(document).unbind('click', this.autoCloseFunction)
		}
	}

	dropdownMenu.prototype.show = function()
	{
		dropdownMenu.parentClass.show.apply(this);
		this.eachItem(function() {this.show()});
	}

	dropdownMenu.prototype.hide = function()
	{
		this.eachItem(function() {this.hide()});
		dropdownMenu.parentClass.hide.apply(this);
	}	

	jQuery.fn.dropdownMenu = function(options)
	{
		var elements = this.get();
		for(var i = 0; i < elements.length; i++)
		{
			options.element = elements[i];
			var menu = new dropdownMenu(options)
		}

		return menu; // ?
	}

	jQuery.fn.dropdownMenuItem = function(menu, options)
	{
		var elements = this.get();
		for(var i = 0; i < elements.length; i++)
		{
			options.element = elements[i];
			new dropdownMenuItem(menu, options)
		}
	}

	// TEMP
	/*
	
	$(document).ready(function(){
		function loader()
		{
			var item = this;

			var complete = function(data)
			{
				loader.data = data;

				var itemElements = $(data).find("[component='page.navigation.menu'] item[key='"+ item.menu.parentItem.name +"'] > list > item").get();

				if(itemElements.length)
				{
					for(var i = 0; i < itemElements.length; i++)
					{
						var name = $(itemElements[i]).attr('key');
						var link = $(itemElements[i]).children('link');
						var url = link.attr('uri');

						var options = {caption: link.text(), url: url};
						item.menu.addItem(options);
					}

					item.remove();
				}
				else item.menu.parentItem.removeSubmenu();
			}

			var data = {
				componentFilter: 'page.navigation.menu',
				outputMimeType: 'application/xml',
				'page.navigation.menu.items.submenus.display' : 1
			};

			if(loader.data) complete(loader.data);
			else $.get('/', data, complete, 'xml')
		}

		var itemFunction = function() { this.addSubmenu().addItem({loader: loader}) }
		var menu = $("ul.mainMenu");
		if(menu.length)	menu.dropdownMenu({className: 'menu', itemOptions: {className: 'item'}}).eachItem(itemFunction);
	})
*/
