(function()
{
	Common.Object.create('Typical.Chat.WindowsBar', {

		screenManager : null,
		
		mainBarElement : null,
		backgroundElement : null,
		barElement : null,
		windowButtonsElement : null,
		
		windows : null,
		windowButtons : null,
		
		WindowsBar : function(screenManager)
		{
			this.screenManager = screenManager;
			
			this.screenManager.onNewWindow.add(this.handleNewWindow, this);
			this.screenManager.onWindowFocus.add(this.handleOnWindowFocus, this);
			
			this.windows = [];
			this.windowButtons = [];
			
			this.initGUI();
		},
		
		initGUI : function()
		{
			// Build container.
			this.mainBarElement = Builder.node('div', {'class' : 'chat windowsbar', 'style' : 'display: none'});
			{
				// Container, needed for effects.
				var container = Builder.node('div');
				{
					// Builder bar.
					this.barElement = Builder.node('div', {'class' : 'bar'});
					{
						// Build close.
						var closeElement = Builder.node('img', {src : Config.baseUrl + 'external/image/typical/chat/windowsbar/close.png', 'class' : 'action close'});
						{
							Event.observe(closeElement, 'mouseover', 	this.focusElement.bindAsEventListener(this, closeElement));
							Event.observe(closeElement, 'mouseout', 	this.unfocusElement.bindAsEventListener(this, closeElement));
							Event.observe(closeElement, 'click', 		this.screenManager.close.bindAsEventListener(this.screenManager, closeElement));
							
							this.unfocusElement(null, closeElement);
						}
						Element.insert(this.barElement, closeElement);
						
						// Build windows.
						this.windowButtonsElement = Builder.node('div', {'class' : 'windows'});
						{
						}
						Element.insert(this.barElement, this.windowButtonsElement);
					}
					Element.insert(container, this.barElement);
					
					// Build background.
					this.backgroundElement = Builder.node('div', {'class' : 'background'});
					{
						Element.setOpacity(this.backgroundElement, 0.5);
					}
					Element.insert(container, this.backgroundElement);
				}
				Element.insert(this.mainBarElement, container);
			}
			Element.insert(document.body, this.mainBarElement);
			
			// Build invitor.
			this.invitorElement = Builder.node('div', {className : 'chat invitor', style : 'display: none'});
			{
				setTimeout(function()
				{
					if(this.invitorElement)
						new Effect.Appear(this.invitorElement);
						
				}.bind(this), 5 * 1000);
			}
			Element.insert(document.body, this.invitorElement);
			
			// Resize and listen to resized.
			this.resize();
			
			Event.observe(window, 'resize', this.resize.bind(this));

			// Show
			new Effect.BlindDown(this.mainBarElement);
		},
		
		handleOnWindowFocus : function()
		{
			if(this.invitorElement)
			{
				var invitorElement = this.invitorElement;
				
				new Effect.Fade(invitorElement, {
					afterFinish : function()
					{
						if(invitorElement && invitorElement.parentNode)
							Element.remove(invitorElement);
					}
				});
				
				this.invitorElement = null;
			}
		},
		
		focusElement : function(event, element)
		{
			Element.setOpacity(element, 1);
		},
		
		unfocusElement : function(event, element)
		{
			Element.setOpacity(element, 0.75);
		},
		
		resize : function()
		{
			Element.setStyle(this.mainBarElement, {width : (document.viewport.getWidth() - 18) + 'px'});
		},
		
		handleNewWindow : function(window)
		{
			if(this.windows.indexOf(window) != -1)
				return;
			
			var windowButton = new Typical.Chat.WindowBarButton(this, window);
			
			this.windows.push(window);
			this.windowButtons.push(windowButton);
			
			if(this.windows.length == 1)
				Element.addClassName(windowButton.element, 'last');
			
			Element.insert(this.windowButtonsElement, { bottom : windowButton.element });
			
			window.onClose.add(this.unregisterWindow, this);
			
			windowButton.onResize.add(this.handleButtonResize, this);
			
			this.handleButtonResize(windowButton);
		},
		
		unregisterWindow : function(window)
		{
			var windowIndex = this.windows.indexOf(window);
			
			if(windowIndex == -1)
				return;
			
			Element.remove(this.windowButtons[windowIndex].element);
			
			this.windows.splice(windowIndex, 1);
			this.windowButtons.splice(windowIndex, 1);

			if(this.windows.length > 0)
				Element.addClassName(this.windowButtons[0].element, 'last');
		},
		
		handleButtonResize : function(windowButton)
		{
			var window = this.windows[this.windowButtons.indexOf(windowButton)];
			
			if(window)
				this.screenManager.setWindowOverlap(window, windowButton.getWindowOverlap());
		},
		
		close : function()
		{
			if(this.invitorElement)
			{
				new Effect.Fade(this.invitorElement);
			
				this.invitorElement = null;
			}
			
			new Effect.BlindUp(this.mainBarElement);
		}
		
	});
})();
