var triggers = new Array();
var timeOutID;
var activeSubMenu = -1;
var menuNumber = 1;
var ns = (document.layers) ? true: false;
var ie = (document.all) ? true: false;
var ieVersion = ieVer();
var dom = (document.getElementById) ? true: false;
var safari = (navigator.userAgent.indexOf('Safari') != -1) ? true: false;
var opera = (navigator.userAgent.indexOf('Opera') != -1) ? true: false;
var lastTriggerRef;
var basicZIndex = 2000;
var openMenus = Array();
var visitedTriggers = Array();

function ieVer() {
	msieIndex = navigator.appVersion.indexOf("MSIE") + 5;
	return (parseFloat(navigator.appVersion.substr(msieIndex, 3)));

}

function getDivReference(key) {
	if (ie) {
		return document.all[key];

	} else if (ns) {
		return document.layers[key];

	} else if (dom) {
		return document.getElementById(key);

	}

}

function getStyleReference(key) {
	var ref = getDivReference(key)
	if (ie || dom) {
		return ref.style;

	} else if (ns) {
		return ref
	}

}

function generateDIV(top, left, width, sectionRef, content, id, zIndex, visibility, subImage, display,lvl,omitTopBorder) {
	var currentZIndex = basicZIndex + zIndex;

	if (content[1] == 1) {
		var temp = content[0].split("'");
		var lnk = (temp[1] && temp[1].indexOf('optional_bullet' )== -1 ) ? temp[1] : 'javascript:void(0)';

	} else {
		var lnk = content[1];

	}
	if(  content[0].indexOf('onClick') == - 1  ) { 
		out = '<a href="' + lnk + '" style="text-decoration:none"'
		if (content[2]) {
			out += ' target="' + content[2] + '"';
		}
		out += '>';
	} else {
		out = '';
	}
	out += '<div id="' + id + '" ';
	out += 'align="' + sectionRef['alignment'] + '" style="';
	out += 'position: absolute;';
	out += 'top:' + top + 'px;';
	out += 'left:' + left + 'px;';
	out += 'width:' + width + 'px;';
	

		 if( typeof sectionRef['padding'] != "number" ) {
			var ow = 2 * sectionRef['borderWidth'] +  ( sectionRef['padding'][ 1 ] + sectionRef['padding'][ 3 ]);
			out += 'width:' + (width - (2 * sectionRef['borderWidth'] +   sectionRef['padding'][ 3 ] )) + 'px;';
		} else {
			out += 'width:' + (width - (2 * sectionRef['borderWidth'] + 2 * sectionRef['padding'])) + 'px;';
		}


	if( typeof sectionRef['padding'] != "number" ) {
		out += 'padding: ' + sectionRef['padding'][ 0 ] + 'px ' + sectionRef['padding'][ 1 ] +  'px '; 
		if( ie && omitTopBorder ) {
			out += (sectionRef['padding'][ 2 ]-2) + 'px ' + (  sectionRef['padding'][ 3 ] ) +  'px;'; 
		} else {
			out += sectionRef['padding'][ 2 ] + 'px ' + sectionRef['padding'][ 3 ] +  'px;';
		}
	} else {
		out += 'padding:' + sectionRef['padding'] + 'px;';
	}
	if( omitTopBorder ) {
		out += 'border:' + sectionRef['borderWidth'] + 'px solid ' + sectionRef['borderColor'] + ';';
	} else {
		var bw = sectionRef['borderWidth'] + 'px ';
		out += 'border: solid ' + sectionRef['borderColor'] + ';'
		out += 'border-width:'  + bw + bw +'0px ' + bw + ';';
	}
	out += 'color:' + sectionRef['fgColorOut'] + ';';
	out += 'font-family:' + sectionRef['font'] + ';';
	out += 'font-size:' + sectionRef['fontSize'] + 'px;';
	out += 'font-weight:' + sectionRef['fontWeight'] + ';';
	out += 'font-style:' + sectionRef['fontStyle'] + ';';
	out += 'z-index:' + currentZIndex + ';';
	if (!lvl) {
		if (ie && sectionRef['bg_img_section'] != '' ) {
			out += 'background:url(' + sectionRef['bg_img_section'] + ');'

		} else if( sectionRef['bg_img_section'] != '' ) {
			out += 'background:url(' + sectionRef['bg_img_section'] + ');'

		}
		out += 'background-repeat:repeat-y;'

	}
	if ((mainProps['bgFirstSection'] == 1 && sectionRef == sectionProps1) || (mainProps['bgSecondSection'] == 1 && sectionRef == sectionProps2)) {
		out += 'background-color:' + sectionRef['bgColorOut'] + ';';

	}
	out += 'display:' + display + ';';
	if( content[ 0 ].indexOf('Portal') != - 1) {
		var temp = content[ 0 ].split( 'S-Portal' );
		var portaldiv = '<div style="position:absolute;left:'+ left + 'px; top:' + (top + 1) + 'px;"><img style="vertical-align:absmiddle" src="/static/images/slogo.gif" border=0 hspace=1></div>';
		content[ 0 ] = temp[ 0 ] + '<img width=12 height=1 src="/static/images/slogo_trans.gif">-Portal' +  temp[ 1]; 
	}
	if (subImage && lvl > 0) {
		// out += '" onMouseOver=rollOver(this) onMouseOut=rollOut(this)><img src ="' + subImage + '" border = 0  vspace = ' + (sectionRef['padding'] / 2) + ' align = "right">' + content[0] + '</div>';
		out += '" onMouseOver=rollOver(this) onMouseOut=rollOut(this)>' + content[0] + '<img src ="' + subImage + '" border = 0  vspace = ' + (sectionRef['padding'] / 2) + ' style="margin-left:15px;"></div>';

	} else {
		out += '" onMouseOver=rollOver(this) onMouseOut=rollOut(this)>' + content[0] + '</div>';

	}
	if(  content[0].indexOf('onClick') == - 1  ) {
		out += '</a>';
	}
	document.write(out);
	if( content[ 0 ].indexOf('Portal') != - 1) {
		document.write(portaldiv);
	}

}


//Prototypes for ie 5.5 and below
function shift() {
	var val = this[0];
	for (var i = 1; i < this.length; ++i) {
		this[i - 1] = this[i];

	}
	this.length--;
	return val;

}

function push(v) {
	this[this.length] = v;

}

function unshift() {
	var i = unshift.arguments.length;
	for (var j = this.length - 1; j >= 0; --j) {
		this[j + i] = this[j];

	}
	for (j = 0; j < i; ++j) {
		this[j] = unshift.arguments[j];

	}

}

if (ie && ieVersion <= 5.5) {
	Array.prototype.shift = shift;
	Array.prototype.unshift = unshift;
	Array.prototype.push = push;

}

//end prototypes
function reverseMenuEntries() {
	var i = 1;
	while (++i) {
		var menuRef = eval('menu' + i);
		if (menuRef == null) break
		var width = menuRef.shift()
		menuRef.reverse();
		menuRef.unshift(width);

	}

}


function generateMenu(lvl, parentRef) {
	if (menuNumber == null) {
		menuNumber = 1;

	}
	var display = (menuNumber == 1) ? 'inline' : 'none';
	if (lvl == null) {
		if (mainProps['switchDirection']) {
			reverseMenuEntries();

		}
		var lvl = 0;

	}
	var menuRef = eval('menu' + menuNumber);
	var sectionRef = (menuNumber == 1) ? sectionProps1: sectionProps2;
	var width = menuRef[0];
	if(!ie) { width += 1 };
	var count = menuRef.length;
	var height = 0;
	var top = 0;
	var left = 0;
	var baseID = 'menu_' + menuNumber + '_';
	var visibility = (menuNumber == 1) ? 'visible': 'hidden';
	var display = (menuNumber == 1) ? 'inline': 'none';
	for (var i = 1; i < count; i++) {
		id = baseID + i
		if( typeof sectionRef['padding'] != 'number' ) {
			height = (sectionRef['fontSize'] + sectionRef['borderWidth']  + sectionRef['padding'][ 0 ] + sectionRef['padding'][ 2 ]  );
		} else {
			height = (sectionRef['fontSize'] + sectionRef['padding'] * 2 +  sectionRef['borderWidth']      );
		}
		if (lvl == 0) {
			if( ie ) { width = menuRef[0] +  2  }; 
			if (mainProps['orientation'] == 'horizontal') {
				top = mainProps['y'];
				if (typeof menuRef[0] != "number") {
					if( typeof menu_language_id != 'undefined' ) {
						left = menuRef[0][(i - 1)][ menu_language_id ];
					} else {
						left = menuRef[0][(i - 1)];
					}
					if( left == -1 ) { continue; }
				} else if (mainProps['x'] == -1) {
					left = getMenuOffset() + (menuRef[0] * (i - 1))

				} else {
					left = mainProps['x'] + (menuRef[0] * (i - 1));

				}
				if (i > 1) {
					left += leftCorrection * (i - 1);

				}

			} else {
				if (mainProps['switchDirection']) {
					top = mainProps['y'] - height * (i - 1);

				} else {
					top = mainProps['y'] + height * (i - 1);

				}
				if (i > 1) {
					if (mainProps['switchDirection']) {
						top -= topCorrection * (i - 1);

					} else {
						top += topCorrection * (i - 1);

					}

				}
				left = mainProps['x'];

			}

		} else {

			var pixelLeft = ie ? (parentRef.style.pixelLeft)  : (safari ? parseInt(parentRef.style.left) + 6: parseInt(parentRef.style.left));
			var pixelWidth = ie ? parentRef.style.pixelWidth: (safari ? parseInt(parentRef.style.width): parseInt(parentRef.style.width));
			pixelWidth += ((mac || dom) && !safari) ? parseInt(parentRef.style.paddingLeft)  +  parseInt(parentRef.style.paddingRight)  : 0;
			if( ie ) { 
				if( typeof sectionRef['padding'] != 'number' ) {

					pixelLeft -=-2+ sectionRef['padding'][1 ] + sectionRef['padding'][3 ]
			 	} else {
					pixelLeft -=-2+ sectionRef['padding'] * 2
				}
			}
			var pixelTop = ie ?  parentRef.style.pixelTop: (safari ? parseInt(parentRef.style.top): parseInt(parentRef.style.top));
			height +=  topCorrection;
			if (mainProps['orientation'] == 'horizontal') {
				left = (lvl == 1) ? pixelLeft: pixelLeft + pixelWidth + leftCorrection;
			} else {
				left = pixelLeft + pixelWidth + leftCorrection  ;
			}

			if (mainProps['switchDirection']) {
				top = pixelTop - height * (i - 1);

			} else {
				top =-1 +  pixelTop + height   * (i - 1);

			}
			if (lvl == 1 && mainProps['orientation'] == 'horizontal') {
				var padding = (safari) ? parseInt(parentRef.style.paddingTop) : parseInt(parentRef.style.padding);
				var borderWidth = (safari) ? parseInt(parentRef.style.borderTopWidth) : parseInt(parentRef.style.borderLeftWidth)*2;
				var fontSize = parseInt(parentRef.style.fontSize);
				if (mainProps['switchDirection']) {
					top -= padding * 2 + borderWidth * 2 + fontSize + 4;

				} else {
					top += padding * 2 + borderWidth * 2 + fontSize + 4;

				}

			}

		}
		if(ie && lvl != 0 ) { left += 8; }
		if (opera && lvl != 0) {
			left += 2;
		}
		var obr = (i == count-1 );
		if (menuRef[i][1] == 1) {
			generateDIV(top, left, width, sectionRef, menuRef[i], id, menuNumber, visibility, sectionRef['subImage'], display,lvl, obr);
		} else {
			generateDIV(top, left, width, sectionRef, menuRef[i], id, menuNumber, visibility, '', display, lvl, obr);
		}
		if (menuRef[i][1] == 1) {
			triggers[triggers.length] = id;
			menuNumber++;
			generateMenu(++lvl, getDivReference(id));
			lvl--;

		}

	}

}

function rollOver(ref, pID) {
	var sectionRef = (ref.id.indexOf('menu_1_') != -1) ? sectionProps1: sectionProps2;
	if ((ref.id.indexOf('menu_1_') != -1) && activeSubMenu != -1) {
		resetOpenMenus();

	}

	if ((sectionRef == sectionProps1 && mainProps['bgFirstSection']) || (sectionRef == sectionProps2 && mainProps['bgSecondSection'])) {
		ref.style.backgroundColor = sectionRef['bgColorOver']

	}
	ref.style.color = sectionRef['fgColorOver'];
	ref.style.cursor = 'hand';
	var menuToTrigger = getTriggerRef(ref.id) + 1;
	if (menuToTrigger) {
		if (menuToTrigger != activeSubMenu && activeSubMenu != -1 && ref.id.indexOf('menu_' + activeSubMenu) == -1) {
			closeMenuByNumber(activeSubMenu)

		}

		var menuLength = eval('menu' + menuToTrigger + '.length');
		for (var i = 1; i < menuLength; i++) {
			getStyleReference('menu_' + menuToTrigger + '_' + i).display = 'inline';

		}
		activeSubMenu = menuToTrigger;
		lastTriggerRef = ref;
		visitedTriggers.push(lastTriggerRef);

	} else if (ref.id.indexOf('menu_' + activeSubMenu) == -1 && activeSubMenu != -1) {
		closeMenuByNumber(activeSubMenu);

	}
	if (ref.id.indexOf('menu_1_') != -1 && !menuToTrigger) {
		hideAllSubMenus()

	}

	clearTimeout(timeOutID);

}

function rollOut(ref, pID) {
	var sectionRef = (ref.id.indexOf('menu_1_') != -1) ? sectionProps1: sectionProps2;
	if ((sectionRef == sectionProps1 && mainProps['bgFirstSection']) || (sectionRef == sectionProps2 && mainProps['bgSecondSection'])) {
		if (ref != lastTriggerRef) {
			ref.style.backgroundColor = sectionRef['bgColorOut']

		}

	}
	if (ref != lastTriggerRef) {
		ref.style.color = sectionRef['fgColorOut'];
		//ref.style.cursor = 'default';

	}
	timeOutID = setTimeout('hideAllSubMenus()', timeGap);

}

function getTriggerRef(ref) {
	var tCount = triggers.length;
	for (var i = 0; i < tCount; i++) {
		if (triggers[i] == ref) {
			return i + 1;
			break;

		}

	}
	return - 1;

}

function hideAllSubMenus() {
	for (var m = 1; m <= menuNumber; m++) {
		var menuRef = eval('menu' + m);
		for (var i = 1; i < menuRef.length; i++) {
			if (lastTriggerRef) {
				baseID = 'menu_' + m + '_' + i;
				//getStyleReference(baseID).visibility = (menuRef == menu1) ? 'visible' :'hidden';
				getStyleReference(baseID).display = (menuRef == menu1) ? 'inline': 'none';
				var sectionRef = (baseID.indexOf('menu_1_') != -1) ? sectionProps1: sectionProps2;
				if ((sectionRef == sectionProps1 && mainProps['bgFirstSection']) || (sectionRef == sectionProps2 && mainProps['bgSecondSection'])) {
					getStyleReference(baseID).backgroundColor = sectionRef['bgColorOut'];

				}
				getStyleReference(baseID).color = sectionRef['fgColorOut'];


			}

		}

	}
	visitedTriggers.length = 0;
	openMenus.length = 0;

}

function resetOpenMenus() {
	for (var i = 0; i < openMenus.length; i++) {
		closeMenuByNumber(openMenus[i]);

	}
	for (var i = 0; i < visitedTriggers.length; i++) {
		var sectionRef = (visitedTriggers[i].id.indexOf('menu_1_') != -1) ? sectionProps1: sectionProps2;
		if ((sectionRef == sectionProps1 && mainProps['bgFirstSection']) || (sectionRef == sectionProps2 && mainProps['bgSecondSection'])) {
			visitedTriggers[i].style.backgroundColor = sectionRef['bgColorOut'];

		}
		visitedTriggers[i].style.color = sectionRef['fgColorOut'];

	}
	visitedTriggers.length = 0;
	openMenus.length = 0;

}

function closeMenuByNumber(menuToClose) {
	var menuLength = eval('menu' + menuToClose + '.length');
	for (var i = 1; i < menuLength; i++) {
		//getStyleReference('menu_' + menuToClose + '_' + i).visibility = 'hidden';
		getStyleReference('menu_' + menuToClose + '_' + i).display = "none";

	}
	if (lastTriggerRef) {
		var sectionRef = (lastTriggerRef.id.indexOf('menu_1_') != -1) ? sectionProps1: sectionProps2;
		if ((sectionRef == sectionProps1 && mainProps['bgFirstSection']) || (sectionRef == sectionProps2 && mainProps['bgSecondSection'])) {
			lastTriggerRef.style.backgroundColor = sectionRef['bgColorOut'];

		}
		lastTriggerRef.style.color = sectionRef['fgColorOut'];
		lastTriggerRef = null;

	}


}
