// set gloabal variables
var array_ids = new Array("")

// declare items array
var array_items = new Array()

// setup imitation two dimentional array
function add_item(id, text, parent, link_to) {
  this.id = id
	this.text = text
  this.parent = parent
  this.link_to = link_to
}

function setup(status) {
	// item information goes here
	//  | id || text to show || parents id || url of link |
	var num = 0
	array_items[eval(num++)] = new add_item("home", "Home", "root", "index.htm")
	array_items[eval(num++)] = new add_item("news", "News", "root", "news.htm")
	
	if (status == "0") {
		array_items[eval(num++)] = new add_item("login", "Login", "root", "login.htm")
	}
	
	array_items[eval(num++)] = new add_item("products", "Products", "root", "")
	array_items[eval(num++)] = new add_item("range",    "Range of Gemstones",       "products", "range.htm")
	array_items[eval(num++)] = new add_item("zodiac",   "Birthstones",              "products", "zodiac.htm")
	array_items[eval(num++)] = new add_item("catagory", "Select Items By Category", "products", "byCat.htm")
	array_items[eval(num++)] = new add_item("gemstone", "Select Items By Stone",    "products", "sh_gst.htm")
	array_items[eval(num++)] = new add_item("special",  "Special offers!",          "products", "special.htm")
	array_items[eval(num++)] = new add_item("new",      "New Items!",               "products", "new.htm")
	if (status != "0") {
		array_items[eval(num++)] = new add_item("basket", "View Order", "root", "basket.htm")
	}
	array_items[eval(num++)] = new add_item("about", "About RGM", "root", "")
	array_items[eval(num++)] = new add_item("profile", "Company Profile", "about", "compProfile.htm")
	array_items[eval(num++)] = new add_item("finding", "Finding RGM",     "about", "finding.htm")
	array_items[eval(num++)] = new add_item("contact", "Contact RGM",     "about", "contact.htm")

	array_items[eval(num++)] = new add_item("information", "Order Information", "root", "")
	array_items[eval(num++)] = new add_item("how2pay",    "How to Pay",         "information", "how2pay.htm")
	array_items[eval(num++)] = new add_item("how2order",  "How to order",       "information", "how2order.htm")
	array_items[eval(num++)] = new add_item("how2search", "How to Search",      "information", "how2search.htm")
	array_items[eval(num++)] = new add_item("tandc",      "Terms & Conditions", "information", "tandc.htm")
	
	if (status == "0") {
		array_items[eval(num++)] = new add_item(" ", "Register New User", "root", "apply.htm")
	}
	else {
		array_items[eval(num++)] = new add_item("profile", "Your Profile", "root", "profile.htm")
	}
	if (status == "2") {
			array_items[eval(num++)] = new add_item("admin", "Administration", "root", "admin.htm")
	}
	
	show("root", "")
}

function show(id, parent) {
	
	// loop thorough ids aray
	for (var counter = 0; counter < array_ids.length; counter++) {
		// if the parent element is found and the next element is this one
		if (array_ids[counter] == parent && array_ids[counter+1] == id) {
			close_last(id)
			array_ids.length = counter+1
		}	
		// if the parent element is found and the next element exists
		else if (array_ids[counter] == parent) {
			if (array_ids[counter+1]) {
				close_last(array_ids[counter+1])
				array_ids.length = counter+1
			}	
			array_ids[counter+1] = id
			open_level(id)
		}
	}
}

function open_level(id) {
	// clear stuff string
	if (id == "root") {
		var stuff_string = "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"1\" border=\"0\"><tbody align=\"right\" valign=\"middle\">"
	} else {
		var stuff_string = "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tbody align=\"right\" valign=\"middle\">"
	}

	for (var counter = 0; counter < array_items.length; counter++) {
		// if item has the correct parent
		if (array_items[counter].parent == id) {
			// add it to stuff_string
			if (array_items[counter].link_to == "") {		
				stuff_string += "<tr><td id=\"" + array_items[counter].id + "_td\"><a href=\"javascript:show('" + array_items[counter].id + "', '" + array_items[counter].parent + "')\" class=\"menu" + array_ids.length + "\" onmouseover=\"javascript:window.status = ''; return true\" onmouseup=\"javascript:status = ''; return true\">" + array_items[counter].text + "</a></td></tr>"
				stuff_string += "<tr><td id=\"" + array_items[counter].id + "_data\" height=\"1\"></td><td></td></tr>"
			}
			else {
				stuff_string += "<tr><td id=\"" + array_items[counter].id + "_td\"><a href=\"javascript:link_to('" + array_items[counter].link_to + "')\" class=\"menu" + array_ids.length + "\" onmouseover=\"javascript:window.status = ''; return true\">" + array_items[counter].text + "</a></td>"
				stuff_string += "</tr><tr><td height=\"1\"></td><td></td></tr>"
			}
		}
	}
	// stuff the string
	document.getElementById(id + "_data").innerHTML = stuff_string + "</tbody></table>"

}

function close_last(id) {
	// clear the last subcells contents
	document.getElementById(id + "_data").innerHTML = ""
	document.getElementById(id + "_td").className = "menu"
}

function link_to(link_to) {
	document.location = link_to + "?menu=" + array_ids[array_ids.length-1]
}

