function GEHelpLayer(defaultHelp, popupURL, firstFieldId) {

    this.element = document.createElement("div");
    this.element.id = "help";
    this.element.className = "mainwindow";
    // main window
    this.element.style.position = "absolute";
    this.element.style.visibility="visible";
    this.element.style.width = '163px';
    this.element.style.zIndex = 999;

    this.styleTop = 'margin-bottom: 6px;';
    this.styleContent = 'border: 1px solid #ccc; background-color: #F7F7F7; margin: 0px; padding: 5px; clear: left; font-size: 11px; line-height: 14px;';
    
    this._parent = document.getElementById('parentform');
    this._parent.appendChild(this.element);

    this.defaultHelp = "";
    this.infoOnClick = "";
    this.fieldName = new Array();
    this.fieldHelp = new Array();

    this.helpTop = "";
    this.helpBottom = "";
    this.language = "cz";

    this.isFixed = false; //Set up to true if you want to fixed top offset position of help window.

    this.defaultHelpTopPad = 0;

    this.parentDiv = document.getElementById('mainBody');
    this.parentHeight = this.parentDiv.offsetHeight;
    this.parentOffset = this.getOffset(this.parentDiv);

    this.makeHelpTop();
    this.makeHelpBottom();

    this.templateWidth = this.getTemplateWidth();
    this.width = 163; // width of help window, height is automaticaly setted by text DIV
    this.setDefaultHelp(defaultHelp);

    if(popupURL && popupURL.length>0) {
        this.setInfoOnClick('javascript: popupPage(\''+popupURL+'\',\'Popup_help\',420,400,\'yes\');');
    }

    // look for firstField element (needed for initialization height offset)
    this.firstField = document.getElementById(firstFieldId);    

    this.showHelp();
    this.initHelpOffsetTop();
}

GEHelpLayer.prototype.getTemplateWidth = function () {

    width = 0;
    var _elm = document.getElementById('document');

    if ((typeof _elm != "undefined") && (_elm)) {
        width = _elm.offsetWidth;
    } else {
        _elm = document.getElementById('documentPopup');
        width = _elm.offsetWidth;
    }

    return width;
}

GEHelpLayer.prototype.setDefaultTopPad = function (top) {
       this.defaultHelpTopPad = top;
       this.initHelpOffsetTop();
}

GEHelpLayer.prototype.oldWidth = function () {
        this.width = 132;
        this.element.style.width = '132px';
        this.showHelp();
        this.initHelpOffsetTop();
}

GEHelpLayer.prototype.initHelpOffsetTop = function () {
       // initialize help offset, if there is firstField present, set offset to this field, otherwise to parent element
       if ((typeof this.firstField != "undefined") && (this.firstField)) {
            var offset = this.getOffset(this.firstField);
            offset -= 26; // minus height of helptop area
            this.element.style.top = (offset - (this.defaultHelpTopPad / 1) + 'px');
       } else {
            this.element.style.top = ((this.getOffset(this._parent) - (this.defaultHelpTopPad / 1)) + 'px');
       }
       this.setHelpOffsetLeft();
}

GEHelpLayer.prototype.setFixed = function (fixed) {
    this.isFixed = fixed;
}

GEHelpLayer.prototype.setVisible = function (visible) {
    this.element.style.visibility= (visible)? "visible" : "hidden";
}


GEHelpLayer.prototype.setLanguage = function (language) {
    this.language = language;
    this.makeHelpTop(); // init top of help
    this.showHelp(); // make new help
}

GEHelpLayer.prototype.setHelpOffsetLeft = function () {

        this.clientWidth = document.body.scrollWidth;
        this.delta = 0;

        if (this.clientWidth > this.templateWidth) {
            this.delta = (this.clientWidth - this.templateWidth) / 2;
        }

        if (this.delta > 0) {
            this.offsetLeft = (this.clientWidth - this.delta - this.width);
        } else {
            this.offsetLeft = (this.templateWidth - this.width);
        }

        if (this.offsetLeft > 0) {
            this.element.style.left = (this.offsetLeft + 'px');
        }
}

GEHelpLayer.prototype.getOffset = function(elm) {

			var par = elm;
			offset = 0;

			while (true) {
				tmp = par.offsetTop;
				if (par.offsetParent.id == 'body') {
					offset += tmp;
					break;
				} else {
					par = par.offsetParent;
					offset += tmp;
				}
			}

			return offset;
		}

GEHelpLayer.prototype.setHelpPosition = function(elm) {

            this.parentHeight = this.parentDiv.offsetHeight;
            this.parentOffset = this.getOffset(this.parentDiv);

			this.setHelpOffsetLeft();

			scrollHeight = document.body.scrollHeight;
            windowHeight = this.element.offsetHeight;

			if (document.documentElement && document.documentElement.scrollTop) {
	            scrollTop = document.documentElement.scrollTop;
            } else {
                scrollTop = document.body.scrollTop;
            }

			if (document.compatMode && document.compatMode != "BackCompat") {
                clientHeight = document.documentElement.clientHeight;
            }
            else {
                clientHeight = document.body.clientHeight;
            }

			offset = this.getOffset(elm);

            offset -= 26; // minus height of helptop area

            // window correction
			if ((offset + windowHeight) > scrollHeight) {
				offset -= ((offset + windowHeight + 10) - scrollHeight);
			} else if ((offset + windowHeight) > (clientHeight + scrollTop)) { // scroll correction
				offset -= ((offset + windowHeight + 10) - (clientHeight + scrollTop));
			}

			// body area correcction
			if ((offset + windowHeight) > (this.parentOffset + this.parentHeight)) {
				offset -= ((offset + windowHeight + 2) - (this.parentOffset + this.parentHeight));
			}

			this.element.style.top = (offset + 'px');
}

// sets position for help that is fixed
GEHelpLayer.prototype.setHelpPositionFixed = function(elm) {
    if (elm != null) {
        var offset = this.getOffset(elm);
        offset -= 26; // minus height of helptop area
        this.element.style.top = (offset + 'px');
    }
}


GEHelpLayer.prototype.setInfoOnClick = function(infoOnClick) {
    this.infoOnClick = infoOnClick;
}

GEHelpLayer.prototype.makeHelpTop = function(spec) {
    var spec1 = '';
    if (spec!=null && spec!='') {
      spec1 = '_'+spec;
    }
    this.helpTop = '<div id=\"helptop\" style=\"' + this.styleTop + '\"><img src=\"/images/money/' + this.language + '/buttons/helptop'+spec1+'.gif\" /></div>';
}

GEHelpLayer.prototype.makeHelpBottom = function() {
    this.helpBottom = '<div id=\"helpbottom\">';
    if(this.infoOnClick && this.infoOnClick.length > 0) {
        this.helpBottom += '<a href=\"#\" onclick=\"'+this.infoOnClick+'\" border=\"0\">';
    }

    this.helpBottom += '<img src=\"/images/money/cz/buttons/helpbottom.gif\" />'

    if(this.infoOnClick && this.infoOnClick.length > 0) {
        this.helpBottom += '</a>';
    }
    this.helpBottom += '</div>';
}

GEHelpLayer.prototype.setDefaultHelp = function(help) {
    this.defaultHelp = help;
}
GEHelpLayer.prototype.setFieldHelp = function(name,help) {
    var pos = this.findID(name);
    if(pos==-1) pos = this.fieldName.length;
    this.fieldName[pos] = name;
    this.fieldHelp[pos] = help;
}
GEHelpLayer.prototype.findID = function(name) {
    for( i=0 ; i<this.fieldName.length; i++ )
        if(name == this.fieldName[i]) return i;
    return -1;
}
// skip position parameter means it`ll not do position recalculation
// needed for initial running/displaying help
// when this is run first, skipPosition is set to true and we relay on position calculated in setup/init
GEHelpLayer.prototype.showHelp = function(elm, name, skipPosition) {
    var pos = (name == null) ? -1 : this.findID(name);
    this.setHTML((pos>-1) ? this.fieldHelp[pos] : this.defaultHelp);
    if ((skipPosition == null || !skipPosition) && !this.isFixed && elm != 'undefined' && elm) this.setHelpPosition(elm);
}
GEHelpLayer.prototype.getFieldHelp = function(name) {
    var pos = this.findID(name);
    return ((pos>-1) ? this.fieldHelp[pos] : null);
}
GEHelpLayer.prototype.getDefaultHelp = function(name) {
    return this.defaultHelp;
}

GEHelpLayer.prototype.setHTML = function(actHelp) {
    this.element.innerHTML = this.helpTop + '<div id=\"helptext\" style=\"' + this.styleContent + '\">' + actHelp + '</div>';// + this.helpBottom; bottom not used
}

GEHelpLayer.prototype.setSpec = function(spec) {
    if (spec=='avon') {
      this.styleContent = 'border: 1px solid #c5318c; background-color: #F7deef; margin: 0px; padding: 5px; clear: left; font-size: 11px; line-height: 14px; color: #c5318c';
    }
    this.makeHelpTop(spec); // init top of help
    this.showHelp(); // make new help
}

