var SNM = function(data, config) {
	this.ie = (document.all && !window.opera);
	this.level = 0;
	this.config = config;
	this.theme = new Array();
	this.data = data;
	this.strHTML="";
	this.isDefined=function(el){return (typeof(el)=="undefined"||el==null)?false:true;};
	this.build=function()
	{	
		var strHTML = "";
		strHTML += "<ul id=\""+this.config.prefix+"primary\">";
		for(var i=0; i<this.data.length; i++)
		{
			strHTML += this.buildRow(this.data[i], i);
		}
		strHTML += "</ul>";
		this.container.innerHTML = strHTML;
	};
	this.init=function(current, preferences)
	{
		this.container = $(this.config.container);
		this.current = (this.isDefined(current))?current:null;
		this.build();
		preferences = (preferences)?preferences:null;
	};
	this.setPref = function(pref)
	{
		var pref = (this.isDefined(pref))?pref:false;
		this.config.prefix = (pref && pref.prefix)?pref.prefix:this.config.prefix;
		this.config.indent = (pref && pref.indent)?pref.indent:this.config.indent;
		this.config.bg = (pref && pref.bg)?pref.bg:this.config.bg;
		this.config.iconPosition = (pref && pref.iconPosition)?pref.iconPosition:this.config.iconPosition;
	};
	this.addItems=function(arr, theme)
	{
		var str = "<ul class=\"subnav\" "+((this.ie)?"style=\"display:none;\"":"")+">";
		var classname = null;
		for(var i=0; i<arr.length; i++)
		{
			str += "<li class=\"s\"><a class=\"s\" href=\""+arr[i].url+"\" "+((classname!=null)?"class=\""+classname+"\"":"")+">"+arr[i].name+"</a></li>";
		}
		str += "</ul>";
		return str;
	};
	this.buildRow=function(node, nodeNo)
	{
		var str = "";
		var level = (this.current!=null)?this.current.substring(0, this.current.indexOf(".")):null;	
		var mo = (this.ie && node.children!=null)?"main.hoverForIE(this);":"";
		var mout = (this.ie && node.children!=null)?"main.hoverForIE(this);":"";
		
		str += "<li class=\"p\" onmouseover=\""+mo+"\" onmouseout=\""+mout+"\"><a href=\""+node.url+"\" class=\""+((nodeNo==(level-1))?"selected":"p")+"\">"+node.name+"</a>";	
		if(node.children!=null)
		{
			this.level++;
			str += this.addItems(node.children);
			this.level--;
		}
		str += "</li>";		
		return str;
	};
	this.hoverForIE = function(e)
	{
		var el = e.childNodes[1];
		if(el.style.display == "none")
		{
			el.style.display = "block";
		}
		else
			el.style.display = "none";
	};
}
var el;
function findChild(a, b)
{	
	for(var i=0; i<a.childNodes.length; i++)
	{
		if(a.childNodes[i].tagName==b)
		{
			if(!el)
				el = a.childNodes[i];
			return el;
		}
		if(a.childNodes[i].hasChildNodes())
			findChild(a.childNodes[i], b);
	}
	return el;
}
function findParent(e, t)
{
	var tmp = null;
	while(e.parentNode)
	{
		if(e.parentNode.tagName==t)
		{
			tmp = e.parentNode;
			break;
		}
		e = e.parentNode;	
	}
	return tmp;
}
var main = new SNM(data,
	{
		name:"main",
		container:"menu",
		prefix:"nav_"
	}
);