/*
 * Copyright (C) 1995-2014 by 4system Polska Sp. z o.o.
 * All Rights Reserved. v4
 */
 
componentLexiconTree = function(name)
{
	this.componentSimplePopup = componentSimplePopup;
	this.componentSimplePopup(name);

	this.selected = -1;
	this.filter = "";
	this.scrollable = true;
	this.resourcesStatus = "online";
	this.resourcesURL = resourcesURL;
	this.keyword = "";

	this.cssTextOut = "";
	this.cssTextOver = "";
	this.closeButtonOffsetTop = 0;
	this.closeButtonOffsetRight = 0;
	this.headerTextOffsetTop = 0;
	this.headerTextOffsetLeft = 0;
	this.headerTextOffsetRight = 0;
	this.textAreaOffsetTop = 0;
	this.textAreaOffsetDown = 0;
	this.textAreaOffsetLeft = 0;
	this.textAreaOffsetRight = 0;
};

componentLexiconTree.prototype = new componentSimplePopup;
componentLexiconTree.prototype.type = "componentLexiconTree";

componentLexiconTree.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.arrowTopOver, this.arrowTopOut, this.arrowBottomOver, this.arrowBottomOut, this.sliderOver, this.sliderOut, this.sliderBckg ];
	this.getPreloadImagesArr();	 

	this.setCompleteArrayInfo(this.completeInfoStyleImages);
	this.captureMouseEvents();
};

componentLexiconTree.prototype.onCreate = function(obj)
{
	if (lexicon.items.length > 0) {
		this.selected = 0;
	};

	var ff = this.getProperty(this.treeCSSStyle, "fontFamily", "Arial");
	var fs = this.getProperty(this.treeCSSStyle, "fontSize", "2");
	var fc = this.getProperty(this.treeCSSStyle, "fontColor", "#000000");
	var fw = this.getProperty(this.treeCSSStyle, "fontWeight", "bold");
	var td = this.getProperty(this.treeCSSStyle, "textDecoration", "none");
	this.cssTextOut = 'text-decoration: ' + td + '; font-family: ' + ff + '; font-size:' + getFontSize(fs, "px") + '; color: ' + fc + '; font-weight: ' + fw + ';';

	var ff = this.getProperty(this.treeCSSStyleHover, "fontFamily", "Arial");
	var fs = this.getProperty(this.treeCSSStyleHover, "fontSize", "2");
	var fc = this.getProperty(this.treeCSSStyleHover, "fontColor", "#000000");
	var fw = this.getProperty(this.treeCSSStyleHover, "fontWeight", "bold");
	var td = this.getProperty(this.treeCSSStyleHover, "textDecoration", "none");
	this.cssTextOver = 'text-decoration: ' + td + '; font-family: ' + ff + '; font-size:' + getFontSize(fs, "px") + '; color: ' + fc + '; font-weight: ' + fw + ';';
};

componentLexiconTree.prototype.componentLexiconTreeBaseOnPaint = componentLexiconTree.prototype.onPaint;
componentLexiconTree.prototype.onPaint = function(obj)
{
	this.keyword = unescape(getCommandUrl("key"));
	this.generateOutline();
	this.doClick(null);
	this.componentLexiconTreeBaseOnPaint();
	this.setScroll();
};

componentLexiconTree.prototype.getProperty = function(propStr, propName, propDef)
{
	var result = propDef;
	var propArr = propStr.split(";");
	for (var i=0; i<propArr.length; i++) {
		var pArr = propArr[i].split(":");
		if (trim(pArr[0]) == propName) {
			result = trim(pArr[1]);
			break;
		};
	};
	return result;
};

componentLexiconTree.prototype.getItemOffsettop = function()
{
	var obj = document.getElementById(this.id+"a"+this.selected);

	var curtop = 0;
	if (obj) { //pg2
		if (obj.offsetParent) {
			while (obj.offsetParent) {
				curtop += obj.offsetTop;
				obj = obj.offsetParent;
			};
		} else
		if (obj.y) {
			curtop += obj.y;
		};
	};
	return curtop-this.y;
};

componentLexiconTree.prototype.onAfterLoad = function()
{
	this.setScroll();
};

componentLexiconTree.prototype.setScroll = function()
{
	this.getTextHeight();
	this.setHTMLObiects();
	this.moveDivToItem();

	if (this.buttonsActive) this.moveSliderToItem();
};

componentLexiconTree.prototype.moveDivToItem = function()
{
	var lexiconTreeDIV = document.getElementById(this.id + "_tableDiv");
	if (lexiconTreeDIV) lexiconTreeDIV.scrollTop = 0;
};

componentLexiconTree.prototype.moveSliderToItem = function()
{
	var	tableDiv = document.getElementById(this.id + "_tableDiv");
	var	trackDiv = document.getElementById(this.id + "_trackDiv");
	var scrollPos = 0;
	var offsettop = 0;

	if (this.selected != 0) {
		offsettop = this.getItemOffsettop();
	};
	tableDiv.scrollTop = offsettop;

	var maxScrollHeight = tableDiv.scrollHeight - this.h + this.headerHeight + 2*this.contentPadding + 2*this.textSpacing + this.footerHeight + this.checkboxesFieldH;
	maxScrollHeight = (maxScrollHeight == 0)?1:maxScrollHeight;
	var percScrollPos = (tableDiv.scrollTop/maxScrollHeight);

	scrollPos = this.sliderLowOffset + this.trackHeight*percScrollPos - this.getImageHeight('sliderOut')*percScrollPos;
	trackDiv.style.top = scrollPos;
};

componentLexiconTree.prototype.reinit = function(obj)
{
	this.generateOutline();
	this.runPNGFix();
	this.selected = this.getFirstItemID();
	if (this.selected != -1) this.doClick(null);
	this.setScroll();
};

componentLexiconTree.prototype.getFirstItemID = function()
{
	var i = 0;
	while (!document.getElementById(this.id + "a" + i)) i++;
	return i;
};

componentLexiconTree.prototype.generateOutline = function()
{
	var outline = '<table>';
	if (lexicon.items.length == 0) {
			outline += '<tr><td valign="top" style="padding-top: 3px">';
			outline += '<img id="' + this.id + 'img' + i + '" src="' + this.resourcesURL + this.treeIMG + '" width="' + this.treeIMGWidth + '" height="' + this.treeIMGHeight + '" border="0"/>';
			outline += '</td><td>';
			outline += '<a id="' + this.id + 'a' + i + '" href="javascript:void(0);" onmouseover="pc.'+this.id+'.doOver(this);" onmouseout="pc.'+this.id+'.doOut(this);" style="' + this.cssTextOut + '" >'+this.noticeInfo+'</a></br>';
			outline += '</td></tr>';
	} else {
		this.selected = 0;
		for (var i=0; i<lexicon.items.length; i++) {
			if (!lexicon.items[i].visible) continue;
			outline += '<tr><td valign="top" style="padding-top: 3px">';
			outline += '<img id="' + this.id + 'img' + i + '" src="' + this.resourcesURL + this.treeIMG + '" width="' + this.treeIMGWidth + '" height="' + this.treeIMGHeight + '" border="0"/>';
			outline += '</td><td>';
			outline += '<a id="' + this.id + 'a' + i + '" href="javascript:void(0);" onmouseover="pc.'+this.id+'.doOver(this);" onmouseout="pc.'+this.id+'.doOut(this);" style="' + this.cssTextOut + '" title="' + unescape(lexicon.items[i].key) + '" onclick="pc.'+this.id+'.doClick(this);">' + unescape(lexicon.items[i].key) + "</a></br>";
			if (this.keyword == unescape(lexicon.items[i].key)) {
				this.selected = i;
			};
			outline += '</td></tr>';
		};
	};
	outline += '</table>';
	var content = this.findContentObj();
	content.innerHTML = outline;
	//this.setHTML(outline);
};

componentLexiconTree.prototype.doOver = function(obj)
{
	var no = obj.id.replace(this.id + "a", "");

	if(this.selected != no){
		var img = document.getElementById(this.id + "img" + no);
		this.setImageSrc(this.resourcesURL + this.treeIMGOver, img);
	};
	obj.style.cssText = this.cssTextOver;
};

componentLexiconTree.prototype.doOut = function(obj)
{
	var no = obj.id.replace(this.id + "a", "");
	if (no != this.selected) {
		var img = document.getElementById(this.id + "img" + no);
		this.setImageSrc(this.resourcesURL + this.treeIMG, img);
		//img.src = this.resourcesURL + this.treeIMG;
	};
	obj.style.cssText = this.cssTextOut;
};

componentLexiconTree.prototype.doClick = function(obj)
{
	var fVis = -1;
	for (var i=0; i<lexicon.items.length; i++) {
		if (!lexicon.items[i].visible) continue;
		//if (fVis == -1) fVis = i;
		var img = document.getElementById(this.id + "img" + i);
		this.setImageSrc(this.resourcesURL + this.treeIMG, img);
	};
	if (obj == null) {
		obj = document.getElementById(this.id + "a" + this.selected);
	};
	if (obj == null) {
		var lex_content_comp = page.getElementsType("componentLexiconContent");
		for (var i=0; i<lex_content_comp.length; i++) {
			var comp = lex_content_comp[i];
			comp.reFillComponent(null);
		};
		return;
	};
	var no = obj.id.replace(this.id + "a", "");
	var img = document.getElementById(this.id + "img" + no);
	this.setImageSrc(this.resourcesURL + this.treeIMGSelected, img);
	this.selected = no;
	var lex_content_comp = page.getElementsType("componentLexiconContent");
	for (var i=0; i<lex_content_comp.length; i++) {
		var comp = lex_content_comp[i];
		comp.reFillComponent(lexicon.items[this.selected]);
	};
};

componentLexiconTree.prototype.setFilter = function(value)
{
	lexicon.setFilter(value);
};

componentLexiconTree.prototype.setKey = function(value)
{
	for (var i=0; i<lexicon.items.length; i++) {
		if (!lexicon.items[i].visible || lexicon.items[i].key != value) continue;
		var obj = document.getElementById(this.id + "a" + i);
		this.doClick(obj);
		this.obj.scrollTop = obj.offsetParent.offsetTop;
		break;
	};
};
