var rcsbuttons = new Array();
var hs = 0;
var TemplatePath = TemplatePath;
var mastervalue = 0;
var PageLoaded = PageLoaded;

function AddButton(bid, text, url, picture1) {
	rcsbuttons[bid] = new Array();
	rcsbuttons[bid] = new UpdateButtonx(bid, text, url, picture1);
}

function ClearSubMenu(bid) {
	rcsbuttons[bid].submenus = new Array();
}

function AddSubMenu(bid, text, url, picture1, js) {
	var l = rcsbuttons[bid].submenus.length;
	rcsbuttons[bid].submenus[l] = new Array();
	rcsbuttons[bid].submenus[l] = new UpdateSubMenux(bid, text, url, picture1, js);
}

function UpdateButtonx(bid, text, url, picture1) {
this.bid = bid;
this.text = text;
this.url = url;
this.picture1 = picture1;
this.picture2 = ''
this.picture3 = '';
this.clicktimer = 0;
this.submenus = new Array();
}

function UpdateSubMenux(bid, text, url, picture1, js) {
this.bid = bid;
this.text = text;
this.js = js;
this.url = url;
this.picture1 = picture1;
//this.clicktimer = 0;
}

function AddButtonImage(b, s, p) {
	if (s == 2) {
		rcsbuttons[b].picture2 = p;
	} else {
		rcsbuttons[b].picture3 = p;
	}
}

function getObj(objn) {
	if (document.getElementsByName(objn).length <= 1) {
		return document.getElementById(objn);
	} else {
		return document.getElementsByName(objn);
	}
}

function HideSubMenu() {
	var rcspopup = getObj('rcspopup');
	if (rcspopup != null) {
		rcspopup.parentNode.removeChild(rcspopup);
	}
}

function ShowSubMenu(b, divx, mv) {
	if (mv == null) {
		mv = 0;
	}
	mastervalue = mv;
	if (hs > 0) {
		clearTimeout(hs);
	}
	if (divx == null) {
		divx = 'rcsbutton' + b;
	}
	var strout = '';
	var x = 0;
	for (x = 0; x < rcsbuttons[b].submenus.length; x++) {	
		strout += '<div id="rcssm' + x + '" style="margin: 2px; padding: 1px; border: 1px solid RGB(86, 78, 76); background-Color: RGB(86, 78, 76); cursor: pointer;" onmouseover="RCSSMOver(' + b + ',' + x + ');" onclick="RCSSMClick(' + b + ',' + x + ');" onmouseout="RCSSMOut(' + b + ',' + x + ');"><table cellpadding=0 cellspacing=0 style="font-family: Helvetica; font-size: 8pt;"><tr><td style="width: 20px;">' + (rcsbuttons[b].submenus[x].picture1 != '' ? '<img src=\'menubuilder/' + rcsbuttons[b].submenus[x].picture1 + '\' width=16 height=16>' : '') + '</td><td>' + rcsbuttons[b].submenus[x].text + '</td></tr></table></div>'
	}
	strout += ''

	var rcspopup = getObj('rcspopup');
	if (rcspopup != null) {
		rcspopup.parentNode.removeChild(rcspopup);
	}

	var rcspopdiv = document.createElement("div");
	rcspopdiv.style.position = "absolute";
	rcspopdiv.style.border = "1px solid RGB(86, 78, 76)";
	rcspopdiv.style.width = "140px";
	rcspopdiv.style.zIndex = 150;
	//Add image width and height
	if (b == 0) {
		rcspopdiv.style.left = Math.abs(getObj(divx).offsetLeft) + 482 + 40 + Math.abs(getObj('MainBoxTable').offsetLeft);
		rcspopdiv.style.top = getObj(divx).offsetTop + getObj('RightBox').offsetTop + 32 + 40;
	} else {
		rcspopdiv.style.left = Math.abs(getObj(divx).offsetLeft) + getObj('rcsbuttonbox').offsetLeft;
		rcspopdiv.style.top = getObj(divx).offsetTop + 32;
	}
	rcspopdiv.style.display = "";
	rcspopdiv.id = "rcspopup";
	rcspopdiv.style.backgroundColor = 'RGB(86, 78, 76)';
	rcspopdiv.innerHTML = strout;

	hs = setTimeout('HideSubMenu();', 2500);
	document.body.appendChild(rcspopdiv);
	
}

function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (1) {
            curleft+=obj.offsetLeft;
            if (!obj.offsetParent) {
                break;
            }
            obj=obj.offsetParent;
        }
    } else if (obj.x) {
        curleft+=obj.x;
    }
    return curleft;
}
function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (1) {
            curtop+=obj.offsetTop;
            if (!obj.offsetParent) {
                break;
            }
            obj=obj.offsetParent;
        }
    } else if (obj.y) {
        curtop+=obj.y;
    }
    return curtop;
}

function RCSSMOver(b, x) {
	var bb = getObj('rcssm' + x);
	if (bb != null) {
		if (rcsbuttons[b] != null) {
			bb.style.border = '1px solid RGB(86, 78, 76)';
			bb.style.backgroundColor = 'RGB(43, 28, 23)';
		}
	}
}

function RCSSMOut(b, x) {
	var bb = getObj('rcssm' + x);

	if (bb != null) {
		if (rcsbuttons[b] != null) {
			bb.style.border = '1px solid RGB(86, 78, 76)';
			bb.style.backgroundColor = 'RGB(86, 78, 76)';
		}
	}
}

function RCSSMClick(b, x) {
	var bb = getObj('rcssm' + x);
	if (bb != null) {
		if (rcsbuttons[b] != null) {
			bb.style.backgroundColor = 'RGB(43, 28, 23)';
			if (rcsbuttons[b].submenus[x].js != null) {
				HideSubMenu();
				var jsexec = rcsbuttons[b].submenus[x].js.replace("\{" + 0 + "\}", mastervalue);
				eval(jsexec);
			} else if (rcsbuttons[b].submenus[x].url != '') {
				HideSubMenu();
				window.location.replace(rcsbuttons[b].submenus[x].url);
			}
		}
	}
}
	

function RCSBOver(b) {
	if (PageLoaded == false || PageLoaded == null) {
	} else {
		var bb = getObj('rcsbutton' + b);
		if (bb != null) {
			if (rcsbuttons[b] != null) {
				bb.style.backgroundImage = 'URL("' + TemplatePath + rcsbuttons[b].picture2 + '")';
				if (rcsbuttons[b].submenus.length > 0)  {
					ShowSubMenu(b);
				} else {
					HideSubMenu();
				}
				clearTimeout(rcsbuttons[b].clicktimer);
			}
		}
	}
}

function RCSBOut(b) {
	var bb = getObj('rcsbutton' + b);

	if (bb != null) {
		if (rcsbuttons[b] != null) {
			bb.style.backgroundImage = 'URL("' + TemplatePath + rcsbuttons[b].picture1 + '")';
			clearTimeout(rcsbuttons[b].clicktimer);
		}
	}
}

function RCSBClick(b) {
	var bb = getObj('rcsbutton' + b);
	if (bb != null) {
		if (rcsbuttons[b] != null) {
			bb.style.backgroundImage = 'URL("' + TemplatePath + rcsbuttons[b].picture3 + '")';
			rcsbuttons[b].clicktimer = setTimeout('RCSBOver(' + b + ');', 150);
			if (rcsbuttons[b].url.indexOf(';') != -1) {
				eval(rcsbuttons[b].url);
			} else if (rcsbuttons[b].url != '') {
				window.location.replace(rcsbuttons[b].url);
			}
		}
	}
}