Web.Components.Selects = function () {
	this.Objetos 	= Web.Library.Instances.create('Objects');
	this.Documents 	= Web.Components.Instances.create('Documents');
	this.fl_retorno = 1;
	this.OptGroups  = null;
	this.str_name   = "";
	this.obj_events = {};
	
	this.create = function (strObj,criarEm) {
		var objObj, criar, nome, Events;
		nome     = this.Objetos.getNonEmpty(this.getName(), strObj);
		objObj = this.Objetos.getObj(strObj, 'SELECT');
		criar    = (typeof objObj == 'string') ? true : false;
		criarEm  = (criarEm != null && typeof this.Objetos.getObj(criarEm) == 'object') ? this.Objetos.getObj(criarEm) : criarEm;
		criarEm  = (criarEm != null) ? criarEm : this.Objetos.getBody();
		Events   = this.getEvents();

		if (criar) {
			objObj = this.Documents.newSelect(strObj, null, null);
			objObj.setAttribute('id', strObj);
			objObj.setAttribute('name', nome);

			criarEm.appendChild(objObj);
		}
		
		if (typeof Events == 'object') {
			for (x in Events) {
				if (Events[x] == '______array' || Events[x] == '') continue;
				this.Objetos.register(objObj, Events[x][0], Events[x][1]);
			}
		}
		
		objObj['objObj'] = this;
		
		this.setInput(objObj);
		this.setInputID(strObj);
		this.setCreateAT(criarEm);
		
		return (this.getRetorno() == 2) ? this : objObj;
	}
	
	this.AddOptGroup = function (strName) {
		if (strName == null) return true;

		var Obj = this.Documents.newSelectGroup(null, {'label':strName});
		this.getInput().appendChild(Obj);
	}
	
	this.RemOptGroup = function (strName) {
		var Opts = this.getInput().getElementsByTagName("OPTGROUP");
		for (var i = 0; i < Opts.length; i++) {
			if (Opts[i].getAttribute('label') == strName) {
				this.getInput().removeChild(Opts[i]);
				break;
			}
		}
	}
	
	this.Fill = function (objArray) {
		for (x in objArray) {
			if (objArray[x] == '______array') continue;
			this.Add(objArray[x], x);
		}
	}
	
	this.unSelect = function () {
		var Options = this.getInput().options;
		for (var i = 0; i < Options.length; i++) {
			this.getInput().options[i].selected = false;
		}
	}
	
	this.Select = function (Indice) {
		var Options = this.getInput().options;
		this.unSelect();
		
		
		if (typeof Indice == 'string') {
			if (typeof this.getIndex(Indice) == 'number') {
				this.getInput().options[this.getIndex(Indice)].selected = true;
			}
		} else {
			for (x in Indice) {
				if (Indice[x] == '______array' || x == 'isArray' || Indice[x] == '') continue;
				
				if (typeof this.getIndex(Indice[x]) == 'number') {
					this.getInput().options[this.getIndex(Indice[x])].selected = true;
				}
			}
		}
	}
	
	this.getIndex = function(valor) {
		var objObj  = this.getInput();
		var Options = objObj.options;
		
		for (var i = 0; i < Options.length; i++) {
			if (Options[i].value == valor) {
				return i;
			}
		}
		
		return false;
	}
	
	this.RemoveAll = function () {
		var objSelect = this.getInput();
		var Options   = objSelect.options;
		
		for (var i = 0; i < Options.length; i++) {
			Options[x] = null;
		}
	}
	
	this.Add = function (texto, valor) {
		var objSelect = this.getInput();
		var objOptGps = this.getOptGroups();
		var last      = Math.abs(objSelect.options.length);
		valor = (valor != null) ? valor : last;

		for (x in objOptGps) {
			if (objOptGps[x] == '______array') continue;
			if (x == last) {
				this.AddOptGroup(objOptGps[x]);
			}
		}
		
		objSelect.options[last] = new Option(texto, valor);
		objSelect.options[last].selected = false;
	}
	
	this.Remove = function (indice) {
		var objSelect = this.getInput();
		var Options   = objSelect.options;
		
		indice = (indice != null) ? indice : Math.abs(Options.length-1);
		for (var x = 0; x < Options.length; x++) {
			if (Options[x].value != indice) continue;
			
			Options[x] = null;
			return true;
		}
	}
	
	this.AddEvent = function (strEvent, strFunc) {
		this.Objetos.register(this.getInput(), strEvent, strFunc);
	}
	
	this.RemoveEvent = function (strEvent, strFunc) {
		this.Objetos.unregister(this.getInput(), strEvent, strFunc);
	}
	
	this.setAttributes = function (argumentos) {
		this.Objetos.setAttributes(this.getInput(), argumentos);
	}
	
	this.getObjObj = function () { return this.obj_input; }
	this.setObjObj = function (valor) { this.obj_input = valor; }
	
	this.getName = function () { return this.str_name; }
	this.setName = function (valor) { this.str_name = valor; }
	
	this.getInput = function () { return this.obj_input; }
	this.setInput = function (valor) { this.obj_input = valor; }
	
	this.getInputID = function () { return this.input_id; }
	this.setInputID = function (valor) { this.input_id = valor; }
	
	this.getCreateAT = function () { return this.create_at; }
	this.setCreateAT = function (valor) { this.create_at = valor; }

	this.getRetorno = function () { return this.fl_retorno; }
	this.setRetorno = function (valor) { this.fl_retorno = valor; }
	
	this.setOptGroups = function (valor) { this.OptGroups = valor; }
	this.getOptGroups = function (valor) { return this.OptGroups; }
	
	this.getEvents = function () { return this.obj_events; }
	this.setEvents = function (valor) { this.obj_events = valor; }
}
