if ( !KH )  KH = {};
KH.SchedPage = {};

    KH.SchedPage.curView = null;
    KH.SchedPage.breadCrumb = null;
    KH.SchedPage.curGroup = null;

    KH.SchedPage.initSchedulePage = function() {
	var self = KH.SchedPage;
	//  find the second breadcrumb
	var bc = $$( ".breadcrumbs" );
	if ( bc ) {
	    var links = bc[0].select( "a" );
	    if ( links && links.length>1 )  self.breadCrumb=links[1];
	}
        //  set up the behavior for the "filter" radio buttons
        self.initFilterSelect( "Location" );
        self.initFilterSelect( "Category" );
	self.initGroupSelect();
	//  set up the tool-tips for states on the map
        new KH.ToolTip( "area", self.territoryTooltip );
	// new KH.ToolTip( ".acct-mgr-lookup", self.mgrLookupTooltip );
	//  if there's an "acct-mgr-display" <div>, put the
	//  account-manager data into it.
	var el = $("acct-mgr-display");
	if ( el )  el.innerHTML = VWEB.getAcctMgrContent(self.thisSchedCode,1);
        //  set up the on-change behavior for the location select box
        el = $( "us-map-select" );
        if ( el )  el.onchange = function() {
			//  depends on "scheduleUrl" being set by server!
			document.location = scheduleUrl+"/location/"
				+this.options[this.selectedIndex].value
				+".html";
              		 }
    }

    //  this initializes the "select filter" menu for either "Location"
    //  or "Category", depending on naming conventions and knowledge
    //  of the way the Breadcrumbs are generated.
    KH.SchedPage.initFilterSelect = function( name ) {
        var key = name.toLowerCase();
	var menuitem = $( "do-"+key );
	var div = $( "filter-by-"+key );
	if ( !menuitem || !div )  return;
	menuitem.viewDiv = div;
	var el = menuitem.select("input");
	if ( !el ) throw "Must be an <input> in "+name+" menu item";
	if ( el[0].checked ) KH.SchedPage.curView = menuitem.viewDiv;
	menuitem.checkInput = el[0];
	menuitem.savedHref = menuitem.href;
	menuitem.href = "#";
	menuitem.onclick = KH.SchedPage.changeFilterView;
	menuitem.viewName = name;
    }


    KH.SchedPage.initGroupSelect = function() {
	var self = KH.SchedPage;
	// the "details-by-primary" and "details-by-month" divs are required
	var primary = $("details-by-primary");
	var month = $("details-by-month");
	if ( !primary || !month )  return;
	//  hopefully, only one of these two is true!
	if ( primary.style.display == "block" )  self.curGroup = primary;
	if ( month.style.display == "block" )  self.curGroup = month;
        // find all "group-menu" <div>s and set up their links
	var divs = $$( ".group-menu" );
	if ( divs ==  null )  return;
	for ( i=0; i<divs.length; i++ ) {
	    var links = divs[i].select( "a" );
	    if ( links < 3 )  return;
	    //  first link is the "primary"
	    links[0].groupDiv = primary;
	    links[0].href = "#";
	    links[0].onclick = self.changeGrouping;
	    //  second link is the "month"
	    links[1].groupDiv = month;
	    links[1].href = "#";
	    links[1].onclick = self.changeGrouping;
	    //  third link is the "include/exclude web"
	    //      if the page was created with web-based classes
	    //      NOT included (i.e., this link is generated to
	    //      turn them on and therefore does NOT contain "wb=n"),
	    //      then there's nothing we'll be able to do, so leave it
	    //      that way and the page will reload if user wants to
	    //      get the web-based classes back in it.
	    if ( links[2].href.indexOf("wb=n") >= 0 ) {
	        links[2].onclick = self.toggleWebInclusion;
	        links[2].isIncluded=true;
	        links[2].href = "#";
	    }
	}
    }






    //  when the filter changes, we need to do the same things that the
    //  server would do when it reloads the page:
    //     - set the "display" style appropriately on each <div>
    //     - change the value of the breadcrumb appropriately
    //     - change the page title appropriately
    KH.SchedPage.changeFilterView = function( evt ) {
        if ( !evt ) evt = window.event;
	var self = KH.SchedPage;
        //  make sure it's radio becomes checked
        this.checkInput.checked = true;
        //  get rid of the focus indicators
        this.blur(); this.checkInput.blur();
	//  if this buttons view is already current, that's it
        if ( this.viewDir == self.curView )  return;
        //  out with the old view, and in with the new one
        self.curView.style.display = "none"
        this.viewDiv.style.display = "block";
        self.curView = this.viewDiv;
	//  patch up the breadcrumb
	self.breadCrumb.innerHTML = "By "+this.viewName;
	self.breadCrumb.href = scheduleUrl+"/"
			+this.viewName.toLowerCase()+"/index.html";
	//  patch up the page title
	document.title = "Choose "+this.viewName+" For Class Schedule";
	//  hijack the click, so the request doesn't go to the server
	//  (Can't do this hijack, or else if click was on radio,
	//  it doesn't update it's state right!)
	//if ( evt.preventDefault ) evt.preventDefault();
	//else evt.returnValue = false;
    }


    KH.SchedPage.changeGrouping = function( evt ) {
        if ( !evt )  evt = window.event;
        var self = KH.SchedPage;
	this.blur();
	//  if group for this link is already current, don't do anything
	if ( this.groupDiv == self.curGroup )  return;
	//  otherwise, swap the visibility between the groups
	//  (which also will swap the "selected" state of the link
	//  since they are created correctly in each section of the page!)
	self.curGroup.style.display = "none";
	this.groupDiv.style.display = "block";
	self.curGroup = this.groupDiv;
	//  hijack the click, so the request doesn't go to the server
	//if ( evt.preventDefault ) evt.preventDefault();
	//else evt.returnValue = false;
    }


    KH.SchedPage.toggleWebInclusion = function( evt ) {
	if ( !evt )  evt = window.event;
	this.blur();
	var doExclude = this.isIncluded;
	//  swap the wording on all the "include-web" links
	var list = $$( ".include-web" );
	for ( i=0; i<list.length; i++ ) {
	    list[i].innerHTML = (doExclude) ? "Include Web-Based Classes"
	    				    : "Exclude Web-Based Classes";
	    list[i].isIncluded = !doExclude;
	    //  toggle the visibility of rows that have class "web-based"
	    KH.SchedPage.setWebRowsInAllGrids( !doExclude );
	}
	//  hijack the click, so the request doesn't go to the server
	if ( evt.preventDefault ) evt.preventDefault();
	else evt.returnValue = false;
    }

    KH.SchedPage.setWebRowsInAllGrids = function( makeVisible ) {
        var grids = $$( ".gridlist" );
	if ( !grids  ||  grids.length < 1 )  return;
	//  for each grid, remove/replace the items that have class
	//  "web-based", and fix up the shading as we go:
	var g, rows, r, darkbg;
	for ( g=0; g<grids.length; g++ ) {
	    rows = grids[g].select("tr");
	    if ( rows == null  ||  rows.length<2 )  continue;
	    darkbg = false;
	    //  for each row in this grid:
	    for ( r=1; r<rows.length; r++ ) {
		//  if it's "web-based", set it's visibility, and if
		//  we set it to invisible, then leave it out of the
		//  alternating background-shading
	        if ( rows[r].hasClassName("web-based") ) {
		    KH.SchedPage.setRowVisibility(rows[r],makeVisible);
		    if ( !makeVisible )  continue;
		}
		rows[r].addClassName( ((darkbg)?"darkbg":"lightbg") );
		darkbg = !darkbg;
	    }

	}
    }


    //  Should have been easy to do this, but can't figure out a way that
    //  works on both FF and IE, and also doesn't require us to actually
    //  remove the row from the grid.  Fortunately I did find a way to
    //  do it on each browser, so this method does it appropriately
    //  depending on the browser.
    KH.SchedPage.setRowVisibility = function( row, makeVisible ) {
	if ( Prototype.Browser.Gecko ) {
	    //  on Gecko browsers, style.display doesn't do the job, so we'll
	    //  just make the row appear to be empty to make it invisible.
	    if ( makeVisible ) {
	        if ( !row.savedHTML )
			throw "Bad news: original HTML has been lost!"
		row.innerHTML = row.savedHTML;
	    } else {
		if ( !row.savedHTML )  row.savedHTML = row.innerHTML;
	        row.innerHTML = "";
	    }
	} else {
	    //  on IE (and hopefully on all other browsers), this works
	    //  (but the above trick doesn't!)
	    row.style.display = (makeVisible)?"block":"none";
	}
    }





    //  create a tool-tip showing account manager for the lookup icon
    KH.SchedPage.mgrLookupTooltip = function( eventnum ) {
        if ( eventnum != 2 )  return;
	return ( VWEB.getAcctMgrContent(KH.SchedPage.thisSchedCode) );
    }

    //  create a tool-tip showing account manager for a link
    KH.SchedPage.territoryTooltip = function( eventnum ) {
        if ( eventnum != 2 )  return;
	terrcode = VWEB.extractCode( this.href );
	return ( VWEB.getAcctMgrContent(terrcode) );
    }

