
// Declare Global Varaiables
var my_x_offset = 10;
var my_y_offset = 2;
var my_button_spacing = 0;
var my_m3_x_offset = 10;
var my_m3_y_offset = 10;
var my_arch_x_offset = 20;
var my_arch_y_offset = 0;

var bgcolor_off = "";
var bgcolor_on = "#ddddee";
var m2_bgcolor_off = "";
var m2_bgcolor_on = "#ddddee";
var m3_bgcolor_off = "";
var m3_bgcolor_on = "#ddddee";

function set_up() {

	var nav_container = document.getElementById('nav_container');
	var divs = nav_container.getElementsByTagName("div");
	var links = nav_container.getElementsByTagName("a");
	var archive_nav_container = document.getElementById('archive_nav_container');

	// Set up Level One buttons - step one, find them and positition them
	for(i=0;i<links.length;i++) {
		if (links[i].getAttribute('menu_level')) {
			menu_level = links[i].getAttribute('menu_level');
			if (menu_level == "1") {
				rel_to = links[i].getAttribute('rel_to');
				if (rel_to != "root") {
					positionLevelOneButton(links[i].id, rel_to);
				}
			}
		}
	}
	// step 2 - find which one is the tallest
	var max_level_1_height = 0;
	for(i=0;i<links.length;i++) {
		if (links[i].getAttribute('menu_level')) {
			menu_level = links[i].getAttribute('menu_level');
			if (menu_level == "1") {
				this_height = links[i].offsetHeight;
				if (this_height > max_level_1_height) {
					max_level_1_height = this_height;
				}
			}
		}
	}
	// step 3 - set them all to the same height
	for(i=0;i<links.length;i++) {
		if (links[i].getAttribute('menu_level')) {
			menu_level = links[i].getAttribute('menu_level');
			if (menu_level == "1") {
				links[i].style.height = max_level_1_height;
				links[i].height = max_level_1_height;
			}
		}
	}
	for(i=0;i<divs.length;i++) {
		if (divs[i].getAttribute('menu_level')) {
	     		menu_level = divs[i].getAttribute('menu_level');
			if (menu_level == "2") {
				rel_to = divs[i].getAttribute('rel_to');
				positionLevel2Menu(divs[i].id, rel_to);
			}
		}
	}
	for(i=0;i<divs.length;i++) {
		if (divs[i].getAttribute('menu_level')) {
	     		menu_level = divs[i].getAttribute('menu_level');
			if (menu_level == "3") {
				rel_to = divs[i].getAttribute('rel_to');
				positionLevel3Menu(divs[i].id, rel_to);
			}
		}
	}

	if (archive_nav_container != null) {
		var archive_divs = archive_nav_container.getElementsByTagName("div");
		var archive_links = archive_nav_container.getElementsByTagName("a");

		// Set up Level One archive button - step one, find it and positition it
		for(i=0;i<archive_links.length;i++) {
			if (archive_links[i].getAttribute('menu_level')) {
				menu_level = archive_links[i].getAttribute('menu_level');
				name = archive_links[i].getAttribute('id')
				if (menu_level == "1") {
					rel_to = archive_links[i].getAttribute('rel_to');
						positionLevelOneArchiveButton(archive_links[i].id, rel_to);
				}
			}
		}
		for(i=0;i<archive_divs.length;i++) {
			if (archive_divs[i].getAttribute('menu_level')) {
		     		menu_level = archive_divs[i].getAttribute('menu_level');
				if (menu_level == "2") {
					rel_to = archive_divs[i].getAttribute('rel_to');
					positionLevel2Menu(archive_divs[i].id, rel_to);
				}
			}
		}
		for(i=0;i<archive_divs.length;i++) {
			if (archive_divs[i].getAttribute('menu_level')) {
		     		menu_level = archive_divs[i].getAttribute('menu_level');
				if (menu_level == "3") {
					rel_to = archive_divs[i].getAttribute('rel_to');
					positionLevel3Menu(archive_divs[i].id, rel_to);
				}
			}
		}

	}

	// Finally, make level 1 buttons visible
	for(i=0;i<links.length;i++) {
		if (links[i].getAttribute('menu_level')) {
			menu_level = links[i].getAttribute('menu_level');
			if (menu_level == "1") {
				bObj = document.getElementById(links[i].id);
				bObj.style.visibility = 'visible';
			}
		}
	}


}




var this_obj_x = 0;
var this_obj_y = 0;
var this_obj_w = 0;
var this_obj_h = 0;
function getTotalOffsets(obj_id){
	Obj = document.getElementById(obj_id)
	this_obj_w = Obj.offsetWidth;
	this_obj_h = Obj.offsetHeight;
	totaloffset_x = Obj.offsetLeft;
	totaloffset_y = Obj.offsetTop;
	var parentEl = Obj.offsetParent;
	while (parentEl != null){
		totaloffset_x = totaloffset_x + parentEl.offsetLeft;
		totaloffset_y = totaloffset_y + parentEl.offsetTop;
		parentEl=parentEl.offsetParent;
	}
	this_obj_x = totaloffset_x;
	this_obj_y = totaloffset_y;
}

function positionLevelOneButton(button_id, rel_button_id) {
	getTotalOffsets(rel_button_id)
	bObj = document.getElementById(button_id)
	bObj.style.left = this_obj_x + this_obj_w + my_button_spacing + "px";
	bObj.style.top = this_obj_y + "px";
}

function positionLevel2Menu(menu_id, rel_button_id) {
	getTotalOffsets(rel_button_id)
	mObj = document.getElementById(menu_id)
	mObj.style.left = this_obj_x + my_x_offset + "px";
	mObj.style.top = this_obj_y + this_obj_h - my_y_offset + "px";
}

function positionLevel3Menu(menu_id, rel_button_id) {
	getTotalOffsets(rel_button_id)
	mObj = document.getElementById(menu_id)
	mObj.style.left = this_obj_x + this_obj_w  - my_m3_x_offset + "px";
	mObj.style.top = this_obj_y + "px";
}

function positionLevelOneArchiveButton(button_id, rel_button_id) {
	getTotalOffsets(rel_button_id)
	mObj = document.getElementById(button_id)
	mObj.style.left = this_obj_x + my_arch_x_offset + "px";
	mObj.style.top = this_obj_y + this_obj_h + my_arch_y_offset + "px";
}



function toggle_level_2(menu, visibility) {
	if (menu != "") {
		document.getElementById(menu).style.visibility = visibility;
	}
}

function toggle_level_3(menu, submenu, visibility) {
	document.getElementById(menu).style.visibility = visibility;
	document.getElementById(submenu).style.visibility = visibility;
}

function rollover_1(button_obj, state) {
	if (state == 'on') {
		button_obj.style.backgroundColor = bgcolor_on;
	}
	else {
		button_obj.style.backgroundColor = bgcolor_off;
	}
}

function rollover_2(button_obj, state) {
	if (state == 'on') {
		button_obj.style.backgroundColor = m2_bgcolor_on;
	}
	else {
		button_obj.style.backgroundColor = m2_bgcolor_off;
	}
}

function rollover_3(button_obj, state) {
	if (state == 'on') {
		button_obj.style.backgroundColor = m3_bgcolor_on;
	}
	else {
		button_obj.style.backgroundColor = m3_bgcolor_off;
	}
}
