/*
 * Copyright (C) 1995-2011 by 4system Polska Sp. z o.o.
 * All Rights Reserved.
 */

componentContainer = function(name)
{
	//inherited	
	this.componentElement = componentElement;
	this.componentElement(name);
	
	this.allowNextStep = true;
	this.stepsTimer = null;
};

componentContainer.prototype = new componentElement;
componentContainer.prototype.type = "componentContainer";

componentContainer.prototype.onCreate = function(obj)
{
	this.captureMouseEvents();
	
	var notSet = false;
	for (var i=0; i<this.children.length; i++) {
		var obj = this.children[i];
		//this.children[i].stopPropagation = true; //pg2-odznaczenie
		if (obj.parentDragging) {
			obj.stopPropagation = false;
		} else {
			obj.stopPropagation = true; 
		}
		if (obj.checkAnswer && !notSet) {
			this.checkAnswer = function(e)
			{
				return this.checkAnswerHidden();
			}
			notSet = true;
		}
	}
};

componentContainer.prototype.componentContainerBaseShow = componentContainer.prototype.show;
componentContainer.prototype.show = function()
{
	this.componentContainerBaseShow();
	for (var i = 0; i < this.children.length; i++)
	{
		if (this.children[i].step != STEPS_HIDDEN) {
			this.children[i].show();
		} else {
			this.children[i].hide();
		}
	}
	this.allowNextStep = false;
	this._stepsTimerRun(100);
	
	if (is.moz){
		this.obj.style.width = this.w-2*this.borderWidth;
		this.obj.style.height = this.h-2*this.borderWidth;
	}	
	
	if (this.canDragging) { //pg2- allow container to move
		if (PageInterface.GetValue("state") != psDesigning)
			PageInterface.Execute("setOnTop", this);
	};
};

componentContainer.prototype.beginDrag = function(obj) //pg2- look above
{
	PageInterface.Execute("setOnTop", this);
}

componentContainer.prototype.componentContainerBaseHide = componentContainer.prototype.hide;
componentContainer.prototype.hide = function()
{
	this.componentContainerBaseHide();
	this._stepsTimerStop();
	this.allowNextStep = true;
	for (var i = 0; i < this.children.length; i++)
	{
		this.children[i].hide();
	}
};

componentContainer.prototype.componentContainerBaseUnload = componentContainer.prototype.unload;
componentContainer.prototype.unload = function()
{
	this.componentContainerBaseUnload();
	this.hide();
}

componentContainer.prototype.repaint = function()
{
	for (var i = 0; i < this.children.length; i++) this.children[i].repaint();
};

var counter = 0;
componentContainer.prototype._stepsTimerRun = function(interval)
{
	if (this.stepsTimer) this._stepsTimerStop();
	if (page.stepsRunAutomate) {
		this.stepsTimer = window.setTimeout("pc." + this.id + ".prepareNextStep();", interval);
	}
};

componentContainer.prototype._stepsTimerStop = function()
{
	if (this.stepsTimer != null) {
		window.clearTimeout(this.stepsTimer);
		this.stepsTimer = null;
	}
};

componentContainer.prototype.prepareNextStep = function()
{
	counter++;
	for (var i = 0; i < this.children.length; i++) {
		var child = this.children[i];
		if (!child.allowNextStep || child.anim.animated) {
			this.allowNextStep = false;
			this._stepsTimerRun(100);
			return;
		}
	}
	this.allowNextStep = true;
};

componentContainer.prototype.checkAnswerHidden = function()
{
	var result = true;
	for (var i = 0; i < this.children.length; i++) {
		if (this.children[i].isComponentElement && this.children[i].checkAnswer
				&& (this.children[i].visible || this.checkHiddenElementsAnswer)) {
			result &= this.children[i].checkAnswer();
		}
	}
	return result;
};

componentContainer.prototype.componentContainerBaseLock = componentContainer.prototype.lock;
componentContainer.prototype.lock = function()
{
	for (var i = 0; i < this.children.length; i++)
	{
		this.children[i].lock();
	}
	this.locked = true;
	this.componentContainerBaseLock();
};

componentContainer.prototype.componentContainerBaseUnlock = componentContainer.prototype.unlock;
componentContainer.prototype.unlock = function()
{
	for (var i = 0; i < this.children.length; i++)
	{
		this.children[i].unlock();
	}
	this.locked = false;
	this.componentContainerBaseUnlock();
};

componentContainer.prototype.refreshElement = function()
{
	for (var i = 0; i < this.children.length; i++) {
		if (this.children[i].type == "componentFlash") this.children[i].actionFlashRewind();
		this.children[i].show();
	}
	this.hide();
};

componentContainer.prototype.componentContainerBaseResetElement = componentContainer.prototype.resetElement;
componentContainer.prototype.resetElement = function()
{
	this.componentContainerBaseResetElement();
	
	for (var i = 0; i < this.children.length; i++) {
		if (this.children[i].checkAnswer && this.children[i].visible)
		{
			this.children[i].repaint();
		}
		
		this.children[i].unlock();
	};
	
	this.unlock();
	this.refreshElement();
};

componentContainer.prototype.clearAnswer = function()
{
	for (var i = 0; i < this.children.length; i++) {
		if (this.children[i].clearAnswer) this.children[i].clearAnswer();
		else if (this.children[i].setAnswer) this.children[i].setAnswer("");
	}
};

componentContainer.prototype.getAnswer = function()
{
	var answer = new Array();
	for (var i = 0; i < this.children.length; i++) {
		if (this.children[i].getAnswer) {
			answer[answer.length] = this.children[i].getAnswer();
		}
	}
	return answer;
};

componentContainer.prototype.getAnswerString = function()
{
	var answer = new Array();
	var answerStr = "";
	var sep = "#|#";
	var pCount = this.getParentsCount();
	if (pCount > 0) sep = "#|" + pCount + "#";
		
	for (var i=0; i<this.children.length; i++) {
		if (this.children[i].getAnswerString) {
			answerStr += this.children[i].getAnswerString() + sep;
		} else if (this.children[i].getAnswer) {
			answerStr += this.children[i].getAnswer() + sep;
		}
	}
	return(answerStr);
};

componentContainer.prototype.setAnswer = function(answer, sep, showCorrect)
{
	var ans = new Array();
	this.clearAnswer();
	if (typeof(answer) == "string") {
		var sep = "#|#";
		var pCount = this.getParentsCount();
		if (pCount > 0) sep = "#|" + pCount + "#";
		ans = answer.split(sep);
	} else if (typeof(answer) == "array" || typeof(answer) == "object") {
		ans = answer;
	}
	var ai = 0;
	for (var i = 0; i < this.children.length; i++) {
		if (this.children[i].setAnswer) {
			if (ai >= ans.length) break;
			this.children[i].setAnswer(ans[ai++], "", showCorrect);
		}
	}
	for (var i = 0; i < this.children.length; i++) this.children[i].invokeEvent("Repaint");
};

