

/* ---------------------------------------------------------------
*
*                  User configurable variables
*/

// milliseconds before a menu disappears
var DELAY = 350;

// menu row hilight colour
var MAIN_MENU_HILIGHT = "#a9b7de";
var MENU_HILIGHT = "#c9d0e3";

// menu row un-hilight colour
var MAIN_MENU_LOLIGHT = "#c9d0e3";
var MENU_LOLIGHT = "#fff";

// voodoo x and y offsets
var XOFFSET = 0;
var YOFFSET = 26;

// placeholder for the currently selected main menu item
var MAINMENU_SELECTED = "";

// placeholder for the currently selected subnav menu item
var SIDENAV_SELECTED = "";

var NUM_MAINMENU_ITEMS = 6;

/* 
*                  End User configurable variables
*
* ---------------------------------------------------------------
*/

function initPage()
{
 initMenuPositions();

 for ( var i = 0; i < document.forms.length ; i++ ) {
   var form = document.forms[i];
   if ( "" + form.elements["pulldown"] == "undefined" ) continue;
   InitLanguageDroplist(form);
 }
}


/* ----------------------------------- main menu functions ----------------------------------- */

function menuIn(item, menu) {
 item.style.background = MENU_HILIGHT;
 showMenu(menu);
}

function menuOut(item, menu) {
 item.style.background = MENU_LOLIGHT;
 hideMenu(menu);
}

function mainMenuIn(item, menu) {
 if ( "" + item.id != MAINMENU_SELECTED ) {
  item.className = "mainnavhi";
 }
 showMenu(menu);
}

function mainMenuOut(item, menu) {
 if ( "" + item.id != MAINMENU_SELECTED ) {
  item.className = "mainnav";
 }
 hideMenu(menu);
}

function initMenuPositions() {
 var x = XOFFSET;
 var base = document.getElementById("mainnavbox");
 if ( base + "" == "null" ) return;
 
 var y = base.offsetTop + YOFFSET;

 // needs to reflect the number of mainnav siteareas (currently starting courtlist)
for ( var i = 1 ; i <= NUM_MAINMENU_ITEMS ; i++ ) {
  var menu = document.getElementById("menu" + i);
  var anchor = document.getElementById("anchor" + i);
  menu.style.left = anchor.offsetLeft + x + "px";
  menu.style.top = anchor.offsetTop + y + "px";
 }
}

var visibleMenu = "";
var hidingMenu = "";

function showMenu(m) {
 initMenuPositions();
 
 // if the menu is already visible, do nothing
 if ( m == visibleMenu ) return;

 // if a previous menu is showing, hide it
 var prev = visibleMenu;
 if ( prev == "" ) prev = hidingMenu;
 visibleMenu = m;

 if ( prev != "" ) hideMenuAux(prev);
 
 var menu = document.getElementById(m);
 menu.style.visibility = 'visible';
}

function hideMenu(m) {
 setTimeout("hideMenuAux('" + m + "');", DELAY);
 visibleMenu = "";
 hidingMenu = m;
}

function hideMenuAux(m) {
 if ( visibleMenu != m ) {
  var menu = document.getElementById(m);
  menu.style.visibility = 'hidden';
 }
 hidingMenu = "";
}

/* ----------------------------------- sidenav menu functions ----------------------------------- */

function sidenavIn(item) {
 if ( "" + item.id != SIDENAV_SELECTED ) {
  item.style.background = MENU_HILIGHT;
 }
}

function sidenavOut(item) {
 if ( "" + item.id != SIDENAV_SELECTED ) {
  item.style.background = MENU_LOLIGHT;
 }
}

/* ----------------------------------- functions to set the currently selected items ----------------------------------- */

function setMainmenuSelected(id) {
 if ( "" + id == "" ) return;
 if ( !document.getElementById ) return;
 MAINMENU_SELECTED = id;
 var item = document.getElementById(id);
 item.className = "mainnavhi";
}

function setSidenavSelected(id) {
 if ( "" + id == "" ) return;
 if ( !document.getElementById ) return;
 SIDENAV_SELECTED = id;
 var item = document.getElementById(id);
 item.style.background = MENU_HILIGHT;
}

/* --------------------------------------- stylesheet manipulation -------------------------------------*/


// from http://www.howtocreate.co.uk/tutorials/index.php?tut=0&part=27
function getAllSheets() {
  //if you want ICEbrowser's limited support, do it this way
  if( !window.ScriptEngine && navigator.__ice_version ) {
   //IE errors if it sees navigator.__ice_version when a window is closing
   //window.ScriptEngine hides it from that
    return document.styleSheets; }
  if( document.getElementsByTagName ) {
    //DOM browsers - get link and style tags
    var Lt = document.getElementsByTagName('LINK');
    var St = document.getElementsByTagName('STYLE');
  } else if( document.styleSheets && document.all ) {
    //not all browsers that supply document.all supply document.all.tags
    //but those that do and can switch stylesheets will also provide
    //document.styleSheets (checking for document.all.tags produces errors [WHY?!])
    var Lt = document.all.tags('LINK'), St = document.all.tags('STYLE');
  } else { return []; } //lesser browser - return a blank array
  //for all link tags ...
  for( var x = 0, os = []; Lt[x]; x++ ) {
    //check for the rel attribute to see if it contains 'style'
    if( Lt[x].rel ) { var rel = Lt[x].rel;
    } else if( Lt[x].getAttribute ) { var rel = Lt[x].getAttribute('rel');
    } else { var rel = ''; }
    if( typeof( rel ) == 'string' &&
        rel.toLowerCase().indexOf('style') + 1 ) {
      //fill os with linked stylesheets
      os[os.length] = Lt[x];
    }
  }
  //include all style tags too and return the array
  for( var x = 0; St[x]; x++ ) { os[os.length] = St[x]; } return os;
}


// from http://www.howtocreate.co.uk/tutorials/index.php?tut=0&part=27
function changeStyleAux() {
  for( var x = 0, ss = getAllSheets(); ss[x]; x++ ) {
    //for each stylesheet ...
    if( ss[x].title ) {
      //disable the stylesheet if it is switchable
      ss[x].disabled = true;
    }
    for( var y = 0; y < arguments.length; y++ ) {
      //check each title ...
      if( ss[x].title == arguments[y] ) {
        //and re-enable the stylesheet if it has a chosen title
        ss[x].disabled = false;
      }
    }
  }
  if( !ss.length ) { alert( 'Your browser cannot change stylesheets' ); }
}


// from http://www.tneoh.zoneit.com/javascript/
function SetCookie(cookiename, cookievalue) {
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;

  document.cookie = cookiename + "=" + escape(cookievalue) +
    ((expires == null) ? ""      : ("; expires=" + expires.toGMTString())) +
    ((path    == null) ? ""     : ("; path="    + path                 )) +
    ((domain  == null) ? ""      : ("; domain="  + domain               )) +
    ((secure  == true) ? "; secure" : "");

  return;

}

// from http://www.tneoh.zoneit.com/javascript/
function GetCookie(name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;

  while (i < clen) {
    var j = i + alen;

    if (document.cookie.substring(i, j) == arg)
      return GetCookieVal(j);

    i = document.cookie.indexOf(" ", i) + 1;

    if (i == 0) 
      break; 
  }

  return null;
}

// from http://www.tneoh.zoneit.com/javascript/
function GetCookieVal(offset) {
  var endstr = document.cookie.indexOf(";", offset);

  if (("" + endstr) == "" || endstr == -1)
    endstr = document.cookie.length;

  return unescape(document.cookie.substring(offset, endstr));
}


function changeStyle(val) {
  // changeStyleAux(val);
  SetCookie("StyleSheet", val, null, "/");
  var pageurl = "" + document.location;
  var i = pageurl.indexOf("?");
  if ( i >  -1 ) pageurl = pageurl.substring(0, i);
  document.location = pageurl + "?t=" + ( new Date().getTime() );
}


function checkStyleSheetCookie() {
  var c = GetCookie("StyleSheet");
}



