/**
 * Estrutura inicial de STORAGE
 * Deve ser complementada com a estrutura de STORAGE no cliente
 * @constructor
 * @version ALPHA 0.1.0.0
 */
compono.Storage = new compono.Object("Storage");


/**
 * compono.Storage.COOKIE = "cookie";
 * compono.Storage.HIDDEN = "hidden";
 * compono.Storage.CLIENT = "client";
 * compono.Storage.FLASH = "flash"; 
 */


/**
 * STORAGE via COOKIE
 */
compono.Storage.Cookie = new compono.Object("Cookie");
/**
 * @param {String} prop Propriedade
 * @see http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:RegExp:exec;
 */
compono.Storage.Cookie.get = function(prop){
	var r;
	try{
		if(prop == undefined){
			return document.cookie.split(";");
		} 
		var re = new RegExp(prop + "=([^;]+)","i");
		var rg = re.exec(document.cookie);
		return rg[1] || null;
	}catch(e){}
	return false;
}		
/**
 * Setar propriedade/valor no cookie
 * @param {String} prop Propriedade
 * @param {String} value Valor
 * @param {String} days Data da expiracao do cookie
 * @param {String} path
 */
compono.Storage.Cookie.set = function(prop, value, days, path){
	var expires = days ? " expires="+(new Date().setTime(new Date().getTime()+(days*24*60*60*1000)).toGMTString()) : "";
	path = path ? " path="+path : "";
	document.cookie=prop+"="+value+";"+expires+";"+path;
}
/** @ignore */
compono.Storage.Cookie.IDRegExp = /([^&=]+)=([^&]*)/gim;
/**
 * @param {Object} id
 */
compono.Storage.Cookie.ID = function(id){
	var o, s, v;	
	this[id] = new this.Object();
	this[id].id = id;
	s = this.get(id);
	if(s){
		while((v = this.IDRegExp.exec(s))){
			this[id].values[	v[1]	] = v[2];			
		}
	}
	return this[id];
}
compono.Storage.Cookie.Object = function(){};
/** @ignore */
compono.Storage.Cookie.Object.prototype.parent = compono.Storage.Cookie;
compono.Storage.Cookie.Object.prototype.values = {};
/**
 * @param {Object} prop
 */
compono.Storage.Cookie.Object.prototype.get = function(prop){
	return this.values[prop];
}
/**
 * @param {Object} prop
 * @param {Object} value
 */
compono.Storage.Cookie.Object.prototype.set = function(prop, value){
	this.values[prop] = value;
	return this.write();
}
compono.Storage.Cookie.Object.prototype.remove = function(prop){
	delete this.values[prop];
	return this.write();
}
compono.Storage.Cookie.Object.prototype.write = function(){
	var s="",i=0;
	for(p in this.values){
		s += (i?"&":"") + p + "=" + this.values[p];
		i++;
	};
	this.parent.set(this.id, s, null, "/");
	return true;
}




/**
 * STORAGE via HIDDEN
 */
compono.Storage.Hidden = new compono.Object("Hidden");

/** @ignore */
compono.Storage.Hidden.IDRegExp = /([^&=]+)=([^&]*)/gim;
/**
 * @param {Object} id
 */
compono.Storage.Hidden.ID = function(id,form){
	var o, s, v;
	if((o = this.getByID(id))) id = o.getAttribute("id");
	this[id] = new this.Object();
	if(!o){
		this[id].input = document.forms[form||0].appendChild(document.createElement("input"));
		this[id].input.type = "hidden";
		this[id].input.name = id;
		this[id].input.id = id;
	}else{
		this[id].input = o;
		this[id].id = this[id].input.getAttribute("id");
		if((s = this[id].input.value)){
			while((v = this.IDRegExp.exec(s))){
				this[id].values[ v[1] ] = v[2];			
			}
		}
	}
	return this[id];
}
compono.Storage.Hidden.Object = function(){};
/** @ignore */
compono.Storage.Hidden.Object.prototype.parent = compono.Storage.Hidden;
compono.Storage.Hidden.Object.prototype.values = {};
compono.Storage.Hidden.Object.prototype.get = function(prop){
	return this.values[prop];
}
compono.Storage.Hidden.Object.prototype.set = function(prop, value){
	this.values[prop] = value;
	return this.write();
}
compono.Storage.Hidden.Object.prototype.remove = function(prop){
	delete this.values[prop];
	return this.write();
}
compono.Storage.Hidden.Object.prototype.write = function(){
	var s="",i=0;
	for(p in this.values){
		s += (i?"&":"") + p + "=" + this.values[p];
		i++;
	};
	this.input.value = s;
	return true;
}



