/*
**    File: fw_data.js
**    Created by: Zolotarev Anton (http://sourceforge.net/projects/ajaxos)
**
**    License Information:
**    -------------------------------------------------------------------------
**    Copyright (C) 2005 Zolotarev Anton
**
**    This program is free software; you can redistribute it and/or modify it
**    under the terms of the GNU General Public License as published by the
**    Free Software Foundation; either version 2 of the License, or (at your
**    option) any later version.
**    
**    This program is distributed in the hope that it will be useful, but
**    WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
**    General Public License for more details.
**    
**    You should have received a copy of the GNU General Public License along
**    with this program; if not, write to the Free Software Foundation, Inc.,
**    59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

if(!fw.data)
fw.data = function(){
    var out = {};
    for(var i in fw.data) out[i] = (typeof fw.data[i]);
    out['version'] = "v0.31";
    return out;
}

fw.data.isObj = function(el){ return ((typeof el == 'object')&&(el !== null)); }

fw.data.isStr = function(el){ return ((typeof el == 'string')&&(el !== null)); }

fw.data.isBool = function(el){ return ((typeof el == 'boolean')&&(el !== null)); }

fw.data.isNum = function(el){ return ((typeof el == 'number')&&(el !== null)); }

fw.data.isFun = function(el){ return ((typeof el == 'function')&&(el !== null)); }

fw.data.isUnd = function(el){ return ((typeof el == 'undefined')&&(el !== null)); }

fw.data.isSet = function(el){ return ((typeof el != 'undefined')&&(el !== null)); }

fw.data.isArr = function(el){
    try{
        return (this.isObj(el) && this.isSet(el.length) && el.push && el.pop);
    }catch(e){ return false; }
}

fw.data.isNode = function(el){
    try{
        return (this.isObj(el) && !this.isUnd(el.parentNode));
    }catch(e){ return false; }
}


fw.data.isType = function(el, type){
	
    return (this.type(el) == type);
}

fw.data.isError = function(el){
    return this.isType(el, 'ERROR');
}

fw.data.isCall = function(el){
    try{
        return eval(el)? true : false;
    }catch(e){ return false; }
}

fw.data.isCnv = function(el){
	try{
        return (this.isObj(el) && el.id && this.isObj(el.ch) && this.isObj(el.us) && this.isObj(el.dp));
    }catch(e){ return false; }
}

fw.data.escape = function(s){
    return escape(s).replace(new RegExp('\\+','g'), '%2B');
}

fw.data.sizeof = function(s){
    var sz = 0;
    if(this.isObj(s)){
        if(this.isSet(s.length)) sz = s.length;
        else for(var i in s){
            if(i != sz) return null;
            sz++;
        }
    }else return null;
    return sz;
}

fw.data.argm = function(argum,shift){
    var arg = [];
    for(var i=shift;i < argum.length;i++) arg.push(argum[i]);
    return arg;
}

fw.data.funFlat = function(fun,arg){
    var out = '';
    for(var i in arg){
        if(out) out += ',';
        out += this.flat(arg[i]);
    }
    return (fun + '(' + out + ')');
}

fw.data.flat = function(content){
    var prefix = "";
    var query = [];
    if(content instanceof Object){
	prefix = "{";
        for(var k in content){
            var v = content[k];
	    if(prefix != "{") prefix += ",";
	    prefix += k + ':';
            if(v instanceof Object) prefix += this.flat(v);
            else{
		if(this.isNum(v)) prefix += v;
		if(this.isStr(v)) prefix += "'" + v + "'";
		if(this.isBool(v)) prefix += (v)? 'true' : 'false';
	    }
        }
	prefix += "}";
    }else{
	if(this.isNum(v)) prefix = content;
	if(this.isStr(v)) prefix = "'" + content + "'";
	if(this.isBool(v)) prefix = (content)? 'true' : 'false';
    }
    return prefix;
}

fw.data.flat_2 = function(content){
		
		if(!content)return false;
		
    var prefix = "";
    var query = [];
    if(content instanceof Object){
	prefix = "{";
        for(var k in content)
        {
        	     	
        		var v = content[k];
        		if(v!=null)
        		{
						  if(prefix != "{") prefix += ",";
						  prefix += k + ':';
						  if(v instanceof Object) prefix += this.flat_2(v);
						  else{
									if(this.isNum(v)) prefix += v;
									if(this.isStr(v)) prefix += "'" + v + "'";
									if(this.isBool(v)) prefix += (v)? 'true' : 'false';
								  }
						}
				}  
        
	prefix += "}";
    }else{
	if(this.isNum(v)) prefix = content;
	if(this.isStr(v)) prefix = "'" + content + "'";
	if(this.isBool(v)) prefix = (content)? 'true' : 'false';
    }
    return prefix;
}

fw.data.plane = function(content, esc, prefix){
    if(prefix == null) prefix = "";
    var query = [];
    if(content instanceof Object){
        for(var k in content){
            var v = content[k];
            
            var curPrefix = prefix? prefix+'['+(esc? this.escape(k) : k)+']' : (esc? this.escape(k) : k);
            if(v instanceof Object) query[query.length] = this.plane(v, esc, curPrefix);
            else query[query.length] = curPrefix + "=" + (esc? this.escape(v) : v);
        }
    }else{
        query = [content];
    }
    return query.join('&');
}

fw.data.json2xml = function(content,prefix){
	var out = '';
	if(this.isArr(content)){
		for(var i=0;i < content.length;i++){
			if(this.isArr(content[i])){
				out += this.json2xml(content[i],i);
			}else if(this.isObj(content[i])){
				out += '<'+prefix+this._xmlattr(content[i])+' index="'+i+'"'+'>'+this.json2xml(content[i])+'</'+prefix+'>';
			}else if(this.isSet(content[i])){
				out += '<'+prefix+' index="'+i+'"'+'>'+content[i]+'</'+prefix+'>';
			}
		}
	}else if(this.isObj(content)){
		if(prefix) out += '<'+prefix+'>';
		for(var i in content){
			if(this.isArr(content[i])){
				out += this.json2xml(content[i],i);
			}else if(this.isObj(content[i])){
				out += '<'+i+this._xmlattr(content[i])+'>'+this.json2xml(content[i])+'</'+i+'>';
			}else if(this.isSet(content[i])){
				out += '<'+i+'>'+content[i]+'</'+i+'>';
			}
		}
		if(prefix) out += '</'+prefix+'>';
	}else out += '<'+prefix+'>'+content+'</'+prefix+'>';
	return out;
}

fw.data._xmlattr = function(content){
	var out = '';
	if(this.isObj(content)){
		for(var i in content){
			if(i.indexOf("@") == 0){
				out += ' '+i.slice(1)+'="'+content[i].replace(/&/g, '&amp;').replace(/"/g, '&quot;')+'"';
				content[i] = null;
			}
		}
	}	
	return out;
}

fw.data.merge = function(out){
    if(!this.isObj(out)) return out;
    var arg = this.argm(arguments,1);
    
    for(var el in arg){
        if(this.isArr(arg[el])){
            out = this.combine(out,arg[el]);
        }else if(this.isObj(arg[el])){
            for(var i in arg[el]){
                if(this.isObj(out[i])) out[i] = this.merge(out[i],arg[el][i]);
                else{
                    if(this.isObj(arg[el][i])){
                        var o = this._path(out,[i],true);
                        out[i] = this.merge(out[i],arg[el][i]);
                    }else out[i] = arg[el][i];
                }
            }
        }
    }
    return out;
}

fw.data.combine = function(){
    var res = [];
    
    for(var i=0; i < arguments.length; i++){
        if(this.isObj(arguments[i])){
            if(this.isArr(arguments[i])){
                var sz = arguments[i].length;
                for(var n=0;n < sz;n++) res.push(arguments[i][n]);
            }else for(var n in arguments[i]) res.push(arguments[i][n]);
        }else res.push(arguments[i]);
    }
    return res;
}

fw.data.unlink = function(out){
    if(!this.isObj(out)) return out;
    if(this.isNode(out)) return out;
    var data = {};
    for(var i in out){
        data[i] = (this.isObj(out[i]))? this.unlink(out[i]) : out[i];
    }
    return data;
}

fw.data.vacuum = function(out,type,casc){
    if(!this.isObj(out)) return out;
    if(this.isNode(out)) return out;
    var empty = true;
    if(this.isArr(out)){
    	var ret = [];
    	for(var i=0;i < out.length;i++) if(out[i] !== null){
	        empty = false;
	        var o = (this.isObj(out[i]) && casc)? this.vacuum(out[i],type,casc) : out[i];
	        if(fw.data.isSet(o)) ret.push(o);
	    }
    }else{
    	var ret = {};
    	for(var i in out) if(out[i] !== null){
	        empty = false;
	        var o = (this.isObj(out[i]) && casc)? this.vacuum(out[i],type,casc) : out[i];
	        if(fw.data.isSet(o)) ret[i] = o;
	    }
    }
    
    if(type && empty) ret = null;
    return ret;
}

fw.data.arr = function(pth){
    if(this.isArr(pth)) return pth;
    if(this.isStr(pth)){
        pth = pth.split('/');
        if(pth.length && (pth[pth.length-1] === '')) pth.pop();
        return pth;
    }
    return null;
}

fw.data.pathF = function(el,pth){
    pth = this.arr(pth);
    if(!pth) return null;
    return this._path(el, pth, false);
}

fw.data.pathN = function(el){
    return this._path(el, this.argm(arguments,1), false);
}

fw.data.pathCF = function(el,pth){
    pth = this.arr(pth);
    if(!pth) return null;
    return this._path(el, pth, true);
}

fw.data.pathCN = function(el){
    return this._path(el, this.argm(arguments,1), true);
}

fw.data._path = function(el,pth,crt,tmpl){
    if(!this.isObj(el) || !this.isObj(pth)) return null;
    if(!this.isObj(tmpl)) tmpl = null;
    
    if(this.isArr(pth)){
        for(var i=0; i < pth.length; i++){
            if(!this.isSet(pth[i])) break;
            el = this.__path(el, pth[i], crt, tmpl);
            if(el === null) break;
        }
    }else{
        for(var i in arg){
            if(!this.isSet(pth[i])) break;
            el = this.__path(el, pth[i], crt, tmpl);
            if(el === null) break;
        }
    }
    return el;
}

fw.data.__path = function(el,pth,crt,tmpl){
    if(this.isObj(el)){
        if(!this.isUnd(el[pth])) el = el[pth];
        else{
            if(crt){
		el[pth] = {};
		el = el[pth];
		if(tmpl) this.merge( el, tmpl);
            }else return null;
        }
    }else return null
    
    return el;
}

fw.data.type = function(el){
    if(this.isStr(el)){
        var s = el.split('_');
        if(!this.isUnd(s[0])) return s[0];
    }
    return null;
}

fw.data.error = function(s,desc){
    if(!desc) desc = '';
    return ('ERROR_' + s + '_' + desc);
}

fw.data.errorDscr = function(el){
    var res = '';
    if(!this.isStr(el)) return res;
    if(!this.isError(el)) return res;
    var s = el.split('_');
    switch(s[1]){
        case '1':
            res = 'Processing Data is Incorrect';
        break;
        
        case '2':
            res = 'Wrong Type';
        break;
        
        case '3':
            res = 'Wrong Resource';
        break;
        
        case '4':
            res = 'Create Resource Error';
        break;
        
        case '5':
            res = 'Already declared';
        break;
        
        case '6':
            res = 'Undeclared';
        break;
        
        case '7':
            res = 'Data Locked';
        break;
        
        case '8':
            res = 'Internal Error';
        break;
        
        default:
            res = 'Unknown';
    }
    if(this.isSet(s[2])) res += ' : '+s[2];
    return res;
}

fw.data._ = function(str, lng){
    lng = (lng)? lng : this.pathN(fw._data,'config','lng');
    if(!lng) lng = 'en';
    var res = this.pathN(fw._data,'lang',str,lng);
    return (res)? res : str;
}

fw.data.trim = function(str)
{
	return str.replace(/^\s+|\s+$/g,"");
}

fw.data.trim_ap = function(str)
{
	return str.replace(/'/g,'').replace(/{/g,'').replace(/}/g,'');
}

fw.data.trim_space = function(str)
{
	return str.replace(/ /g,'');
}
fw.data.random = function()
{
	return ''+Math.random()*Math.random()*Math.random()+'';
}

fw.data();