Labrix.UI = {};

Labrix.UI.Windows = {
	_windows: [],

	createWindow:  function(id, options)
	{
		var win = new Labrix.UI.Windows.Window(id, options);
		Labrix.UI.Windows._windows[id] = win;
		return win;
	},

	getWindow: function(id)
	{
		return Labrix.UI.Windows._windows[id];
	},

	closeWindow: function(id)
	{
		var win = Labrix.UI.Windows._windows[id];
		if(win)
		{
			win.close();
		}
	}
}

Labrix.UI.Windows.Window = Class.create(
{
	initialize: function(id, options)
	{
		this.id = id;
		this.options = options || { };
		
		document.observe('dom:loaded', this.render.bind(this));
	},
	
	render: function()
	{
		var container = $('window_container'); 
		if(!container)
		{
			var container = document.createElement('div');
			container.id = 'window_container';
			container.style.display = 'none';
			document.getElementsByTagName('body')[0].appendChild(container);
		}

		this._container = $(container);
		this._buttons = [];
		
		var cls = (this.options.cls || '');
		var style = (this.options.style || '');

		cls = 'window ' + cls;
		
		this._container.insert(
			'<div id="' + this.id + '" class="'+cls+'" style="'+style+'">' + 
				'<div class="window_inner">' +
					'<form name="' + this.id + '_f">' +
						'<div class="window_button_close">' +
							'<a href="#" onclick="Labrix.UI.Windows.closeWindow('+"'" + this.id + "'"+'); return false;"><img src="/images/window_close.gif" alt="X" title="Schließen" /></a>' +
						'</div>' +
						'<div class="window_title window_handle">&nbsp;</div>' +
						'<div class="window_content">' +
							'<div class="window_content_inner"></div>' +
							'<div class="window_buttons" style="display:none;">' +
							'</div>' +
						'</div>' +
					'</form>' +
				'</div>' +
			'</div>'
		);

		new Draggable(this.id, { handle: 'window_handle' });

		this.setTitle(this.options.title || '');
		this.setContent(this.options.content || '');
	},

	setTitle: function(title)
	{
		this.title = title;

		var divTitle = $$('#'+this.id+' .window_title')[0];
		if(divTitle)
		{
			divTitle.innerHTML = title;
		}
	},
	
	setContent: function(content)
	{
		this.content = content;

		var divContent = $$('#'+this.id+' .window_content_inner')[0];
		if(divContent)
		{
			divContent.innerHTML = content;
		}
	},

	addButton: function(name, title)
	{
		var divButtons = $$('#'+this.id+' .window_buttons')[0];
		if(divButtons)
		{
			if(divButtons.style.display == 'none')
			{
				divButtons.show();
			}

			var inputBtn = document.createElement('input');
			inputBtn.setAttribute('type', 'button');
			inputBtn.setAttribute('name', name);
			inputBtn.setAttribute('value', title);
			inputBtn.className = 'button ' + name;

			divButtons.appendChild(inputBtn);
			this._buttons.push(inputBtn);

			$(inputBtn).observe('click', this.eventButtonClick.bindAsEventListener(this, inputBtn));
		}
	},

	eventButtonClick: function(event, button)
	{
		//console.log(button.name);
		if(this.options.onButtonClick)
		{
			this.options.onButtonClick(button.name, this);
		}
	},
	
	show: function()
	{
		var viewPortDimensions = document.viewport.getDimensions();

		$('window_container').show();
		var windowDimensions = $(this.id).getDimensions();
		$('window_container').hide();

		$(this.id).setStyle({
			left: (viewPortDimensions.width/2 - windowDimensions.width/2)+'px',
			top: (viewPortDimensions.height/2 - windowDimensions.height/2)+'px'
		});

		$(this.id).show();
		
		if(Prototype.Browser.IE)
		{
			$('window_container').show();
		}
		else
		{
			$('window_container').appear({ duration: 0.5 });
		}

		if(this._buttons.length > 0)
		{
			(function(btn) { btn.focus(); }).delay(0.1, this._buttons[0]);
		}
		
		//window.setTimeout(function() { $('mybox').appear({ duration: 0.75 }); }, 750 );
	},

	close: function()
	{
		if(Prototype.Browser.IE)
		{
			$('window_container').hide();
			$(this.id).hide();
		}
		else
		{
			$('window_container').fade({ duration: 0.5 });
			(function(id) { $(id).hide(); }).delay(0.5, this.id);
		}

		//$('mywindow').fade({ duration: 0.75 });
		//window.setTimeout(function() { $('window_container').hide(); }, 750);
	}

});



Labrix.UI.Tabs = {
	_tabs: [],
	
	register: function(tabGroup, tabs)
	{
		Labrix.UI.Tabs._tabs[tabGroup] = tabs;
	},

	switchTab: function(tabGroup, tab)
	{
		for(var i=0; i < Labrix.UI.Tabs._tabs[tabGroup].length; i++)
		{
			var currentTab = Labrix.UI.Tabs._tabs[tabGroup][i];
			
			if(currentTab == tab)
			{
				$(currentTab+'_content').show();
				$(currentTab).addClassName('tab_active');
			}
			else
			{
				$(currentTab+'_content').hide();
				$(currentTab).removeClassName('tab_active');	
			}
		}
	}
};

