function getElementsByTagNameAndClassName(element, tagName, className) {
	if (tagName == null) {
		tagName = '*';
	}

	var children = element.getElementsByTagName(tagName);
	var elements = new Array();

	if (className == null) {
		return children;
	}

	for (var i = 0; i < children.length; i++) {
		var child = children[i];
		var classNames = child.className.split(' ');
		for (var j = 0; j < classNames.length; j++) {
			if (classNames[j] == className) {
				elements.push(child);
				break;
			}
		}
	}

	return elements;
}

function taskQueue() {
	this.queue = new Array();

	this.addTask = function(task) {
		this.queue.push(task);
		if (this.queue.length == 1) {
			task.run();
		}
	}
	
	this.removeTask = function() {
		this.queue.shift();
		if (this.queue.length > 0) {
			this.queue[0].run();
		}
	}
	
	this.run = function() {
		if (this.queue.length > 0) {
			this.queue[0].run();
		}
	}
}

var globalTaskQueue = new taskQueue();

function timerTask() {
	this.run = function() {
		var interval = this.timer();
		if (interval > 0) {
			window.setTimeout("globalTaskQueue.run()", interval);
		} else {
			globalTaskQueue.removeTask();
		}
	}

	this.timer = function() {
		return 0;
	}
}

function movePageMenu(menuContent1, menuContent2, contentHeight, pageMenuBar) {
	var menu1 = menuContent1;
	var menu2 = menuContent2;
	var height1 = contentHeight;
	var height2 = 0;

	this.task = new timerTask();

	this.task.timer = function() {
		if (height1 <= 60) {
			height2 += height1;
			height1 = 0;
			menu1.style.height = height1 + "px";
			menu1.style.overflow = "auto";
			menu2.style.height = height2 + "px";
			menu2.style.overflow = "auto";
			var mark1 = getElementsByTagNameAndClassName(menu1.parentNode, "IMG", "PageTitleMark")[0];
			mark1.src = pageMenuBar.marksrc1;
			var mark2 = getElementsByTagNameAndClassName(menu2.parentNode, "IMG", "PageTitleMark")[0];
			mark2.src = pageMenuBar.marksrc2;
			return 0;
		}
		height1 -= 60;
		height2 += 60;
		menu1.style.overflow = "hidden";
		menu2.style.overflow = "hidden";
		menu1.style.height = height1 + "px";
		menu2.style.height = height2 + "px";
		return 30;
	}

	globalTaskQueue.addTask(this.task);
}

function showPageMenu(pageMenuBar, showPageNumber) {
	this.show = function() {
		var oldPageNumber = pageMenuBar.currentPage;
		if (oldPageNumber == showPageNumber) {
			return;
		}
		var pageContent1 = getElementsByTagNameAndClassName(pageMenuBar.pages[pageMenuBar.currentPage], "DIV", "PageContent")[0];
		var pageContent2 = getElementsByTagNameAndClassName(pageMenuBar.pages[showPageNumber], "DIV", "PageContent")[0];
		pageMenuBar.currentPage = showPageNumber;
		new movePageMenu(pageContent1, pageContent2, pageMenuBar.contentHeight, pageMenuBar);
	}
}

function PageMenuBar(barId, totalHeight, src1, src2) {
	this.marksrc1 = src1;
	this.marksrc2 = src2;
	var bar = document.getElementById(barId);
	var height = 0;
	this.currentPage = 0;
	this.pages = getElementsByTagNameAndClassName(bar, "DIV", "PageMenu");
	for (var i = 0; i < this.pages.length; i++) {
		var page = this.pages[i];
		var pageTitle = getElementsByTagNameAndClassName(page, "DIV", "PageTitle")[0];
		var titleHeight = pageTitle.offsetHeight;
		height += titleHeight;
	}
	this.contentHeight = totalHeight - height;

	for (var i = 0; i < this.pages.length; i++) {
		var page = this.pages[i];
		var pageTitle = getElementsByTagNameAndClassName(page, "DIV", "PageTitle")[0];
		var showObject = new showPageMenu(this, i);
		pageTitle.onclick = showObject.show;
	}

	for (var i = 0; i < this.pages.length; i++) {
		var page = this.pages[i];
		var pageContent = getElementsByTagNameAndClassName(page, "DIV", "PageContent")[0];
		if (i == this.currentPage) {
			pageContent.style.height = this.contentHeight + "px";
		} else {
			pageContent.style.height = "0";
		}
	}
	
	var mark = getElementsByTagNameAndClassName(this.pages[0], "IMG", "PageTitleMark")[0];
	mark.src = src2;
}

function showPageMenuBar(barId, totalHeight, marksrc1, marksrc2) {
	new PageMenuBar(barId, totalHeight, marksrc1, marksrc2);
}
