/*
 * Copyright (C) 1995-2011 by 4system Polska Sp. z o.o.
 * All Rights Reserved.
 */
componentCourseTree = function(name){
    this.componentPagesList = componentPagesList;
    this.componentPagesList(name);
    
    this.src = '';
    this.menu = '';
    
    this.treeCreated = false;
    this.pagesArr = new Array();
    
    this.imagesArr = new Array();
};

componentCourseTree.prototype = new componentPagesList();
componentCourseTree.prototype.type = "componentCourseTree";

componentCourseTree.prototype.onBaseCreate = function(obj){
    this.resourcesStatus = "online";
    this.resourcesURL = resourcesURL;
    if (PageInterface.GetValue("state") != psDesigning && lms && lms.name != "lmsOffline") {
        this.resourcesStatus = lms.resourcesStatus();
        if (this.disableOnlineOfflineMix) 
            this.resourcesURL = resourcesURL;
        else 
            this.resourcesURL = lms.resourcesURL();
    };
    this.resourcesURL = unescape(this.resourcesURL);
    
    this.imagesNameArr = [this.bckgTop, this.bckgMiddle, this.bckgBottom, this.bckgSeparator, this.buttonCloseOut, this.buttonCloseOver];
	this.getPreloadImagesArr();
};

componentCourseTree.prototype.getStruct = function()
{
	var struct = getStruct();
	
	if (!struct) {
		var page1 = {
			"gix": 1,
			"index": 1,
			"title": this.exampleTitle + " 1",
			"realParent": 0,
			"status": "N",
			"visible": 1,
			"haveParent": false
		};
		var page2 = {
			"gix": 2,
			"index": 2,
			"title": this.exampleTitle + " 2",
			"realParent": 0,
			"status": "N",
			"visible": 1,
			"haveParent": false
		};
		var page3 = {
			"gix": 3,
			"index": 3,
			"title": this.exampleTitle,
			"realParent": 2,
			"status": "N",
			"visible": 1,
			"haveParent": false
		};
		
		struct = new Object();
		
		struct.m_list = new Array();
		struct.m_list = [page1, page2, page3];
		
		struct.length = function() {return 3; };
		struct.rootItem = function() {return {"title":"rootTitle", "index":0, "parent":1}; };
		struct.item = function(index) {return this.m_list[index]; };
	}
	
	return struct;
}

componentCourseTree.prototype.onShow = function()
{
	if (typeof(this.bringToFront) != "undefined") this.bringToFront(); //pg2- in old wbte, it could be undefined and an exception will be invoked; result = no tree
	
	var struct = this.getStruct();
	var structLength = struct.length();
	
	var player = getPlayer(); //pg2-multiversions

    if (!this.treeCreated) {
        if (this.treeType == 0) {
            this.objComboMenu = new comboMenu(this.id, page.p_globalIndex);
            this.objComboMenu.insertTree(struct);
            this.menu = this.objComboMenu.tree;
        }
        else 
            if (this.treeType == 1) {
                if (this.useImagesBckg) {
                    var bckgTopH = getImageHeight(this.bckgTop);
                }else {
                    var bckgTopH = 0;
                };
                
                this.setNewTreeImages(this.treeImages);
                
                var chaptersColors = this.chaptersColors.split('||');
                
                var fontSize = getFontSize(this.treeFontSize, 'px');
                var ind = struct.m_list[0];
                var addIndexShift = 0;
                if (structPage && structPage.index == 0) 
                    addIndexShift = 2;
                var multiSCOInd = 0;
                var chColorIndex = 0;
                var chapterColor = '';
                var ind = 0;
                
                for (var i = 0; i < structLength; i++) {
                    var structPage = struct.m_list[i];
                    if (structPage.visible == 1) {
					
						//pg2-multiversions
						var pageTitle = structPage.title;
						if (player) {
							try {
								var version = player.courseVersion;
								pageTitle = struct.item(i)["title" + version];
							} catch(e) {
								pageTitle = structPage.title;
							};
						}
						//
						
                        var pageIndex = parseInt(structPage.index) + addIndexShift;
                        if (structPage.realParent == '1') {
                            ind = chColorIndex % chaptersColors.length;
                            chapterColor = chaptersColors[ind];
                            chColorIndex++;
                        }
                        else 
                            if (this.colorOnlyChapterPage) {
                                chapterColor = 'transparent';
                            }
                        var item = new courseTreePageItem(
									i, 
									this.id, 
									structPage.gix,
									pageIndex, 
									//structPage.title,  //pg2-removed, replaced with below one
									pageTitle, //pg2-multiversions
									'pc.' + this.id + '.checkIsGoToAllowed(' + structPage.gix + ', ' + pageIndex + ', ' + multiSCOInd + ')', 
									structPage.realParent, 
									structPage.status, 
									this.addPrefix, 
									this.resourcesURL, 
									this.imagesArr['pageNotAtt'], 
									this.imagesArr['pageIncomplete'], 
									this.imagesArr['pageCompleted'],
									this.imagesArr['pagePass'], 
									this.imagesArr['pageFail'], 
									this.imagesArr['blankIcon'], 
									this.imagesArr['minusIcon'], 
									this.imagesArr['plusIcon'], 
									this.treeFontFamily, 
									fontSize, 
									this.treeFontColor, 
									chapterColor, 
									(structPage.realParent == '1')
						);
                        multiSCOInd++;
                        this.addChildToParent(structPage.realParent, item);
                        this.pagesArr[pageIndex] = item;
                    };
                                    };
                if (this.showTREEicons) {
                    this.menu += '<p>';
                    this.menu += '<img src="' + this.resourcesURL + this.TREE_maksymalize + '" border="0" style="cursor: pointer;" onClick="pc.' + this.id + '.expandAllTree();"/>';
                    this.menu += '&nbsp;';
                    this.menu += '<img src="' + this.resourcesURL + this.TREE_minimalize + '" border="0" style="cursor: pointer;" onClick="pc.' + this.id + '.collapseAllTree();"/>';
                    this.menu += '</p>';
                };
                var chaptersNamesArr = this.chaptersNames.split('||');
                
                for (var i in this.pagesArr) {
                    if (this.pagesArr[i] && !this.pagesArr[i].haveParent) {
                        if (!this.isInArray(chaptersNamesArr, this.pagesArr[i].title)) {
                            this.menu += this.pagesArr[i].toString();
                        }
                    };
                };
        	}
            else 
                if (this.treeType == 2) {
                    this.objCascadeMenu = new cascadeMainMenu(this.id);
                    this.objCascadeMenu.insertTree(struct);
                    this.menu = this.objCascadeMenu.tree;
                };
        this.setTree();
		this.treeCreated = true
    };
    
    if (this.treeType == 1) {
		var single = checkExportType(config.exportDetails, 'single');
		var singleMulti = checkExportType(config.exportDetails, 'singleMulti');
		
		if (single || singleMulti)
		{
			this.setTreeIconsStatus(struct);
		}else
		{
			if (PageInterface.GetValue('isExam')) {
				this.setTreeIconsStatus_mltpSCO(struct, true);
			}else{	
			this.setTreeIconsStatus_mltpSCO(struct, false);
			}
		};
        if (PageInterface.GetValue("state") == psDesigning) 
		{
            this.setFocus(1);
		}
    }
	this.expandTree();	
};	

componentCourseTree.prototype.expandTree = function(){
    this.setFocus(PageInterface.GetValue("pageIndex"));
    if (this.treeType == 1) {
        switch (this.expand) {
            case 'openBranch':
                this.expandPageParents(PageInterface.GetValue("pageIndex"));
                break;
            case 'openAll':
                this.expandAllTree();
                break;
        };
            }
}

componentCourseTree.prototype.componentCourseTreeBaseSetDivElements = componentCourseTree.prototype.setDivElements;
componentCourseTree.prototype.setDivElements = function(){
    this.componentCourseTreeBaseSetDivElements();
    var addPageDiv = document.getElementById(this.id + "_addPageDiv");
    if (addPageDiv) {
        addPageDiv.style.top = this.paddingTop;
    }
}

componentCourseTree.prototype.setImagesIntoHTMLObj = function(){
    var bckgTopImg = document.getElementById(this.id + "_bckgTopImg");
    var bckgMiddleImg = document.getElementById(this.id + "_bckgMiddleImg");
    var bckgBottomImg = document.getElementById(this.id + "_bckgBottomImg");
    var addPageImg = document.getElementById(this.id + "_addPageImg");
    
    if (bckgTopImg) {
        bckgTopImg.src = this.resourcesURL + this.bckgTop;
        bckgTopImg.width = this.w;
        bckgTopImg.height = this.bckgTopH;
    }
    if (bckgMiddleImg) {
        bckgMiddleImg.src = this.resourcesURL + this.bckgMiddle;
        bckgMiddleImg.width = this.w;
        bckgMiddleImg.height = this.h - this.bckgTopH - this.bckgBottomH;
    }
    if (bckgBottomImg) {
        bckgBottomImg.src = this.resourcesURL + this.bckgBottom;
        bckgBottomImg.width = this.w;
        bckgBottomImg.height = this.bckgBottomH;
    }
    if (addPageImg) {
        addPageImg.src = this.resourcesURL + this.buttonCloseOut;
        addPageImg.width = this.buttonCloseW;
        addPageImg.height = this.buttonCloseH;

		var closeButtonDiv = document.getElementById(this.id + "_addPageDiv"); //pg2
		if (closeButtonDiv) {
			if (this.useImagesBckg && this.showCloseButton == false) {
				closeButtonDiv.style.visibility = 'hidden';
			}
		}
    }
};

componentCourseTree.prototype.setTreeIconsStatus = function(struct){
    var structLength = struct.length();
    
    for (var i = 0; i < structLength; i++) {
        var structPage = struct.m_list[i];
        var treeItemIcon = document.getElementById(this.id + "_" + i + "_icon");
        
        if (treeItemIcon) {
            switch (structPage.status) {
                case 'I':
                    treeItemIcon.src = this.resourcesURL + this.imagesArr['pageIncomplete'];
                    break;
                case 'C':
                    treeItemIcon.src = this.resourcesURL + this.imagesArr['pageCompleted'];
                    break;
                case 'P':
                    treeItemIcon.src = this.resourcesURL + this.imagesArr['pagePass']
                    break;
                case 'F':
                    treeItemIcon.src = this.resourcesURL + this.imagesArr['pageFail'];
                    break;
                default:
                    treeItemIcon.src = this.resourcesURL + this.imagesArr['pageNotAtt'];
                    break;
            };
                    };
            };
    }

componentCourseTree.prototype.setTreeIconsStatus_mltpSCO = function(struct, isExam){
    if (lms) {
        var structLength = struct.length();
        var SCOStatus = '';
		var pageServerIndex = 0;
        
        for (var i = 0; i < structLength; i++) {
            var structPage = struct.m_list[i];
            var treeItemIcon = document.getElementById(this.id + "_" + i + "_icon");
            if (structPage.visible == "0") continue;
            if (isExam) {
                SCOStatus = lms.getValueForSco(pageServerIndex);
            }
            else {
                SCOStatus = lms.getValueForSco('cmi.core.lesson_status', pageServerIndex);
            }
            if (SCOStatus == null || SCOStatus.toString() == "null") {
                SCOStatus = lms.getValueForSco('cmi.success_status', pageServerIndex);
				if (SCOStatus == 'unknown'){
					SCOStatus = lms.getValueForSco('cmi.completion_status', pageServerIndex);				
				}
            }
            
            if (treeItemIcon) {
                switch (SCOStatus) {
                    case 'incomplete':
                        treeItemIcon.src = this.resourcesURL + this.imagesArr['pageIncomplete'];
                        break;
                    case 'completed':
                        treeItemIcon.src = this.resourcesURL + this.imagesArr['pageCompleted'];
                        break;
                    case 'passed':
                        treeItemIcon.src = this.resourcesURL + this.imagesArr['pagePass']
                        break;
                    case 'failed':
                        treeItemIcon.src = this.resourcesURL + this.imagesArr['pageFail'];
                        break;
                    default:
                        treeItemIcon.src = this.resourcesURL + this.imagesArr['pageNotAtt'];
                        break;
                };
			};
			pageServerIndex++;
		};
	}
}

componentCourseTree.prototype.onRepaint = function(){
    var pageType = PageInterface.GetValue("pageType");
    if (pageType == "Exercise" && this.treeType == 1) {
    
        var userScore = PageInterface.GetValue("userScore");
        var masteryScore = PageInterface.GetValue("masteryScore");
        var maxScore = PageInterface.GetValue("maxScore");
        var answerWeighting = PageInterface.GetValue("answerWeighting");
        var corAns = (answerWeighting) ? (userScore >= masteryScore) : (userScore == maxScore);
        var pageIndex = PageInterface.GetValue('pageIndex');// - 2;
        var pInStruct = 0;
        
        if (this.pagesArr) {
            var item = this.pagesArr[pageIndex];
            if (item) {
                pInStruct = item.posInStruct;
            }
        };
        
        var treeItemIcon = document.getElementById(this.id + "_" + pInStruct + "_icon");
        if (treeItemIcon) {
            if (corAns) {
                treeItemIcon.src = this.resourcesURL + this.imagesArr['pagePass'];
                if (treeItemIcon.style.filter) {
                    treeItemIcon.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../resources/" + this.imagesArr['pagePass'] + "', sizingMethod='image');"
                }
            }
            else {
                treeItemIcon.src = this.resourcesURL + this.imagesArr['pageFail'];
                if (treeItemIcon.style.filter) {
                    treeItemIcon.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../resources/" + this.imagesArr['pageFail'] + "', sizingMethod='image');"
                }
            };
                    };
            };
    };

componentCourseTree.prototype.componentCourseTreeResetElement = componentCourseTree.prototype.resetElement;
componentCourseTree.prototype.resetElement = function(){
    this.componentCourseTreeResetElement();
    
    var pageIndex = PageInterface.GetValue('pageIndex') - 2;
    
    var treeItemIcon = document.getElementById(this.id + "_" + pageIndex + "_icon");
    if (treeItemIcon) {
        treeItemIcon.src = this.resourcesURL + this.imagesArr['pageNotAtt'];
    };
    }

componentCourseTree.prototype.isInArray = function(arr, value){
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] == value) {
            return true;
        };
            };
    return false;
}

componentCourseTree.prototype.onPaint = function(){
    return;
};

componentCourseTree.prototype.setTree = function(){
    var treeDIV = document.getElementById(this.id + "_treeDIV");
    if (this.useImagesBckg) {
        this.setImagesIntoHTMLObj();
        this.setDivElements();
        
        var titleDiv = document.getElementById(this.id + "_titleDiv");
        if (titleDiv) {
            var bckgTopH = getImageHeight(this.bckgTop);
            var bckgTopW = getImageWidth(this.bckgTop);
            titleDiv.style.left = 10;
            titleDiv.style.top = 0;
            titleDiv.style.height = bckgTopH;
            titleDiv.style.width = this.w - 10 - 10;
            titleDiv.style.clip = 'rect(0px ' + eval(this.w - 10) + 'px ' + bckgTopH + 'px 0px)';
            titleDiv.dir = this.dir;
        };
        
        if (treeDIV) {
            var bckgTopH = getImageHeight(this.bckgTop);
            var bckgTopW = getImageWidth(this.bckgTop);
            treeDIV.style.left = this.paddingLeft;
            treeDIV.style.top = bckgTopH;
            treeDIV.style.height = this.h - bckgTopH;// - 20; //pg2-removed - APS for MGO, scrollbar was higher than tree's bottom image
            treeDIV.style.width = this.w - this.paddingLeft;
            treeDIV.style.clip = 'rect(0px ' + eval(this.w - this.paddingLeft) + 'px ' + eval(this.h - bckgTopH) + 'px 0px)';
            treeDIV.dir = this.dir;
            
            treeDIV.style.cssText += '; overflow-x: auto; overflow-y: auto;';
            if (is.ie) {
                treeDIV.style.scrollbarFaceColor = this.getColor('FaceColor');
                treeDIV.style.scrollbarTrackColor = this.getColor('TrackColor');
                treeDIV.style.scrollbar3dlightColor = this.getColor('3dlightColor');
                treeDIV.style.scrollbarDarkshadowColor = this.getColor('DarkshadowColor');
                treeDIV.style.scrollbarArrowColor = this.getColor('ArrowColor');
                treeDIV.style.scrollbarShadowColor = this.getColor('ShadowColor');
                treeDIV.style.scrollbarHighlightColor = this.getColor('HighlightColor');
            };
            treeDIV.innerHTML = this.menu;
        };
            }
    else {
        if (treeDIV) {
            treeDIV.style.left = 0;
            treeDIV.style.top = 0;
            treeDIV.style.height = this.h;
            treeDIV.style.width = this.w;
            treeDIV.style.clip = 'rect(0px ' + this.w + 'px ' + this.h + 'px 0px)';
            treeDIV.dir = this.dir;
            
            if (this.treeType != 2) 
                treeDIV.style.cssText += '; overflow-x: auto; overflow-y: auto;';
            if (is.ie) {
                treeDIV.style.scrollbarFaceColor = this.getColor('FaceColor');
                treeDIV.style.scrollbarTrackColor = this.getColor('TrackColor');
                treeDIV.style.scrollbar3dlightColor = this.getColor('3dlightColor');
                treeDIV.style.scrollbarDarkshadowColor = this.getColor('DarkshadowColor');
                treeDIV.style.scrollbarArrowColor = this.getColor('ArrowColor');
                treeDIV.style.scrollbarShadowColor = this.getColor('ShadowColor');
                treeDIV.style.scrollbarHighlightColor = this.getColor('HighlightColor');
            };
            treeDIV.innerHTML = this.menu;
        };
            };
    if (PageInterface.GetValue("state") == psNormal && this.treeType == 2) {
        document.getElementById(this.id).style.clip = 'rect(-2000px 2000px 2000px -2000px)';
    };
    };

componentCourseTree.prototype.checkIsGoToAllowed = function(pageGix, pageIndex, multiSCOInd){
    if (this.blockGoToPages) {
        var gotoPage = {};
        gotoPage.gix = pageGix;
        gotoPage.index = pageIndex;
        
        var openPage = {};
        if (opener) {
            openPage.gix = parseInt(opener.PageInterface.GetValue('pageGix'));
            openPage.index = parseInt(opener.PageInterface.GetValue('pageIndex'));
        }
        else {
            openPage.gix = parseInt(PageInterface.GetValue('pageGix'));
            openPage.index = parseInt(PageInterface.GetValue('pageIndex'));
        };
        
        var opened = this.checkWasPageOpened(pageIndex);
        if (opened) {
            this.gotoPage(pageGix, multiSCOInd);
        }
        else {
            if (parseInt(openPage.index) + 1 == gotoPage.index) {
                this.gotoPage(pageGix, multiSCOInd);
            }
            else {
                var allOpened = this.checkAllPagesBeforeOpened(pageGix);
                if (allOpened) {
                    this.gotoPage(pageGix, multiSCOInd);
                }
                else 
                    if (this.showAlert) {
                        showMessage(this.alertText);
				};
			};
		};
	}
    else {
        this.gotoPage(pageGix, multiSCOInd);
    };
};

componentCourseTree.prototype.gotoPage = function(pageGix, multiSCOInd){
    var topWin = null;
    if (typeof(getWBTServerTop) != 'undefined') {
        topWin = getWBTServerTop();
    };
    if (opener && opener.lms) {
        opener.lms.gotoPage(pageGix);
    } else 
	if (PageInterface.GetValue('isExam') || (topWin && topWin.oneSCOCourse && topWin.oneSCOCourse.toString() == 'false')) {
		window.parent.changePage(multiSCOInd);
	} else {
		PlayerInterface.Execute('gotoPage', pageGix);
	}
	
	try { //pg2-if it is in new window which isn't closed after "gotoPage", select proper item in the tree
		if (opener && !this.closeOpenerPage) {
			var struct = getStruct();
			for (var i=0; i<this.pagesArr.length; i++) {
				var item = this.pagesArr[i];
				if (item) {
					if (item.gix == pageGix) {
						item.focus(this.pageNameFocusColor, this.pageNameTitleColor);
					} else item.unselect();
					
					var imgObj = document.getElementById(item.compName + '_' + item.posInStruct + '_icon');
					if (struct && imgObj) {
						var p = struct.findItem(item.gix);
						if (p) {
							var pageStatus = p["status"];
							
							var iconSrc = "";
							if (pageStatus == "I") iconSrc = item.pageIncompleted; else
							if (pageStatus == "C") iconSrc = item.pageCompleted; else
							if (pageStatus == "P") iconSrc = item.pagePass; else
							if (pageStatus == "F") iconSrc = item.pageFail; else
							//if (pageStatus == "N") 
							iconSrc = item.pageNotAtt;
							imgObj.src = item.resourcesURL + iconSrc;
						}
					}
				}
			}
		}
	} catch (e) {
	}
	
    if (opener && this.closeOpenerPage) {
        window.close();
	} else 
	if (opener) window.focus(); //pg2
}

componentCourseTree.prototype.setNewTreeImages = function(str){
    var images = str.split(';');
    
    for (var i = 0; i < images.length; i++) {
        var oneImg = images[i].split(':');
        if (images[i] != '') {
            this.imagesArr[oneImg[0]] = oneImg[1];
        };
            };
    };

componentCourseTree.prototype.getColor = function(value){
    var colorArr = this.treeCSSStyle.split(';')
    for (var i = 0; i < colorArr.length; i++) {
        var oneColor = colorArr[i].split(':');
        if (oneColor[0].indexOf(value) > -1) 
            return oneColor[1];
    };
    return 'transparent';
};

componentCourseTree.prototype.checkAllPagesBeforeOpened = function(pageGix){
    var SCOStatus = '';
    var topWin = null;
    if (typeof(getWBTServerTop) != 'undefined') {
        topWin = getWBTServerTop();
    };
    if (topWin && topWin.oneSCOCourse && topWin.oneSCOCourse.toString() == 'false') {
        for (var i in this.pagesArr) {
            var item = this.pagesArr[i];
            SCOStatus = lms.getValueForSco('cmi.core.lesson_status', (item.index - 2));
            if (item.gix == pageGix) {
                return true;
            }
            else 
                if (SCOStatus == "not attempted") {
                    return false;
                };
                    };
        return (SCOStatus != "not attempted");
    }
    else 
        if (PageInterface.GetValue('isExam')) {
            for (var i in this.pagesArr) {
                var item = this.pagesArr[i];
                SCOStatus = lms.getValueForSco(eval(item.index - 2));
                if (item.gix == pageGix) {
                    return true;
                }
                else 
                    if (SCOStatus == "not attempted") {
                        return false;
                    };
                            };
            return (SCOStatus != "not attempted");
        }
        else {
            for (var i in this.pagesArr) {
                var item = this.pagesArr[i];
                if (item.gix == pageGix) {
                    return true;
                }
                else 
                    if (item.status == "N") {
                        return false;
                    };
                            };
                    };
    };

componentCourseTree.prototype.checkWasPageOpened = function(pageIndex){
    var SCOStatus = '';
    var topWin = null;
    if (typeof(getWBTServerTop) != 'undefined') {
        topWin = getWBTServerTop();
    };
    if (topWin && topWin.oneSCOCourse && topWin.oneSCOCourse.toString() == 'false') {
        SCOStatus = lms.getValueForSco('cmi.core.lesson_status', (pageIndex - 2));
        return (SCOStatus != "not attempted");
    }
    else 
        if (PageInterface.GetValue('isExam')) {
            SCOStatus = lms.getValueForSco(eval(pageIndex - 2));
            return (SCOStatus != "not attempted");
        }
        else {
            var item = this.pagesArr[pageIndex];
            
            if (item) {
                return (item.status != 'N');
            };
            return false;
        };
    };

componentCourseTree.prototype.addChildToParent = function(pageIndex, child){
    var chaptersNamesArr = this.chaptersNames.split('||');
    var item = this.pagesArr[pageIndex];
    
    if (item) {
        if (!this.isInArray(chaptersNamesArr, child.title)) {
            item.children.push(child);
            child.haveParent = true;
            child.parentObj = item;
        }
    }
    else {
        child.haveParent = false;
    };
    }

componentCourseTree.prototype.expandAllTree = function(){
    for (var i in this.pagesArr) {
        var item = this.pagesArr[i];
        
        if (item && item.children.length > 0) {
            item.expand();
        };
            };
    }

componentCourseTree.prototype.collapseAllTree = function(){
    for (var i in this.pagesArr) {
        var item = this.pagesArr[i];
        
        if (item && item.children.length > 0) {
            item.collapse();
        };
            };
    }

componentCourseTree.prototype.expandPageParents = function(index){
    if (this.pagesArr) {
        var item = this.pagesArr[index];
        if (item) 
            var itemParent = this.pagesArr[item.parent];
        
        while (itemParent) {
            if (itemParent && itemParent.children.length > 0) 
                itemParent.expand();
            itemParent = this.pagesArr[itemParent.parent];
        };
            };
    };

componentCourseTree.prototype.setFocus = function(index){
	if (this.treeType == 0) {
		this.objComboMenu.focus(index, this.pageNameFocusColor, this.pageNameTitleColor);
    }else if (this.treeType == 1) {
        if (this.pagesArr) {
            var item = this.pagesArr[index];
            
            if (item && item.visible == 1) {
                item.focus(this.pageNameFocusColor, this.pageNameTitleColor);
            }
            else 
                if (this.markPageParent) {
                    var parentItem = this.findVisibleParent(item);
                    if (parentItem != null) parentItem.focus(this.pageNameFocusColor, this.pageNameTitleColor); //pg2-if
                }
        };
    }
    else 
        if (this.treeType == 2) {
			this.objCascadeMenu.focus(index, this.pageNameFocusColor, this.pageNameTitleColor);
		};
   };

componentCourseTree.prototype.findVisibleParent = function(child)
{
    if (typeof(child) == "undefined") return null; //pg2
	var itemParent = this.pagesArr[child.parent];
	while (itemParent && itemParent.visible == 0){
		itemParent = this.pagesArr[itemParent.parent];
	}
	return itemParent;
}

componentCourseTree.prototype.expandAllBranch = function(branchParent){
    branchParent.expand();
    for (var i = 0; i < branchParent.children.length; i++) {
        var item = branchParent.children[i];
        
        if (item && item.children.length > 0) {
            this.expandAllBranch(item);
        };
            };
    }

componentCourseTree.prototype.showChapter = function(chapterName){
    chapterName = unescape(chapterName);
    
    var chapterParent = -1;
    var pageVisible = null;
    var pageChapter = null;
    var hideOtherPages = false;
    var foundChapterPage = false;
    
    for (var i in this.pagesArr) {
        var item = this.pagesArr[i];
        
        if (item) {
            if (chapterName == item.title) {
                chapterParent = item.parent;
                pageChapter = item;
                document.getElementById(item.id + '-focus').style.display = 'block';
                foundChapterPage = true;
            }
            else {
                if (foundChapterPage && chapterParent >= item.parent) {
                    hideOtherPages = true;
                };
                if (!foundChapterPage || hideOtherPages) {
                    document.getElementById(item.id + '-focus').style.display = 'none';
                }
                else {
                    pageVisible = item;
                    document.getElementById(item.id + '-focus').style.display = 'block';
                };
                            };
                    };
            };
    //if (pageChapter) pageChapter.expand();
    if (pageChapter) 
        this.expandAllBranch(pageChapter);
}

function courseTreePageItem(posInStruct, compName, gix, index, title, action, parent, status, addPrefix, resourcesURL, pageNotAtt, pageIncompleted, pageCompleted, pagePass, pageFail, blankIcon, minusIcon, plusIcon, fontFamily, fontSize, fontColor, chapterColor, isChapter){
    this.posInStruct = posInStruct;
    this.compName = compName;
    this.id = compName + '-tree-item-' + index;
    this.gix = gix;
    this.index = index;
    this.title = title;
    this.action = action;
    this.parent = parent;
    this.status = status;
    this.haveParent = false;
    this.parentObj = null;
    this.open = false;
    this.addPrefix = addPrefix;
    
    this.resourcesURL = resourcesURL;
    this.pageNotAtt = pageNotAtt;
    this.pageIncompleted = pageIncompleted;
    this.pageCompleted = pageCompleted;
    this.pagePass = pagePass;
    this.pageFail = pageFail;
    this.blankIcon = blankIcon;
    this.minusIcon = minusIcon;
    this.plusIcon = plusIcon;
	this.visible = 0;

    this.fontFamily = fontFamily;
    this.fontSize = fontSize;
    this.fontColor = fontColor;
    this.chapterColor = chapterColor;
    this.isChapter = isChapter;
    
    this.children = new Array();
};

courseTreePageItem.prototype.expand = function(){
    var obj = document.getElementById(this.id + '-cont');
    var objSrc = document.getElementById(this.id + '-indent');
    
    if (obj) {
        obj.style.display = "block";
        this.open = true;
    };
    if (objSrc) {
        objSrc.src = this.resourcesURL + this.minusIcon;
    };
    };

courseTreePageItem.prototype.collapse = function(){
    var obj = document.getElementById(this.id + '-cont');
    var objSrc = document.getElementById(this.id + '-indent');
    
    if (obj) {
        obj.style.display = "none";
        this.open = false;
    };
    if (objSrc) {
        objSrc.src = this.resourcesURL + this.plusIcon;
    };
    };

courseTreePageItem.prototype.expandCollapse = function(){
    if (this.open) {
        this.collapse();
    }
    else {
        this.expand();
    };
    };

courseTreePageItem.prototype.focus = function(bckgColor, titleColor){
    var span = document.getElementById(this.id + '-anchor');
    var par = document.getElementById(this.id + '-focus');
    
    if (span) {
        if (bckgColor != 'transparent') {
            span.style.background = bckgColor;
		}
        if (titleColor != 'transparent') {
            span.style.color = titleColor;
		}
    }
    if (par && bckgColor != 'transparent') {
        par.style.background = bckgColor;
	}
};

courseTreePageItem.prototype.unselect = function() //pg2
{
	var span = document.getElementById(this.id+'-anchor');
	var par = document.getElementById(this.id+'-focus');
	
	if (span) span.style.background = 'transparent'; //'#FFFFFF'; //pg2-fix -after click on page (component on external page in new window), all rows' background turns to white (not transparent)
	if (par) par.style.background = 'transparent';
};

courseTreePageItem.prototype.isInArray = function(){
    for (var i = 0; i < arr.length; i++) {
        if (arr[i] == value) {
            return true;
        };
            };
    return false;
}

courseTreePageItem.prototype.toString = function(){
    var str = '';
    var treeComp = page.all[this.compName];
    var chaptersNamesArr = treeComp.chaptersNames.split('||');
    var fontWeight = (treeComp.chaptersBold && this.isChapter) ? 'bold' : 'normal';
    var fontColor = (this.isChapter || !treeComp.colorOnlyChapterPage) ? this.chapterColor : this.fontColor;
    var showIco = treeComp.showTreeIcons2; //pg2-added '2', cause there was a conflict in 'definition.xml'
	  
    if (treeComp.isInArray(chaptersNamesArr, this.title)) {
        return str;
    };
    
	var myParent = this.parentObj;
	var expandStr = '';
	var indent = '';
	var fixDisplay = '';
	var fixPaddingLeft = (typeof(this.paddingLeft) != "undefined" ? 'padding-left:' + this.paddingLeft + 'px; ' : '');
	if (!is.ie) fixDisplay = 'display: table;'; //pg2-in Firefox div wasn't resized to span's width
	str = '<p id="' + this.id + '-focus" style="white-space: nowrap; ' + fixPaddingLeft + fixDisplay + '">';
    
    while (myParent) {
        myParent = myParent.parentObj;
        indent = "<img src=\"" + this.resourcesURL + this.blankIcon + "\">" + indent;
    };
    
    if (this.children.length > 0) {
        expandStr += "<img id=\"" + this.id + "-indent\" onclick=\"pc." + this.compName + ".pagesArr[" + this.index + "].expandCollapse();\" src=\"" + this.resourcesURL + this.plusIcon + "\">";
    }
    else {
        expandStr += "<img src=\"" + this.resourcesURL + this.blankIcon + "\">"
    };
    
    str += indent;
    str += expandStr;
	
	str += "<span>&nbsp;</span>"; //pg2- a little bit of space between expand icon and status icon
    
	if (showIco) {
		switch (this.status) {
			case 'I':
				str += "<img id=\"" + this.compName + "_" + this.posInStruct + "_icon\" src=\"" + this.resourcesURL + this.pageIncompleted + "\">";
				break;
			case 'C':
				str += "<img id=\"" + this.compName + "_" + this.posInStruct + "_icon\" src=\"" + this.resourcesURL + this.pageCompleted + "\">";
				break;
			case 'P':
				str += "<img id=\"" + this.compName + "_" + this.posInStruct + "_icon\" src=\"" + this.resourcesURL + this.pagePass + "\">";
				break;
			case 'F':
				str += "<img id=\"" + this.compName + "_" + this.posInStruct + "_icon\" src=\"" + this.resourcesURL + this.pageFail + "\">";
				break;
			default:
				str += "<img id=\"" + this.compName + "_" + this.posInStruct + "_icon\" src=\"" + this.resourcesURL + this.pageNotAtt + "\">";
				break;
		};
	}
    
	str += "<span>&nbsp;</span>"; //pg2- a little bit of space between icon and page name
	
    var prefix = '';
    if (this.addPrefix) {
        prefix = eval(this.posInStruct + 1) + '. ';
    };
    
    str += "<span id=\"" + this.id + "-anchor\" title=\"" + this.title + "\" style=\"";
	str += "font-weight: " + ((treeComp.treeFontBold) ? "bold":"normal") + ";";
	str += "font-style: " + ((treeComp.treeFontItalic) ? "italic":"normal") + ";";
	str += "text-decoration: " + ((treeComp.treeFontUnderline) ? "underline":"none") + ";";
	str += "font-family: " + this.fontFamily + ";";
	str += "font-size:" + this.fontSize + ";";
	str += "color: " + fontColor + ";";
	str += "cursor:pointer;\" onclick=\"" + this.action + "\" >" + prefix + this.title +  
			"<img src=\"" + this.resourcesURL + this.blankIcon + "\">" + //pg2-to expand backlight
			"</span>";
    str += "<div id=\"" + this.id + "-cont\" style=\"position: relative; display: " + ((this.open) ? 'block' : 'none') + ";\">";
	this.visible = 1;
    
    for (var i = 0; i < this.children.length; i++) {
        str += this.children[i].toString();
    };
    
    str += "</div>";
    str += "</p>";
    
    return str;
};
