/**
 * PTools SelectBox
 * @author henning
 */

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

_ptools.Select = function( settings )
{
	var t = this;
	t.settings = settings;
	t.type = '_ptools::Select';
	t.obj = null;
	t.input = null;
	t.text = null;
	t.dropdown = null;
	t.items = new Array();

	t.value;

	t.dropdownWidth = 200;

	t.active = null;
	t.created = false;
	
	t.setAttribute = function(name, value) 
	{
		t.settings[ name ] = value;
	};
	
	t.getAttribute = function(name) 
	{
		if(t.settings[name]) {
			return t.settings[name];
		};
		
		return false;
	};
	
	t.getChildren = function() 
	{
		return t.items;
	};
	
	
	return t;
};

_ptools.Select.prototype.create =  function()
{
	var t = this;
	var _System = _ptools._System;
	var path = _System.getAttribute('path');
	
	t.obj = document.createElement('div');
	
	t.text = t.obj.cloneNode(true);
	t.text.className = 'text';
	t.dropdown = t.obj.cloneNode(true);

	var left = t.obj.cloneNode(true);
		left.className = 'left';
	var right = t.obj.cloneNode(true);
		right.className = 'right';		
	
	t.obj.appendChild(left);
	t.obj.appendChild(right);

	var oArrow = document.createElement('img');
	oArrow.src = path +'select/images/arrow.png';
	
	var style = oArrow.style;	
	style.position = 'absolute';
	style.top = '10px';
	style.right = '5px';

	t.obj.appendChild( oArrow );

	t.obj.className = 'sSelect';
	style = t.obj.style;
	style.position = 'relative';
	style.zIndex = 5000;
	style.cursor = 'pointer';
	
	if(t.settings.width) {
		t.obj.style.width = t.settings.width +'px'; 
	};

	style = t.dropdown.style;
	style.MozOutline = 'none';
	style.position = 'absolute';
	style.top = '22px';
	style.left = '0px';
	style.display = 'none';

	t.dropdown.className = 'sSelectDropDown';
	t.dropdown.setAttribute('tabindex', "-1");

	t.text.style.paddingLeft = '5px';
	
	t.dropdown.onblur = function()
	{
		t.close();
	};

	t.obj.onclick = function()
	{
		t.open();
	};
	
	t.obj.onmousedown = function()
	{
		t.onmousedown();
	};
	
	for( var i = 0, len = t.items.length; i < len; i++ )
	{
		t.dropdown.appendChild(t.items[i].create());
		t.items[i].obj.style.width = (t.dropdownWidth-10) + 'px';
	};

	t.dropdown.style.width = t.dropdownWidth + 'px';
	t.obj.appendChild(t.text);
	t.obj.appendChild(t.dropdown);
	
	t.input = document.createElement('input');
	t.input.type = 'hidden';
	t.input.name = t.getAttribute('name');
	t.obj.appendChild(t.input);
	
	t.created = true;

	return t.obj;
};

_ptools.Select.prototype.appendChild = function( itm )
{
	if((itm.text.length*5) > this.dropdownWidth) {
		this.dropdownWidth = (this.items[i].text.length*5);
	};

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

	if(this.created == true)
	{
		this.dropdown.appendChild(itm.create());
		itm.obj.style.width = (this.dropdownWidth-10) + 'px';
	};
	
	return this;
};

_ptools.Select.prototype.open =  function()
{
	this.dropdown.style.display = '';
	this.dropdown.focus();
};


_ptools.Select.prototype.onmousedown =  function()
{
	var t = this;
	
	if(t.settings.onmousedown) {
		eval(t.settings.onmousedown +'(t)');
	};
};

_ptools.Select.prototype.close =  function()
{
	this.dropdown.style.display = 'none';
};

_ptools.Select.prototype.setValue =  function( val )
{
	var t = this;
	t.value = val;
	t.input.value = val;
};

_ptools.Select.prototype.setText =  function( val )
{
	this.text.innerHTML = val;
};

// @depricated
_ptools.Select.prototype.removeAllItems = function()
{
	this.clear();
};

_ptools.Select.prototype.clear = function()
{
	delete this.items;
	this.items = new Array();
	this.dropdown.innerHTML = '';
};

_ptools.Select.prototype.selectItemByValue = function( val )
{
	for(itm in this.items)
	{
		if(this.items[itm].value == val)
		{
			this.items[itm].onclick();
			break;
		};
	};
};

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

// settings.click
// settings.text
// settings.value
_ptools.Option = function( settings )
{
	var t = this;
	t.settings = settings;

	t.obj = null;
	t.value = t.settings.value;
	t.text = t.settings.text;
	t.parent = null;

	t.active = false;
	
	// Attribute
	t.setAttribute = function( name, value )
	{
		t.settings[ name ] = value;
	};
	
	t.getAttribute = function( name )
	{
		if( t.settings[ name ] )
		{
			return t.settings[ name ]; 
		} else
		{
			return false;
		};
	};
};

_ptools.Option.prototype.create =  function()
{
	var t = this;
	t.obj = document.createElement('div');
	t.obj.className = 'sOption';
	
	t.obj.onmouseover = function()
	{
		t.onmouseover();
	};

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

	t.obj.onmousedown = function()
	{
		t.onmousedown();
		t.onclick();
	};
	
	var oSpan = document.createElement('span');
	oSpan.innerHTML = t.text;

	if(t.settings.image)
	{
		var oImg = document.createElement('img');
			oImg.src = t.settings.image;
			oImg.className = 'sOptionImage';
		this.obj.appendChild(oImg);
	};

	t.obj.appendChild(oSpan);

	return t.obj;
};

_ptools.Option.prototype.onmouseover =  function()
{
	if(this.active == false)
	{
		this.obj.className = 'sOptionHover';
	};
};

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

_ptools.Option.prototype.onmousedown =  function()
{
	if(this.settings.onmousedown)
	{
		var self = this;
		eval(this.settings.onmousedown + '(self.parent)');
	};
};

_ptools.Option.prototype.onclick =  function()
{
	var t = this;
	
	if(t.active == false && this.parent)
	{
		t.parent.close();
		t.setActive();

		if(typeof t.settings.onclick == 'function')
		{
			t.settings.onclick(t.parent, t);
		} else if(typeof t.settings.onclick != 'undefined')
		{
			eval( t.settings.onclick + '(t.parent, t)' );
		};
	};
};

_ptools.Option.prototype.setActive = function()
{
	var t = this;
	
	t.active = true;
	t.obj.className = 'sOptionHover';

	if(t.parent && t.parent.active) {
		t.parent.active.setNormal();
	};

	if(t.parent)
	{
		t.parent.setValue(t.value);
		t.parent.setText(t.text);
		
		t.parent.active = this;
	};
};

_ptools.Option.prototype.setNormal = function()
{
	var t = this;
	
	t.active = false;
	t.obj.className = 'sOption';
	
	t.parent.setValue('');
	t.parent.setText('');
};
