var additionalButtons = new Array("d", "e", "alt", "noj", "nof");
var additionalButtonIds = new Array("akuep", "akueus", "alt", "noj", "nof");
var langPrefix = 'e';
var menuIds = "0123456acdeghkinopqrs";
var menuSections = new Array("", "a", "cghk", "dei", "nopqrs");
var subChapters = "cghkinor";

var bbId = '0';
var bbMainUrl = "";
var bbSelectUrl = "";
var headLoaded = false;
var naviLoaded = false;
var standardButtons = new Object();
var pressedButtons = new Object();
var currentSection = "";
var currentChapter = "";
var hoveringButton = "";


function getArgs() {
	var args = "";
	var arg = "";
	var key = "";
	var value = "";
	var index = -1;

	index = window.location.href.indexOf("?");
	if (index >= 0) {
		args = window.location.href.substring(index + 1, window.location.href.length);
	}
	while (args != "") {
		index = args.indexOf("&");
		if (index < 0) {
			arg = args;
			args = "";
		}
		else {
			arg = args.substring(0, index);
			args = args.substring(index + 1, args.length);
		}
		index = arg.indexOf("=");
		if (index >= 0) {
			key = arg.substring(0, index);
			value = arg.substring(index + 1, arg.length);
			if (key == "bbid") {
				bbId = value;
			}
			else if (key == "bbmain") {
				bbMainUrl = unescape(value);
			}
			else if (key == "bbselect") {
				bbSelectUrl = value;
			}
		}
	}
	if (bbMainUrl == "") {
		window.top.location.href = "index.html";
	}
	if (bbSelectUrl == "") {
		bbSelectUrl = "bbgoogle.htm";
	}
}


function loadNavi() {
	var i = 0;
	var chapterId = "";
	var sectionId = "";

	if (bbId.length > 0) {
		chapterId = bbId.charAt(0);
		if (chapterId == '0' || subChapters.indexOf(chapterId) >= 0) {
			window.frames["bbnavi"].location.href = "bb" + chapterId + "navi.htm#" + chapterId;
		}
		else {
			for (i = 0; i < menuSections.length; ++i) {
				if (menuSections[i].indexOf(chapterId) >= 0) {
					sectionId = "" + i;
					break;
				}
			}
			window.frames["bbnavi"].location.href = "bb" + sectionId + "navi.htm#" + chapterId;
		}
	}
	else {
		window.frames["bbnavi"].location.href = "bb0navi.htm";
	}
}


function loadMain() {
	if (bbMainUrl.length > 0) {
		window.frames["bbmain"].frames["bbdisplay"].location.href = bbMainUrl;
	}
	else {
		window.frames["bbmain"].frames["bbdisplay"].location.href = "bb0.htm";
	}
}


function loadSel() {
	if (bbSelectUrl.length > 0) {
		window.frames["bbmain"].frames["bbselect"].location.href = bbSelectUrl;
	}
}


function getFrame(buttonId) {
	if (buttonId == '0') {
		return window.frames["bbhead"];
	}
	else {
		return window.frames["bbnavi"];
	}
}


function isSection(buttonId) {
	var i = 0;
	var result = true;

	for (i = 0; i < buttonId.length; ++i) {
		if (buttonId.charAt(i) < '0' || buttonId.charAt(i) > '9') {
			result = false;
			break;
		}
	}
	return result;
}


function getSection(chapterId) {
	var i = 0;
	var result = "";

	for (i = 0; i < menuSections.length; ++i) {
		if (menuSections[i].indexOf(chapterId) >= 0) {
			result = "" + i;
			break;
		}
	}
	return result;
}


function releaseButton(buttonId) {
	var targetFrame = getFrame(buttonId);

	if (targetFrame.document.images && standardButtons[buttonId]) {
		targetFrame.document.images["img_" + buttonId].src = standardButtons[buttonId].src;
	}
	if (isSection(buttonId)) {
		if (currentSection == buttonId) {
			currentSection = "";
		}
	}
	else {
		if (currentChapter == buttonId) {
			currentChapter = "";
		}
	}
	if (hoveringButton == buttonId) {
		hoveringButton = "";
	}
}


function pressButton(buttonId) {
	var targetFrame = getFrame(buttonId);

	if (isSection(buttonId)) {
		if (currentSection != "" && currentSection != buttonId) {
			releaseButton(currentSection);
		}
		currentSection = buttonId;
	}
	else {
		if (currentChapter != "" && currentChapter != buttonId) {
			releaseButton(currentChapter);
		}
		currentChapter = buttonId;
	}
	if (targetFrame.document.images && pressedButtons[buttonId]) {
		targetFrame.document.images["img_" + buttonId].src = pressedButtons[buttonId].src;
	}
}


function hoverButton(buttonId) {
	var targetFrame = getFrame(buttonId);

	if (hoveringButton != "" && hoveringButton != buttonId) {
		if (currentSection == hoveringButton || currentChapter == hoveringButton) {
			pressButton(hoveringButton);
		}
		else {
			releaseButton(hoveringButton);
		}
	}
	if (targetFrame.document.images && pressedButtons[buttonId]) {
		targetFrame.document.images["img_" + buttonId].src = pressedButtons[buttonId].src;
	}
	hoveringButton = buttonId;
}


function mouseOver(buttonId) {
	hoverButton(buttonId);
}


function mouseOut(buttonId) {
	if (hoveringButton == buttonId) {
		if (currentSection == buttonId || currentChapter == buttonId) {
			pressButton(buttonId);
		}
		else {
			releaseButton(buttonId);
		}
		hoveringButton = "";
	}
}


function buttonClicked(buttonId) {
	var newId = "";
	var newUrl = "";

	pressButton(buttonId);
	if (!isSection(buttonId) || buttonId == "0") {
		if (buttonId == "0" || subChapters.indexOf(buttonId) >= 0) {
			newId = buttonId;
		}
		else {
			newId = getSection(buttonId);
		}
		if (newId != naviId) {
			newUrl = "bb" + newId + "navi.htm#" + buttonId;
			window.setTimeout("window.frames['bbnavi'].location.href = '" + newUrl + "'", 0);
		}
	}
	return true;
}


function loadImages() {
	var i = 0;
	var menuId = ' ';
	buttonId = "";

	for (i = 0; i < menuIds.length; ++i) {
		menuId = menuIds.charAt(i);
		standardButtons[menuId] = new Image();
		standardButtons[menuId].src = "../images/bu_" + langPrefix + "b" + menuId + ".gif";
	}
	for (i = 0; i < additionalButtons.length; ++i) {
		buttonId = additionalButtonIds[i];
		standardButtons[buttonId] = new Image();
		standardButtons[buttonId].src = "../images/bu_" + langPrefix + "b" + additionalButtons[i] + ".gif";
	}
	for (i = 0; i < menuIds.length; ++i) {
		menuId = menuIds.charAt(i);
		pressedButtons[menuId] = new Image();
		pressedButtons[menuId].src = "../images/bd_" + langPrefix + "b" + menuId + ".gif";
	}
	for (i = 0; i < additionalButtons.length; ++i) {
		buttonId = additionalButtonIds[i];
		pressedButtons[buttonId] = new Image();
		pressedButtons[buttonId].src = "../images/bd_" + langPrefix + "b" + additionalButtons[i] + ".gif";
	}
}


function pressDefault() {
	var buttonId = "";
	var index = -1;

	buttonId = naviId;
	if (naviId != "0") {
		index = window.frames["bbnavi"].location.href.indexOf("#");
		if (index >= 0 && index <= window.location.href.length - 2) {
			buttonId = window.frames["bbnavi"].location.href.substring(index + 1, index + 2);
		}
	}
	if (buttonId != "") {
		if (buttonId != "0") {
			releaseButton("0");
		}
		if (menuIds.indexOf(buttonId) >= 0 && (!isSection(buttonId) || buttonId == '0')) {
			pressButton(buttonId);
		}
	}
}


function msgHeadLoaded() {
	if (!headLoaded) {
		headLoaded = true;
		if (naviLoaded) {
			window.setTimeout("pressDefault()", 0);
		}
	}
}


function msgNaviLoaded() {
	if (!naviLoaded) {
		loadImages();
	}
	naviLoaded = true;
	if (headLoaded) {
		window.setTimeout("pressDefault()", 0);
	}
}


function initNavi(aId) {
	naviId = aId;
	if (isSection(naviId)) {
		currentChapter = "";
		currentSection = naviId;
	}
	else {
		currentChapter = naviId;
		currentSection = getSection(currentChapter);
	}
}
