/* browser values */
var opera = window.opera;
var ie5 = document.all && !opera;
var ns6 = document.getElementById;

/* PopupMenu init values */
var delayTime = 500; // delay time for hiding
var rowCnt = 10; // visible row count                       *
var colCnt = 3; // columns count                            *
var colSpace = 10; // space beetwen columns                 *
var reloadData = true; // reload PopupMenu every time
var divBackgroundColor = "#f2232a"; // PopupMenu color      *
var aStyleClass = "NORMAL"; // class for links              *,***
var aHoverColor = ""; // links hightlight color      **
var menuShift = 3; // shift PopupMenu by X and Y
/** COMMENTS
    *   - need to reload PopupMenu
    **  - reload by function "reloadHover"                  - do not work in opera yet
    *** - reload by function "reloadPStyle"                 - do not work in opera yet
*/
/* private zone */
var linkPrefix = ""; // user link
var target = "";

var popupData = ""; // menu data

var maincolor= "";
var hovercolor = "";

/* PopupMenu constants */
/* !DO NOT CHANGE! */
var POPUPMENU_NAME = "popupmenu";
var DIV_MAIN = '<DIV ID="$" style="position: absolute; border:1px solid black; z-index: 100; visibility: hidden;" onmouseover="javascript:clearHiding()" onmouseout="javascript:hidePopupMenu()"></DIV>';
var DIV_TAG_OPEN = '<DIV class="POPUPMENU" style="';
var DIV_TAG_CLOSE = '</DIV>\n';
var TABLE_TAG_OPEN = '<TABLE border="0" cellPadding="0" cellSpacing="0"';
var TABLE_TAG_CLOSE = '</TABLE>\n';
var P_TAG_STYLE = 'font-family: $; font-size: $; font-weight: bold;';

/* registerring event and style functions */
if(window.addEventListener && !opera) {
	HTMLElement.prototype.__defineGetter__("runtimeStyle", overrideStyles);
	window.constructor.prototype.__defineGetter__("event", overrideEvents);
}
function overrideEvents() {
	/* private */
	if(document.all) {
		return window.event;
	}
	func = overrideEvents.caller;
	while(func != null) {
		var arg0=func.arguments[0];
		if(arg0 && arg0.constructor == Event) {
			return arg0;
		}
		if(arg0 && arg0 == '[object Event]') {
			return arg0;
		}
		func = func.caller;
	}
	return null;
}
function overrideStyles(){
	/* private */
	return this.style;
}

/* adding popup menu */
if (ie5 || ns6) {
	document.write(setParams(DIV_MAIN, POPUPMENU_NAME));
}

/* init functions */
function initPopupMenu(idFirst, idLast, changeStep, idSelected, linkPrefix, target) {
	/* private */
	/* init PopupMenu */
	maincolor = '#AAAAAA';
	hovercolor = '#AAAAAA';
	if (popupData == "" || reloadData) {
		this.linkPrefix = linkPrefix;
		this.target = target;
		getPopupMenu().style.visibility = 'hidden';
		//reloadHover();
		//reloadPStyle();
		preparePopupMenu(idFirst, idLast, changeStep, idSelected);
	}
}
function reloadHover() {
	/* NOT USE */
	/* private */
	/* reload hover for PopupMenu links */
	if (opera){
	}
	else{
		if (isEmp(aHoverColor)) {
			return;
		}
		stl = getStyle("#" + POPUPMENU_NAME.toUpperCase() + " A:HOVER");
		reloadStyle(stl == null, "#" + POPUPMENU_NAME.toLowerCase() + " A:HOVER", "background-color: " + aHoverColor + ";");
	}
}
function reloadPStyle() {
	/* NOT USE */
	/* private */
	/* reload style for selected element */
	if (opera){
	}
	else{
		astl = getStyle("A." + aStyleClass.toUpperCase());
		if (astl == null) {
			return;
		}
		stl = getStyle("#" + POPUPMENU_NAME.toUpperCase() + " P");
		pstl = setParams(P_TAG_STYLE, astl.fontFamily);
		pstl = setParams(pstl, astl.fontSize);
		reloadStyle(stl == null, "#" + POPUPMENU_NAME.toLowerCase() + " P", pstl)
	}

}
function reloadStyle(newStyle, name, attr) {
	/* NOT USE */
	/* private */
	/* reload style for PopupMenu links */
	if (newStyle) {
		if (ie5) {
			document.styleSheets[0].addRule(name, attr);
		}
		else {
			document.styleSheets[0].insertRule(name + " {" + attr + "}", document.styleSheets[0].cssRules.length);
		}
	}
	else {
		for (i = 0; i < document.styleSheets.length; i++) {
			rules = getCSSRules(document.styleSheets[i]);
			if (rules == null){
				continue;
			}
			for (j = 0; j < rules.length; j++) {
				s = rules[j];
				if (s.selectorText.toUpperCase() == name) {
					if (ie5) {
						document.styleSheets[i].removeRule(j);
					}
					else{
						document.styleSheets[i].deleteRule(j);
					}
					reloadStyle(true, name, attr);
					return;
				}
			}
		}
	}
}

/* business functions */
function roundData(dbl) {
	/* private */
	return Math.ceil(dbl);
}
function getIntVal(val) {
	/* private */
	return isEmp(val) ? 0 : val;
}
function getStyle(name) {
	/* NOT USE */
	/* private */
	/* return registered on page style by name */
	for (i = 0; i < document.styleSheets.length; i++) {
		rules = getCSSRules(document.styleSheets[i]);
		if (rules == null){
			continue;
		}
		for (j = 0; j < rules.length; j++) {
			s = rules[j];
			if (s.selectorText.toUpperCase() == name) {
				return s.style;
			}
		}
	}
	return null;
}
function getCSSRules(styleSheet){
	/* NOT USE */
	/* private */
	/* return registered on page style by name */
	rules = styleSheet.rules;
	if (rules != undefined){
		return rules;
	}
	rules = styleSheet.cssRules;
	if (rules != undefined){
		return rules;
	}
	return null;
}
function isEmp(val){
	/* private */
	return val == null || val == undefined || val == "" && val != "0";
}
function getPopupMenu() {
	/* private */
	/* return PopupMenu as object on HTML page */
	return ie5 ? document.all.popupmenu : document.getElementById(POPUPMENU_NAME);
}
function execPopupCmd(page, idStart, idEnd) {
	/* private */
	/* execute user link */
	hideMenu();
	
	execLink = setParams(linkPrefix, page);
	execLink = setParams(execLink, idStart);
	execLink = setParams(execLink, idEnd);
	
	if (linkPrefix.substring(0, 10) == "javascript") {
		setTimeout(execLink, 0);
	}
	else if (target=="!"){
		location.href = execLink;
	}	
	else{
		parent.frames[target].location.href = execLink;
	}	
}
function setParams(txt, val) {
	/* private */
	return txt.replace("$", val);
}

/* hide/show functions */
function showPopupMenu(idFirst, idLast, changeStep, idSelected, linkPrefix, target) {
	/* public */
	/* show PopupMenu */
	
	initPopupMenu(idFirst, idLast, changeStep, idSelected, linkPrefix, target);
	if (getPopupMenu().style.visibility == 'visible') {
		clearHiding();
	}
	else {
		positionMenu();
		getPopupMenu().style.visibility = 'visible';
		clearHiding();
	}
}
function clearHiding() {
	/* private */
	/* clear timer for hiding */
	if (window.delayhide) {
		clearTimeout(window.delayhide);
	}
}
function hidePopupMenu() {
	/* public */
	/* hide PopupMenu by timer */
	window.delayhide = window.setTimeout("hideMenu()", delayTime);
}
function hideMenu() {
	/* private */
	/* hide PopupMenu immediately */
	if (getPopupMenu().style.visibility == 'visible') {
		getPopupMenu().style.visibility = 'hidden';
	}
}
function preparePopupMenu(idFirst, idLast, changeStep, idSelected) {
	/* private */
	/* filling PopupMenu data */
	/* setting PopupMenu color */
	divTagOpen = DIV_TAG_OPEN;

	/* prepare table data */
	sz = roundData((idLast - idFirst) / changeStep / colCnt);

	/* hide scrollbar appearance */
	if (opera){
		pm = getPopupMenu();
		cont = TABLE_TAG_OPEN + '><TR><TD><A HREF="#" CLASS="' + aStyleClass + '"> TEST TEXT</A></TD></TR>' + TABLE_TAG_CLOSE;
		pm.innerHTML = cont;
		pm.style.left = 0;
		pm.style.top = window.document.body.clientHeight - pm.offsetHeight * sz;
	}

	popupData = "";
	for (i = 0; i < sz; i++) {
		popupData += "<TR>"
		for (j = 0; j < colCnt; j++) {
			posStart = idFirst + (i + j * sz) * changeStep;
			if (posStart > idLast) {
				popupData += '<TD class="TABELLE">&nbsp;</TD>';
				break;
			}
			posEnd = idFirst + (i + 1 + j * sz) * changeStep - 1;
			if (posEnd > idLast) {
				posEnd = idLast;
			}
			popupData += '<TD class="TABELLE"  align="center">';
			// bgcolor="' + maincolor + '"  onmouseover="this.bgcolor=\'' + hovercolor + '\'" onmouseout="this.bgcolor=\'' + maincolor + '\'"
			sltd = posStart <= idSelected && idSelected <= posEnd;
			capt = (posStart < posEnd) ? posStart + "..." + posEnd : posStart;
			if (posStart <= idSelected && idSelected <= posEnd) {
				popupData += '<A CLASS="' + aStyleClass + '"><b>[';
				popupData += capt;
				popupData += ']</b></A>&nbsp;';
			}
			else {
				popupData += '<A HREF="javascript:execPopupCmd(' + (i+j*sz+1) + "," + posStart + "," + posEnd + ')" CLASS="' + aStyleClass + '">';
				popupData += capt;
				popupData += '</A>&nbsp;';
			}
			popupData += '</TD>'
		}
		if (opera){
			popupData += '<TD class="TABELLE" width="15">&nbsp;</TD>'
		}
	popupData += '</TR>\n';
	}

	/* setting menu params */
	getPopupMenu().innerHTML = TABLE_TAG_OPEN + ' ID="tblPopupMenu">\n' + popupData + TABLE_TAG_CLOSE;
	tbl = document.getElementById("tblPopupMenu");
	w = tbl.offsetWidth + (colCnt - 1) * colSpace;
	if (tbl.rows.length > rowCnt) {
		h = roundData(tbl.offsetHeight / tbl.rows.length * rowCnt);
	}
	else {
		h = tbl.offsetHeight;
	}
	tmp = divTagOpen + ' height:' + h + 'px;">\n';
	tmp += TABLE_TAG_OPEN + ' width="' + w + 'px">\n';
	popupData = tmp + popupData;
	popupData += TABLE_TAG_CLOSE;
	popupData += DIV_TAG_CLOSE;
	getPopupMenu().innerHTML = popupData;
	
	//if (opera){
	//	return;
	//}

	pm = getPopupMenu();
	sm = pm.childNodes[0];
	tb = sm.childNodes[0];
	if (getIntVal(tb.scrollWidth) != 0 || opera) {
		pm.style.width = pm.scrollWidth - sm.scrollWidth + tb.scrollWidth;
	}
	else if (tbl.rows.length > rowCnt) {
		pm.style.width = w + 16;
	}
}
function positionMenu() {
	/* private */
	/* positioning PopupMenu */
	shiftX = menuShift;
	shiftY = menuShift;

	posX = event.clientX;
	posY = event.clientY;

	pm = getPopupMenu();
	b = window.document.body;

	ww = !isEmp(b.clientWidth) ? b.clientWidth : window.innerWidth;
	wh = !isEmp(b.clientHeight) ? b.clientHeight : window.innerHeight;
	mw = pm.offsetWidth;
	mh = pm.offsetHeight;

	wsx = !isEmp(b.scrollLeft) ? b.scrollLeft : window.pageXOffset;
	wsy = !isEmp(b.scrollTop) ? b.scrollTop : window.pageYOffset;

	if (posX + mw > ww){
		posX -= mw;
		if (posX < 0){
			posX = 0;
		}
		else{
			shiftX = -shiftX;
		}
	}
	if (posY + mh > wh){
		posY -= mh;
		if (posY < 0){
			posY = 0;
		}
		else{
			shiftY = -shiftY;
		}
	}

	pm.style.left = posX + wsx + shiftX;
	pm.style.top= posY + wsy + shiftY;
}

