function UOSMenu(){
    this.data = null;
    this.regular = true;
}
UOSMenu.prototype.toString = function(){
    var shtml = this.parse();

    var s = '<div class="skipnav"><a href="#skipleftnav">Skip Left Navigation</a></div>';
	s += '<div id="leftnav" class="clear">';
    s += shtml;
    s += "</div>";
    s += '<div class="skipnavanchor"><a name="skipleftnav" id="skipleftnav"></a></div>';
    
    /*if (document.location.href.indexOf("debugger")!=-1){
        s += "<textarea style='width:600;height:500;z-index:1000;position:absolute;left:210;top:200'>";
        s += '<div class="skipnav"><a href="#skipleftnav">Skip Left Navigation</a></div>';
	    s += '<div id="leftnav" class="clear">';
        s += shtml;
        s += "</div>";
        s += "</textarea>";
    }*/

    return s;
}
UOSMenu.prototype.parse = function(){
    var levelout = {"level":-1};
    this.findLevel(this.data, 0, levelout);
    
    this.sort(this.data);

    var sb = new StringBuilder();
    this.findPath(this.data, 0, sb, levelout);
    
    return sb.toString();
}

UOSMenu.prototype.findPath = function(ar, level, sb, levelout, parent){
    
    //has to match the bottom of the for loop
    if (level==0 || level>=2){
        sb.append("<ul");
        if (levelout.level>level && level==0) sb.append(" class=leftnav-currentsection");
        if (typeof(parent)=="undefined") sb.append(" class=leftnav-currentsection");
        sb.append(">");
    }
    
    if (level==2 && this.match(parent[2])) sb.append("<ul>");
    
    for (var i=0; i<ar.length; i++){
    
        var hasSub = (typeof(ar[i][6])!="undefined");
        var within = (hasSub) ? this.isWithin(ar[i][6]) : false;
        var here = (this.match(ar[i][2]) || this.match(ar[i][5]));
        var dig = (hasSub && (within||here));
         
        if (!this.regular) if (level==1 && (!dig && levelout.level!=level) && !this.match(parent[2])) continue; //questionable

        sb.append("<li");
        if (level==0){  
            sb.append(" class='leftnav-sectionheader");
            if (here) sb.append(" leftnav-currentsectionheader");
            sb.append("'");
        }
        
        //edged level 1 item
        if (!this.regular) if (level==1 && levelout.level>level) sb.append(" class=leftnav-path");
        
        //last page for level 2
        if (level==2 && i==ar.length-1){
            if (ar.length>1 && level!=2){
                sb.append(" class='leftnav-lastpage");
                if (here || dig) sb.append(" leftnav-currentpage"); 
                sb.append("'");
            }
        }
        
        //last page for level 1
        if (level==1 && i==ar.length-1){
            sb.append(" class='leftnav-lastpage");
            if (here) sb.append(" leftnav-currentpage");
            sb.append("'");
        }
        
        if (level>0 && (here)){
            sb.append(" class='leftnav-currentpage");
            if (ar.length==1 || (level==1 && !here)) sb.append(" leftnav-lastpage");
            sb.append("'");
        }
        
        
        sb.append(">");
        
        //triangle for level 0 selected (open/closed)
        if (level==0){
            if (here) sb.append('<img class="leftnav-highlight" src="/files/static/uos/images/navigation/leftnav/highlight-currenttophousepage.gif" alt="You are within:" />');
            else sb.append('<img class="leftnav-highlight" src="/files/static/uos/images/navigation/leftnav/highlight-house.gif" alt="You are within:" />');
        }
        
        //bullet for level 0 unselected
        if (level==0 && !dig && !here) sb.append('<img class="leftnav-highlight" src="/files/static/uos/images/navigation/leftnav/highlight-house-inactive.gif" alt="Go to:" />');
        
        //blue arrow down
        //if (level==1 && levelout.level<level && !this.match(parent[2])) sb.append('<img class="leftnav-highlight" src="/files/static/uos/images/navigation/leftnav/highlight-path.gif" alt="You are within:" />');
        //if (level==1 && dig && !here) sb.append('<img class="leftnav-highlight" src="/files/static/uos/images/navigation/leftnav/highlight-path.gif" alt="You are within:" />');
        
        //chevron on the left
        if (level>0 && here) sb.append('<img class="leftnav-highlight" src="/files/static/uos/images/navigation/leftnav/highlight-currentpage.gif" alt="You are on:" />');
        
        if (!here){
            sb.append("<a");
            sb.append(" href=\"" + ar[i][2] + "\"");
            sb.append(">");
        }
        sb.append(ar[i][1]);
        if (!here){
            sb.append("</a>");
        }
        
        if (dig){
            this.findPath(ar[i][6], level+1, sb, levelout, ar[i]);
        }
        
        sb.append("</li>");
        
        //level 0 boxes at the bottom of the nav
        if (level==0 && i==0){
            sb.append("</ul>");
            sb.append("<ul>");
        }
    }
    
    //has to match the top of the for loop
    if (level==0 || level>=2){
        sb.append("</ul>");
    }
    
    if (level==2 && this.match(parent[2])) sb.append("</ul>");
    
}

UOSMenu.prototype.findLevel = function(ar, level, levelout){
    for (var i=0; i<ar.length; i++){
        if (levelout.level>-1) break;
        
        if (this.match(ar[i][2])){
            levelout.level = level;
            break;
        }
        if (typeof(ar[i][6])!="undefined"){
            this.findLevel(ar[i][6], level+1, levelout);
        }
    }
}
UOSMenu.prototype.match = function(s){
    var b = (s.indexOf("cid-" + CMS_PageId + ".html")!=-1);
    var c = (s.indexOf(";" + CMS_PageId + ";")!=-1);
    
    if (b || c) return true;
    return false;
}
UOSMenu.prototype.isWithin = function(s){
    return this.match(s.toString());
}

UOSMenu.prototype.sort = function(ar){
    var a = [];
    var j = 0;
    for (var i=0; i<ar.length; i++){
        var hasSub = (typeof(ar[i][6])!="undefined");
        var within = (hasSub) ? this.isWithin(ar[i][6]) : false;
        var here = this.match(ar[i][2]);
        if (within||here){
            a.push(ar[i]);
            j = i;
            break;
        }
    }
    for (var i=0; i<ar.length; i++){
        if (i==j) continue;
        a.push(ar[i]);
    }
    this.data = a;
}

function StringBuilder() {  
  this._strings = [];  
  this.append = function(s) { 
      this._strings.push(s); 
      //return this; //allows chaining (.append.append)
    };  
  this.toString = function() { return this._strings.join(''); };
}








//=== Initializer ===//

function FSMenu(id){
	var a = eval(id + "_array");
	
	if (a==null) return;
	
	var m = new UOSMenu;
		m.data = a;

	var li = document.getElementById(id);
	li.style.display = "none";
	var strmenu = m.toString();

	if (navigator.appName.toLowerCase().indexOf("internet explorer")==-1){
		var r = li.ownerDocument.createRange();
			r.setStartBefore(li);
			
		var parsedHTML = r.createContextualFragment(strmenu);

		if (li.nextSibling)
		{ 
			li.parentNode.insertBefore(parsedHTML, li.nextSibling);
		}
		else
		{
			li.parentNode.appendChild(parsedHTML);
		}
	}
	else
	{
		li.insertAdjacentHTML("beforeBegin", strmenu);
	}
}

//2b deprecated
function addEvent(){}

//==================//
