
/**
 * Button
 * @author PCSG - Henning
 * @copyright PCSG
 * @package _ptools
 */

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

_ptools.Button = function( settings )
{
	var t  = this;
	t.type = '_ptools::Button';
	
	t.settings = settings;
	t.items    = new Array();
	
	t.created = false;
	t.obj     = null;
	t.oText   = null;
	t.oImage  = null;
	t.sub     = null;
	
	t.active  = 0;	
	t.disable = 0;
		
	var overTempAction = false;
	
	// Onmouseover
	t.onmouseover = function()
	{
		if (t.disable) {
			return;
		};
		
		if (t.active != 1) {
			t.obj.className = 'sButtonHover';
		};
	};
	
	// onmouseout
	t.onmouseout = function()
	{
		if (t.disable) {
			return;
		};
		
		if (t.active != 1) {
			t.obj.className = 'sButton';
		};
	};
	
	// Klick Event ausführen und gegebenfalls Submenü anzeigen
	t.onclick = function()
	{
		if (t.dbl_disable == 1 || t.disable == 1) {
			return;
		};
		
		var result = '';
		
		if (t.settings.onclick)
	 	{
	 		if (typeof t.settings.onclick == 'function')
	 		{
	 			result = t.settings.onclick(t);
	 		} else
	 		{
		 		var self = t;
		 		result = eval(t.settings.onclick+"(self)")
		 	};
	 	};
	 	
	 	if (t.sub != null)
		{
			t.sub.show();
			
			// IE FIX
			if (_ptools._Browser.isMSIE) {
				t.sub.obj.focus();
			};
		};
		
		// double klick fix
		t.dbl_disable = 1;
		window.setTimeout(t.setEnable, 300);
		
		return result;
	};
	
	t.onmousedown = function()
	{
		if (t.disable == 1) {
			return;
		};
		
		if (typeof t.settings.onmousedown == 'function')
		{
			t.settings.onmousedown(t);
		} else if (t.settings.onmousedown)
	 	{
	 		eval(t.settings.onmousedown+"(t)");
	 	};
	};
	
	t.setActive = function()
	{
		if (t.disable == 1) {
			return;
		};
		
		if (t.obj) {
			t.obj.className = 'sButtonActive';
		};
		
		if (t.parent) {
			t.parent.activeItem = t;
		};
		
		t.active = 1;
	};
	
	t.isActive = function()
	{
		if (t.active) {
			return true;
		};
		
		return false;
	};
	
	t.setDisable = function()
	{
		t.disable = 1;
		
		if (t.obj) {
			t.obj.className = 'sButtonDisable';
		};
	};
	
	t.setEnable = function()
	{
		t.disable     = 0;
		t.dbl_disable = 0;
		
		if (t.active != 1) { // wegen dbl klick FIX
			t.setNormal();
		};
	};
	
	t.setNormal = function()
	{
		if (t.disable) {
			return;
		};
		
		if (t.obj) {
			t.obj.className = 'sButton';
		};
		
		t.active = 0;
	};
	
	t.onblur = function()
	{
		if (t.disable) {
			return;
		};
		
		if (t.sub) {
			t.sub.hide();
		};
	};
};

_ptools.Button.prototype.create = function()
{
	var t = this;
	t.obj = document.createElement('table');
	
	if (t.active == 1)
	{
		t.obj.className = 'sButtonActive';
	} else
	{
		t.obj.className = 'sButton';
	};
	
	if (t.settings.width) {
		t.obj.style.width = t.settings.width+'px';
	};
	
	if (t.settings.height) {
		t.obj.style.height = t.settings.height+'px';
	};
	
	// disabled
	if (t.settings.disabled == 1)
	{
		t.disable = true;
		t.obj.className = 'sButtonDisable';
	};
	
	t.obj.style.MozOutline = 'none';
	t.obj.setAttribute('tabindex', "-1");
	
	var tbody = document.createElement('tbody');
	var tr_1  = document.createElement('tr');
	var tr_2  = tr_1.cloneNode(true);
	var tr_3  = tr_1.cloneNode(true);
	
	var td_left_1 = document.createElement('td');
	var td_left_2 = td_left_1.cloneNode(true);
	var td_left_3 = td_left_1.cloneNode(true);
	
	var td_middle_1 = td_left_1.cloneNode(true);
	var td_middle_2 = td_left_1.cloneNode(true);
	var td_middle_3 = td_left_1.cloneNode(true);
	
	var td_right_1 = td_left_1.cloneNode(true);
	var td_right_2 = td_left_1.cloneNode(true);
	var td_right_3 = td_left_1.cloneNode(true);
	
	// CSS
	td_left_1.className = 'sButton_left1';
	td_left_2.className = 'sButton_left2';
	td_left_3.className = 'sButton_left3';
	
	td_middle_1.className = 'sButton_middle1';
	td_middle_2.className = 'sButton_middle2';
	td_middle_3.className = 'sButton_middle3';
	
	td_right_1.className = 'sButton_right1';
	td_right_2.className = 'sButton_right2';
	td_right_3.className = 'sButton_right3';
	
	tr_1.appendChild(td_left_1);
	tr_1.appendChild(td_middle_1);
	tr_1.appendChild(td_right_1);
	
	tr_2.appendChild(td_left_2);
	tr_2.appendChild(td_middle_2);
	tr_2.appendChild(td_right_2);
	
	tr_3.appendChild(td_left_3);
	tr_3.appendChild(td_middle_3);
	tr_3.appendChild(td_right_3);
	
	tbody.appendChild(tr_1);
	tbody.appendChild(tr_2);
	tbody.appendChild(tr_3);
	
	t.obj.onclick = function() {
		t.onclick();
	};
	
	t.obj.onmouseout = function() {
		t.onmouseout();
	};
	
	t.obj.onmouseover = function() {
		t.onmouseover();
	};
	
	t.obj.onmousedown = function() {
		t.onmousedown();
	};
	
	t.obj.onmouseout = function() {
		t.onmouseout();
	};
	
	t.obj.onblur = function() {
		t.onblur();
	};
	
	t.obj.appendChild(tbody);
	
	if (t.settings.image) {	
		t.setAttribute('image', t.settings.image);
	};
	
	if (t.settings.text) {
		t.setAttribute('text', t.settings.text);
	};
	
	if (t.settings.title) {
		t.obj.setAttribute('title', t.settings.title);
	};
	
	if (t.items.length > 0)
	{
		t.createSubMenu();
		
		for (var i = 0, len = t.items.length; i < len; i++) {
			t.sub.appendChild( t.items[i] );
		};
	};
	
	if (typeof t.settings.oncreate == 'function') {
		t.settings.oncreate(t);
	};
	
	this.created = true;
	return t.obj;
};

_ptools.Button.prototype.appendChild = function( itm )
{
	var t = this;
	
	if (itm.type != '_ptools::ContextMenuItem') {
		return false;
	};
	
	t.items.push( itm );
	
	if (t.created)
	{
		if (!t.sub) {
			t.createSubMenu();
		};

		t.sub.appendChild(itm);
	};
};

_ptools.Button.prototype.clear = function()
{
	var t = this;
	
	if (t.sub)
	{
		t.sub.del();
		t.sub = null;
		
		if (t.oArrowParent) {
			t.oArrowParent.parentNode.removeChild( t.oArrowParent );
		};		
	};
};

_ptools.Button.prototype.createSubMenu = function(att, value)
{
	var t = this;
	
	if (!t.sub)
	{
		t.sub = new _ptools.ContextMenu({
			name : 'sub'
		});
		
		t.sub.parent = this;
		
		t.obj.rows[1].cells[1].appendChild( t.sub.create() );
		
		t.oArrowParent = document.createElement('div');
		var oArrow     = t.oArrowParent.cloneNode(true);
		
		t.oArrowParent.className = '_pButtonArrowDiv';
		oArrow.className         = '_pButtonArrow';
		
		t.oArrowParent.appendChild(oArrow);
		
		t.obj.rows[1].cells[1].appendChild( t.oArrowParent );
	};
};

_ptools.Button.prototype.getAttribute = function( name )
{
	var t = this;
	
	switch (name)
	{
		case "text":
			if (t.oText != null) {
				return t.oText.innerHTML;
			};
			
			return '';
		break;
		
		case "image":
			if (t.oImage != null) {
				return t.oImage.src;
			};
			
			return '';
		break;
		
		default:
			if (t.settings[ name ]) {
				return t.settings[ name ];
			};
			
			return false;
		break;
	};
};

_ptools.Button.prototype.setAttribute = function(att, value)
{
	var t = this;
	
	switch (att)
	{
		case "onclick":
			t.settings.onclick = value;
		break;
		
		case "text":
			if (t.oText != null)
			{
				t.oText.innerHTML = value;
			} else
			{
				t.oText = document.createElement('div');
				
				t.oText.style.marginLeft  = '5px';
				t.oText.style.marginRight = '5px';
				
				t.oText.innerHTML = value;
				t.obj.rows[1].cells[1].appendChild(this.oText);
			};
			
			t.settings.text = value;
		break;
		
		case "image":
			if (t.oImage != null)
			{
				t.oImage.src = value;
			} else
			{
				var oIDiv = document.createElement('div');
					oIDiv.style.fontSize = '1px';
					oIDiv.align = 'center';
					
				t.oImage = document.createElement('img');
				t.oImage.src = value;
				
				oIDiv.appendChild(t.oImage);
				
				t.obj.rows[1].cells[1].appendChild(oIDiv);
			};
			
			this.settings.image = value;
		break;
		
		case "title":
			t.settings.title = value;
			
			if (t.obj) {
				t.obj.setAttribute('title', value);
			};
		break;
		
		default:
			this.settings[att] = value;
			
		break;
	};
};


/**
 * Button Seperator
 */
_ptools.ButtonSeperator = function(settings)
{
	var t    = this;
	t.type   = '_ptools::ButtonSeperator';
	settings = typeof settings != 'undefined' ? settings : {};
	
	t.obj    = null;
	t.parent = null;
	
	t.getAttribute = function( name )
	{
		if (settings[ name ]) {
			return settings[ name ];
		};
		
		return false;
	};
	
	t.setAttribute = function(name, value) {
		settings[ name ] = value;
	};
};

_ptools.ButtonSeperator.prototype.create = function()
{
	var t = this;
	t.obj = document.createElement('div');
	t.obj.className = 'sButtonSeperator';
	
	if (t.parent != null) {
		t.obj.style.height = t.parent.obj.offsetHeight +"px";
	};
	
	if (t.getAttribute('height')) {
		t.obj.style.height = t.getAttribute('height') +"px";
	};
	
	return t.obj;
};

