/*
 * JavaScript ArrayCollection
 * version 0.1
 *
 * Copyright (c) 2009 Rafael Lucio (poste9 [at] gmail [dot] com[nospam])
 * JavaScript Array Collection is licensed under the LGPL license.
 *
 * Visit www.postenove.com.br for more information.
 * @dependence: utmjs
 * download at: http://utmproject.org
 */

ArrayCollection = new utm.Class({
	/**
	* Construtor da classe
	* @param array data
	**/
	__construct:function(data) {
		this.data = (data) ? data : new Array();
		this.length = 0;
	},
	
	/**
	*Atribue o arrayCollection como data provider de um select de forma que o que for alterado no array será alterado no select
	* @name provideSelect
	*@param utmobject select
	**/
	provideSelect:function(select) {	
		this.select = select;
		this.refreshProvider();
	},
	
	/**
	*Função de atalho ao Array.pslice
	*@name pslice
	*@param int position
	*@param int length
	*@param array | object newItem 
	**/
	splice:function(position,length,newItem) {
		if (newItem) this.data.splice(position,length,newItem);
		else this.data.splice(position,length);
		this.refreshProvider();
	},
	
	/**
	*Função de atalho ao Array.slice
	*@name slice
	*@param int position
	*@param int length
	*@param array | object newItem 
	**/
	slice:function(position,length,newItem) {
		if (newItem) this.data.slice(position,length,newItem);
		else this.data.slice(position,length);
		this.refreshProvider();
	},
	
	/**
	*Limpa os dados tanto do array como do objeto
	*@name clear
	**/
	clear:function(){ this.data = new Array(); this.refreshProvider(); },
	
	/**
	*Remove um determinado item no array
	*@name removeItemAt
	*@param int position
	**/
	removeItemAt:function(position) { this.data.slice(position,1); this.refreshProvider(); },
	
	/**
	*Função de atalho ao Array.push
	*@name push
	*@param * value 
	**/
	push:function(value) { this.data.push(value); this.refreshProvider(); },
	
	/**
	*Atualiza os dados no objeto com os que tem no array
	*@name refreshProvider
	**/
	refreshProvider:function() {
		this.length = this.data.length;
		if (this.select) {
			this.select.empty();
			for(var i=-1;this.data[++i];) {
				var selected = (this.data[i].selected) ? true : false,
					option   = new Option(this.data[i].label,this.data[i].value);
				this.select[0].options[this.select[0].options.length] = option;
			}
		}
	}
});