var shown = "";
var sClass = "off";
var noHelp = "<br /><br /><strong>Help Unavailable</strong></div>";
var dept = new Array();
var menuShowing = false;
var menuBuilt = false;
var done = false;
var str = "";
var pro = false;
var lang = "";

var ua = navigator.userAgent;
var mac = (ua.indexOf("Mac")   > 0);
var macLo = false;
if(mac){
  macLo = (ua.indexOf("MSIE 5.0")>-1 || ua.indexOf("MSIE 4.")>-1) ? true : false;
}
var ie = (ua.indexOf("MSIE")  > 0);
var ns = (ua.indexOf("Gecko") > 0);

var buildParam;
var menuArray = new Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Help", "Detail");
var buttonArray = new Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Help", "All", "showHide");

function myShow(o,language){
lang = language;
	if(pro) { 
		return;
	}

	if(o == shown && menuShowing)  {
		return;
	} else if(o == shown && !menuShowing) {
		toggleMenu(language); 
		return; 
	}

	var o1 = gebi("group" + o);

	if(!menuBuilt) {
	       	buildMenu(o);
		return;
	}

	if(!menuShowing)  { 
		toggleMenu(language);
	}

	if(shown != "") {
		hideShown();
	}	

	if(o1){
  		o1.style.display = "block";
  		o1.style.cursor = "";
	} else if(o == "All"){
  		showAll();
	}

	togTab(o, shown);
	shown = o;
}

function showHelp(o,language){
lang = language;
if(pro) return;
if(o == shown && menuShowing) return;
else if(o == shown && !menuShowing) {toggleMenu(language); return;}
var o1 = gebi("group" + o);
if(!menuBuilt) {buildMenu(o);return;}
if(!menuShowing) toggleMenu(language);
if(shown != "") hideShown();
if(o1){
  o1.style.display = "block";
  o1.style.cursor = "";
} else if(o == "All"){
  showAll();
}
togTab(o, shown);
shown = o;
}
function buildMenu(o){
  buildParam = o || "";
  loadJSContent("/HISDConnectDS/templates/includes/deptLocator/deptInclude.jsp?func=build&reg=false");
  //loadJSContent("/HISD/Portal/dept/include/?func=build&reg=true");
  pro = !menuBuilt;
 
}

function showAll(){
for(var i=1; i < 11; i++){
  gebi("group" + i).style.display = "block";
}
}

function hideShown(){
if(gebi("group" + shown)) gebi("group" + shown).style.display = "none";
else if(shown == "All"){
  for(var i=0; i<menuArray.length; i++){
    var g = gebi("group" + menuArray[i]);
    if(g) g.style.display = "none";
  }
}
}

function nsHideAll(){
  for(var i=0; i<menuArray.length; i++){
    var g = gebi("group" + menuArray[i]);
    if(g) g.style.display = "none";
  }
}

function nsShow(o){
var o1 = gebi("group" + o);
if(o1){
  nsHideAll();
  o1.style.display = "block";
  o1.style.cursor = "";
} else if(o == "All"){
  showAll();
}
}

function toggleMenu(language){
lang=language;

if(!menuBuilt) {buildMenu(o);return;}
var o = gebi("deptNavWidget");
var t = gebi("showHide");
if(!menuShowing){

  if(ns) nsShow(shown);
  else o.style.display = "block";
  
  //Commented below by Hersh
  //if(otherContent) otherContent.style.display = "none";
  if(language == "es"){
        
  	setInnerText(t, "Ocultar");
  }else{
      
  	setInnerText(t, "Hide");
  }	
} else {

  if(ns) hideShown();
  else o.style.display = "none";
  
  //Commented below by Hersh
  //if(otherContent) otherContent.style.display = "block";
  if(language == "es")
  	setInnerText(t, "Ver");
  else
  	setInnerText(t, "Show");
}
menuShowing = !menuShowing;
if(menuShowing) togTab(shown, "");
else togTab("", shown);
}

function setInnerText(obj, text){
if(mac && ie && !macLo) {
  var child = obj.firstChild;
  if(child) obj.removeChild(child);
  var t = document.createTextNode(text);
  obj.appendChild(t);
} else {
  obj.innerHTML =text;
}
}

function togTab(newby, old){
var o1 = gebi(old);
var o2 = gebi(newby);
 if(o1){
    o1.className = sClass;}
  if(o2){
    var tmp = o2.className;
    o2.className = "on";
    o2.style.backgroundColor = "";
    sClass = tmp;}
}

function hideAll(){
  for(i = 1;i < 12; i++){
    gebi("group"+i).style.display = "none";}
}
function hLite(o,key){
  for(var i = 0; i < buttonArray.length; i++){
    var obj = gebi(buttonArray[i]);
    obj.style.color = "";
    obj.style.backgroundColor = "";}
  if(o.id!=shown && !key){
    if(o.className.indexOf("2") > -1) { o.style.color = "black";}
    else {o.style.backgroundColor = "#ffffff";}
  }
}

function go(n){
  if(!dept[n]){
    gebi("groupDetail").innerHTML = "<br/><br/>Information Unavailable.<br/><br/>";
    myShow("Detail");
    return;
  }
  var id = dept[n][6];
  var url = dept[n][1];
  if(id.length > 0){//Checking if Portal Site
    window.location = url;
  } 
  else if(url.length == 0) {
    gebi("groupDetail").innerHTML = getDetail(n);
    myShow("Detail");
  }
  else {
    var win = window.open(url,null,"resizable=yes,scrollbars=yes,status=yes,toolbar=yes,menubar=yes,location=yes");
  }
}

function getDetail(n){
  var str = "<div style='padding:0px;width:100%;'><b>" + dept[n][0] + "</b><br/>[No Site Available]<br/><br/>";
  str += (dept[n][2].indexOf("Level ") > -1) ? "<b>Address: <br/></b>Hattie Mae White Bldg.<br/>" + dept[n][2] + "<br/>3830 Richmond Ave.<br/>Houston, Texas 77027<br/>" :  "<b>Address: </b><br/>" + dept[n][2] + "<br/>";
  if(dept[n][3] != "&nbsp;" && dept[n][3] != "") str += "<b>Route: </b>" + dept[n][3] + "<br/>";
  str += "<b>Phone: </b>" + dept[n][4] + "<br/>";
  str += "<b>Fax: </b>" + dept[n][5] + "<br/></div>";
  return str;
}

function overItem(o){
  o.style.textDecoration = "underline";
  o.style.cursor = "pointer";
}
function outItem(o){
  o.style.textDecoration = "";
  o.style.cursor = "";
}

function build(o){

var show = buildParam || "1";
var letterStr = "ACDFHJNRST}";
var currentLetter = "";
var groupCounter = 0;
var recCount = 0;
var nextLetter = "A";
var deptName = "";
var deptStr = "";
var tempStyle = "";
var tempDivEnd = "";
var overOut = " onmouseover='overItem(this);' onmouseout='outItem(this);'";
for(var i=1; i<dept.length; i++){
  deptName = dept[i][0];
  currentLetter = deptName.substring(0,1).toUpperCase();
  if(currentLetter == nextLetter || currentLetter > nextLetter){
    groupCounter++;
    nextLetter = letterStr.substring(groupCounter, groupCounter+1);
    tempDivEnd = (groupCounter<2) ? "" : "</div>\n";
    tempStyle = (groupCounter == parseInt(show) || show == "All") ? "block" : "none";
    deptStr += tempDivEnd + "<div class=\"menuItems\" id=\"group"+groupCounter+"\" style=\"display:"+tempStyle+";\">\n";
  }
  deptStr += "<div class=\"itm\" width=\"100%\" onclick=\"go('"+ i +"');\"" + overOut + ">"+deptName+"</div>\n";
  recCount++;
}
var hStr = (typeof helpString !="undefined") ? helpString : noHelp;
var styleStr = (buildParam == 'Help') ? "display:block;" : "display:none;" ;
deptStr += "</div>\n<div class=\"menuItems\" id=\"groupHelp"+"\" style=\"" + styleStr + "\">\n" + hStr + "\n</div>\n";
deptStr += "<div class=\"menuItems\" id=\"groupDetail\" style=\"display:none;\"></div>\n";
gebi("menuContents").innerHTML = deptStr;
menuBuilt = true;
shown = show;
pro = false;
toggleMenu(lang);
window.status = "Data Transferred Successfully.";
if(ns) gebi("deptNavWidget").style.display = "block";
if(mac) toggleMenu();
}

function loadJSContent(file){
  var head = gebi('head')
  var scriptTag = gebi('deptArray');
  if(scriptTag) head.removeChild(scriptTag);
  var scripty = document.createElement('script');
  scripty.src = file;
  scripty.type = 'text/javascript';
  scripty.id = 'deptArray';
  head.appendChild(scripty);
}

function gebi(o){
  return document.getElementById(o);
}

function toggleSchNav(){
  var o = gebi("schNav");
  var b = gebi("more");
  if(o && o.style.display == "none") {
    o.style.display = "block";
    b.value = "Hide";
    
  } else {
    o.style.display = "none";
    b.value = "More";
  }
}

function catchReturn(form){
  

}
