/*Class Hash implements functionality of Hash table*/
function Hash()
{
	this.length = 0;
	this.items = new Array();
	for (var i = 0; i < arguments.length; i += 2) {
		if (typeof(arguments[i + 1]) != 'undefined') {
			this.items[arguments[i]] = arguments[i + 1];
			this.length++;
		}
	}

	this.removeItem = function(in_key)
	{
		var tmp_value;
		if (typeof(this.items[in_key]) != 'undefined') {
			this.length--;
			var tmp_value = this.items[in_key];
			delete this.items[in_key];
		}

		return tmp_value;
	}

	this.getItem = function(in_key) {
		return this.items[in_key];
	}

	this.setItem = function(in_key, in_value)
	{
		if (typeof(in_value) != 'undefined') {
			if (typeof(this.items[in_key]) == 'undefined') {
				this.length++;
			}

			this.items[in_key] = in_value;
		}

		return in_value;
	}

	this.hasItem = function(in_key)
	{
		return typeof(this.items[in_key]) != 'undefined';
	}
}
/*End of Hash class definition*/

/*Definition of NCMComponent class*/
//This is the parent class for all NCM components
var NCMComponent = Class.create();

NCMComponent.prototype = {
	initialize: function(id,type,parent,tpl) {
					this.m_id = id;						// ID of component
					this.m_type = type;					// Type of component. 
					this.m_parent = parent;				// Reference to the parent page of this component
					this.m_template = tpl;				//NCM template of component
					this.m_parObjId = -1;				// ID of parent NCM component
	
					this.m_hashTexts = new Hash();		//Hash table which has the text resources for this component
					this.m_steps = new Array();			//Array of steps for component
					this.m_stepparams = new Array();	//External parameters for steps;
					this.m_numSteps = 0;				//Number of steps for component
					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
					this.isPublic = true;				//Is this component for public view?
					//Additional initialization
					this.init();
	},
	
	init: function() {},
	
	//-------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;},

	/**
	 * @author Nivaria Development Team
	 * 
 	 * Returns the reference to parent page of the component.
 	 * @return Object
 	*/
	getPage : function() {return this.m_parent;},
	
	getTemplate: function() {return this.m_template;},
	
	getIsLogged: function() {return this.isLogged;},
	
	//-------Public methods-------------
	setTemplate : function(tmName) { this.m_template=tmName; },
	
	/**
	 * @author Nivaria Development Team
	 * 
 	 * Add the text resource for this component
 	 * @param key - String, key of text resource
 	 * @param value - String, value of text resource 
 	*/
	addText: function(key,value) {this.m_hashTexts.setItem(key,value);},

	/**
	 * @author Nivaria Development Team
	 * 
 	 * This function returns the text resource of component.
 	 * @param key - String, key of text resource
 	*/		
	getTextResource: function(key) {
		var res = "";
		if(this.m_hashTexts.hasItem(key))
			res = this.m_hashTexts.getItem(key);
		else {
			if(this.m_parObjId!=-1) {
				var parObj = ncmMngr.getComponent(this.m_parObjId);
				if(parObj!=null)
					res = parObj.getTextResource(key);
			}
		}	
		res = res.replace(/&aacute;/,"\xE1").replace(/&Aacute;/,"\xC1").replace(/&Iacute;/,"\xCD");
		res = res.replace(/&Oacute;/,"\xD3").replace(/&Uacute;/,"\xDA").replace(/&eacute;/,"\xE9");
		res = res.replace(/&iacute;/,"\xED").replace(/&oacute;/,"\xF3").replace(/&uacute;/,"\xFA").replace(/&ntilde;/,"\xF1");
		return res;	
	},
	
	setParentObjId: function(id) {this.m_parObjId = id;},
	
	getParentObjId: function() {return this.m_parObjId},
	
	setProgress: function(bval,elemId) {
    	if($("progress"+this.m_id)!=null) {
    		$("progress"+this.m_id).style.display = (bval?"block":"none");
    		$("progress"+this.m_id).style.visibility = (bval?"visible":"hidden");
    	}
    	if($(elemId)!=null) {
    		$(elemId).style.display = (bval?"none":"block");
    		$(elemId).style.visibility = (bval?"hidden":"visible");
    	}
	},
	
	initSteps: function(num) {
		this.m_steps = new Array(num);
		this.m_stepparams = new Array(num);
		this.m_numSteps = num;
		for(var i=0;i<num;i++) {
			this.m_steps[i] = null;
			this.m_stepparams[i] = "";
		}
	},
	
	setStep: function(index,idval) {
		try {
			if(index>=0 && index<this.m_steps.length)
				this.m_steps[index] = idval;
		} catch(mErr) {
		}	
	},
	
	getStep: function(index) {
		if(index>=0 && index<this.m_steps.length)
	        return this.m_steps[index];
	    else
	        return null;    
	}, 
	
	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++) {
			var obj = $("obj"+this.m_id+"_step"+i);
			if(obj!=null) {
				if(i==index) {
					Element.show(obj);
					this.m_histsteps.shift();
					this.m_histsteps[this.m_histsteps.length] = index;
				} else
					Element.hide(obj);	
			}
		}		
	},
	
	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 = ncmMngr.getComponent(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 = ncmMngr.getComponent(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;
	},
	
	confirms: function(prdId) {},
	
	setLogged: function(bval) {this.isLogged=bval;} 
}
/*End definition of NCMComponent class*/