//vars
//----------------------------------------------------
var expand;
var displayedCurrentLetter ="";
var displayedCurrentText ="";
var myinterval;

//functions
//----------------------------------------------------
function displayAll() {
  for (i=97; i<97+26; i++) {
    cletter = String.fromCharCode(i);
      div = document.getElementById("letter-"+cletter);
    if (div) div.style.display="block";

    // ***** All Children display
    displayAllChildren(cletter);

  }
}

function displayAllChildren(letter) {
  var div = document.getElementById("letter-"+letter);
  var child,elementText;
  if (div) {
    for (key in div.childNodes) {
      child = div.childNodes[key];
      if (child.nodeName=="DT") {
	child.style.display="block";
      }
    }
  }
}

function filterDisplay(text) {
  var text = text.toLowerCase();
  var letters = new Array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");
  var child,elementText;

  for (i=0; i<=letters.length; i++)  {
    letter = letters[i];
    div = document.getElementById("letter-"+letter);
    if (div)  {
      for (key in div.childNodes) {
	child = div.childNodes[key];
	if (child.nodeName && child.nodeName.toUpperCase()=="DT") {
	  elementText = child.innerText || child.textContent;
	  elementText = elementText.toLowerCase();
	  if (elementText.indexOf(text) >=0 || text=="")
	    child.style.display="block";
	      else {
		child.style.display="none";
		//alert (elementText.substring(0,text.length-1) +"==" +text);

	      }
	}
      }
    }
  }
}

function updateDisplay () {

  var currentText = document.getElementById("degree").value.toLowerCase();
  var currentLetter = currentText.substring(0,1);

  if (currentText != displayedCurrentText) {
    //the text in the box has changed

    if ( currentText=="" ) {
      //it's now empty so show all courses
      displayAll();
    }
    else {
      //show only courses that match the text
      filterDisplay(currentText)
    }

    //close all open courses
    $("#sort-text dt.active-open a").removeClass("white").parent().removeClass("active-open").next("dd").slideUp(300);

    //debug stuff:
    //console.log("changing displayedCurrentLetter to " + currentLetter);
    //console.log("changing displayedCurrentText to " + currentText);

    //set the global vars
    displayedCurrentLetter = currentLetter;
    displayedCurrentText = currentText;
  }
}

function initSearch () {
  //console.log("starting interval");
  myinterval = setInterval("updateDisplay()",250)
  //close any open courses
}

function stopSearch () {
  //console.log("clearing interval");
  clearInterval(myinterval);
}

/*function showHide(obj) {
 var showDiv = "#"+obj;
 $(".collegeListing").hide();
 $(showDiv).show();
}*/

$(document).ready(function() {

  //this sets up the accordian like behaviour of the courses/subject areas/schools
  $("a.link-program, a.expandable-link").click(function(event) {

    //work out scope (the containing <dl>)
    var thisScope = $(this).parent().parent();

    /////////////////////////////////////////////
    //if ($.browser.msie && parseInt($.browser.version) == 6) { then we need to hide() not slide() ??? }
    ////////////////////////////////////////////

    if ( $(this).parent().hasClass("active-open") ) {
      //we don't want to slide up and down if this is the current active <dt>
      //console.log("closing an already active element");
      $(this).removeClass("white").parent().removeClass("active-open").next("dd").slideUp(300);

    }
    else {
      //console.log("opening a new active element");
      //hide any open ones (within the same scope)
      $("dt.active-open a", thisScope).removeClass("white").parent().removeClass("active-open").next("dd").slideUp(300);

      //add "white" class to this <a>
      $(this).addClass("white");

      //add active class to this <dt>
      $(this).parent().addClass("active-open");

      //toggle the accordian down
      $(this).parent().next("dd").slideToggle(300);
    }

    event.preventDefault();

});

  //this sets up the jquery UI tabs
  //$("#course-search").tabs();

  //this hides all the subject areas/schools in 2nd tab
  $(".collegeListing, .program").hide();

  //this starts the interval, for interactive searching, and stops it when not needed
  $("input#degree")
    .focus(function() { initSearch(); })
    .blur(function() { stopSearch(); });

});
