/*Definition of NCMAbstractComponent class*/
function ncmAbstractComponent(id,props) {
    this.m_id = id;
    this.m_namePattern = "ncmObject";                        // Suppose that name of every object is construct as 'ncmObject<fx:id/>'                                            // ID of component
    this.m_type = props.type?props.type:"NO_TYPE";            // Type of component. 
    this.m_template = props.tpl?props.tpl:"NO_TEMPLATE";    // NCM template of component
    this.m_parObjId = props.parObjId?props.parObjId:-1;        // ID of parent NCM component
    this.m_numSteps = props.steps?props.steps.length:0;        // Number of steps for component
    this.isPublic = props.isPublic?props.isPublic:true;        // Is this component for public view?

    this.m_passpar = "";                                    //External parameters for component
    this.m_histsteps = new Array(10);                        //Array for save last 10 steps of component (steps history)
    this.m_isDirty = false;                                    //Has changed the component
    this.isLogged = false;                                    //Is user logged into component
    
    //Initialize component steps
    this.m_steps = new Array(this.m_numSteps);                // Array of steps for component
    this.m_stepparams = new Array(this.m_numSteps);            // External parameters for steps;
    for(var i=0;i<this.m_numSteps;i++) {
        this.m_steps[i] = props.steps[i];
        this.m_stepparams[i] = "";
    }
}

ncmAbstractComponent.prototype = {
    //-------Public read-only properties-------------
    /**
     * @author Nivaria Development Team
     *
      * Returns id of the component.
      * @return int
     */
    getId : function() {return this.m_id;},

    /**
     * @author Nivaria Development Team
     *
      * Returns type of the component.
      * @return String
     */
    getType : function() {return this.m_type;},

    getTemplate: function() {return this.m_template;},
    
    getIsLogged: function() {return this.isLogged;},

    getParentObjId: function() {return this.m_parObjId},
    
    getStep: function(index) {
        if(index>=0 && index<this.m_steps.length)
   	   return this.m_steps[index];
        else
   	   return null;   
    },

    //-------Public methods-------------
    setProgress: function(bval,elemId) {
        bval?$jq("#progress"+this.m_id).show():$jq("#progress"+this.m_id).hide();
        bval?$jq("#"+elemId).hide():$jq("#"+elemId).show();
    },
    
    showStep: function(index) {
        if(this.m_type=="BLOG" && index==8) this.isLogged = false;
        if(this.m_type=="BLOG" && this.m_histsteps[this.m_histsteps.length-1]==8 && !this.isLogged && !this.isPublic) return;
        for(var i=0;i<this.m_numSteps;i++) {
            if($jq("#obj"+this.m_id+"_step"+i).get(0)!=null) {
                if(i==index) {
                    $jq("#obj"+this.m_id+"_step"+i).show();
                    this.m_histsteps.shift();
                    this.m_histsteps[this.m_histsteps.length] = index;
                } else
                    $jq("#obj"+this.m_id+"_step"+i).hide();    
            }
        }        
    },
    
    runStep: function(index) {
        if(this.m_type=="BLOG" && this.m_histsteps[this.m_histsteps.length-1]==8 && !this.isLogged && !this.isPublic) return;
        var objId = this.getStep(index);
        if(objId!=null && objId!=-1) {
            var obj = eval(this.m_namePattern+objId);
            if(obj!=null) {
                this.showStep(index);
                obj.run();
            }    
        }        
    },
    
    run: function() {},
    
    runAll: function() {
        for(var i=0;i<this.m_numSteps;i++) {
            this.runStep(i);
        }
    },
    
    setPassPar: function(val) {this.m_passpar = val;},
    
    setStepParams: function(index,objPar) {
        if(index>=0 && index<this.m_steps.length) {
            this.m_stepparams[index] = objPar;
            var objId = this.getStep(index);
            if(objId!=null) {
                var obj = eval(this.m_namePattern+objId);
                if(obj!=null) {
                    if(objPar instanceof Array) {
                        for(var i=0;i<objPar.length;i++) {
                            obj.setStepParams(i,objPar[i]);
                        }            
                    } else {
                        obj.setStepParams(0,objPar);
                    }    
                }    
            }
        } else {
            if(objPar instanceof Array)
                this.setPassPar(objPar[0]);
            else
                this.setPassPar(objPar);
        }
    },
    
    getPreviousStep: function(depth) {
        var res = 0;
        try {
            res = this.m_histsteps[this.m_histsteps.length-1-depth];
        } catch(mErr) {
        }
        return res;
    },
    
    setDirty: function(bval) {
        this.m_isDirty = bval;
    },
    
    getDirty: function() {
        return this.m_isDirty;
    },
    
    activate: function(prdId) {},
    
    setLogged: function(bval) {this.isLogged=bval;},
    
    showError: function(msg,ex) {
        var m = msg + ":\n";
        if(ex) {
            m += ex.message?ex.message+"\n":"";
            m += ex.fileName?"File: "+ex.fileName+"\n":"";
            m += ex.lineNumber?"Line: "+ex.lineNumber+"\n":"";
            m += ex.stack?"Stack: "+ex.stack+"\n":"";
        }
        alert(m);
    }
}

ncmAbstractComponent.initProperties = function(properties,hash) {
    //Setting static properties of component
    if(properties) {
        if(properties instanceof Array) {
            for(var i=0;i<properties.length;i++) {
                var property = properties[i];
                if(property.key && property.value)
                    hash.setItem(property.key,property.value);
            }
        }    
    }
}

/**
 * @author Nivaria Development Team
 * 
 * This function returns the static property of component.
 * @param key - String, key of property
 * @param hash - NCMHash, hash table of properties
*/        
ncmAbstractComponent.getProperty = function(key,hash) {
    var res = "";
    if(hash.hasItem(key)) {
        res = hash.getItem(key);
        res = NCMTextUtil.replaceBadChars(res);
    }    
    return res;    
}
/*End definition of NCMComponent class*/
