startList = function() {
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("bttns");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
	}
}

function ul2finder()
{
	// Define variables used and classes to be applied/removed
	var i,uls,als,finder;
	var parentClass='parent';
	var showClass='shown';
	var hideClass='hidden';
	var openClass='open';

	// check if our finder list exists, if not, stop all activities
	finder=document.getElementById('finder');
	if(!finder){return;}

	// add the class domenabled to the body
	cssjs('add',document.body,'domenabled')

	// loop through all lists inside finder, position and hide them 
	// by applying the class hidden
	uls=document.getElementById('finder').getElementsByTagName('ul');
	for(i=0;i<uls.length;i++)
	{
		cssjs('add',uls[i],hideClass);
	}	

	// loop through all links of inside finder
	lis=document.getElementById('finder').getElementsByTagName('li');
	for(i=0;i<lis.length;i++)
	{
		// if the li containing the link has no nested list, skip this one
		if(!lis[i].getElementsByTagName('ul')[0])
		{
			// if the list item is one of the final options
			lis[i].getElementsByTagName('a')[0].onclick=function()
			{
				// test for the order list element and the order form field
				var o=document.getElementById('orders');
				var items=document.getElementById('items');
				if(!o || !items){return;}
				// create a new paragraph inside the orders list and add the item
				var newp=document.createElement('p');
				newp.appendChild(document.createTextNode(this.firstChild.nodeValue));
				o.appendChild(newp);
				// grab the order id and add it to the final order field
				var orderid=this.href.match(/\?a=(\d+)/)
				items.value+=items.value==''?orderid[1]:'|'+orderid[1];
				return false;
			}	
			continue;
		}
		var newa=document.createElement('a');
		newa.href='#';
		newa.appendChild(document.createTextNode(lis[i].firstChild.nodeValue));
		lis[i].replaceChild(newa,lis[i].firstChild);
		// otherwise apply the parent class
		cssjs('add',newa,parentClass);
		
		// if the user clicks on the link
		lis[i].getElementsByTagName('a')[0].onclick=function()
		{
		// loop through all lists inside finder
			for(var i=0;i<uls.length;i++)
			{
				// avoid the list connected to this link
				var found=false;
				for(j=0;j<uls[i].getElementsByTagName('ul').length;j++)
				{
					if(uls[i].getElementsByTagName('ul')[j] == 		
						this.parentNode.getElementsByTagName('ul')[0])
					{
						found=true;
						break;
					}
				}
				// and hide all others
				if(!found)
				{
					cssjs('add',uls[i],hideClass)
					cssjs('remove',uls[i],showClass)
					cssjs('remove',uls[i].parentNode.getElementsByTagName('a')[0],openClass)
					cssjs('add',uls[i].parentNode.getElementsByTagName('a')[0],parentClass)
				}
			}	
			// change the current link from parent to open 	
			cssjs('swap',this,parentClass,openClass)
			// show the current nested list 
			cssjs('add',this.parentNode.getElementsByTagName('ul')[0],showClass)

			// don't follow the real HREF of the link
			return false;
		}
	}	
	/*
	 * cssjs
	 * written by Christian Heilmann (http://icant.co.uk)
	 * eases the dynamic application of CSS classes via DOM
	 * parameters: action a, object o and class names c1 and c2 (c2 optional)
	 * actions: swap exchanges c1 and c2 in object o
	 *			add adds class c1 to object o
	 *			remove removes class c1 from object o
	 *			check tests if class c1 is applied to object o
	 * example:	cssjs('swap',document.getElementById('foo'),'bar','baz');
	 */
	function cssjs(a,o,c1,c2)
	{
		switch (a){
			case 'swap':
				o.className=!cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
			break;
			case 'add':
				if(!cssjs('check',o,c1)){o.className+=o.className?' '+c1:c1;}
			break;
			case 'remove':
				var rep=o.className.match(' '+c1)?' '+c1:c1;
				o.className=o.className.replace(rep,'');
			break;
			case 'check':
				return new RegExp('\\b'+c1+'\\b').test(o.className)
			break;
		}
	}
}

function initAll(){
  ul2finder();
  startList();
  setFooter();
}

function xmlhttpPost(strURL) {
    var xmlHttpReq = false;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        xmlHttpReq = new XMLHttpRequest();
        xmlHttpReq.overrideMimeType('text/xml');
    }
    // IE
    else if (window.ActiveXObject) {
        xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlHttpReq.open('POST', '/php/getBusinessInfo.php?id=' + strURL, true);
    xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlHttpReq.onreadystatechange = function() {
        if (xmlHttpReq.readyState == 4) {
            updatepage(xmlHttpReq.responseText);
        }
    }
    xmlHttpReq.send(null);
}

function getquerystring() {
    var form     = document.forms['f1'];
    var word = form.word.value;
    qstr = 'w=' + escape(word);  // NOTE: no '?' before querystring
    return qstr;
}

function updatepage(str){
    document.getElementById("business_info").innerHTML = str;
}

function setFooter() {
	if (document.getElementById('finder')) {
			var finderHeight = document.getElementById('finder').offsetHeight + 25;
			var busCatElement = document.getElementById('bussiness_cat_div');
				busCatElement.style.height = finderHeight + 'px';
	}
}

function togg(what) {
	toggbtn = document.getElementById('mn' + what);
	toggtxt = document.getElementById('nfo' + what);
	if(toggtxt.style.display == 'none') {
		toggbtn.src = '/images/hide_info.gif';
		toggtxt.style.display = 'block';
	} else {
		toggbtn.src = '/images/more_info.gif';
		toggtxt.style.display = 'none';
	}
}

window.onload=initAll;