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

VersionsItem = function()
{
	this.name = "";
	this.index = 0;
};

Struct = function()
{
	this.m_list = new Array();
	this.m_root = new Array();
	this.firstOffsetPages = 0;
	this.lastOffsetPages = 0;
	this.countShowPages = 0;
	this.examParentGix = -1;	
	this.flatStructure = false;	
	this.isCourseMixed = false;
	this.drawingCriterions = "";
	this.versionsItems = new Object();
};

Struct.prototype.root = function(data)
{
	this.m_root = data;
};

Struct.prototype.rootItem = function()
{
	return this.m_root;
};

Struct.prototype.add = function(data)
{
	data["status"] = (data["href"] != "") ? "N" : "X";
	data["realParent"] = data["parent"];	
	data["blockRestorePage"] = false;	
	data["sessionTime"] = 0;
	this.m_list[this.m_list.length] = data;
};

Struct.prototype.firstItem = function()
{
	return this.m_list[0];
};

Struct.prototype.getNextItem = function(gix)
{
	for (var i = 0; i < this.m_list.length; i++) {
		if (gix == this.m_list[i]["gix"] && i < this.m_list.length) {
			return this.m_list[i + 1];
		}
	}
	return null;
};

Struct.prototype.findPageGixByIndex = function(index)
{
	for (var i = 0; i < this.m_list.length; i++) {
		if (index == this.m_list[i]["index"]) {
			return this.m_list[i]["gix"];
		}
	}
	return null;
};

Struct.prototype.getPreviousItem = function(gix)
{
	for (var i = 0; i < this.m_list.length; i++) {
		if (gix == this.m_list[i]["gix"] && i > 0) {
			return this.m_list[i - 1];
		}
	}
	return null;
};

Struct.prototype.findItem = function(gix)
{
	for (var i = 0; i < this.m_list.length; i++) {
		if (gix == this.m_list[i]["gix"]) {
			return this.m_list[i];
		}
	}
	return null;
};

Struct.prototype.findItemGixByArrIndex = function(index)
{
	if(typeof(this.m_list[index]) != 'undefined'){
		return this.m_list[index]['gix'];
	}else{
		return null;	
	}
}

Struct.prototype.findItemBy = function(value, col)
{
	if (typeof(col) == "undefined" || col == "" || col == null) col = "gix";
	for (var i = 0; i < this.m_list.length; i++) {
		if (value == this.m_list[i][col]) {
			return this.m_list[i];
		}
	}
	return null;
};

Struct.prototype.findNextVisibleItem = function(gix)
{
	var item = this.findItem(gix);
	if (item) {
		var scan = false;
		for (var i = 0; i < this.m_list.length; i++) {
			if (scan) {
				if (this.m_list[i]["visible"] == "1") {
					return this.m_list[i];
				}
			}
			if (this.m_list[i] == item) scan = true;
		}
	}
	return null;
};

Struct.prototype.findPrevVisibleItem = function(gix)
{
	var item = this.findItem(gix);
	if (item) {
		var scan = false;
		for (var i = this.m_list.length; i>0; i--) {
			if (scan) {
				if (this.m_list[i - 1]["visible"] == "1") {
					return this.m_list[i - 1];
				}
			}
			if (this.m_list[i - 1] == item) scan = true;
		}
	}
	return null;
};

Struct.prototype.findItemByParent = function(index)
{
	for (var i = 0; i < this.m_list.length; i++) {
		if (index == this.m_list[i]["index"]) {
			return this.m_list[i];
		}
	}
	return null;
};

Struct.prototype.isFirstItem = function(gix)
{
	return false;
};

Struct.prototype.isLastItem = function(gix)
{
	return false;
};

Struct.prototype.item = function(index)
{
	var item = this.m_list[index];
	return item;
};

Struct.prototype.length = function(index)
{
	var len = this.m_list.length;
	return len;
};

Struct.prototype.getVisibleItemsCount = function()
{
	var count = 0;
	for (var i = 0; i < this.length(); i++) {
		if (this.m_list[i]["visible"] == "1") count++;
	}
	return count;
};

Struct.prototype.getVisibleItemsIndex = function(gix)
{
	var index = 0;
	for (var i = 0; i < this.length(); i++) {
		if (this.m_list[i]["visible"] == "1") index++;
		if (this.m_list[i]["gix"] == gix) return index;
	}
	return index;
};

Struct.prototype.getColToString = function(colName)
{
	//var result = new Array();
	var result = "";
	for (var i = 0; i < this.m_list.length; i++) {
		result[result.length] = this.m_list[i][colName];
		result += this.m_list[i][colName];
	}
	//return result.toString();
	return result;
};

Struct.prototype.setColFromString = function(colName, v)
{
	var sep = "";
	if (v.indexOf(",") > -1) sep = ",";
	var arr = v.split(sep);
	for (var i = 0; i < arr.length; i++) {
		if (typeof(this.m_list[i])!="undefined" && !this.m_list[i]["blockRestorePage"]) {
			this.m_list[i][colName] = arr[i];
		}
	}
};

Struct.prototype.setAllHidden = function(gix)
{
	for (var i = 0; i < this.m_list.length; i++) {
		this.m_list[i]["visible"] = 0;
	}
	return true;
};

Struct.prototype.setAllVisible = function(gix)
{
	for (var i = 0; i < this.m_list.length; i++) {
		this.m_list[i]["visible"] = 1;
	};
	return true;
};

Struct.prototype.Mixed = function()
{
	this.isCourseMixed = true;
	var tempList = new Array();
	this.examParentGix = this.m_list[0]['gix'];
	
	var draw = new Array();
	
	if (this.drawingCriterions != "") {
		var a = this.drawingCriterions.split(",");
		for (var i in a) {
			var vg = a[i].split("x");
			draw[draw.length] = new Array(vg[0], vg[1]);
		}
	} else {
		draw[draw.length] = new Array(this.countShowPages, "*");
	}
	
	for (var i = this.firstOffsetPages; i < this.m_list.length - this.lastOffsetPages; i++) {
		uelo.pages[i].visible = 0;
		uelo.pages[i].blockRestorePage = true;
		//
		uelo.pages[i].iss_old = uelo.pages[i].iss; //pg2-qr
		uelo.pages[i].iss = 0;
		//uelo.pages[i].score = 0; //pg2-qr -removed 4 test
	}
	
	for (var i = 0; i < this.firstOffsetPages; i++) {
		tempList[tempList.length] = this.m_list[i];
		tempList[tempList.length-1].isExamPage = 0;		
		this.examParentGix = this.m_list[i]['gix'];
	}

	for (var drawIndex in draw) {
		var groupName = draw[drawIndex][1];
		var groupCount = draw[drawIndex][0];
		
		var indexList = new Array();
		for (var i = this.firstOffsetPages; i < this.m_list.length - this.lastOffsetPages; i++) {
			if (groupName == "*" || this.m_list[i]['group'] == groupName) {
				indexList[indexList.length] = i;
			}
		}
		
		var ueloPage = null;
		if (groupCount > indexList.length) groupCount = indexList.length;
		
		for (var i = 0; i < groupCount; i++) {
			var index = Math.round(Math.random() * (indexList.length - 1));
			tempList[tempList.length] = this.m_list[indexList[index]];
			tempList[tempList.length-1].isExamPage = 1;		
			tempList[tempList.length-1].blockRestorePage = true;		
			ueloPage = uelo.FindPage(this.m_list[indexList[index]]['gix']);
			ueloPage.visible = 1;
			//pg2-qr
			ueloPage.iss = ueloPage.iss_old;
			ueloPage.isExamPage = true;
			//
			indexList.splice(index, 1);
		}
	}
	
	for (var i = this.m_list.length - this.lastOffsetPages; i < this.m_list.length; i++) {
		tempList[tempList.length] = this.m_list[i];
		tempList[tempList.length-1].isExamPage = 0;		
	}
	
	if (this.firstOffsetPages == 0) this.examParentGix = tempList[0]['gix'];
	
	this.m_list = tempList;
};

Struct.prototype.TreeToList = function()
{
	this.flatStructure = true;
	for (var i = 0; i < this.length(); i++) {
		this.item(i)["parent"] = 1;
	}
};

Struct.prototype.existsVersionItem = function(vindex)
{
	for (var k in this.versionsItems) {
		if (this.versionsItems[k].index == vindex) {
			return true;
		}
	}
	
	return false;
};

Struct.prototype.getRootVersion = function(vindex)
{
	if (this.m_root["title" + vindex])
	{
		return this.m_root["title" + vindex];
	}else{
		return this.m_root["title"];
	}
}

Struct.prototype.addRootVersion = function(index, vindex, s, vname)
{
	this.m_root["title" + vindex] = s;
}

Struct.prototype.addVersionItem = function(vindex, vname)
{
	if (!this.versionsItems["_" + vindex]) {
		this.versionsItems["_" + vindex] = new VersionsItem();
		this.versionsItems["_" + vindex].index = vindex;
		this.versionsItems["_" + vindex].name = vname;
	}
};

Struct.prototype.addVersion = function(index, vindex, s, vname)
{
	for (var i = 0; i < this.m_list.length; i++)
	{
		if (index == this.m_list[i]["index"])
		{
			this.m_list[i]["title" + vindex] = s;
			this.addVersionItem(vindex, vname);
		}
	}
};

Struct.prototype.getGIX = function(idx)
{
	var item = this.findItemBy(idx, "index");
	if (item) {
		return parseInt(item["gix"]);
	}
	return -1;
};


Struct.prototype.setNewStatus = function(newStatus)
{
	for (var i = 0; i < this.m_list.length; i++) {
		this.m_list[i]["status"] = newStatus;
	};
	return true;
};

Struct.prototype.setInformationsPagesCompleted = function()
{
	for (var i = 0; i < this.m_list.length; i++) {
		if (this.m_list[i]["status"] == "I" || this.m_list[i]["status"] == "N") {
			this.m_list[i]["status"] = "C";
		};
	};
	return true;	
};

Struct.prototype.setPagesStatus = function(pageType, newStatus)
{
	//pageType = Exercise, Information, All
	if (pageType != 'Exercise' &&  pageType != 'Information' && pageType != 'All')
	{
		pageType = 'All';
	}	
	
	for (var i = 0; i < this.m_list.length; i++) 
	{
		if (
				(pageType == 'All') || 
				(pageType == 'Exercise' && this.m_list[i]["ms"] != "") || 
				(pageType == 'Information' && this.m_list[i]["ms"] == "")
			)
		{
			this.m_list[i]["status"] = newStatus;
		}
	};
};

Struct.prototype.getMainChapters = function()
{
	var item = null;
	var tmpItem = null;
	var itemInd = -1;
	var chapterName = "";	
	
	for (var i = 0; i < this.m_list.length; i++) {
		item = this.m_list[i];
		if (item['parent'] == 1) {
			item['isMainChapter'] = true;
			item['chapterName'] = item['title'];
		}else{
			item['isMainChapter'] = false;
			itemInd = item['realParent']-2;
			tmpItem = this.m_list[itemInd];
			while (tmpItem) {
				itemInd = tmpItem['realParent']-2;
				chapterName = tmpItem['title'];
				tmpItem = this.m_list[itemInd];		
			};
			item['chapterName'] = chapterName;			
		};
	};
};

Struct.prototype.resetPage = function(item) //pg2-bsh
{
	item["status"] = "N";
}

struct = new Struct();
