var buttons = Array();
preloadXPButtonImages();

function addXPButton(sParentID, iSize, bPressed, sLabel, sImgSrc, sURL) {
	var oParent = document.getElementById(sParentID);
	if (oParent != null)
		buttons.push(new XPButton(oParent, iSize, bPressed, sLabel, sImgSrc, sURL));
}

function XPButton(oParent, iSize, bPressed, sLabel, sImgSrc, sURL) {
	this.width = iSize;
	this.height = iSize;
	this.parent = oParent;
	this.url = sURL;
	this.pressed = bPressed;
	this.label = sLabel;
	this.img = document.createElement("IMG");
	this.img.setAttribute("src", sImgSrc);
	this.img.setAttribute("border", "0");
	this.img.setAttribute("align", "absmiddle");
	if (sLabel != null && sLabel != "")
		sLabel = " " + sLabel;
	this.lbl = document.createTextNode(sLabel);
	
	this.td1 = null;
	this.td2 = null;
	this.td3 = null;
	this.td4 = null;
	this.td5 = null;
	this.td6 = null;
	this.td7 = null;
	this.td8 = null;
	this.td9 = null;
	this.tbl = null;
	
	createTable(this);
	clearXPButton(this);
	
	this.tbl.setAttribute("index", buttons.length);
	this.tbl.style.cursor = "pointer";
	this.td5.noWrap = true;
	this.td5.appendChild(this.img);
	this.td5.appendChild(this.lbl);
	this.drawNormal();
	
	this.setPressed(bPressed);
}

XPButton.prototype.drawNormal = drawNormal;
XPButton.prototype.drawHover = drawHover;
XPButton.prototype.drawPressed = drawPressed;
XPButton.prototype.click = click;
XPButton.prototype.setPressed = setPressed;

function setPressed(bPressed) {
	this.pressed = bPressed;
	clearXPButton(this);
	if (bPressed) {
		this.td1.appendChild(createImg("images/button_selected_01.gif"));
		this.td2.setAttribute("background", "images/button_selected_02.gif");
		this.td3.appendChild(createImg("images/button_selected_03.gif"));
		this.td4.setAttribute("background", "images/button_selected_04.gif");
		this.td6.setAttribute("background", "images/button_selected_05.gif");
		this.td7.appendChild(createImg("images/button_selected_06.gif"));
		this.td8.setAttribute("background", "images/button_selected_07.gif");
		this.td9.appendChild(createImg("images/button_selected_08.gif"));
	}
}

function createTable(oXPButton) {
	var oTable = document.createElement("TABLE");
	var oTBody = document.createElement("TBODY");
	oTable.appendChild(oTBody);
	oTable.setAttribute("border", "0");
	oTable.setAttribute("cellpadding", "0");
	oTable.setAttribute("cellspacing", "0");
	oTable.style.borderCollapse = "collapse";
	oTable.cellSpacing = 0;
	oTable.cellPadding = 0;
	
	var oTR = document.createElement("TR");
	var oTD1 = document.createElement("TD");
	var oTD2 = document.createElement("TD");
	var oTD3 = document.createElement("TD");
	oTD1.width = 4;
	oTD1.height = 4;
	oTD1.noWrap = true;
	oTD3.width = 4;
	oTD3.height = 4;
	oTD3.noWrap = true;
	oTBody.appendChild(oTR);
	oTR.appendChild(oTD1);
	oTR.appendChild(oTD2);
	oTR.appendChild(oTD3);
	oXPButton.td1 = oTD1;
	oXPButton.td2 = oTD2;
	oXPButton.td3 = oTD3;
	
	oTR = document.createElement("TR");
	oTD1 = document.createElement("TD");
	oTD2 = document.createElement("TD");
	oTD3 = document.createElement("TD");
	oTBody.appendChild(oTR);
	oTR.appendChild(oTD1);
	oTR.appendChild(oTD2);
	oTR.appendChild(oTD3);
	oXPButton.td4 = oTD1;
	oXPButton.td5 = oTD2;
	oXPButton.td6 = oTD3;
	
	oTR = document.createElement("TR");
	oTD1 = document.createElement("TD");
	oTD2 = document.createElement("TD");
	oTD3 = document.createElement("TD");
	oTD1.width = 4;
	oTD1.height = 4;
	oTD1.noWrap = true;
	oTBody.appendChild(oTR);
	oTR.appendChild(oTD1);
	oTR.appendChild(oTD2);
	oTR.appendChild(oTD3);
	oXPButton.td7 = oTD1;
	oXPButton.td8 = oTD2;
	oXPButton.td9 = oTD3;
	
	oXPButton.parent.appendChild(oTable);
	oXPButton.tbl = oTable;
	
	if (!oXPButton.pressed) {
		oTable.onmousedown = onXPButtonDown;
		oTable.onmouseover = onXPButtonOver;
		oTable.onmouseout = onXPButtonOut;
		oTable.onmouseup = onXPButtonUp;
		oTable.onclick = onXPButtonClick;
	}
}

function drawNormal() {
	this.td2.setAttribute("background", "");
	this.td4.setAttribute("background", "");
	this.td5.setAttribute("background", "");
	this.td6.setAttribute("background", "");
	this.td8.setAttribute("background", "");
}

function drawHover() {
	this.td1.appendChild(createImg("images/button_hover_01.gif"));
	this.td2.setAttribute("background", "images/button_hover_02.gif");
	this.td3.appendChild(createImg("images/button_hover_03.gif"));
	
	this.td4.setAttribute("background", "images/button_hover_04.gif");
	this.td5.setAttribute("background", "images/button_hover_05.gif");
	this.td6.setAttribute("background", "images/button_hover_06.gif");
	
	this.td7.appendChild(createImg("images/button_hover_07.gif"));
	this.td8.setAttribute("background", "images/button_hover_08.gif");
	this.td9.appendChild(createImg("images/button_hover_09.gif"));
}

function drawPressed() {
	this.td1.appendChild(createImg("images/button_pressed_01.gif"));
	this.td2.setAttribute("background", "images/button_pressed_02.gif");
	this.td3.appendChild(createImg("images/button_pressed_03.gif"));
	
	this.td4.setAttribute("background", "images/button_pressed_04.gif");
	this.td5.setAttribute("background", "images/button_pressed_05.gif");
	this.td6.setAttribute("background", "images/button_pressed_06.gif");
	
	this.td7.appendChild(createImg("images/button_pressed_07.gif"));
	this.td8.setAttribute("background", "images/button_pressed_08.gif");
	this.td9.appendChild(createImg("images/button_pressed_09.gif"));
}

function click() {
	document.location.href = this.url;
}

function onXPButtonClick(e) {
	var btn = getTargetButton(e);
	if (btn.pressed) return;
	btn.click();
}

function onXPButtonDown(e) {
	var btn = getTargetButton(e);
	if (btn.pressed) return;
	clearXPButton(btn);
	btn.drawPressed();
}

var bXPHovered = false;
function onXPButtonOver(e) {
	var btn = getTargetButton(e);
	if (btn.pressed) return;
	clearXPButton(btn);
	bXPHovered = true;
	btn.drawHover();
}

function onXPButtonOut(e) {
	var btn = getTargetButton(e);
	if (btn.pressed) return;
	clearXPButton(btn);
	bXPHovered = false;
	btn.drawNormal();
}

function onXPButtonUp(e) {
	var btn = getTargetButton(e);
	if (btn.pressed) return;
	clearXPButton(btn);
	if (bXPHovered)
		btn.drawHover();
	else
		btn.drawNormal();
}

function getTargetButton(e) {
	var oObj = findValidObj(getTargetEventObj(e), "index");
	var index = oObj.getAttribute("index");
	return buttons[index];
}

function clearXPButton(oXPButton) {
	clearChildNodes(oXPButton.td1);
	clearChildNodes(oXPButton.td2);
	clearChildNodes(oXPButton.td3);
	clearChildNodes(oXPButton.td4);
	//clearChildNodes(oXPButton.td5);
	clearChildNodes(oXPButton.td6);
	clearChildNodes(oXPButton.td7);
	clearChildNodes(oXPButton.td8);
	clearChildNodes(oXPButton.td9);
}

function preloadXPButtonImages() {
	var tmp = new Image();
	tmp.src = "images/button_hover_01.gif";
	tmp = new Image();
	tmp.src = "images/button_hover_02.gif";
	tmp = new Image();
	tmp.src = "images/button_hover_03.gif";
	tmp = new Image();
	tmp.src = "images/button_hover_04.gif";
	tmp = new Image();
	tmp.src = "images/button_hover_05.gif";
	tmp = new Image();
	tmp.src = "images/button_hover_06.gif";
	tmp = new Image();
	tmp.src = "images/button_hover_07.gif";
	tmp = new Image();
	tmp.src = "images/button_hover_08.gif";
	tmp = new Image();
	tmp.src = "images/button_hover_09.gif";
	
	tmp = new Image();
	tmp.src = "images/button_pressed_01.gif";
	tmp = new Image();
	tmp.src = "images/button_pressed_02.gif";
	tmp = new Image();
	tmp.src = "images/button_pressed_03.gif";
	tmp = new Image();
	tmp.src = "images/button_pressed_04.gif";
	tmp = new Image();
	tmp.src = "images/button_pressed_05.gif";
	tmp = new Image();
	tmp.src = "images/button_pressed_06.gif";
	tmp = new Image();
	tmp.src = "images/button_pressed_07.gif";
	tmp = new Image();
	tmp.src = "images/button_pressed_08.gif";
	tmp = new Image();
	tmp.src = "images/button_pressed_09.gif";
	
	tmp = new Image();
	tmp.src = "images/button_selected_01.gif";
	tmp = new Image();
	tmp.src = "images/button_selected_02.gif";
	tmp = new Image();
	tmp.src = "images/button_selected_03.gif";
	tmp = new Image();
	tmp.src = "images/button_selected_04.gif";
	tmp = new Image();
	tmp.src = "images/button_selected_05.gif";
	tmp = new Image();
	tmp.src = "images/button_selected_06.gif";
	tmp = new Image();
	tmp.src = "images/button_selected_07.gif";
	tmp = new Image();
	tmp.src = "images/button_selected_08.gif";
}

function clearChildNodes(oParent) {
	while(oParent.hasChildNodes())
		oParent.removeChild(oParent.childNodes[0]);
}

function createImg(sSrc) {
	var oIMG = document.createElement("IMG");
	oIMG.setAttribute("src", sSrc);
	oIMG.setAttribute("border", "0");
	return oIMG;
}

function findValidObj(oNode, searchFor) {
	while (oNode != null) {
		if (oNode.getAttribute(searchFor) != null)
			return oNode;

		oNode = oNode.offsetParent;
	}
	return null;
}
		
function getTargetEventObj(objEvent) {
	var targ;
	objEvent = getEventObj(objEvent);
	if (objEvent.target) targ = objEvent.target;
	else if (objEvent.srcElement) targ = objEvent.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
	return targ;
}

function getEventObj(objEvent) {
	if (!objEvent) objEvent = window.event;
	return objEvent;
}
