Ext.define('RandomSettingClass', {
    constructor: function(cfgXML, parentRandomNode){
        this.RandomNodeRef = parentRandomNode;
        this.RandomListCount = XmlHelper.getIntegerAttribute(cfgXML, 'listCount');
        this.RandomLists = new Array();
        var lists = cfgXML.getElementsByTagName('randomList');
        for (var i = 0; i < lists.length; i++) {
            this.RandomLists.push(new RandomListClass(lists[i]));
        }
    },
    toString: function(){
        var str = "";
        for (var i = 0; i < this.RandomLists.length; i++) {
            str += this.RandomLists[i] + "\n";
        }
        return str;
    },
    getRandomizedElementsIDArray: function(eID){
        var ramdomized = new Array();
        for (var i = 0; i < this.RandomLists.length; i++) {
            ramdomized = ramdomized.concat(this.RandomLists[i].getRandomizedElements(eID));
        }
        return ramdomized;
    }
});
Ext.define('RandomListClass', {
    constructor: function(cfgXML){
        this.TotalElements = XmlHelper.getIntegerAttribute(cfgXML, 'totalElements');
        this.PickElementCount = XmlHelper.getIntegerAttribute(cfgXML, 'pickElementCount');
        this.IsRandom = XmlHelper.getBooleanAttribute(cfgXML, 'isRandom');
        var elementIDs = XmlHelper.getStringAttribute(cfgXML, 'elementIDs');
        this.ElementIDArray = elementIDs.split('|');
    },
    containsElementID: function(eID){
        return Ext.Array.indexOf(this.ElementIDArray,eID);
    },
    getRandomizedElements: function(eID){
        if (this.IsRandom) {
            var tmpArray = new Array(), resultArray = new Array();
            tmpArray = tmpArray.concat(this.ElementIDArray);
            if (this.containsElementID(eID)) {
                resultArray.push(eID);
               Ext.Array.remove(tmpArray,eID);
            }
            for (var i = 0; i < this.PickElementCount && i < this.TotalElements; i++) {
                var index = Math.floor(Math.random() * tmpArray.length);
                resultArray.push(tmpArray.splice(index, 1));
            }
            return resultArray;
        } else {
            return this.ElementIDArray;
        }
    },
    toString: function(){
        return Ext.String.format("Total: {0}, Pick: {1}, IsRandom: {2}", this.TotalElements, this.PickElementCount, this.IsRandom);
    }
});
