/**
 * Toolbar
 * @author henning
 */

if (typeof _ptools == 'undefined') {
	var _ptools = {};
};

if (typeof _ptools._toolbars == 'undefined') {
	_ptools._toolbars = new Array();
};

_ptools.Toolbar = function( settings )
{
	var t  = this;
	t.type = '_ptools::Toolbar';
	
	t.settings = settings;
	
	t.items = new Array();
	t.obj   = null;
	t.width = 0;
	
	t.created    = false;
	t.activeItem = null;
	
	t.menuTab   = null;
	t.moveLeft  = null;
	t.moveRight = null;
	
	t.x = 0;
	
	t.fx = null;
	
	t.del = function()
	{
		if (t.main) {
			t.main.parentNode.removeChild( t.main );
		};
	};
};

_ptools.Toolbar.prototype.create = function()
{
	var t = this;
	var slide = typeof t.settings.slide == 'undefined' || t.settings.slide != false ? true : false;
	
	t.main = document.createElement('div');
	t.main.className = '_pToolbarMain';
	
	t.obj = t.main.cloneNode(true);
	t.obj.className = '_pToolbar';
	
	if (t.settings.height) {
		t.obj.style.height = t.settings.height+'px'; 
	};
		
	if (slide == true)
	{
		t.move = t.main.cloneNode(true);
		t.move.className = '_pToolbarMove';
		
		if (t.settings.width) {
			t.move.style.width = (t.settings.width-110) +'px';
		};
		
		t.fx = new Fx.Scroll(t.move);
		
		t.menuTab = new _ptools.Button({
			name : '_tab',
			text : '<div class="_pToolbarDrop"></div>'
		});
		t.menuTab.parent = this;
		t.main.appendChild( t.menuTab.create() );
		
		// left
		t.moveLeft = new _ptools.Button({
			name    : '_tab',
			text    : '<div class="_pToolbarLeft"></div>',
			onclick : function(_me) {
				_me.getAttribute('Toolbar').toLeft();
				//'_ptools._toolbars["'+t.settings.name+'"].toLeft'
			},
			Toolbar : t
		});
		
		t.moveLeft.parent = this;
		t.main.appendChild( t.moveLeft.create() );
		
		// @todo children grössen berechnung
		t.obj.style.width = '1000px';
	} else
	{
		if (t.settings.width)
		{
			t.main.style.width = (t.settings.width-110) +'px';
			t.obj.style.width  = t.settings.width +'px';
		};
	};
	
	for (var i = 0, len = t.items.length; i < len; i++)
	{
		t.obj.appendChild(t.items[i].create());
		
		if (slide == true)
		{
			var _cm = new _ptools.ContextMenuItem(t.items[i].settings);
			_cm.setAttribute('TAB', t.items[i]);
			
			t.menuTab.appendChild( _cm );
		};
	};
	
	if (slide == true)
	{
		t.move.appendChild( t.obj );
		t.main.appendChild( t.move );
	
		// left
		t.moveRight = new _ptools.Button({
			name : '_tab',
			text : '<div class="_pToolbarRight"></div>',
			onclick : function(_me) {
				_me.getAttribute('Toolbar').toRight();
				//_ptools._toolbars["'+t.settings.name+'"].toRight()
			},
			Toolbar : t
		});
		t.moveRight.parent = this;
		t.main.appendChild( t.moveRight.create() );
		
		t.main.style.width = t.settings.width+'px';
		
	} else
	{
		t.main.appendChild( t.obj );
	};
	
	t.created = true;
	
	if (t.settings.name) {
		_ptools._toolbars[ t.settings.name ] = this;
	};
	
	return t.main;
};

_ptools.Toolbar.prototype.toLeft = function()
{
	this.x = 0;
	this.fx.toLeft();
};

_ptools.Toolbar.prototype.toRight = function()
{
	this.x = this.x+100;
	this.fx.start(this.x, 0);
};

_ptools.Toolbar.prototype.toTab = function(Tab)
{
	var t = this;
	var slide = t.settings.slide || false;
	
	if (slide == false) {
		return;
	};
	
	if (!t.created) {
		return false;
	};
	
	this.x = Tab.text.offsetLeft - Tab.obj.offsetLeft - 20;
	this.fx.start(this.x, 0);
};

_ptools.Toolbar.prototype.resize = function()
{
	var t = this;
	
	/*
	if (t.obj.offsetWidth)
	{
		var w = 0;
		var ow = t.obj.offsetWidth-70;
		
		t.obj.innerHTML = '';
		
		for(var i = 0, len = t.items.length; i < len; i++)
		{
			t.obj.appendChild( t.items[i].create() );
			
			w = w + t.items[i].obj.offsetWidth;
			
			if (w > ow)
			{
				t.items[i].obj.parentNode.removeChild( t.items[i].obj );
				
				t.menuTab = new _ptools.ToolbarTab({
					name : '_tab',
					text : '<div class="_pToolbarDrop"></div>'
				});
				t.menuTab.parent = this;
					
				t.obj.appendChild( t.menuTab.create() );
				t.menuTab.obj.style.marginLeft = '10px';
				
				i = i-1;
				
				break;
			};		
		};
		
		for(var c = i; c < len; c++)
		{
			var _cm = new _ptools.ContextMenuItem(t.items[c].settings);
			t.menuTab.appendChild( _cm );
		};
	};
	*/
};

_ptools.Toolbar.prototype.appendChild = function( itm )
{
	var t = this;
	
	if (typeof itm == 'undefined') {
		return;
	};
	
	if (itm.type == '_ptools::Button' ||
		itm.type == '_ptools::ButtonSeperator' ||
		itm.type == '_ptools::Select' ||
		itm.type == '_ptools::Breadcrumb' ||
		itm.type == '_ptools::ToolbarTab' ||
		itm.type == '_ptools::ToolbarBlock')
	{
		itm.parent = this;
		t.items.push( itm );
	} else
	{
		return false;
	};
	
	if (t.created == true)
	{
		t.obj.appendChild(itm.create());
		
		if (t.menuTab && itm.type != '_ptools::Breadcrumb')
		{
			var _cm = new _ptools.ContextMenuItem(itm.settings);
			_cm.setAttribute('TAB', itm);
			
			t.menuTab.appendChild( _cm );
		};			
	};
	
	if (t.menuTab && itm.type != '_ptools::Breadcrumb') {
		t.menuTab.setEnable();
	};
	
	if (t.moveLeft) {
		t.moveLeft.setEnable();
	};
	
	if (t.moveRight) {
		t.moveRight.setEnable();
	};
};

_ptools.Toolbar.prototype.setItemActive = function( child )
{
	var t = this;
	
	for (var itm = 0, len = t.items.length; itm < len; itm++)
	{
		if (child == t.items[itm])
		{
			child.onclick();
		} else
		{
			t.items[itm].setNormal();
		};
		
		if (child == t.menuTab) {
			child.onclick();
		};
	};
};

_ptools.Toolbar.prototype.clear = function()
{
	var t = this;
	
	if (t.obj == null) {
		return;
	};
	
	t.obj.innerHTML = '';
	
	delete t.items;
	delete t.activeItem;
	
	t.items      = new Array();
	t.activeItem = null;
	
	if (t.menuTab)
	{
		t.menuTab.clear();
		t.menuTab.setDisable();
	};
	
	if (t.moveLeft) {
		t.moveLeft.setDisable();
	};
	
	if (t.moveRight) {
		t.moveRight.setDisable();
	};
};

_ptools.Toolbar.prototype.firstChild = function()
{
	return this.items[0];
};

_ptools.Toolbar.prototype.getChildren = function()
{
	return this.items;
};

_ptools.Toolbar.prototype.getElementByName = function( name )
{
	for (var i = 0, len = this.items.length; i < len; i++)
	{
		if (this.items[i] && this.items[i].settings && this.items[i].settings.name == name)
		{
			return this.items[i];
			break;
		};
	};
};

_ptools.Toolbar.prototype.del = function()
{
	this.main.parentNode.removeChild( this.main );
};


_ptools.Toolbar.prototype.setAttribute = function(name, value)
{
	var t = this;
	
	switch(name)
	{
		case "width" :
			if (t.move) {
				t.move.style.width = (value-110) +'px';
			};
			
			if (t.main) {	
				t.main.style.width = value +'px';
			};
			
		break;
		
		default:
			this.settings[ name ] = value;
		break;
	};
};

_ptools.ToolbarBlock = function( _settings )
{
	var t = this;
	
	var items   = [];
	var created = false;
	
	var attributes = _settings || {};
	
	t.type = '_ptools::ToolbarBlock';
	t.obj  = null;
	
	t.getAttribute = function( name )
	{
		if (attributes[ name ]) {
			return attributes[ name ];
		};
		
		return false;
	};
	
	t.setAttribute = function(name, value) 
	{
		attributes[ name ] = value;
		
		if (t.obj == null) {
			return;
		};
		
		switch(name)
		{
			case 'width':
				t.obj.style.width = value +'px';
			break;
		};
	};
	
	t.create = function()
	{
		var obj       = document.createElement('div');
		obj.className = '_pToolbarBlock';
		
		if (t.getAttribute('width')) {
			obj.style.width = t.getAttribute('width') +'px';
		};
		
		for (var i = 0, len = items.length; i < len; i++)  {
			obj.appendChild( items[i].create() );
		};
		
		created = true;
		t.obj   = obj;
		
		return t.obj;
	};
	
	t.appendChild = function( itm ) 
	{
		items.push( itm );
		
		if (created == true) {
			t.obj.appendChild( itm.create() );
		};
		
		return t;
	};
	
	t.getChildren = function() 
	{
		return items;
	};
	
	return t;
};


/**
 * Ein Tab
 */
_ptools.ToolbarTab = function( settings )
{
	var t  = this;
	t.type = '_ptools::ToolbarTab';
	
	t.settings = settings;
	
	t.parent = null;
	t.obj    = null;
	t.text   = null;
	
	t.items  = new Array();
	t.sub    = null;
	t.active = false;
};

_ptools.ToolbarTab.prototype.create = function()
{
	var t = this;
	t.obj = document.createElement('div');
	
	var obj  = t.obj.cloneNode(true);
	var oDiv = t.obj.cloneNode(true);
	
	t.obj.className = '_pToolbarTab';
	obj.className   = 'Tab';
	oDiv.className  = 'Right';
	
	t.text = document.createElement('span');
	t.text.innerHTML = t.settings.text;
	
	if (t.settings.width) {
		t.text.style.width = t.settings.width +'px'; 
	};
	
	obj.onmouseover = function()
	{
		t.onmouseover();
	};
	
	obj.onmouseout = function()
	{
		t.onmouseout();
	};
	
	obj.onclick = function()
	{
		t.setActive();
	};
	
	if (t.items.length > 0)
	{
		t.sub = new _ptools.ContextMenu({
			name   : 'sub',
			onblur : t.parent.settings.name +'.setItemActive();'
		});
		
		t.sub.parent = this;
		
		for (var i = 0, len = t.items.length; i < len; i++) {
			t.sub.appendChild(t.items[i]);
		};
		
		obj.appendChild(t.sub.create());
	};
	
	obj.appendChild( t.text );
	
	t.obj.appendChild( obj );
	t.obj.appendChild( oDiv );
	
	return t.obj;
};

_ptools.ToolbarTab.prototype.appendChild = function( itm )
{
	if (itm.type != '_ptools::ContextMenuItem') {
		return false;
	};
	
	var t = this;
	
	if (t.sub == null)
	{
		t.sub = new _ptools.ContextMenu({
			name   : 'sub',
			onblur : t.parent.settings.name +'.activeItem.setNormal'
		});
		
		t.sub.parent = this;
		t.obj.appendChild(t.sub.create());
	};
	
	t.items.push( itm );
	
	if (t.sub) {
		t.sub.appendChild( itm );
	};
};

_ptools.ToolbarTab.prototype.onmouseover = function()
{
	if (this.active == false)
	{
		this.obj.className = '_pToolbarTabHover';
		this.obj.focus();
	};	
};

_ptools.ToolbarTab.prototype.onmouseout = function()
{
	if (this.active == false) {
		this.obj.className = '_pToolbarTab';
	};
};

_ptools.ToolbarTab.prototype.setNormal = function()
{
	this.active = false;
	this.onmouseout();
};

_ptools.ToolbarTab.prototype.setActive = function()
{
	var t = this;
	
	if (t.active == false)
	{
		if (t.parent && t.parent.type == '_ptools::Toolbar')
		{
			t.parent.setItemActive(t);
		} else
		{
			t.onclick();
		};
	};
};

_ptools.ToolbarTab.prototype.onclick = function()
{
	var t = this;
	
	var parent = t.parent;
	t.active   = true;
	
	t.obj.className = '_pToolbarTabActive';
	
	if (t.sub != null) {
		t.sub.show();
	};
	
	if (parent)
	{
		if (parent.activeItem && 
			parent.activeItem.type == '_ptools::ToolbarTab') 
		{
			parent.activeItem.onunload();
		};
		
		parent.activeItem = t;
	};
	
	t.onload();
};

_ptools.ToolbarTab.prototype.onload = function()
{
	var t = this;
	
	if (typeof t.settings.onload == 'function')
	{
		t.settings.onload(t);
	} else if (t.settings.onload)
	{
		eval(t.settings.onload+"(t)");
	};
};

_ptools.ToolbarTab.prototype.onunload = function()
{
	var t = this;
	
	if (typeof t.settings.onunload == 'function')
	{
		t.settings.onunload(t);
	} else if (t.settings.onunload)
	{
		eval(t.settings.onunload +"(t)");
	};
};

_ptools.ToolbarTab.prototype.getAttribute = function( name )
{
	if (this.settings[ name ]) {
		return this.settings[ name ];
	};

	return false;
};


_ptools.ToolbarTab.prototype.setAttribute = function( name, value )
{
	this.settings[ name ] = value;
};
