﻿WUNDER.registerNameSpace("WUNDER.DATA");


// Define DataObjectManager Constructor ---------------------------------------
WUNDER.DATA.DataObjectCollection = function(oScope)
{
    var that = this;
    this.children = [];
    var __childrenById = {};
    
    this.add = function(sId) {
        var sId = sId || "child" + this.children.length;
        var DONew = new WUNDER.DATA.DataObject();
        DONew.parent = this;
        this.children.push(DONew);
        __childrenById[sId] = DONew;
        return DONew;
    };
    
    this.getChildById = function(sId) {
        var oChild = __childrenById[sId];
        var sChildTypeof = typeof(oChild);
        if (sChildTypeof == "undefined") {
            return null;
        }
        return oChild;
    }
    
    this.trace = function() {
        trace("BEGIN DataObjectCollection trace");
        for (var i=0; i<this.children.length; i++) {
            trace("--> item #" + i + ": ");
            this.children[i].trace();
        }
        trace("END DataObjectCollection trace");
    };
    
    this.clearChildren = function() {
        this.children = [];
        __childrenById = {};
    }
    
    this.toString = function(lineBreak) {
        var lineBreak = lineBreak || "";
        var sOutput = "[ "+ lineBreak;
        for (var i=0; i<this.children.length; i++) {
            sOutput += this.children[i].toString();
            if (i+1!=this.children.length) {sOutput += ", ";}
            sOutput += lineBreak;
        }
        return sOutput + " ]" + lineBreak;
    }
}


// DataObject Constructor ------------------------------
WUNDER.DATA.DataObject = function()
{
    var that = this;
    this.aPropertyList = [];
    
    this.set = function(sProperty, value)
    {
        if (typeof(this[sProperty]) == "undefined") {
            this.aPropertyList.push(sProperty);
        }
        this[sProperty] = value;
        return that;
    };
    
    this.get = function(sProperty)
    {
        return this[sProperty];
    };
    
    this.trace = function()
    {
        var nPropCount = this.aPropertyList.length;
        
        for (var i=0; i<nPropCount; i++) {
            var propValue = this[this.aPropertyList[i]];
            trace("----> " + this.aPropertyList[i] + " = "
                + typeof(propValue) + ": " + propValue);
        }
    };
    
    this.toString = function(lineBreak) {
        var lineBreak = lineBreak || "";
        var sOutput = lineBreak + "{ ";
        for (var i=0; i<this.aPropertyList.length; i++) {
            sOutput += this.aPropertyList[i] + ": '" + this[this.aPropertyList[i]] + "'";
            if (i+1!=this.aPropertyList.length) {sOutput += ", ";}
            sOutput += lineBreak;
        }
        return sOutput + "}" + lineBreak;
    }

}

WUNDER.DATA.ObjectArrayToDOC = function(aObjectArray, sIndex, sValue)
{
    // Create DataObject
    var oDOC = new WUNDER.DATA.DataObjectCollection();
    
    var aCollection = [];
    
    for (var j=0; j<aObjectArray.length; j++) {
        var oCurrent = aObjectArray[j];
        
        var oOD = oDOC.add();
        oOD.value("Index", oCurrent[sIndex]);
        oOD.value("Value", oCurrent[sValue]);
        
        aCollection.push(oOD);
    }

    return oDOC;
}

WUNDER.DATA.Pointer = function(value) {
    var __value = value;
    this.toString = function() {return __value};
    this.setValue = function(value) {__value = value};
};