//chk if an object is an array or not.
function isArray(obj) {

	if(obj == undefined)
		return false;
	
	//returns true is it is an array
	if (obj.constructor.toString().indexOf("Array") == -1)
		return false;
	else
		return true;
}


// *************************************************************** //


function Category(id, name, href) {	
	var me = this;
	
	this.id = id;
	this.name = name;
	this.href = href;
	
	return me;
}


function Categories() {
	var me = this;
	
	// the categories array, hashed by id
	var Cs = Array();
	// the categories heirarchy array, hashed by parent id with array of subcat ids
	// this is NOT a nested array, no need to and makes lookup more difficult
	var Ch = Array();


	// add a new category to the heirarchy with parent_id
	// parent_id == 0 is for root tabs
	this.Add = function(c, parent_id) {
		// first add to Cs
		var id = c.id;
		
		Cs[id] = c;
		
		// make sure Ch[parent_id] is an array
		if(! isArray(Ch[parent_id]) )
			Ch[parent_id] = Array();
			
		Ch[parent_id].push(id);
	}


	// fetch the sub-categories of parent_id
	// if parent_id == 0, return the root cats 
	this.Sub = function(parent_id) {
		if(! isArray(Ch[parent_id]) )
			return Array();
	
		return Ch[parent_id];
	}
	
	
	// return the category id
	this.Fetch = function(id) {
		return Cs[id];
	}	

	return me;
}


function Tabs(el, H, co_id) {
	var me = this;
	var container = el;
	
	var dom = document.createElement("ul");
	
	function DrawTabs(co_id) {
	
		var Co = H.Sub(co_id);
		
		if(Co.length == 0)
			return;
		
		for(var i=0; i<Co.length; i++) {		
		
			var co_id = Co[i];
			var co = H.Fetch(co_id);
			
			var t = li(dom);
			
			var link = aa(t, co.name, co.href);
			
			// see if we have children
			if(H.Sub(co.id).length > 0)
				t.className+=" daddy";
			
			dom.appendChild(t);
			
			new Tabs(t, H, co_id);
		}
		
		container.appendChild(dom);
	}
	
	if(co_id == undefined)
		co_id = 0;
	
	DrawTabs(co_id);
}	
	
	
sfHover = function() {
	var sfEls = document.getElementById("cats").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
sfHover();
//if (window.attachEvent) window.attachEvent("onload", sfHover);
	


// create a new li and append to list l
// return li
function li(l) {
	var _li = document.createElement("li");	
	l.appendChild(_li);
	return _li;
}



// create a new td with content el in tr
function td(tr, el) {
	var td = document.createElement("td");	
	td.appendChild(el);
	
	tr.appendChild(td);
	return td;
}


// append a new link to el and return link
function aa(el, text, href) {
	var link = a(text,href);
	el.appendChild(link);
	return link;
}


// return a new link
function a(text, href) {
	var link = document.createElement("a");
	ctn(link, text);
	link.href = href;
	return link;
}

// append a text node
function ctn(el, text) {
	el.appendChild(document.createTextNode(text));
}