/*
* Storage object
* need obj2str-src.inc.js, cookies-src.inc.js
*
* ver 0.9
*
*	History:
*		0.9	01.03.09/goshi	bugfix saving cookie date
*		0.82	13.02.09/goshi	IE5-7 nowonly on cookie technology 
*		0.8	11.02.09/goshi	remove bug with IE5-7 when userData can't load w/o DOMReady 
*		0.7	11.02.09/goshi	added user data
*/


/* prepare storage */
function oStorage(){
	this._init();
}

oStorage.prototype = {

	// storage object uses DOM:Storage, userData or cookies (if enabled)
	_mode: '',
	// name of the storage - in most cases - domain, or subdomain name
	_storageName: '',
	// storage element for IE
	_storageElem: null,
	// flag for blocking storage using
	initialized: false,
	
	_init: function(){
	
		this._storageName = window.location.hostname;
		if (this._storageName == '')
			this._storageName = 'local';
		
		// prepare storage element for IE5-7
		this._storageElem = elem('span', {'id': 'storageElement'}, {'width' : '1px', 'height' : '1px', 'font-size' : '1px'}, '');
		
		/* getting type of the storage for use
		* is DOM:Storage
		* see http://www.whatwg.org/specs/web-apps/current-work/#scs-client-side
		* Working on Firefox 3+, Webkit 3.1.2+ nightlies, and IE8
		* But last Safari uses Database Storage, which not implemented yet
		* 5MB
		*/
		if (window.globalStorage){
			this._mode = 'DOM';
			this.initialized = true;
		} else if (false /*this._storageElem.addBehavior*/) {
		/* 	Not use now - because allow only for same host,directory,protocol :(((
		*	for userData capacity
		*	Security Zone	Document Limit (KB)	Domain Limit (KB)
		*	Local Machine	128			1024
		*	Intranet	512			10240
		*	Trusted Sites	128			1024
		*	Internet	128			1024
		*	Restricted	64			640
		*/ 
			athis = this;
			// upload data after all ready
			domReady(
				function() {
					athis._storageElem = document.appendChild(athis._storageElem);
					athis._storageElem.addBehavior("#default#userData")
					// loading namespace
        				athis._storageElem.load(athis._storageName);
        				athis.initialized = true;
				}
			);
			    			
			this._mode = 'userData';
		} else {
			this._mode = 'cookie';
			this.initialized = true;
		}
		
		// free memory
		if (this._mode != 'userData'){
			delete this._storageElem;
			this._storageElem = null;
		}
		
		// alert(this._mode);
	
	},
	
	/* function store object in storage */
	_storeObject: function(name, data, dtype){
	
		// converting to string
		var tmp_str = (dtype == 'array') ? array2String(data) : object2String(data);
		
		if (this._mode == 'DOM'){
			
			globalStorage[this._storageName][name] = tmp_str;
			
		} else if (this._mode == 'userData'){
		
			this._storageElem.setAttribute(name, tmp_str);
			this._storageElem.save(this._storageName);
			
		} else {
		
			var Expires = new Date();
			Expires.setTime(Expires.getTime() + max_cookie_life);

			if (dtype == 'array')
				setCookieArr(name, data, Expires.toGMTString(), '/', false, false, true);
			else
				setCookieArr(name, data, Expires.toGMTString(), '/', false, false, false);
		}
	
	},
	
	
	/* function restore object from storage */
	_getObject: function(name, dtype){
	
		if (this._mode == 'DOM'){
			
			var tmp = String(globalStorage[this._storageName][name]);
			if (tmp && tmp.length > 0){

				if (dtype == 'array')
					return string2Array(tmp);
				else
					return string2Object(tmp);
			}
			
		} else if (this._mode == 'userData'){
		
			if (dtype == 'array')
				return string2Array(this._storageElem.getAttribute(name));
			else
				return string2Object(this._storageElem.getAttribute(name));
					
		} else {
		
			return getCookieArr(name, (dtype == 'array' ? true : false));
		}
	
	},
	
	
	_storeString: function(name, data){
	
		
		if (this._mode == 'DOM'){
			
			globalStorage[this._storageName][name] = data;
					
		} else if (this._mode == 'userData'){
			
			this._storageElem.setAttribute(name, data);
			this._storageElem.save(this._storageName);
			
		} else {
			var Expires = new Date();
			Expires.setTime(Expires.getTime() + max_cookie_life);
			setCookie(name, data, Expires.toGMTString(), '/');
		}
	
	},
	
	_getString: function(name){
	
		if (this._mode == 'DOM'){
			return String(globalStorage[this._storageName][name]);			
		} else if (this._mode == 'userData'){
			return this._storageElem.getAttribute(name);
		} else {
			return getCookie(name);
		}
	
	},
	
	
	/* saving data in storage 
	* name		string		name of the variable
	* data		string		saving data
	* [dtype]	string		type of the data
	*/
	set: function(name, data, dtype){
	
		// check - if storage present - it is very actual for IE, where storage created only after DOM ready
		if (!this.initialized){
			domReady(
				function() {
					portal.storage.set(name, data, dtype);
				}
			);
			
			return false;
		}
		
		if (dtype != undefined){
			switch (dtype){
			case 'array' : 
			case 'object': this._storeObject(name, data, dtype);
				break;
				
			default: this._storeString(name, data);	
			
			}
			
		} else {		
			this._storeString(name, data);		
		}

	},
	
	/* getting data from storage
		*/
	get: function(name, dtype){
	
		// check - if storage present - it is very actual for IE, where storage created only after DOM ready
		if (!this.initialized){
			domReady(
				function() {
					return portal.storage.get(name, dtype);
				}
			);
			
			return false;
		}
		
		if (dtype != undefined){
			switch (dtype){
			case 'array' : 
			case 'object': return this._getObject(name, dtype);
				break;
				
			default: return this._getString(name);	
			
			}
			
		} else {		
			return this._getString(name);		
		}
	
	},
	
	remove: function(name){
	
		// check - if storage present - it is very actual for IE, where storage created only after DOM ready
		if (!this.initialized){
			domReady(
				function() {
					portal.storage.remove(name);
				}
			);
			
			return false;
		}		
		
		if (this._mode == 'DOM'){
		
			delete globalStorage[this._storageName][name];
			
		} else if (this._mode == 'userData'){
		
			this._storageElem.removeAttribute(name);
			this._storageElem.save(this._storageName);
			
		} else {
			var Expires = new Date();
			Expires.setTime(Expires.getTime() - max_cookie_life);
			
			setCookie(name, '', Expires.getTime(), '/');
		}
	
	}

}