//dbgwin = window.open();

// Browser Detect script borrowed from http://www.quirksmode.org/js/detect.html
var BrowserDetect = {
    init: function () {
	this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
	this.version = this.searchVersion(navigator.userAgent)
	    || this.searchVersion(navigator.appVersion)
	    || "an unknown version";
	this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },
    searchString: function (data) {
	for (var i=0;i<data.length;i++)	{
	    var dataString = data[i].string;
	    var dataProp = data[i].prop;
	    this.versionSearchString = data[i].versionSearch || data[i].identity;
	    if (dataString) {
		if (dataString.indexOf(data[i].subString) != -1)
		    return data[i].identity;
	    }
	    else if (dataProp)
		return data[i].identity;
	}
    },
    searchVersion: function (dataString) {
	var index = dataString.indexOf(this.versionSearchString);
	if (index == -1) return;
	return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
    },
    dataBrowser: [
	{
		string: navigator.userAgent,
		subString: "Chrome",
		identity: "Chrome"
	},
	{ 	string: navigator.userAgent,
		subString: "OmniWeb",
		versionSearch: "OmniWeb/",
		identity: "OmniWeb"
	},
	{
	    string: navigator.vendor,
	    subString: "Apple",
	    identity: "Safari",
		versionSearch: "Version"
	},
	{
	    prop: window.opera,
	    identity: "Opera"
	},
	{
	    string: navigator.vendor,
	    subString: "iCab",
	    identity: "iCab"
	},
	{
	    string: navigator.vendor,
	    subString: "KDE",
	    identity: "Konqueror"
	},
	{
	    string: navigator.userAgent,
	    subString: "Firefox",
	    identity: "Firefox"
	},
	{   // for newer Netscapes (6+)
	    string: navigator.userAgent,
	    subString: "Netscape",
	    identity: "Netscape"
	},
	{
	    string: navigator.userAgent,
	    subString: "MSIE",
	    identity: "Explorer",
	    versionSearch: "MSIE"
	},
	{
	    string: navigator.userAgent,
	    subString: "Gecko",
	    identity: "Mozilla",
	    versionSearch: "rv"
	},
	{   // for older Netscapes (4-)
	    string: navigator.userAgent,
	    subString: "Mozilla",
	    identity: "Netscape",
	    versionSearch: "Mozilla"
	}
    ],
    dataOS : [
	{
	    string: navigator.platform,
	    subString: "Win",
	    identity: "Windows"
	},
	{
	    string: navigator.platform,
	    subString: "Mac",
	    identity: "Mac"
	},
	{
	    string: navigator.platform,
	    subString: "Linux",
	    identity: "Linux"
	}
    ]

};
BrowserDetect.init();

var img_closed = "images/right_arrow_inv_wide.gif";
var img_open = "images/down_arrow_inv_wide.gif";
var img_dot = "images/dot_inv_wide.gif";

//if ( (!document.all) && ( document.getElementById) ) {
if ( BrowserDetect.browser == "Firefox" || BrowserDetect.browser == "Opera" || BrowserDetect.browser == "Chrome" || BrowserDetect.browser == "Safari" ) {
    // Only IE sees the last 'CR' in the HTML text as a seperate HTML entity so sees 2 children rather than 1
    // Also define a function to add the onClick handler
    var number_of_children = 1;
    function addClick (objref,parent) {
	if ( parent == "p" ) {
	    objref.setAttribute('onclick',"alter_sub_menu(this.parentNode.firstChild)");
	    }
	else {
	    objref.setAttribute('onclick',"alter_sub_menu(this)");
	    }
	}
    function addIEimg() {
	// dummy function, not needed for other browsers
	return;
	}
    }

//if ( (document.all) && ( document.getElementById) ) {
if ( BrowserDetect.browser == "Explorer" ) {
    // IE sees the last 'CR' in the HTML text as a seperate HTML entity so sees 2 children
    // Also define a function to add the onclick handler
    var number_of_children = 2;
    function addClick (objref,parent) {
	if ( parent == "p" ) {
	    // found this at: http://www.faqts.com/knowledge_base/view.phtml/aid/9592 - thank you.
	    objref['onclick'] = new Function("alter_sub_menu(this.parentNode.firstChild)");
	    }
	else {
	    objref['onclick'] = new Function("alter_sub_menu(this)");
	    }
	}
    function addIEimg(li_tag) {
	// function to add an image to a list item to emulate :before psuedo class
	var new_ie_img_tag = document.createElement("IMG");
	new_ie_img_tag.setAttribute('src',img_dot);
	li_tag.insertBefore(new_ie_img_tag,li_tag.firstChild);
	return;
	}
    }

function hide_sub_menus(top) {
    var re = /\/index.htm/;
    var re2 = /www.raspberrysoftware.com\/$/;
    var re3 = /dlreg\.php/;
    var href = document.location.href;
    var node = document.getElementById(top);
    if (  re.test(href) || re2.test(href) ) {
	href = "dummy";
	if ( number_of_children == 1 ) {
		// childNodes[1] for firefox
		node.childNodes[1].firstChild.style.color = "#FEC766";
		}
	else {
		// childNodes[0] for IE
		node.childNodes[0].firstChild.style.color = "#FEC766";
		}
	}
    if ( re3.test(href) ) {
	href="http://www.raspberrysoftware.com/trueconnect.htm";
	}
    check_menu_tree(node,href,"No");
    node.style.display = "inline";
    }

function check_menu_tree(node,href,open_node) {
    //var open_node = "No";
    //for (var i = node.firstChild; i != null ; i = i.nextSibling) {
    for (var i = node.lastChild; i != null ; i = i.previousSibling) {
	if ( i.className == "toplevel" ) {
	    open_node = "No";
	    }
	if ( i.nodeName == "LI" ) {
	    var href_name = i.firstChild.href;
	    if ( href_name == href ) {
		open_node = "Yes";
		i.firstChild.style.color = "#FEC766";
		}
	    if ( i.childNodes.length > number_of_children ) {
		addClick(i.firstChild,"p");
		// add a new image tag to contain the 'open'/'close' images
		var new_img_tag = document.createElement("IMG");
		new_img_tag.setAttribute('src',img_closed);
		// use addClick() to add it as an 'onClick' handler function
		addClick(new_img_tag);
		i.insertBefore(new_img_tag,i.firstChild);
		open_node = check_menu_tree(i,href,open_node);
		}
	    else {
		// this node has no sub-menus so give it a class of 'nochildren'
		// and, since IE doesn't recognize :before, add the 'dot' image.
		i.className = "nochildren";
		//addIEimg(i);
		}
	    //dbgwin.document.write(i.nodeName + ":" + open_node)
	    }
	if ( i.nodeName == "UL" ) {
	    //dbgwin.document.write(i.nodeName + ":")
	    var found_href = check_menu_tree(i,href,open_node);
	    if ( found_href == "No" ) {
		i.style.display = "none";
		open_node = "No";
		}
	    else {
		i.parentNode.firstChild.setAttribute('src',img_open);
		i.style.display = "inline";
		open_node = "Yes";
		}
	    //dbgwin.document.write(i.nodeName + ":" + open_node)
	    }
	}
    //dbgwin.document.write("<BR>")
    return open_node;
    }

function alter_sub_menu (top) {
    if ( top.nextSibling.nextSibling.nextSibling.style.display == "inline" ) {
	top.nextSibling.nextSibling.nextSibling.style.display = "none";
	top.setAttribute('src',img_closed);
	}
    else {
	top.nextSibling.nextSibling.nextSibling.style.display = "inline";
	top.setAttribute('src',img_open);
	}
    }


