dojo.require("dojo.fx");


function swapDiv( divNum, expanding )
{
	cdiv = dojo.byId("c" + divNum);
	ediv = dojo.byId("e" + divNum);
	fdiv = dojo.byId("f" + divNum);
	
    if (expanding)
    {
      
    	dojo.style(cdiv, "display", "none");
    	dojo.style(fdiv, "display", "block");
        dojo.style(ediv, "display", "block");

    }
    else
    {
       dojo.style(fdiv, "display", "none");
       dojo.style(ediv, "display", "none");
       dojo.style(cdiv, "display", "block");


    }
}

function swapDivInline( divNum, expanding )
{
	cdiv = dojo.byId("c" + divNum);
	ediv = dojo.byId("e" + divNum);
	fdiv = dojo.byId("f" + divNum);
	
    if (expanding)
    {
      
    	dojo.style(cdiv, "display", "none");
    	dojo.style(fdiv, "display", "inline");
        dojo.style(ediv, "display", "inline");

    }
    else
    {
       dojo.style(fdiv, "display", "none");
       dojo.style(ediv, "display", "none");
       dojo.style(cdiv, "display", "inline");


    }
}


function labelNavLinks(){
    //this function adds classes to links
    //in nav menus/related links, etc
    //so that the piwik tracking script
    //can identify them
    
    dojo.query(".relatedlinks a").addClass("piwik_serc-relatedlink");
    dojo.query("#nav a").addClass("piwik_serc-navlink");
    dojo.query("#crumbs a").addClass("piwik_serc-crumblink");
    dojo.query("#nextprev a").addClass("piwik_serc-nextlink");
    dojo.query(".searchresults a").addClass("piwik_serc-searchlink");


    
}


function repositionFooter()
{
    if(document.getElementById("nav")){
		var contentHeight = document.getElementById("content").offsetHeight;
		var navHeight = document.getElementById("nav").offsetHeight;
		if( contentHeight < navHeight){
			var newFooterTopMargin =  navHeight - contentHeight + 70;
			newFooterTopMargin = newFooterTopMargin + "px";
			document.getElementById("content").style.marginBottom = newFooterTopMargin;
			if(document.getElementById("footer")){
				document.getElementById("footer").style.marginTop = newFooterTopMargin;
			}		
		}
	}
	 if(document.getElementById("rightcolumn")){
         //this fixes thing for merlot and places where we have rightcolumns
                //var url = window.location.href;
                //console.log(url);
                if(document.getElementById("leftcolumn")){
                        var contentHeight = document.getElementById("leftcolumn").offsetHeight;
                        var navHeight = document.getElementById("rightcolumn").offsetHeight;
                        console.log(navHeight);
                        console.log(contentHeight);
                        if( contentHeight < navHeight){
                                var newFooterTopMargin =  navHeight - contentHeight + 30;
                                newFooterTopMargin = newFooterTopMargin + "px";
                                document.getElementById("leftcolumn").style.marginBottom = newFooterTopMargin;
                                if(document.getElementById("footer")){
                                        document.getElementById("footer").style.marginTop = newFooterTopMargin;
                                }		
                        }
                }
                
	}
         if(document.getElementById("upper")){
         //this fixes things for pkal chrome
                var url = window.location.href;
                if(url.search('/pkal/') != -1){
                        var contentHeight = document.getElementById("upper").offsetHeight;
                        var navHeight = document.getElementById("nav").offsetHeight;
                        if( contentHeight < navHeight+100){
                                var newFooterTopMargin =  navHeight - contentHeight + 160;
                                newFooterTopMargin = newFooterTopMargin + "px";
                                document.getElementById("upper").style.paddingBottom = newFooterTopMargin;
                                if(document.getElementById("footer")){
                                       console.log(newFooterTopMargin);
                                        document.getElementById("footer").style.marginTop = newFooterTopMargin;
                                }		
                        }
                }
	}

	if(document.getElementById("contentsidebar")){
	//for SENCER chrome
		var contentHeight = document.getElementById("contentfeature").offsetHeight;
		var navHeight = document.getElementById("contentsidebar").offsetHeight;
		if( contentHeight < navHeight){
			var newFooterTopMargin =  navHeight - contentHeight;
			newFooterTopMargin = newFooterTopMargin + "px";
			document.getElementById("contentfeature").style.marginBottom = newFooterTopMargin;
					
		}
	}
	
}

//functions for handling collapsed nav menus

				//simple function to collapse/hide menu items marked with the
				//collapse class  --we run it at load
				 function collapseMenu(){
 					dojo.query(".collapse").forEach(
 						function(collapsibleItem){
 				 			dojo.fx.wipeOut({ node: collapsibleItem, duration: 400}).play();
 						}
 					);
 				}
				
				function expandMenu(){
				  //this expands all the collapsed menu items labeled with
				  //the class collapse# where # is the menunumber
				  full_id = this.id;
				  idnumber = full_id.substring(4);
				  var collapselabel = ".collapse" + idnumber;
				  
				  dojo.query(collapselabel).forEach(
				  function(collapsibleItem){
				 			dojo.fx.wipeIn({ node: collapsibleItem ,duration: 300}).play();
						}
					);
				//hide the show text	
				 dojo.style(this, "display", "none");

				}
				
				function hookupNavExpander(){
					//this function hooks up each navExpander to 
					//and expandMenu call for that particular id number
					dojo.query(".navexpander").connect('onclick', expandMenu);

				}
//functions for activating comboboxes
function hookupInstitutionCombo() {
    //var objectStore = new dojo.store.JsonRest({target:"/admin/ajax/wrappers/institution.php"});
    //need wrapper to old datastore format
    //var institutionStore =new dojo.data.ObjectStore({objectStore: objectStore});
    dojo.require("dijit.form.Textarea");
    dojo.require("dijit.form.ComboBox");
    dojo.require("dojox.data.QueryReadStore");
    
    
    dojo.query('.institutionSelect').forEach(
		function(thisdiv) {
            var existingValue = thisdiv.value;
           // existingValue = existingValue.replace("'", "\'");
            var existingName = thisdiv.name;
            var existingId = thisdiv.id;
            //the id for this element is set to 'institutionSelect##'
            //extract the number from the end and use it to uniquely identify the dijit
            var existingNumericId = existingId.substring(17);
            var comboId = 'institutionCombo' + existingNumericId;

            var institutionStore = new dojox.data.QueryReadStore({url:"/admin/ajax/wrappers/institution.php", jsId:"institutionStore"});
            var filteringSelect = new dijit.form.ComboBox({
                name: existingName,
                id: comboId,
                store: institutionStore,       
                value: existingValue,
                searchAttr: "name",
                searchDelay: 150,
                autoComplete: false
                
            },
            thisdiv);
    });
}

function hookupDepartmentCombo() {
    dojo.require("dijit.form.ComboBox");
    dojo.require("dojo.data.ItemFileReadStore");
    dojo.require("dijit.form.Textarea");

    
    
    dojo.query('.departmentSelect').forEach(
		function(thisdiv) {
            var existingValue = thisdiv.value;
            var existingName = thisdiv.name;
            var existingId = thisdiv.id;
            //the id for this element is set to 'departmentSelect##'
            //extract the number from the end and use it to uniquely identify the dijit
            var existingNumericId = existingId.substring(16);
            var comboId = 'departmentCombo' + existingNumericId;
            
           
            
            thisDepartmentStore = new dojo.data.ItemFileReadStore({url:"/admin/ajax/wrappers/department.php", clearOnClose: "True", urlPreventCache: "True"});
        var comboBox = new dijit.form.ComboBox({
            name: existingName,
            id: comboId,
            store: thisDepartmentStore,       
            value: existingValue,
            searchAttr: "name",
            searchDelay: 150,
            autoComplete: false
            
        },
        thisdiv);
        
         //if there isn't yet a departmentStore object in the global namespace create one
            if(typeof departmentStores == 'undefined'){
                departmentStores = {};
            }
            departmentStores[existingNumericId] = thisDepartmentStore;
        
        //see if there's a matching institutionSelect and set an onChange on it
        //so that its value is used to set the url of the read store
        var institutionComboId = 'institutionCombo' + existingNumericId;
        
        var matchingInstitution = dijit.byId(institutionComboId);
        if(matchingInstitution){
            dojo.connect(matchingInstitution,'onChange',updateDepartmentUrl);
        }
        //do an initial setup incase the matching institution is already set
        //have to do it in two lines so that 'this' is set appropriately within
        //updateDepartmentUrl()
        matchingInstitution.initialUrlSetup = updateDepartmentUrl;
        matchingInstitution.initialUrlSetup();
    });
}

function updateDepartmentUrl(){
    //an institutionComboBox dijit is passed as 'this'
    //it updates the url in departmentStores
    //to point to the url that will retrieve the department list for the
    //currently selected institution
    //departmentStores is index by an id that associates pairs of institution/department
    //comboboxes (so we can have more than one on the page).
    //get current name set in institution combobox
    var institutionName = this.value;
    var existingId = this.id;
    //the id for this element is set to 'institutionSelect##'
    //extract the number from the end and use it to uniquely identify the dijit
    var existingNumericId = existingId.substring(16);

    institutionName = encodeURIComponent(institutionName);
    //could check existence of departmentStores and this particular element but
    //we really shouldn't be getting calls here for ones that don't exist.
    departmentStores[existingNumericId].url = "/admin/ajax/wrappers/department.php?name=" + institutionName;
    departmentStores[existingNumericId].close();
    
    
}
dojo.addOnLoad(collapseMenu);
dojo.addOnLoad(hookupNavExpander);
dojo.addOnLoad(repositionFooter);
dojo.addOnLoad(labelNavLinks);
dojo.addOnLoad(hookupInstitutionCombo);
dojo.addOnLoad(hookupDepartmentCombo);





