/**************************************************************************
* Document	: bwt_top_nav_util.js
* Author	: Wayne J. Earl
* Created	: 2007-06-11
* Purpose	: JavaScript functions for BWT top navigation
* Comments	: 
*************************************************************************/
/*************************************************************************
* Revised by	: 
* Revised date	: 
* Description	: 
*************************************************************************/

var f_remove_link = false;
var nav_options = [{"name": "home", "href": "bwt_welcome.php", "text": ""},
				   {"name": "enter", "href": "bwt_choice.php?type=w", "text": "Enter Workout"},
				   {"name": "create", "href": "bwt_choice.php?type=t", "text": "Create Workout"},
				   {"name": "stats", "href": "bwt_choice.php?type=o&mode=s", "text": "Progress/Stats"},
				   {"name": "history", "href": "bwt_choice.php?type=w&mode=h", "text": "View/Edit History"},
				   {"name": "library", "href": "bwt_choice.php?type=t&mode=h", "text": "View/Edit Library"},
				   {"name": "settings", "href": "bwt_settings.php", "text": "Personal Settings"}];

//set nav buttons/text based on selected nav option
function setNavButtons(param_elem)
{
	if (typeof(param_elem) == "undefined") return false;
	
	var elem = null;
	if (typeof(param_elem) == "string") elem = document.getElementById(param_elem);
	if (typeof(param_elem) == "object") elem = param_elem;
	
	//get ref
	var ref = getAttrib(elem, "ref");
//	if (!ref) ref = "home";
//	if (typeof(bwt_nav_option) == "undefined") bwt_nav_option = "home";

	if (ref == bwt_nav_option) return false; //currently select option is same as clicked option
	//update selected option
	bwt_nav_option = ref;
	//update button status
	for (var idx = 0; idx < nav_options.length; idx ++) {
	
		var nav_option = nav_options[idx];
		var name = nav_option.name;
		//update button
		var mode = "default";
		if (name == ref) mode = "selected";
		//check to see if this option needs to be updated
		var test_id = "bwt_" + name + "_button";
		var elem_test = document.getElementById(test_id);
		if (elem_test) {
		
			var test_ref = getAttrib(elem_test, "ref");
			if ((test_ref) && (mode == "default")) continue;
		}
		setNavButton(nav_option, mode);
	}
	return true;
}


function setNavButton(option, status)
{
	if (typeof(option) == "undefined") return false;
	if (typeof(status) == "undefined") status = "default";
	
	if ((status != "default") && (status != "selected")) status = "default";

	var name = option.name;
	var href = option.href;
	var text = option.text;
	//set class suffixes
	if (status == "default") {
	
		var old_class_sfx = "selected";
	}
	else { //selected
	
		var old_class_sfx = "default";
	}
	var button_wrapper = "bwt_top_nav_" + name + "_wrapper";
	var elem_button_wrapper = document.getElementById(button_wrapper);
	if (elem_button_wrapper) {

		//update button wrapper class
		if (name != "home") { //name not 'home'
		
			if (old_class_sfx) { //sfx not empty
		
				var old_button_class = "bwt_nav_button_" + old_class_sfx;
				var new_button_class = "bwt_nav_button_" + status;
				addRemoveClassName(elem_button_wrapper, old_button_class, "remove");
				addRemoveClassName(elem_button_wrapper, new_button_class);
			}
		}
		if (f_remove_link) {
		
			//set vars
			switch(name) {
			
				case 'home':

					if (status == "default") { //build as anchor
					
						//default
					//	var old_class_sfx = "";
						var html_open = "<a href='";
						var html_href = href + "' ";
						var html_extra = "ref='" + name + "' onclick='return setNavButtons(this);' ";
						var html_outer_class = "class='bwt_top_nav_" + name + "_link' ";
						var html_outer_id = "id='bwt_" + name + "_button'>";
						var html_text_open = "<div ";
						var html_text_class = "class='bwt_nav_" + name + "_text' ";
						var html_text_id = "id='bwt_top_nav_" + name + "_text'>";
						var html_text = "&nbsp;";
						var html_text_close = "</div>";
						var html_close = "</a>";
					}
					else { //empty
					
						//selected
					//	var old_class_sfx = "";
						var html_open = "";
						var html_href = "";
						var html_extra = "";
						var html_outer_class = "";
						var html_outer_id = "";
						var html_text_open = "";
						var html_text_class = "";
						var html_text_id = "";
						var html_text = "";
						var html_text_close = "";
						var html_close = "";
					}
					break;
					
				default:
				
					if (status == "default") { //build as anchor
					
						//default
					//	var old_class_sfx = "selected";
						var html_open = "<a href='";
						var html_href = href + "' ";
						var html_extra = "ref='" + name + "' onclick='return setNavButtons(this);' ";
						var html_outer_class = "class='bwt_top_nav_link' ";
						var html_outer_id = "id='bwt_" + name + "_button'>";
						var html_text_open = "<div ";
						var html_text_class = "class='bwt_nav_text bwt_nav_text_" + status + "' ";
						var html_text_id = "id='bwt_top_nav_" + name + "_text'>";
						var html_text = text;
						var html_text_close = "</div>";
						var html_close = "</a>";
					}
					else { //build as div
					
						//selected
					//	var old_class_sfx = "default";
						var html_open = "<div ";
						var html_href = "";
						var html_extra = "";
						var html_outer_class = "class='bwt_top_nav_link' ";
						var html_outer_id = "id='bwt_" + name + "_button'>";
						var html_text_open = "<div ";
						var html_text_class = "class='bwt_nav_text bwt_nav_text_" + status + "' ";
						var html_text_id = "id='bwt_top_nav_" + name + "_text'>";
						var html_text = text;
						var html_text_close = "</div>";
						var html_close = "</div>";
					}
			}
			//build new innerHTML
			var html_parts = new Array()
			html_parts.push(html_open);
			html_parts.push(html_href);
			html_parts.push(html_extra);
			html_parts.push(html_outer_class);
			html_parts.push(html_outer_id);
			html_parts.push(html_text_open);
			html_parts.push(html_text_class);
			html_parts.push(html_text_id);
			html_parts.push(html_text);
			html_parts.push(html_text_close);
			html_parts.push(html_close);

			var new_html = html_parts.join('');
			elem_button_wrapper.innerHTML = new_html;
		}
		else { //only alter text class
		
			var text_wrapper = "bwt_top_nav_" + name + "_text";
			var elem_text_wrapper = document.getElementById(text_wrapper);
			if (elem_text_wrapper) {
			
				//update text wrapper class
				if (name != "home") { //name not 'home'
				
					var old_text_class = "bwt_nav_text_" + old_class_sfx;
					var new_text_class = "bwt_nav_text_" + status;
					addRemoveClassName(elem_text_wrapper, old_text_class, "remove");
					addRemoveClassName(elem_text_wrapper, new_text_class);
				}
			}
		}
	}
}