// poolSplash.js version 1.5, modified by Jon to tweak formatting of choose-a-theme interstice.
// =======================================================================================
// ====================== Choose-a-Theme interstice for Art Pool. ======================
// =======================================================================================
/*__________ Constants. __________*/
tagMaximumEmsNum = 3 ; // Default is 4. Increase to increase size of biggest tags.
tagFactorRelativeToMaximumNum = .5 ; // Default is .5. Increase to increase size of smaller tags.
/*__________ Called when jumping to Art Pool. __________*/
	function openInterstice () {
		// Gateway filter by theme for Art Pool.
		var intersticeDiv = document.createElement("div") ;
		document.body.appendChild( intersticeDiv ) ;
		intersticeDiv.id = "interstice" ;
		intersticeDiv.style.position = "absolute" ;
		intersticeDiv.style.backgroundColor = "paleturquoise" ;
		intersticeDiv.style.height = "70%" ;
		intersticeDiv.style.width = "80%" ;
		intersticeDiv.style.color = "#06c" ;
		intersticeDiv.style.fontFamily = "verdana" ;
		intersticeDiv.style.textAlign = "justify" ;
		intersticeDiv.style.top = "150px";
		intersticeDiv.style.left = "48px" ;
		intersticeDiv.style.padding = "2% 5% 2% 5%" ;
		intersticeDiv.style.zIndex = 10000 ;
		/*__________ Add a header for interstice. __________*/
		var showAllLinkHTML = "<a href='http://pool.newmedia.umaine.edu/art_pool.php' style='left: 10%'>Show projects for all themes</a>" ;
		var backLinkHTML = "<a href='#' onclick='closeInterstice()' style='position: absolute; right: 10%;'>Return to Pool introduction</a> " ;
		var chooseThemeHTML = "<div style='position: absolute; left: 40%;'>Or choose a theme to filter by:</div>" ;
		var footerHTML = "<div style='clear: both; height: 20px;'></div>" ;
		intersticeDiv.innerHTML += showAllLinkHTML + " " + backLinkHTML + "<hr />" + chooseThemeHTML + footerHTML ;
		/*__________ Calculate tag sizes. __________*/
		// Decide how big frequent tags should be, using the max and minimum tag frequencies as parameters.
		// UPGRADE: May need to trap for no subjects, if Pool is reconstituted without data.
		// PoolSubject is an object rather than array, so make a dummy array just for sorting.
		var subjectFrequencies = [] ;
		for (subjectId in PoolSubject) {
			 subjectFrequencies.push( PoolSubject[subjectId]['frequency'] ) ;
		}
		subjectFrequencies.sort( ) ;
		tagFrequencyMin = subjectFrequencies[ 0 ];
		tagFrequencyMax = subjectFrequencies[ subjectFrequencies.length-1 ] ;
		
		//John Added:
		var linkContainer = document.createElement('div');
		intersticeDiv.appendChild(linkContainer);
		/*__________ Add tags to interstice. __________*/
		// Fill it with themes from dynamic array.
		for (subjectId in PoolSubject) {
			var subjectName = PoolSubject[subjectId]['name'] ;
			var subjectSize = setTagSize( {
				frequency: PoolSubject[subjectId]['frequency'],
				minimum: tagFrequencyMin,
				maximum: tagFrequencyMax
			}) ;
			var subjectStyle = " style='font-size:" + subjectSize + "em;'" ;
			
//			 Example: http://pool.newmedia.umaine.edu/art_pool.php#ax=basis&ay=rating&az=cardinality&azAcc=4&ayAcc=4&axAcc=4&f=subjects|103
//			Firefox makes this: http://pool.newmedia.umaine.edu/art_pool.php#ax=basis&ay=rating&az=cardinality&azAcc=4&ayAcc=4&axAcc=4&f=subjects%7C54
			// Using the W3C method solves this problem.
			var newLink = document.createElement('a');
			newLink.href = "art_pool.php#ax=basis&ay=rating&az=cardinality&azAcc=4&ayAcc=4&axAcc=4&f=subjects|" + subjectId;
			newLink.style.fontSize = subjectSize + "em";
			newLink.innerHTML = subjectName;
			linkContainer.appendChild(newLink);
			// In v1.5, Jon moved space out of link text so underline wouldn't go beyond subject.
			var spacingText =  document.createTextNode(" ") ;
			linkContainer.appendChild(spacingText) ;
		};
	}
	function closeInterstice (argument) {
		document.getElementById("interstice").style.display = "none" ;
	}
	/*__________ Utilities __________*/
	function setTagSize( argObj ) {
		// Jon used square root to give small numbers a fighting chance. In ems.
		// It doesn't matter how many there are, it matters how big the max is.
		// Goal is for the maximum size to be 4em, minimum .8em. So I want a square root function with f(argObj.maximum) = 4 and f(argObj.minimum) = .8.
		var tagSize = tagMaximumEmsNum * (   Math.sqrt( tagFactorRelativeToMaximumNum * (argObj.frequency/argObj.maximum) )   );
		if (tagSize < 1 ) tagSize = 1 ;
		return tagSize ;
	}
// ==============================================================================
// ====================== Global utilities for all menus ======================
// ==============================================================================
ExpandingMenu.prototype.setMenuResponse = function(argObj) { // Customized for Pool Splash.
    // This particular response function must be called after parent-child relationships established.
    // It also assumes a certain naming convention for menu instances and page styles:
    // "this" is the JS wrapper of the menu div clicked on. "this.id" looks like "navMenu2Obj". The variable name of this object is like navMenu2ObjVar.
    var responseFunVar = " " ;
	/*______________ Global changes ______________*/
	// Hide splash screen. (Hiding when a menu is clicked creates less bubbling than adding an onclick to the body, and permits show on logo click.) 
	document.getElementById("logo-a").onmouseover = function() {
		document.getElementById("logo-a").style.cursor = "pointer" ;
	}
	document.getElementById("logo-a").onmouseout = function() {
		document.getElementById("logo-a").style.cursor = "normal" ;
	}
	document.getElementById("logo-a").onclick = function() { 
		respondAsIf( document.getElementById('orient') ) ;
		document.getElementById('splash-only').style.display='block' ;
		var firstMenuItems = document.getElementById('orient').menu.htmlElement.getElementsByTagName('div') ;
		for (var itemCounter=1; itemCounter < firstMenuItems.length; itemCounter++) { // Starting with 1 so as to omit lead item.
			firstMenuItems[itemCounter].style.display='none' ;
		};
	}
	responseFunVar += "document.getElementById('splash-only').style.display='none' ; " ;
	/*______________ Local changes ______________*/
    // This particular response function must be called after parent-child relationships established.
    var menuSelected = this.htmlElement.id + "Obj" ;
    for ( var menuNum = 0; menuNum < argObj.menusNotSelected.length ; menuNum++ ) {
        responseFunVar += " " + argObj.menusNotSelected[menuNum] + ".setInitialCondition( " + argObj.menusNotSelected[menuNum] + ".itemManager[0] ); " ;
    }
    // Collapse menu selected if you clicked on a child (expand if you didn't).
    if ( this.itemManager[argObj.item].linkType == "child" ) {
        responseFunVar += " " + menuSelected + ".setInitialCondition(" + menuSelected + ".itemManager[0] );" ;
        responseFunVar += " " + menuSelected + ".itemManager[" + argObj.item + "].htmlElement.style.display = 'block' ; " ;
        responseFunVar += " " + menuSelected + ".itemManager[" + argObj.item + "].htmlElement.style.color = 'rgb(0%, 40%, 80%)' ; "; // Emulates visited link color (middyblue).
        responseFunVar += " " + menuSelected + ".itemManager[" + argObj.item + "].htmlElement.style.border = 'rgb(10%, 40%, 60%) solid 1px' ; " ; // logo shadow.
        
    }
    for ( var menuNum = 0; menuNum < argObj.menusNotSelected.length ; menuNum++ ) {
        responseFunVar += " " + argObj.menusNotSelected[menuNum] + ".hideAllContent(); " ;
    }
    return responseFunVar ;
}
ExpandingMenu.prototype.prepareMenu = function() {
    //Associate superlinks with sublinks. UPGRADE: NEW FEATURE--add to ExpandingMenu.js as example.
    // Assume the first link may be a parent.
    menuParentCandidateObj = this.itemManager[0] ;
    for (var item=1; item < this.itemManager.length; item++) {
        // If the link id has no hyphen, then assume it may be a parent.
        if (this.itemManager[item].htmlElement.id.indexOf("-") == -1 ) {
            menuParentCandidateObj = this.itemManager[item] ;
             // _parentCandidate = this.itemManager[item].htmlElement.id ; debug("_parentCandidate") ;
        }
        // If the link id has a hyphen, associate it with the parent.
        else {
            menuParentCandidateObj.associateChildren(  [ this.itemManager[item] ]  ) ;
             // _childCandidate = this.itemManager[item].htmlElement.id ; debug("_childCandidate") ;
        }
    }
    //Here we automate these assignments for each particular application.
    //Associate all the content using a formulaic approach.
    for (var counter=0; counter < this.itemManager.length; counter++) {
        var contentIdStr = "content-" + this.itemManager[counter].htmlElement.id;
        this.itemManager[counter].associateContent( document.getElementById(contentIdStr) );
    }
    // Set classes and custom menu response using formulaic approach based on id nomenclature.
    // UPGRADE: think of a way to use className here. The problem is applying styles without knowing which class the target has.
    for (var item=0; item < this.itemManager.length; item++) {
        var itemClassesObj = {
            argBachelorUnselected: "menulink-bachelor-unselected", 
            argBachelorSelected: "menulink-bachelor-selected", 
            argParentCollapsedUnselected: "menulink-parent-collapsed-unselected",
            argParentCollapsedSelected: "menulink-parent-collapsed-selected",
            argParentExpandedUnselected: "menulink-parent-expanded-unselected",
            argParentExpandedSelected: "menulink-parent-expanded-selected",
            argChildUnselected: "menulink-child-unselected",
            argChildSelected: "menulink-child-selected",
            argMouseover: "this.style.cursor = 'pointer'; this.style.border = 'rgb(10%, 40%, 60%) solid 1px' ; " ,
            argMouseout: "this.style.cursor = 'default'; this.style.border = '#99d9e8 solid 1px'; "
        } // mouseover color = logo shadow 
        /* Use if you want a special style for parent menu items.
        if (this.itemManager[item].linkType != "child") {
            itemClassesObj.argMouseover = "this.style.cursor = 'pointer'; this.style.border = 'rgb(10%, 40%, 60%) solid 1px' ; " ; // logo shadow.
            itemClassesObj.argMouseout = "this.style.cursor = 'default';this.style.border = '#99d9e8 solid 1px'; " ;
        }
        */
        this.itemManager[item].setClasses( itemClassesObj ) ;
    }
    // Initialize the menu and choose an initial item to select.
    this.setInitialCondition( this.itemManager[0] );
}
function nextContent(anchorEle) {
    // nextContent should be called from <a> in content div.
    // UPGRADE: not sure this version will always leap across menus and all other obstacles. You could add a for loop to find the next content div.
    // First trap for spaces between content divs.
    if (anchorEle.parentNode.nextSibling.nodeType == 1) { // Must be a tag, not just text (white space).
        var menuItemObj = anchorEle.parentNode.nextSibling.menuItem;
    }
    else {
        var menuItemObj = anchorEle.parentNode.nextSibling.nextSibling.menuItem; 
    }
    menuItemObj.respond({ menuItemEle: menuItemObj.htmlElement }) ;
}
//____________________ Special functions for this page ___________________ //
function hideSplashDescription(thing) {
    document.getElementById("content-orient").style.display = 'none' ;
}
function expandToFullScreen() {
    window.moveTo(0,0);
    window.resizeTo(screen.availWidth,screen.availHeight);
}
//____________________ Instantiate a drop-down ___________________ //
// UPGRADE: CONSOLIDATE BELOW INTO GLOBAL FUNCTIONS CALLED FOR PARTICULAR MENUS.
function loadSplashMenus () {
	navMenu0Obj = new ExpandingMenu({ menuId: "navMenu0", isTwoLevelMenu: true, useDefaultContentManagement: true} ) ;
	navMenu0Obj.prepareMenu() ;
	navMenu0Obj.setInitialCondition( navMenu0Obj.itemManager[1] ) ; // ADDED FOR POOL TO SHOW LOGIN INFO ONLOAD.
	// Adjust menu settings on click. Note that responseFun has to be a string, else it triggers at the wrong times.
	for (var item=0; item < navMenu0Obj.itemManager.length; item++) {
	    navMenu0Obj.itemManager[item].responseFun = navMenu0Obj.setMenuResponse( { menusNotSelected: ["navMenu1Obj" , "navMenu2Obj", "navMenu3Obj"], item: item } ) ;
	    if (item != 0) navMenu0Obj.itemManager[item].responseFun += "hideSplashDescription(item)" ;
	}
	//____________________ Instantiate drop-downs ___________________ //
	navMenu1Obj = new ExpandingMenu({ menuId: "navMenu1", isTwoLevelMenu: true, useDefaultContentManagement: true} ) ;
	navMenu1Obj.prepareMenu() ;
	// Adjust menu settings on click.
	for (var item=0; item < navMenu1Obj.itemManager.length; item++) {
	    navMenu1Obj.itemManager[item].responseFun = navMenu1Obj.setMenuResponse( { menusNotSelected: ["navMenu0Obj" , "navMenu2Obj", "navMenu3Obj"], item: item } ) ;
	}
	//____________________ Instantiate a drop-down ___________________ //
	navMenu2Obj = new ExpandingMenu({ menuId: "navMenu2", isTwoLevelMenu: true, useDefaultContentManagement: true} ) ;
	navMenu2Obj.prepareMenu() ;
	// Adjust menu settings on click.
	for (var item=0; item < navMenu2Obj.itemManager.length; item++) {
	    navMenu2Obj.itemManager[item].responseFun = navMenu2Obj.setMenuResponse( { menusNotSelected: ["navMenu0Obj" , "navMenu1Obj", "navMenu3Obj"], item: item } ) ;
	}
	//____________________ Instantiate a drop-down ___________________ //
	navMenu3Obj = new ExpandingMenu({ menuId: "navMenu3", isTwoLevelMenu: true, useDefaultContentManagement: true} ) ;
	navMenu3Obj.prepareMenu() ;
	// Adjust menu settings on click.
	for (var item=0; item < navMenu3Obj.itemManager.length; item++) {
	    navMenu3Obj.itemManager[item].responseFun = navMenu3Obj.setMenuResponse( { menusNotSelected: ["navMenu0Obj" , "navMenu1Obj", "navMenu2Obj"], item: item } ) ;
	}
	//____________________ Special response functions ___________________ //
	navMenu3Obj.itemManager[1].responseFun += "openInterstice() ; "
	navMenu3Obj.itemManager[2].responseFun += "window.location = 'artreference_pool.php' ; "
	navMenu3Obj.itemManager[3].responseFun += "window.location = 'code_pool.php' ; "
	navMenu3Obj.itemManager[4].responseFun += "window.location = 'codereference_pool.php' ; "
	navMenu3Obj.itemManager[5].responseFun += "expandToFullScreen() ; "
	//____________________ Generate printable text ___________________ //
	// navMenu0Obj.generatePrintableText() ; Unused here.
	//____________________ DEBUG ___________________ //
	// alert(document.getElementById("share").menu)
} ;