var baseURL = "http://chi2011.org/";
//var baseURL = "file:///C:/Users/t-lydiac/Documents/CHI2011b/";


//function onLoad() {

//}
 $(document).ready(function() {
	folder="";
	loc=window.location.href; 
	baseURLlength = baseURL.length;
	restOfURL = loc.substring(baseURLlength);
	indexOfSlash = restOfURL.indexOf("/")
	
	if(indexOfSlash <0){
		//don't need to do anything. we aren't in a folder
		//console.log("/");
	}else{
		folder = restOfURL.substring(0, indexOfSlash);
	}
	//console.log("folder: "+folder);
	
    createTopNav();
    createCommunitiesNav();	
	if(folder==""){
		createTextMenu();
	}
 });	 
/*	 
var showSubNav= function(section){
	console.log("in: "+section);
	var subsection = document.getElementById("sub-nav-"+section);
	if(section!="chi2011"){
		//console.log('before');
		//subsection.setAttribute("className", "sub-nav-visible");
		$(subsection).addClass("visible");
		//console.log('after');
	}
}
var hideSubNav =function(section){
	console.log("out: "+section);
	var subsection = document.getElementById("sub-nav-"+section);
	if(section!="chi2011"){
		//$(subsection).hide();
		//subsection.style.visibility = "collapse";
		//subsection.setAttribute("className", "sub-nav");
		//$(subsection).removeClass("visible");
	}
}
*/
function createTopNav(){
    var div = document.getElementById("main-nav");
    var div2 = document.getElementById("sub-nav-attach");
	
	//find out which folder we are in (this tells us which submenu to make visible)	
	
    for (var i = 0; i < sections.length; i++) {
        var section = sections[i].title;
	var aElt = document.createElement("a");
	if(section==folder || (folder=="" && section=="chi2011")){
		//make it visible
		//console.log("a "+section);
		aElt.className = "header-link selected";
	}else{
		//console.log("b "+section);
		aElt.className = "header-link";
	}
	
	var textNode = document.createTextNode(section.toUpperCase())
	var textNodeContainer = document.createElement("div");
	textNodeContainer.appendChild(textNode)
	aElt.appendChild(textNodeContainer);
	offset = 88*i+"px";
	aElt.style.left=offset ;
	/*
	aElt.setAttribute("onMouseOver", "showSubNav('"+section+"')");
	aElt.setAttribute("onMouseOut", "hideSubNav('"+section+"')");
	*/
	var aEltHref = baseURL+section+"/index.html"; 
	if(section=="chi2011"){
		var aEltHref = baseURL+"index.html"; 
	}
	aElt.href = aEltHref ;  
	
        div.appendChild(aElt);


	//sub navigation
	var subnavdiv = document.createElement("div");
	if(section==folder){
		//make it visible
		subnavdiv.className = "sub-nav visible";
	}else{
		subnavdiv.className = "sub-nav";
	}
	subnavdiv.id="sub-nav-"+section;    
	
	var subnav = sections[i].subsections;
	for(var s = 0; s < subnav.length; s++){
		
		var subsectionTitle = subnav[s].title;
		var subsectionId = subnav[s].id;

		sectionAnchor = document.createElement("a");
		sectionAnchor.className="section-anchor";
		
		sectionAnchor.href=baseURL+section+"/"+subsectionId+".html";
		
		var subsectionTitle = document.createTextNode(subsectionTitle);
		sectionAnchor.appendChild(subsectionTitle);
		subnavdiv.appendChild(sectionAnchor);
				
	}
	div2.appendChild(subnavdiv);
    }
}

function createCommunitiesNav(){

	var communitiesLabelDiv = document.getElementById("communities-label");	
	communitiesLabelDiv.innerHTML ="<a href='"+baseURL+"communities/index.html'>COMMUNITIES</a>";
    
    var featuredCommunitiesLabelDiv = document.createElement('div');
    featuredCommunitiesLabelDiv.id = "featured-communities-label";
    featuredCommunitiesLabelDiv.innerHTML="<a href='"+baseURL+"communities/index.html#featured-communities'>FEATURED <br>COMMUNITIES</a>";
	communitiesLabelDiv.parentNode.appendChild(featuredCommunitiesLabelDiv);
    
    var div = document.getElementById("communities-nav");

	
    for (var i = 0; i < communities.length; i++) {
        var community= communities[i].title;
	var aElt = document.createElement("a");
	aElt.className = "community-link";
	aElt.style.backgroundImage = "url(http://chi2011.org/images/" + community + "_off.jpg)";

	    
	offset = 40+26*i+"px";
	    if(i>3){
		 offset = 102+26*i+"px";   
	    }
	aElt.style.top=offset ;
	
	    

	    
	var aEltHref = baseURL+"communities/"+communities[i].folderName+"/index.html"; 
	aElt.href = aEltHref ;  
	var foo = function (elt, j) {
		Image1= new Image(22,80)
		Image1.src = "http://chi2011.org/images/" + communities[j].title + "_on.jpg";	

		$(elt).hover(function() {
			this.style.backgroundImage = "url(http://chi2011.org/images/" + communities[j].title + "_on.jpg)";
		}, function() {
			this.style.backgroundImage = "url(http://chi2011.org/images/" + communities[j].title + "_off.jpg)"
		});
	}
        foo(aElt, i);

div.appendChild(aElt);
	
    }
}


function createTextMenu(){
/*
    var div = document.getElementById("about-message");

    for (var i = 0; i < menuItems.length; i++) {
	var itemId = menuItems[i].id;
	var itemText = menuItems[i].text;
	
	var elmt = document.createElement("img");
	elmt.src = "images/arrow_image.png";
	elmt.alt = "arrow";

	var aElt = document.createElement("a");  	
        //aElt.href = "#"+item;
	var textNode =  document.createTextNode(" "+itemText); 
	aElt.appendChild(elmt)      
        aElt.appendChild(textNode); 

	div.appendChild(aElt);
	div.appendChild(document.createElement("br"));
	
    }
    div.appendChild(document.createElement("br"));
*/	
}

//});
