
/**
 * Contextmenu
 * @author henning < leutz [ at ] pcsg [ dot ] info
 * @package _ptools
 */

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

_ptools.ContextBar = function( settings )
{
	var t = this;
	t.type = '_ptools::ContextBar';

	t.obj = null;
	t.settings = settings;
	t.items = new Array();
	t.created = false;
	t.active = false;

	t.getChildren = function( name )
	{
		if (name)
		{
			var itm = t.items;
			
			for(var i = 0, len = itm.length; i < len; i++)
			{
				if (itm[i].settings.name == name)
				{
					return itm[i];
					break;
				};
			};
		};
		
		if (t.items.length > 0) {
			return t.items;
		};

		return false;
	};

	t.getElementByName = function( name )
	{
		return t.getChildren( name );
	};

	t.appendChild = function( _barItem )
	{
		if (_barItem.type == '_ptools::ContentBarItem')
		{
			_barItem.parent = t;
			t.items.push( _barItem );
		} else
		{
			return false;
		};

		if (this.created == true)
		{
			t.obj.appendChild(_barItem.create());
		};
	};

	t.create = function()
	{
		obj = document.createElement('div');
		obj.className = '_pContextBar';

		var items = t.items;
		for(var itm = 0, len = items.length; itm < len; itm++)
		{
			obj.appendChild(items[itm].create());
		};

		t.created = true;
		t.obj = obj;

		return obj;
	};

	// Item Aktivieren und DropDown anzeigen
	t.setItemActive = function( obj )
	{
		var items = t.items;
		t.active = true;

		for(var itm = 0, len = t.items.length; itm < len; itm++)
		{
			items[itm].setNormal();
		};
		obj.onclick();
	};

	t.setAllItemsNormal = function()
	{
		var items = t.items;
		t.active = true;

		for(var itm = 0, len = t.items.length; itm < len; itm++)
		{
			items[itm].setNormal();
		};
	};
};

/**
 * this.settings.text = Text für den Button
 */
_ptools.ContextBarItem = function( settings )
{
	var t = this;
	t.settings = settings;
	t.items = new Array();

	t.created = false;

	t.text = t.settings.text;
	t.obj;
	t.type = '_ptools::ContentBarItem';

	t.parent = null;
	t.active = false;
	t.sub = null;
	t.subMenu = null;
	t.tbody = null;

	t.disabled = false;

	t.onmouseover = function()
	{
		if (t.disabled == true) {
			return;
		};

		if (t.parent.active == false)
		{
			t.obj.className = '_pContextBarItem';
			row = t.obj.rows[0];
			row.cells[0].className = 'scbi_hover_1';
			row.cells[1].className = 'scbi_hover_2';
			row.cells[2].className = 'scbi_hover_3';
		} else
		{
			t.onclick();
		};
	};

	t.onmousout = function()
	{
		if (t.disabled == true) {
			return;
		};

		if (t.parent.active == false)
		{
			t.obj.className = '_pContextBarItem';
			row = t.obj.rows[0];
			row.cells[0].className = 'scbi_normal_1';
			row.cells[1].className = 'scbi_normal_2';
			row.cells[2].className = 'scbi_normal_3';

		} else
		{
			t.setNormal();
		};
	};

	t.onclick = function()
	{
		if (t.disabled == true) {
			return;
		};

		if (t.sub == null) {
			t.createSubMenu();
		};

		t.obj.focus();
		t.setActive();
	};

	t.setActive = function()
	{
		if (t.disabled == true) {
			return;
		};

		var row = t.obj.rows[0];

		t.active = true;
		t.obj.className = '_pContextBarItem';
		row.cells[0].className = 'scbi_active_1';
		row.cells[1].className = 'scbi_active_2';
		row.cells[2].className = 'scbi_active_3';

		if (t.sub) {
			t.sub.show();
		};
	};

	t.setNormal = function()
	{
		if (t.disabled == true) {
			return;
		};

		var row = t.obj.rows[0];

		t.active = false;
		t.obj.className = '_pContextBarItem';

		row.cells[0].className = 'scbi_normal_1';
		row.cells[1].className = 'scbi_normal_2';
		row.cells[2].className = 'scbi_normal_3';
	};

	t.onblur = function()
	{
		if (t.disabled == true) {
			return;
		};

		if (t.sub) {
			t.sub.hide();
		};
	};

	t.appendChild = function( obj )
	{
		if (t.created == true) {
			t.createSubMenu();
		};

		if (
			obj.type == '_ptools::ContextMenuItem' ||
			obj.type == '_ptools::ContextMenuSeperator'
			)
		{
			obj.parent = this;
			t.items.push(obj);
		} else
		{
			return false;
		};

		if (t.created == true) {
			t.sub.appendChild( obj );
		};
	};

	t.clear = function()
	{
		delete t.items;
		t.items = new Array();

		if (t.obj.rows[0].cells[0])
		{
			t.obj.rows[0].cells[0].innerHTML = "";
			delete t.sub;
			t.sub = null;
		};
	};

	t.getChildren = function( name )
	{
		if (name)
		{
			var items = t.items;
			for(var i = 0, len = items.length; i < len; i++)
			{
				if (items[i].getAttribute('name') == name)
				{
					return items[i];
					break;
				};
			};
		}

		if (t.items.length > 0) {
			return t.items;
		};
		
		return false;
	};

	t.getElementByName = function( name )
	{
		return t.getChildren( name );
	};
};

_ptools.ContextBarItem.prototype.setDisable = function()
{
	this.disabled = true;

	if (this.obj) {
		this.obj.className = '_pContextBarItemDisabled';
	};
};

_ptools.ContextBarItem.prototype.setEnable = function()
{
	this.disabled = false;

	if (this.obj) {
		this.obj.className = '_pContextBarItem';
	};
};

_ptools.ContextBarItem.prototype.create = function()
{
	var t = this;
	var obj = document.createElement('table'),
	tbody   = document.createElement('tbody'),
	tr      = document.createElement('tr'),
	td_left = document.createElement('td');

	obj.cellPadding = 0;
	obj.cellSpacing = 0;

	if (t.disabled)
	{
		obj.className = '_pContextBarItemDisabled';
	} else
	{
		obj.className = '_pContextBarItem';
	};

	obj.style.MozOutline = 'none';
	obj.setAttribute('tabindex', "-1");

	td_middle = td_left.cloneNode(true);
	td_right  = td_left.cloneNode(true);

	td_left.className   = 'scbi_normal_1';
	td_middle.className = 'scbi_normal_2';
	td_right.className  = 'scbi_normal_3';

	oSpan = document.createElement('span');
	oSpan.innerHTML = this.settings.text;
	td_middle.appendChild(oSpan);

	// Events
	obj.onmouseover = function()
	{
		t.onmouseover();
	};

	obj.onmouseout = function()
	{
		t.onmousout();
	};

	obj.onclick = function()
	{
		t.parent.setItemActive(t);
	};

	obj.onblur = function()
	{
		t.onblur();
	};

	tr.appendChild(td_left);
	tr.appendChild(td_middle);
	tr.appendChild(td_right);

	tbody.appendChild(tr);
	obj.appendChild(tbody);

	t.created = true;
	t.obj = obj;

	return t.obj;
};

_ptools.ContextBarItem.prototype.createSubMenu = function()
{
	var t = this;
	if (t.sub == null)
	{
		t.obj.rows[0].cells[0].style.position = 'relative';

		t.sub = new _ptools.ContextMenu({
			name: 'sub'
		});

		itm = t.items;
		
		for(var i = 0, len = itm.length; i < len; i++) {
			t.sub.appendChild( itm[i] );
		};

		t.obj.rows[0].cells[0].appendChild( t.sub.create() );
		t.sub.obj.style.top = '10px';
	};
};

// Context Menu
_ptools.ContextMenu = function( settings )
{
	var t = this;
	t.settings = settings;

	t.obj   = null;
	t.oText = null;
	t.sub   = null;
	t.tbody = null;

	t.type    = '_ptools::ContextMenu';
	t.items   = new Array();
	t.parent  = null;
	t.created = false;

	t.appendChild = function( itm )
	{
		if (itm.type != '_ptools::ContextMenuItem' &&
			itm.type != '_ptools::ContextMenuSeperator')
		{
			return false;
		};

		itm.parent = t;
		t.items.push( itm );

		if (t.created != true) {
			return;
		};
		
		if (t.tbody == null)
		{
			t.create();
		} else
		{
			t.tbody.appendChild(itm.create());
		};
	};

	t.setPos = function(x, y)
	{
		if (_ptools._System.isInteger(y)) {
			t.obj.style.top = y +'px';
		};

		if (_ptools._System.isInteger(x)) {
			t.obj.style.left = x +'px';
		};
	};

	t.show = function(event)
	{
		t.obj.style.display = '';
		
		if(typeof event == 'undefined') {
			return false;
		};
		
		// Event Cancel	
		if (typeof event.preventDefault == 'function')
		{
			event.preventDefault();
			event.stopPropagation(); 
		} else
		{
			event.returnValue = false;
		};
		
		t.focus();
		
		return false;
	};

	t.focus = function()
	{
		t.obj.onblur = function()
		{
			t.onblur(t);
		};

		t.obj.setAttribute('tabindex', "-1");
		t.obj.focus();
	};

	t.hide = function()
	{
		t.obj.style.display = 'none';
	};

	t.onblur = function()
	{
		if (typeof t.settings.onblur == 'function')
		{
			t.settings.onblur(t);

		} else if (t.settings.onblur)
		{
			eval(t.settings.onblur+'(t)');
		};

		t.hide();
	};

	t.clear = function()
	{
		t.items = [];

		try
		{
			t.tbody.innerHTML = '';
		} catch(e)
		{
			// for IE
			while(t.sub.rows.length) {
				t.sub.deleteRow(0);
			};
		};
	};

	t.getAttribute = function( name )
	{
		if (t.settings[ name ]) {
			return t.settings[ name ];
		};

		return false;
	};

	t.setAttribute = function( name, value )
	{
		t.settings[ name ] = value;
	};
};

_ptools.ContextMenu.prototype.create = function()
{
	var t = this;
	var obj = document.createElement('div'),
	sub     = document.createElement('table'),
	tbody   = document.createElement('tbody');

	obj.className = '_pContextMenuMainDiv';
	obj.style.MozOutline = 'none';
	obj.setAttribute('tabindex', "-1");
	obj.style.display = 'none';

	sub.cellPadding = 0;
	sub.cellSpacing = 0;
	sub.className   = '_pContextMenuDropDown';

	if (t.parent && t.parent.type == '_ptools::Button')
	{
		sub.style.top  = '4px';
		sub.style.left = '0px';
	} else
	{
		sub.style.top  = '10px';
		sub.style.left = '0px';
	};

	var items = t.items;
	
	for(var i = 0, len = items.length; i < len; i++) {
		tbody.appendChild( items[i].create() );
	};

	obj.onblur = function()
	{
		//t.onblur();
	};

	sub.appendChild( tbody );
	obj.appendChild( sub );

	t.tbody = tbody;
	t.sub   = sub;
	t.obj   = obj;

	t.created = true;
	return obj;
};

_ptools.ContextMenu.prototype.del = function()
{
	var t = this;

	if (t.obj)
	{
		t.obj.parentNode.removeChild( t.obj );
		t.obj = null;

		delete t.sub;
		delete t.tbody;
	};
};

/**
 * this.settings
 	text = Text für den Button
 	image = Icon
 */
_ptools.ContextMenuItem = function( _settings )
{
	var t = this;
	var settings = typeof _settings != 'undefined' ? _settings : {};

	t.obj     = null;
	t.oText   = null;
	t.sub     = null;
	t.subMenu = null;
	t.tbody   = null;
	t.parent  = null;

	t.type    = '_ptools::ContextMenuItem';
	t.items   = new Array();
	t.created = false;

	t.disabled = false;

	// Mouseover Effekt
	t.onmouseover = function()
	{
		if (t.disabled) {
			return;
		};

		var obj = t.obj;
		
		if (obj.cells[0].className != 'scmi_hover')
		{
			obj.cells[0].className = 'scmi_hover';
			obj.cells[1].className = 'scmi_hover';
			obj.cells[2].className = 'scmi_hover';
		};

		if (t.sub)
		{
			t.parent.obj.onblur = function(){};
			t.sub.show();
		};
	};

	// Mouseout Effekt
	t.onmouseout = function()
	{
		if (t.disabled) {
			return;
		};

		var obj = t.obj;
		obj.cells[0].className = '';
		obj.cells[1].className = '';
		obj.cells[2].className = '';

		if (t.sub)
		{
			t.parent.obj.onblur = function(){
				t.parent.onblur();
			};
			
			t.sub.hide();
		};
	};

	t.onclick = function()
	{
		if (t.disabled) {
			return;
		};

		if (typeof settings.onclick == 'function')
		{
			settings.onclick(t);
		} else if (settings.onclick)
		{
			eval(settings.onclick+"(t)");
		};

		if (t.parent != null &&
			(t.parent.type == '_ptools::ContextMenu' ||
			t.parent.type == '_ptools::ContentBarItem'))
		{
			setTimeout (function() {t.parent.onblur()}, 50);
		};
	};

	t.setAttribute = function(name, value)
	{
		if (name == 'text' && t.oText != null) {
			t.oText.innerHTML = value;
		};

		settings[ name ] = value;
	};

	t.getAttribute = function( name )
	{
		if (settings[ name ]) {
			return settings[ name ];
		};

		return false;
	};

	t.appendChild = function( child )
	{
		if (
			child.type == '_ptools::ContextMenuItem' ||
			child.type == '_ptools::ContextMenuSeperator'
		)
		{
			t.items.push( child );
		};

		if (t.created == true)
		{
			if (!t.sub)
			{
				t.createSubMenu();
			} else
			{
				t.sub.appendChild( child );
			};
		};
	};

	t.getChildren = function( name )
	{
		if (name)
		{
			var items = t.items;
			for(var i = 0, len = items.length; i < len; i++)
			{
				if (items[i].getAttribute('name') == name)
				{
					return items[i];
					break;
				};
			};
		};
		
		if (t.items.length > 0) {
			return t.items;
		};
		
		return false;
	};

	t.getElementByName = function( name )
	{
		return t.getChildren( name );
	};

	t.clear = function()
	{
		delete t.items;
		t.items = new Array();

		if (t.obj) {
			t.obj.cells[2].innerHTML = '';
		};

		t.sub = null;
		t.tbody = null;
	};
};

_ptools.ContextMenuItem.prototype.del = function()
{
	var t = this;

	if (t.obj)
	{
		t.obj.parentNode.removeChild( t.obj );
		t.obj = null;
	};
};


_ptools.ContextMenuItem.prototype.setDisable = function()
{
	this.disabled = true;

	if (this.obj) {
		this.obj.className = '_pContextItemDisabled';
	};
};

_ptools.ContextMenuItem.prototype.setEnable = function()
{
	this.disabled = false;

	if (this.obj) {
		this.obj.className = '';
	};
};


// Objekt erstellen
_ptools.ContextMenuItem.prototype.create = function()
{
	var t = this;
	var obj   = document.createElement('tr');
	var oText = document.createElement('span');
		oText.innerHTML = t.getAttribute('text');

	var td_left   = document.createElement('td');
	var td_middle = td_left.cloneNode(true);
	var td_right  = td_left.cloneNode(true);

	td_middle.appendChild(oText);
	td_left.style.width = '20px';

	if (t.getAttribute('image'))
	{
		var oImg = document.createElement('img');
		oImg.src = t.getAttribute('image');

		oImg.style.position = 'relative';
		oImg.style.top      = '2px';
		oImg.style.left     = '2px';

		td_left.appendChild(oImg);
	} else
	{
		td_left.innerHTML = '<div style="width:20px;">&nbsp;</div>';
	};

	obj.onmouseover = function() {
		t.onmouseover();
	};

	obj.onmouseout = function() {
		t.onmouseout();
	};

	obj.onmousedown = function() {
		t.onclick();
	};

	obj.onmouseup = function() {
		document.body.focus();
	};

	obj.appendChild(td_left);
	obj.appendChild(td_middle);
	obj.appendChild(td_right);

	if (t.disabled) {
		obj.className = '_pContextItemDisabled';
	};

	//sContextItems[this.settings.name] = this;
	t.oText = oText;
	t.obj   = obj;

	if (t.items.length > 0) {
		t.createSubMenu();
	};

	if (!t.getAttribute('name')) {
		t.setAttribute('name', new Date().getTime());
	};

	t.created = true;
	return obj;
};

// Submenü erstellen
_ptools.ContextMenuItem.prototype.createSubMenu = function()
{
	var t = this;
	if (t.sub == null)
	{
		var oArrow = document.createElement('div');
			oArrow.className = '_pContextItemArrow';

		var oTd = t.obj.firstChild.nextSibling.nextSibling;
		var oDiv = document.createElement('div');

		t.sub = new _ptools.ContextMenu({
			name: 'sub'
		});

		itm = t.items;
		for(var i = 0, len = itm.length; i < len; i++)
		{
			t.sub.appendChild( itm[i] );
		};

		oDiv.className = '_pContextMenuDropDownParent';
		oDiv.appendChild( t.sub.create() );

		oTd.appendChild( oArrow );
		oTd.appendChild( oDiv );
	};
};


// Menü Seperator
_ptools.ContextMenuSeperator = function( _settings )
{
	var t = this;
	var settings = typeof _settings != 'undefined' ? _settings : {};

	t.type = '_ptools::ContextMenuSeperator';
	t.obj = null;

	t.create = function()
	{
		var obj, td, oHr;

		obj = document.createElement('tr');
		td = document.createElement('td');

		td.colSpan = "3";
		td.style.width = '100%';
		td.style.height = '5px';

		oHr = document.createElement('hr');
		oHr.align = 'center';
		oHr.width = '90%';
		oHr.height = '1';
		oHr.color = '#c6c3b9';
		oHr.backgroundColor = '#c6c3b9';

		td.appendChild(oHr);
		obj.appendChild(td);

		t.obj = obj;
		return t.obj;
	};


	t.setAttribute = function(name, value)
	{
		if (name == 'text' && t.oText != null) {
			t.oText.innerHTML = value;
		};

		settings[ name ] = value;
	};

	t.getAttribute = function( name )
	{
		if (settings[ name ]) {
			return settings[ name ];
		};

		return false;
	};

};


