var dots = "";
var snm = function(data, config) {
	this.level = 0;
	this.config = config;
	this.data = data;
	this.strHTML="";
	this.isDefined=function(el){return (typeof(el)=="undefined"||el==null)?false:true;};
	this.build=function()
	{	
		var strHTML = "";
		strHTML += "<div style='width:"+this.config.width+"; height:"+this.config.height+"; overflow:hidden; text-align:left; background-color:"+this.config.bg[this.level]+";'><ul id=\""+this.config.prefix+"_root\" class=\""+this.config.prefix+"menu\">";
		for(var i=0; i<this.data.length; i++)
		{
			strHTML += this.buildRow(this.data[i]);
		}
		strHTML += "</ul></div>";
		this.container.innerHTML = strHTML;
	};
	this.init=function(c, n)
	{
		this.container = $(this.config.container);
		this.setPref(c);
		this.build();
		if(this.isDefined(n)) this.expandToNode(n);
	};
	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)
	{
		// this is the generic add item that's used to add an item to the SNM
		var child = 3;		
		var display = (arr[2]!=null)?"display:none;":"";
		var str = "<ul style=\"list-style-type:none; list-style-image:none; border:none; margin:0px; padding:0px;"+display+"\">";
		var img;
		do{
			str += this.buildRow(arr[child]);
			child++;
		}while(arr[child]&&arr[child]!=null)
		
		str += "</ul>";
		this.menus++;
		return str;
	};
	this.buildRow=function(arr)
	{
		var str = ""; var bg = ""; var style = "";
		if(this.isDefined(arr[3]) && this.level == 0)
		{
			bg = "background: url(resources/images/arrow.gif) no-repeat 187px 50%;";
		}
		if(this.config.style!=null) style = this.getStyle();
		// href=\""+arr[1]+"\"
		if(this.level == 0 && this.isDefined(arr[3]))
			str += "<li id=\""+this.config.prefix+""+arr[2]+"\" style=\""+style[0]+"\"><a onclick=\""+this.config.name+".showChild(event, this)\" style=\""+bg+style[1]+"\">"+arr[0]+"</a>";
		else
			str += "<li id=\""+this.config.prefix+""+arr[2]+"\" style=\""+style[0]+"\"><a href=\""+arr[1]+"\" style=\""+bg+style[1]+"\">"+arr[0]+"</a>";
		
		if(this.isDefined(arr[3]))
		{
			this.level++;
			str += this.addItems(arr);
			this.level--;
		}
		str += "</li>";
		
		return str;
	};
	this.getStyle = function()
	{
		var sli = this.config.style[this.level].li;
		var sa = this.config.style[this.level].a;
		var li = "";
		var a = "";
		for(key in sli)
		{
			li += key+":"+sli[key];
		}
		for(key in sa)
		{
			a += key+":"+sa[key];
		}
		return [li, a];
	};
	this.showChild=function(e, element, section)
	{
		// this is called when you are expanding a node so see it's children
		var elem=(!(this.isDefined(element)&&this.isDefined(element.tagName)))?null:element;
		var ct = (elem!=null)?elem.nextSibling:this.nextSibling;
	
		
		bi = elem.style.backgroundImage;
		if(bi == "" && (ct.style.display != "none"))
			elem.style.backgroundImage = "url(resources/images/arrow.gif)";	
		else
			elem.style.backgroundImage = "";
		ct.style.display = (ct.style.display=="none")?"":"none";
	};
	this.collapseAll=function(e)
	{
		var tbl = findParent(findParent(e, "TD"), "TABLE").firstChild;
		var ri = null; var s = null; var b = null;
		for(var a=0; a<tbl.childNodes.length; a++)
		{
			var s = tbl.childNodes[a].firstChild.childNodes[1];
			if(s!=null && s.style.display=="")
			{				
				ri = findChild(tbl.childNodes[a], "TABLE"); el = null;
				ri.style.backgroundColor = "#EAEEF7";
				b = findChild(ri, "A"); el = null;
				b.style.color = "#333333";
				s.style.display = "none";
				i = findChild(ri, "IMG"); el = null;
				this.changeImage(i);
				
			}
		}
	};	
	this.fadeIn=function(id)
	{
		var el = getElement(id);
		el.style.display = "";
	};
	this.fadeOut=function(id)
	{
		var el = getElement(id);
		el.style.display = "none";
	};
	this.expandToNode=function(node, selItem)
	{
		var n = $(this.config.prefix+""+node);
		if(n!=null)
		{
			var img, a;	
			var icon = (this.config.iconPosition=="right")?["r", 0]:["l", 197];		
			n.style.background = "url(resources/images/selected-"+icon[0]+".gif) "+this.config.bg[2]+" no-repeat "+icon[1]+"px 50%";
			while(n.parentNode)
			{
				if(n.parentNode.id==(this.config.prefix+"_root"))
					break;
				if(n.parentNode.tagName=="UL")
					n.parentNode.style.display = "";
				else if(n.parentNode.tagName == "LI")
				{
					n.parentNode.style.backgroundColor = this.config.bg[1];
					n.parentNode.firstChild.style.backgroundImage = "";
				}
				
				n = n.parentNode;
			}
		}
	};
	this.changeImage=function(el)
	{
		// this is called to change the plus and minus when you're showing and hiding a nodes children
		if(getFileName(el.src)=="arrow_right.gif")
			changeImage(el, this.ptr+dots+'resources/images/arrow_down.gif');
		else
			changeImage(el, this.ptr+dots+'resources/images/arrow_right.gif');
	};
	this.getPadding=function(cl)
	{
		// this is used to get the padding that we're going to apply to an item in the SNM
		var pt, pb, pl;
		if(cl=="root")
		{
			pt=1; pb=2;
		}
		else{
			pt=2; pb=3;
			pl = 6;
		}
		return [pt, pb, pl];
	};
	this.getUrl=function(str)
	{
		// this is used to create a URL, basically wrapping a current url in a confirm function.
		var ahref = str;
		ahref = (ahref!=null)?(ahref.indexOf("javascript:void(")==-1)?"javascript:confirmRedirect(\""+ahref+"\");":ahref:"javascript:void(0);";
		return ahref;
	};
	this.over=function(e, l, cl)
	{		
		this.menuover = true;
		var elem=(!(menu.isDefined(findParent(e, "TABLE"))&&menu.isDefined(findParent(e, "TABLE").tagName)))?null:findParent(e, "TABLE");
		var ct = (elem!=null)?findParent(elem, "DIV"):null;
				
		if(ct.nextSibling && ct.nextSibling.id != "divider")
		{
			if(ct.nextSibling.style.display == "none")
			{				
				elem.style.backgroundColor = (cl=="root")?"#ACBDC7":this.getBC(l-1);
				e.childNodes[0].style.color = "#FFFFFF";
				e.nextSibling.style.color = "#FFFFFF";
			}
		}
		else
		{
			elem.style.backgroundColor = (cl=="root")?"#ACBDC7":this.getBC(l-1);
			e.childNodes[0].style.color = "#FFFFFF";
			e.nextSibling.style.color = "#FFFFFF";	
		}
	};
	this.out=function(e, l, cl)
	{	
		this.menuover = false;
		var elem=(!(menu.isDefined(findParent(e, "TABLE"))&&menu.isDefined(findParent(e, "TABLE").tagName)))?null:findParent(e, "TABLE");
		var ct = (elem!=null)?findParent(elem, "DIV"):null;
		
		if(ct.nextSibling && ct.nextSibling.id != "divider")
		{
			if(ct.nextSibling.style.display == "none")
			{				
				elem.style.backgroundColor = (cl=="root")?"#EAEEF7":"#ACBDC7";
				e.childNodes[0].style.color = "#333333";
				e.nextSibling.style.color = "#333333";
			}
		}
		else
		{
			var a = findParent(ct, "TD");
			if(a.id != this.selectedItem)
			{
				elem.style.backgroundColor = (cl=="root")?"#EAEEF7":"#ACBDC7";
				e.childNodes[0].style.color = "#333333";
				e.nextSibling.style.color = "#333333";
			}
		}
	};
}
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 top = new snm(t,
	{
		name:"top",
		style:[
			{ li:{"border-bottom":"none;", margin:"0px;", "padding-left":"0px;"}, a:{ "padding-left":"23px;"}},
		],
	//	width:"200px",
		height:"115px",
		indent:"23px",
		container:"tN", 
		prefix:"top_",
		iconPosition:"right",
		bg:["#D5C9A3", "", ""]
	}
);
var main = new snm(m,
	{
		name:"main",
		style:[
			{ li:{"border-bottom":"1px solid #C6CABE;", margin:"0px;", "padding-left":"0px;"}, a:{ height:"26px;", "line-height":"2.5em;", padding:"0px;", "padding-left":"23px;"}},
			{ li:{border:"none;", margin:"0px;", "padding":"0px;", "padding-left":"0px;"}, a:{"padding":"0px;", "line-height":"1.6em;", "padding-left":"40px;", "padding-right":"4px;", "text-transform":"none;"}}
		],
		//width:"200px",
		indent:"23",
		container:"mN", 
		prefix:"main_",
		iconPosition:"left",
		bg:["#9F9F9E", "rgb(138, 151, 151)", "rgb(124, 136, 136)"]
	}
);
